Created
October 18, 2010 00:12
-
-
Save rummelonp/631484 to your computer and use it in GitHub Desktop.
Tumblifeのパース処理の各言語間の比較
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net; | |
using System.Windows; | |
using System.Windows.Controls; | |
using System.Windows.Documents; | |
using System.Windows.Ink; | |
using System.Windows.Input; | |
using System.Windows.Media; | |
using System.Windows.Media.Animation; | |
using System.Windows.Shapes; | |
using System.Collections.Generic; | |
using System.Xml.Linq; | |
using Tumblife.Model; | |
using Tumblife.Util; | |
namespace Tumblife.Tumblr | |
{ | |
public class TLPostParser | |
{ | |
public static ICollection<TLPost> Parse(String xml) | |
{ | |
Log.debug("TLPostParser / Parse"); | |
ICollection<TLPost> posts = new List<TLPost>(50); | |
XDocument document = XDocument.Parse(xml); | |
foreach (XElement _post in document.Descendants("post")) { | |
TLPost post = new TLPost(); | |
XElement element; | |
post.Id = (long)_post.Attribute("id"); | |
post.Url = (String)_post.Attribute("url"); | |
post.UrlWithSlug = (String)_post.Attribute("url-with-slug"); | |
post.Type = (String)_post.Attribute("type"); | |
post.DateGmt = (String)_post.Attribute("date-gmt"); | |
post.Date = (String)_post.Attribute("date"); | |
post.UnixTimestamp = (int)_post.Attribute("unix-timestamp"); | |
post.Format = (String)_post.Attribute("format"); | |
post.ReblogKey = (String)_post.Attribute("reblog-key"); | |
post.Slug = (String)_post.Attribute("slug"); | |
post.NoteCount = (int)_post.Attribute("note-count"); | |
var tumblelog = _post.Element("tumblelog"); | |
post.TumblelogTitle = (String)tumblelog.Attribute("title"); | |
post.TumblelogName = (String)tumblelog.Attribute("name"); | |
post.TumblelogUrl = (String)tumblelog.Attribute("url"); | |
post.TumblelogTimezone = (String)tumblelog.Attribute("timezone"); | |
element = _post.Element("tag"); | |
post.Tag = (element == null) ? null : (String)element.Value; | |
switch (post.Type) { | |
case "quote": | |
element = _post.Element("quote-text"); | |
post.QuoteText = (element == null) ? null : (String)element.Value; | |
element = _post.Element("quote-source"); | |
post.QuoteSource = (element == null) ? null : (String)element.Value; | |
break; | |
case "photo": | |
post.IsPhoto = true; | |
element = _post.Element("photo-caption"); | |
post.PhotoCaption = (element == null) ? null : (String)element.Value; | |
element = _post.Element("photo-link-url"); | |
post.PhotoLinkUrl = (element == null) ? null : (String)element.Value; | |
foreach (XElement photoUrl in _post.Descendants("photo-url")) { | |
String maxWidth = (String)photoUrl.Attribute("max-width"); | |
String value = (String)photoUrl.Value; | |
switch (maxWidth) { | |
case "1280": | |
post.PhotoUrlMaxWidth1280 = value; | |
break; | |
case "500": | |
post.PhotoUrlMaxWidth500 = value; | |
break; | |
case "400": | |
post.PhotoUrlMaxWidth400 = value; | |
break; | |
case "250": | |
post.PhotoUrlMaxWidth250 = value; | |
break; | |
case "100": | |
post.PhotoUrlMaxWidth100 = value; | |
break; | |
case "75": | |
post.PhotoUrlMaxWidth75 = value; | |
break; | |
} | |
} | |
break; | |
case "regular": | |
element = _post.Element("regular-title"); | |
post.RegularTitle = (element == null) ? null : (String)element.Value; | |
element = _post.Element("regular-body"); | |
post.RegularBody = (element == null) ? null : (String)element.Value; | |
break; | |
case "link": | |
element = _post.Element("link-text"); | |
post.LinkText = (element == null) ? null : (String)element.Value; | |
element = _post.Element("link-url"); | |
post.LinkUrl = (element == null) ? null : (String)element.Value; | |
element = _post.Element("link-description"); | |
post.LinkDescription = (element == null) ? null : (String)element.Value; | |
break; | |
case "video": | |
element = _post.Element("video-caption"); | |
post.VideoCaption = (element == null) ? null : (String)element.Value; | |
element = _post.Element("video-source"); | |
post.VideoSource = (element == null) ? null : (String)element.Value; | |
element = _post.Element("video-player"); | |
post.VideoPlayer = (element == null) ? null : (String)element.Value; | |
break; | |
case "audio": | |
element = _post.Element("audio-caption"); | |
post.AudioCaption = (element == null) ? null : (String)element.Value; | |
element = _post.Element("audio-player"); | |
post.AudioPlayer = (element == null) ? null : (String)element.Value; | |
element = _post.Element("download-url"); | |
post.DownloadUrl = (element == null) ? null : (String)element.Value; | |
break; | |
case "conversation": | |
element = _post.Element("conversation-title"); | |
post.ConversationTitle = (element == null) ? null : (String)element.Value; | |
element = _post.Element("conversation-text"); | |
post.ConversationText = (element == null) ? null : (String)element.Value; | |
String lines = ""; | |
foreach (XElement line in _post.Descendants("line")) { | |
String label = (String)line.Attribute("label"); | |
String value = (String)line.Value; | |
lines += "<p>" + label + value + "</p>"; | |
} | |
post.Conversation = lines; | |
break; | |
} | |
posts.Add(post); | |
} | |
return posts; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.mitukiii.tumblife.parser; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.util.ArrayList; | |
import java.util.List; | |
import jp.mitukiii.tumblife.model.TLPost; | |
import org.xmlpull.v1.XmlPullParser; | |
import org.xmlpull.v1.XmlPullParserException; | |
public class TLPostParser extends TLParser | |
{ | |
public TLPostParser(InputStream input) | |
throws XmlPullParserException | |
{ | |
super(input); | |
} | |
public List<TLPost> parse() | |
throws NumberFormatException, XmlPullParserException, IOException | |
{ | |
List<TLPost> posts = new ArrayList<TLPost>(50); | |
TLPost post = null; | |
for (int e = parser.getEventType(); e != XmlPullParser.END_DOCUMENT; e = parser.next()) { | |
String tag = parser.getName(); | |
if (e == XmlPullParser.START_TAG) { | |
if ("post".equals(tag)) { | |
post = new TLPost(); | |
post.setId(Long.valueOf(parser.getAttributeValue(NAME_SPACE, "id"))); | |
post.setUrl(parser.getAttributeValue(NAME_SPACE, "url")); | |
post.setUrlWithSlug(parser.getAttributeValue(NAME_SPACE, "url-with-slug")); | |
post.setType(parser.getAttributeValue(NAME_SPACE, "type")); | |
post.setDateGmt(parser.getAttributeValue(NAME_SPACE, "date-gmt")); | |
post.setDate(parser.getAttributeValue(NAME_SPACE, "date")); | |
post.setUnixTimestamp(Integer.valueOf(parser.getAttributeValue(NAME_SPACE, "unix-timestamp"))); | |
post.setFormat(parser.getAttributeValue(NAME_SPACE, "format")); | |
post.setReblogKey(parser.getAttributeValue(NAME_SPACE, "reblog-key")); | |
post.setSlug(parser.getAttributeValue(NAME_SPACE, "slug")); | |
String noteCount = parser.getAttributeValue(NAME_SPACE, "note-count"); | |
if (noteCount == null || noteCount.length() == 0) { | |
post.setNoteCount(0); | |
} else { | |
post.setNoteCount(Integer.valueOf(noteCount)); | |
} | |
} else if ("tumblelog".equals(tag)) { | |
post.setTumblelogTitle(parser.getAttributeValue(NAME_SPACE, "title")); | |
post.setTumblelogName(parser.getAttributeValue(NAME_SPACE, "name")); | |
post.setTumblelogUrl(parser.getAttributeValue(NAME_SPACE, "url")); | |
post.setTumblelogTimezone(parser.getAttributeValue(NAME_SPACE, "timezone")); | |
} else if ("tag".equals(tag)) { | |
post.setTag(parser.nextText()); | |
} else if ("quote-text".equals(tag)) { | |
post.setQuoteText(parser.nextText()); | |
} else if ("quote-source".equals(tag)) { | |
post.setQuoteSource(parser.nextText()); | |
} else if ("photo-caption".equals(tag)) { | |
post.setPhotoCaption(parser.nextText()); | |
} else if ("photo-link-url".equals(tag)) { | |
post.setPhotoLinkUrl(parser.nextText()); | |
} else if ("photo-url".equals(tag)) { | |
String maxWidth = parser.getAttributeValue(NAME_SPACE, "max-width"); | |
if ("1280".equals(maxWidth)) { | |
post.setPhotoUrlMaxWidth1280(parser.nextText()); | |
} else if ("500".equals(maxWidth)) { | |
post.setPhotoUrlMaxWidth500(parser.nextText()); | |
} else if ("400".equals(maxWidth)) { | |
post.setPhotoUrlMaxWidth400(parser.nextText()); | |
} else if ("250".equals(maxWidth)) { | |
post.setPhotoUrlMaxWidth250(parser.nextText()); | |
} else if ("100".equals(maxWidth)) { | |
post.setPhotoUrlMaxWidth100(parser.nextText()); | |
} else if ("75".equals(maxWidth)) { | |
post.setPhotoUrlMaxWidth75(parser.nextText()); | |
} | |
} else if ("link-text".equals(tag)) { | |
post.setLinkText(parser.nextText()); | |
} else if ("link-url".equals(tag)) { | |
post.setLinkUrl(parser.nextText()); | |
} else if ("link-description".equals(tag)) { | |
post.setLinkDescription(parser.nextText()); | |
} else if ("conversation-title".equals(tag)) { | |
post.setConversationTitle(parser.nextText()); | |
} else if ("conversation-text".equals(tag)) { | |
post.setConversationText(parser.nextText()); | |
} else if ("line".equals(tag)) { | |
String beforeText = post.getConversation(); | |
if (beforeText == null) { | |
beforeText = ""; | |
} | |
post.setConversation(beforeText + "<p>" + parser.getAttributeValue(NAME_SPACE, "label") + parser.nextText() + "</p>"); | |
} else if ("video-caption".equals(tag)) { | |
post.setVideoCaption(parser.nextText()); | |
} else if ("video-source".equals(tag)) { | |
post.setVideoSource(parser.nextText()); | |
} else if ("video-player".equals(tag)) { | |
post.setVideoPlayer(parser.nextText()); | |
} else if ("audio-caption".equals(tag)) { | |
post.setAudioCaption(parser.nextText()); | |
} else if ("audio-player".equals(tag)) { | |
post.setAudioPlayer(parser.nextText()); | |
} else if ("download-url".equals(tag)) { | |
post.setDownloadUrl(parser.nextText()); | |
} else if ("regular-title".equals(tag)) { | |
post.setRegularTitle(parser.nextText()); | |
} else if ("regular-body".equals(tag)) { | |
post.setRegularBody(parser.nextText()); | |
} | |
} else if (e == XmlPullParser.END_TAG) { | |
if ("post".equals(tag)) { | |
posts.add(post); | |
} | |
} | |
} | |
return posts; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#import "TLPost.h" | |
@interface TLPostParser : TLParser | |
{ | |
NSMutableArray* posts; | |
TLPost* post; | |
NSString* photoWidth; | |
} | |
- (NSMutableArray*)parse:(NSError**)error; | |
@end | |
@implementation TLPostParser | |
#pragma mark - | |
#pragma mark XML parser action | |
- (NSMutableArray*)parse:(NSError**)error | |
{ | |
[super parse:(NSError**)error]; | |
return posts; | |
} | |
#pragma mark - | |
#pragma mark XML parser delegate | |
- (void)parserDidStartDocument:(NSXMLParser*)parser | |
{ | |
[super parserDidStartDocument:(NSXMLParser*)parser]; | |
posts = [[NSMutableArray alloc] init]; | |
post = [[TLPost alloc] init]; | |
} | |
- (void)parser:(NSXMLParser*)parser | |
didStartElement:(NSString*)elementName | |
namespaceURI:(NSString*)namespaceURI | |
qualifiedName:(NSString*)qName | |
attributes:(NSDictionary*)attributeDict | |
{ | |
[super parser:(NSXMLParser*)parser | |
didStartElement:(NSString*)elementName | |
namespaceURI:(NSString*)namespaceURI | |
qualifiedName:(NSString*)qName | |
attributes:(NSDictionary*)attributeDict]; | |
if ([elementName isEqualToString:@"post"]) { | |
post.id = [attributeDict objectForKey:@"id"]; | |
post.type = [attributeDict objectForKey:@"type"]; | |
post.tumblelog = [attributeDict objectForKey:@"tumblelog"]; | |
post.reblogKey = [attributeDict objectForKey:@"reblog-key"]; | |
post.noteCount = [attributeDict objectForKey:@"note-count"]; | |
post.isPhoto = [post.type isEqualToString:@"photo"]; | |
} else if ([elementName isEqualToString:@"photo-url"]) { | |
if (photoWidth != nil) { | |
[photoWidth release]; | |
} | |
photoWidth = [attributeDict objectForKey:@"max-width"]; | |
} | |
} | |
- (void)parser:(NSXMLParser*)parser | |
didEndElement:(NSString*)elementName | |
namespaceURI:(NSString*)namespaceURI | |
qualifiedName:(NSString*)qName; | |
{ | |
[super parser:(NSXMLParser*)parser | |
didEndElement:(NSString*)elementName | |
namespaceURI:(NSString*)namespaceURI | |
qualifiedName:(NSString*)qName]; | |
if ([elementName isEqualToString:@"post"]) { | |
if (![posts containsObject:post]) { | |
[posts addObject:post]; | |
} | |
post = [[TLPost alloc] init]; | |
} else if ([elementName isEqualToString:@"quote-text"]) { | |
post.quoteText = characters; | |
} else if ([elementName isEqualToString:@"quote-source"]) { | |
post.quoteSource = characters; | |
} else if ([elementName isEqualToString:@"photo-caption"]) { | |
post.photoCaption = characters; | |
} else if ([elementName isEqualToString:@"photo-link-url"]) { | |
post.photoLinkUrl = characters; | |
} else if ([elementName isEqualToString:@"photo-url"] && photoWidth != nil) { | |
if ([photoWidth isEqualToString:@"1280"]) { | |
post.photoUrl1280 = characters; | |
} else if ([photoWidth isEqualToString:@"500"]) { | |
post.photoUrl500 = characters; | |
} | |
photoWidth = nil; | |
} else if ([elementName isEqualToString:@"link-text"]) { | |
post.linkText = characters; | |
} else if ([elementName isEqualToString:@"link-url"]) { | |
post.linkUrl = characters; | |
} else if ([elementName isEqualToString:@"link-description"]) { | |
post.linkDescription = characters; | |
} else if ([elementName isEqualToString:@"regular-title"]) { | |
post.regularTitle = characters; | |
} else if ([elementName isEqualToString:@"regular-body"]) { | |
post.regularBody = characters; | |
} else if ([elementName isEqualToString:@"video-caption"]) { | |
post.videoCaption = characters; | |
} else if ([elementName isEqualToString:@"video-source"]) { | |
post.videoSource = characters; | |
} else if ([elementName isEqualToString:@"audio-caption"]) { | |
post.audioCaption = characters; | |
} else if ([elementName isEqualToString:@"download-url"]) { | |
post.audioDownloadUrl = characters; | |
} else if ([elementName isEqualToString:@"conversation-title"]) { | |
post.conversationTitle = characters; | |
} else if ([elementName isEqualToString:@"conversation-text"]) { | |
post.conversationText = characters; | |
} else if ([elementName isEqualToString:@"line"]) { | |
NSString* beforeText = post.conversation; | |
if (beforeText == nil) { | |
beforeText = @""; | |
} | |
post.conversation = [NSString stringWithFormat:@"%@<p>%@</p>", beforeText, characters]; | |
} | |
} | |
#pragma mark - | |
#pragma mark Memory management | |
- (void)dealloc | |
{ | |
if (posts != nil) { | |
[posts release]; | |
posts = nil; | |
} | |
if (post != nil) { | |
[post release]; | |
post = nil; | |
} | |
if (photoWidth != nil) { | |
[photoWidth release]; | |
photoWidth = nil; | |
} | |
[super dealloc]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment