TFHppleElement.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. //
  2. // TFHppleElement.m
  3. // Hpple
  4. //
  5. // Created by Geoffrey Grosenbach on 1/31/09.
  6. //
  7. // Copyright (c) 2009 Topfunky Corporation, http://topfunky.com
  8. //
  9. // MIT LICENSE
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. #import "TFHppleElement.h"
  30. #import "XPathQuery.h"
  31. static NSString * const TFHppleNodeContentKey = @"nodeContent";
  32. static NSString * const TFHppleNodeNameKey = @"nodeName";
  33. static NSString * const TFHppleNodeChildrenKey = @"nodeChildArray";
  34. static NSString * const TFHppleNodeAttributeArrayKey = @"nodeAttributeArray";
  35. static NSString * const TFHppleNodeAttributeNameKey = @"attributeName";
  36. static NSString * const TFHppleTextNodeName = @"text";
  37. @interface TFHppleElement ()
  38. {
  39. NSDictionary * node;
  40. BOOL isXML;
  41. NSString *encoding;
  42. __unsafe_unretained TFHppleElement *parent;
  43. }
  44. @property (nonatomic, unsafe_unretained, readwrite) TFHppleElement *parent;
  45. @end
  46. @implementation TFHppleElement
  47. @synthesize parent;
  48. - (id) initWithNode:(NSDictionary *) theNode isXML:(BOOL)isDataXML withEncoding:(NSString *)theEncoding
  49. {
  50. if (!(self = [super init]))
  51. return nil;
  52. isXML = isDataXML;
  53. node = theNode;
  54. encoding = theEncoding;
  55. return self;
  56. }
  57. + (TFHppleElement *) hppleElementWithNode:(NSDictionary *) theNode isXML:(BOOL)isDataXML withEncoding:(NSString *)theEncoding
  58. {
  59. return [[[self class] alloc] initWithNode:theNode isXML:isDataXML withEncoding:theEncoding];
  60. }
  61. #pragma mark -
  62. - (NSString *)raw
  63. {
  64. return [node objectForKey:@"raw"];
  65. }
  66. - (NSString *) content
  67. {
  68. return [node objectForKey:TFHppleNodeContentKey];
  69. }
  70. - (NSString *) tagName
  71. {
  72. return [node objectForKey:TFHppleNodeNameKey];
  73. }
  74. - (NSArray *) children
  75. {
  76. NSMutableArray *children = [NSMutableArray array];
  77. for (NSDictionary *child in [node objectForKey:TFHppleNodeChildrenKey]) {
  78. TFHppleElement *element = [TFHppleElement hppleElementWithNode:child isXML:isXML withEncoding:encoding];
  79. element.parent = self;
  80. [children addObject:element];
  81. }
  82. return children;
  83. }
  84. - (TFHppleElement *) firstChild
  85. {
  86. NSArray * children = self.children;
  87. if (children.count)
  88. return [children objectAtIndex:0];
  89. return nil;
  90. }
  91. - (NSDictionary *) attributes
  92. {
  93. NSMutableDictionary * translatedAttributes = [NSMutableDictionary dictionary];
  94. for (NSDictionary * attributeDict in [node objectForKey:TFHppleNodeAttributeArrayKey]) {
  95. if ([attributeDict objectForKey:TFHppleNodeContentKey] && [attributeDict objectForKey:TFHppleNodeAttributeNameKey]) {
  96. [translatedAttributes setObject:[attributeDict objectForKey:TFHppleNodeContentKey]
  97. forKey:[attributeDict objectForKey:TFHppleNodeAttributeNameKey]];
  98. }
  99. }
  100. return translatedAttributes;
  101. }
  102. - (NSString *) objectForKey:(NSString *) theKey
  103. {
  104. return [[self attributes] objectForKey:theKey];
  105. }
  106. - (id) description
  107. {
  108. return [node description];
  109. }
  110. - (BOOL)hasChildren
  111. {
  112. if ([node objectForKey:TFHppleNodeChildrenKey])
  113. return YES;
  114. else
  115. return NO;
  116. }
  117. - (BOOL)isTextNode
  118. {
  119. // we must distinguish between real text nodes and standard nodes with tha name "text" (<text>)
  120. // real text nodes must have content
  121. if ([self.tagName isEqualToString:TFHppleTextNodeName] && (self.content))
  122. return YES;
  123. else
  124. return NO;
  125. }
  126. - (NSArray*) childrenWithTagName:(NSString*)tagName
  127. {
  128. NSMutableArray* matches = [NSMutableArray array];
  129. for (TFHppleElement* child in self.children)
  130. {
  131. if ([child.tagName isEqualToString:tagName])
  132. [matches addObject:child];
  133. }
  134. return matches;
  135. }
  136. - (TFHppleElement *) firstChildWithTagName:(NSString*)tagName
  137. {
  138. for (TFHppleElement* child in self.children)
  139. {
  140. if ([child.tagName isEqualToString:tagName])
  141. return child;
  142. }
  143. return nil;
  144. }
  145. - (NSArray*) childrenWithClassName:(NSString*)className
  146. {
  147. NSMutableArray* matches = [NSMutableArray array];
  148. for (TFHppleElement* child in self.children)
  149. {
  150. if ([[child objectForKey:@"class"] isEqualToString:className])
  151. [matches addObject:child];
  152. }
  153. return matches;
  154. }
  155. - (TFHppleElement *) firstChildWithClassName:(NSString*)className
  156. {
  157. for (TFHppleElement* child in self.children)
  158. {
  159. if ([[child objectForKey:@"class"] isEqualToString:className])
  160. return child;
  161. }
  162. return nil;
  163. }
  164. - (TFHppleElement *) firstTextChild
  165. {
  166. for (TFHppleElement* child in self.children)
  167. {
  168. if ([child isTextNode])
  169. return child;
  170. }
  171. return [self firstChildWithTagName:TFHppleTextNodeName];
  172. }
  173. - (NSString *) text
  174. {
  175. return self.firstTextChild.content;
  176. }
  177. // Returns all elements at xPath.
  178. - (NSArray *) searchWithXPathQuery:(NSString *)xPathOrCSS
  179. {
  180. NSData *data = [self.raw dataUsingEncoding:NSUTF8StringEncoding];
  181. NSArray * detailNodes = nil;
  182. if (isXML) {
  183. detailNodes = PerformXMLXPathQueryWithEncoding(data, xPathOrCSS, encoding);
  184. } else {
  185. detailNodes = PerformHTMLXPathQueryWithEncoding(data, xPathOrCSS, encoding);
  186. }
  187. NSMutableArray * hppleElements = [NSMutableArray array];
  188. for (id newNode in detailNodes) {
  189. [hppleElements addObject:[TFHppleElement hppleElementWithNode:newNode isXML:isXML withEncoding:encoding]];
  190. }
  191. return hppleElements;
  192. }
  193. // Custom keyed subscripting
  194. - (id)objectForKeyedSubscript:(id)key
  195. {
  196. return [self objectForKey:key];
  197. }
  198. @end