TFHpple.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // TFHpple.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 "TFHpple.h"
  30. #import "XPathQuery.h"
  31. @interface TFHpple ()
  32. {
  33. NSData * data;
  34. NSString * encoding;
  35. BOOL isXML;
  36. }
  37. @end
  38. @implementation TFHpple
  39. @synthesize data;
  40. @synthesize encoding;
  41. - (id) initWithData:(NSData *)theData encoding:(NSString *)theEncoding isXML:(BOOL)isDataXML
  42. {
  43. if (!(self = [super init])) {
  44. return nil;
  45. }
  46. data = theData;
  47. encoding = theEncoding;
  48. isXML = isDataXML;
  49. return self;
  50. }
  51. - (id) initWithData:(NSData *)theData isXML:(BOOL)isDataXML
  52. {
  53. return [self initWithData:theData encoding:nil isXML:isDataXML];
  54. }
  55. - (id) initWithXMLData:(NSData *)theData encoding:(NSString *)theEncoding
  56. {
  57. return [self initWithData:theData encoding:theEncoding isXML:YES];
  58. }
  59. - (id) initWithXMLData:(NSData *)theData
  60. {
  61. return [self initWithData:theData encoding:nil isXML:YES];
  62. }
  63. - (id) initWithHTMLData:(NSData *)theData encoding:(NSString *)theEncoding
  64. {
  65. return [self initWithData:theData encoding:theEncoding isXML:NO];
  66. }
  67. - (id) initWithHTMLData:(NSData *)theData
  68. {
  69. return [self initWithData:theData encoding:nil isXML:NO];
  70. }
  71. + (TFHpple *) hppleWithData:(NSData *)theData encoding:(NSString *)theEncoding isXML:(BOOL)isDataXML {
  72. return [[[self class] alloc] initWithData:theData encoding:theEncoding isXML:isDataXML];
  73. }
  74. + (TFHpple *) hppleWithData:(NSData *)theData isXML:(BOOL)isDataXML {
  75. return [[self class] hppleWithData:theData encoding:nil isXML:isDataXML];
  76. }
  77. + (TFHpple *) hppleWithHTMLData:(NSData *)theData encoding:(NSString *)theEncoding {
  78. return [[self class] hppleWithData:theData encoding:theEncoding isXML:NO];
  79. }
  80. + (TFHpple *) hppleWithHTMLData:(NSData *)theData {
  81. return [[self class] hppleWithData:theData encoding:nil isXML:NO];
  82. }
  83. + (TFHpple *) hppleWithXMLData:(NSData *)theData encoding:(NSString *)theEncoding {
  84. return [[self class] hppleWithData:theData encoding:theEncoding isXML:YES];
  85. }
  86. + (TFHpple *) hppleWithXMLData:(NSData *)theData {
  87. return [[self class] hppleWithData:theData encoding:nil isXML:YES];
  88. }
  89. #pragma mark -
  90. // Returns all elements at xPath.
  91. - (NSArray *) searchWithXPathQuery:(NSString *)xPathOrCSS
  92. {
  93. NSArray * detailNodes = nil;
  94. if (isXML) {
  95. detailNodes = PerformXMLXPathQueryWithEncoding(data, xPathOrCSS, encoding);
  96. } else {
  97. detailNodes = PerformHTMLXPathQueryWithEncoding(data, xPathOrCSS, encoding);
  98. }
  99. NSMutableArray * hppleElements = [NSMutableArray array];
  100. for (id node in detailNodes) {
  101. [hppleElements addObject:[TFHppleElement hppleElementWithNode:node isXML:isXML withEncoding:encoding]];
  102. }
  103. return hppleElements;
  104. }
  105. // Returns first element at xPath
  106. - (TFHppleElement *) peekAtSearchWithXPathQuery:(NSString *)xPathOrCSS
  107. {
  108. NSArray * elements = [self searchWithXPathQuery:xPathOrCSS];
  109. if ([elements count] >= 1) {
  110. return [elements objectAtIndex:0];
  111. }
  112. return nil;
  113. }
  114. @end