Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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
Add Comment
Please, Sign In to add comment