Guest User

Untitled

a guest
Jun 18th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. #import <Foundation/Foundation.h>
  2. #import "TLPost.h"
  3.  
  4. @interface TLPostParser : TLParser
  5. {
  6. NSMutableArray* posts;
  7. TLPost* post;
  8.  
  9. NSString* photoWidth;
  10. }
  11.  
  12. - (NSMutableArray*)parse:(NSError**)error;
  13.  
  14. @end
  15.  
  16. @implementation TLPostParser
  17.  
  18. #pragma mark -
  19. #pragma mark XML parser action
  20.  
  21. - (NSMutableArray*)parse:(NSError**)error
  22. {
  23. [super parse:(NSError**)error];
  24. return posts;
  25. }
  26.  
  27. #pragma mark -
  28. #pragma mark XML parser delegate
  29.  
  30. - (void)parserDidStartDocument:(NSXMLParser*)parser
  31. {
  32. [super parserDidStartDocument:(NSXMLParser*)parser];
  33.  
  34. posts = [[NSMutableArray alloc] init];
  35. post = [[TLPost alloc] init];
  36. }
  37.  
  38. - (void)parser:(NSXMLParser*)parser
  39. didStartElement:(NSString*)elementName
  40. namespaceURI:(NSString*)namespaceURI
  41. qualifiedName:(NSString*)qName
  42. attributes:(NSDictionary*)attributeDict
  43. {
  44. [super parser:(NSXMLParser*)parser
  45. didStartElement:(NSString*)elementName
  46. namespaceURI:(NSString*)namespaceURI
  47. qualifiedName:(NSString*)qName
  48. attributes:(NSDictionary*)attributeDict];
  49.  
  50. if ([elementName isEqualToString:@"post"]) {
  51. post.id = [attributeDict objectForKey:@"id"];
  52. post.type = [attributeDict objectForKey:@"type"];
  53. post.tumblelog = [attributeDict objectForKey:@"tumblelog"];
  54. post.reblogKey = [attributeDict objectForKey:@"reblog-key"];
  55. post.noteCount = [attributeDict objectForKey:@"note-count"];
  56. post.isPhoto = [post.type isEqualToString:@"photo"];
  57. } else if ([elementName isEqualToString:@"photo-url"]) {
  58. if (photoWidth != nil) {
  59. [photoWidth release];
  60. }
  61. photoWidth = [attributeDict objectForKey:@"max-width"];
  62. }
  63. }
  64.  
  65. - (void)parser:(NSXMLParser*)parser
  66. didEndElement:(NSString*)elementName
  67. namespaceURI:(NSString*)namespaceURI
  68. qualifiedName:(NSString*)qName;
  69. {
  70. [super parser:(NSXMLParser*)parser
  71. didEndElement:(NSString*)elementName
  72. namespaceURI:(NSString*)namespaceURI
  73. qualifiedName:(NSString*)qName];
  74.  
  75. if ([elementName isEqualToString:@"post"]) {
  76. if (![posts containsObject:post]) {
  77. [posts addObject:post];
  78. }
  79. post = [[TLPost alloc] init];
  80. } else if ([elementName isEqualToString:@"quote-text"]) {
  81. post.quoteText = characters;
  82. } else if ([elementName isEqualToString:@"quote-source"]) {
  83. post.quoteSource = characters;
  84. } else if ([elementName isEqualToString:@"photo-caption"]) {
  85. post.photoCaption = characters;
  86. } else if ([elementName isEqualToString:@"photo-link-url"]) {
  87. post.photoLinkUrl = characters;
  88. } else if ([elementName isEqualToString:@"photo-url"] && photoWidth != nil) {
  89. if ([photoWidth isEqualToString:@"1280"]) {
  90. post.photoUrl1280 = characters;
  91. } else if ([photoWidth isEqualToString:@"500"]) {
  92. post.photoUrl500 = characters;
  93. }
  94. photoWidth = nil;
  95. } else if ([elementName isEqualToString:@"link-text"]) {
  96. post.linkText = characters;
  97. } else if ([elementName isEqualToString:@"link-url"]) {
  98. post.linkUrl = characters;
  99. } else if ([elementName isEqualToString:@"link-description"]) {
  100. post.linkDescription = characters;
  101. } else if ([elementName isEqualToString:@"regular-title"]) {
  102. post.regularTitle = characters;
  103. } else if ([elementName isEqualToString:@"regular-body"]) {
  104. post.regularBody = characters;
  105. } else if ([elementName isEqualToString:@"video-caption"]) {
  106. post.videoCaption = characters;
  107. } else if ([elementName isEqualToString:@"video-source"]) {
  108. post.videoSource = characters;
  109. } else if ([elementName isEqualToString:@"audio-caption"]) {
  110. post.audioCaption = characters;
  111. } else if ([elementName isEqualToString:@"download-url"]) {
  112. post.audioDownloadUrl = characters;
  113. } else if ([elementName isEqualToString:@"conversation-title"]) {
  114. post.conversationTitle = characters;
  115. } else if ([elementName isEqualToString:@"conversation-text"]) {
  116. post.conversationText = characters;
  117. } else if ([elementName isEqualToString:@"line"]) {
  118. NSString* beforeText = post.conversation;
  119. if (beforeText == nil) {
  120. beforeText = @"";
  121. }
  122. post.conversation = [NSString stringWithFormat:@"%@<p>%@</p>", beforeText, characters];
  123. }
  124. }
  125.  
  126. #pragma mark -
  127. #pragma mark Memory management
  128.  
  129. - (void)dealloc
  130. {
  131. if (posts != nil) {
  132. [posts release];
  133. posts = nil;
  134. }
  135. if (post != nil) {
  136. [post release];
  137. post = nil;
  138. }
  139. if (photoWidth != nil) {
  140. [photoWidth release];
  141. photoWidth = nil;
  142. }
  143. [super dealloc];
  144. }
  145.  
  146. @end
Add Comment
Please, Sign In to add comment