motion-hpple 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +52 -0
- data/README.md +1 -7
- data/Rakefile +16 -1
- data/lib/motion-hpple.rb +2 -4
- data/lib/motion-hpple/version.rb +1 -1
- data/motion-hpple.gemspec +1 -0
- data/vendor/Podfile.lock +11 -0
- data/vendor/{hpple → Pods/.build/Pods}/TFHpple.h +1 -1
- data/vendor/{hpple → Pods/.build/Pods}/TFHppleElement.h +7 -7
- data/vendor/Pods/.build/Pods/XPathQuery.h +10 -0
- data/vendor/Pods/.build/libPods.a +0 -0
- data/vendor/Pods/BuildHeaders/hpple/TFHpple.h +54 -0
- data/vendor/Pods/BuildHeaders/hpple/TFHppleElement.h +68 -0
- data/vendor/Pods/BuildHeaders/hpple/XPathQuery.h +10 -0
- data/vendor/Pods/Documentation/hpple/docset-installed.txt +4 -0
- data/vendor/Pods/Documentation/hpple/docset/Contents/Info.plist +39 -0
- data/vendor/Pods/Documentation/hpple/docset/Contents/Resources/Documents/Classes/TFHpple.html +470 -0
- data/vendor/Pods/Documentation/hpple/docset/Contents/Resources/Documents/Classes/TFHppleElement.html +470 -0
- data/vendor/Pods/Documentation/hpple/docset/Contents/Resources/Documents/css/styles.css +584 -0
- data/vendor/Pods/Documentation/hpple/docset/Contents/Resources/Documents/css/stylesPrint.css +22 -0
- data/vendor/Pods/Documentation/hpple/docset/Contents/Resources/Documents/hierarchy.html +72 -0
- data/vendor/Pods/Documentation/hpple/docset/Contents/Resources/Documents/img/button_bar_background.png +0 -0
- data/vendor/Pods/Documentation/hpple/docset/Contents/Resources/Documents/img/disclosure.png +0 -0
- data/vendor/Pods/Documentation/hpple/docset/Contents/Resources/Documents/img/disclosure_open.png +0 -0
- data/vendor/Pods/Documentation/hpple/docset/Contents/Resources/Documents/img/library_background.png +0 -0
- data/vendor/Pods/Documentation/hpple/docset/Contents/Resources/Documents/img/title_background.png +0 -0
- data/vendor/Pods/Documentation/hpple/docset/Contents/Resources/Documents/index.html +135 -0
- data/vendor/Pods/Documentation/hpple/docset/Contents/Resources/Nodes.xml +39 -0
- data/vendor/Pods/Documentation/hpple/docset/Contents/Resources/Tokens1.xml +125 -0
- data/vendor/Pods/Documentation/hpple/docset/Contents/Resources/Tokens2.xml +180 -0
- data/vendor/Pods/Documentation/hpple/docset/Contents/Resources/docSet.dsidx +0 -0
- data/vendor/Pods/Documentation/hpple/docset/Contents/Resources/docSet.mom +0 -0
- data/vendor/Pods/Documentation/hpple/docset/Contents/Resources/docSet.skidx +0 -0
- data/vendor/Pods/Documentation/hpple/docset/Contents/Resources/docSet.toc +0 -0
- data/vendor/Pods/Documentation/hpple/html/Classes/TFHpple.html +470 -0
- data/vendor/Pods/Documentation/hpple/html/Classes/TFHppleElement.html +470 -0
- data/vendor/Pods/Documentation/hpple/html/css/styles.css +584 -0
- data/vendor/Pods/Documentation/hpple/html/css/stylesPrint.css +22 -0
- data/vendor/Pods/Documentation/hpple/html/hierarchy.html +72 -0
- data/vendor/Pods/Documentation/hpple/html/img/button_bar_background.png +0 -0
- data/vendor/Pods/Documentation/hpple/html/img/disclosure.png +0 -0
- data/vendor/Pods/Documentation/hpple/html/img/disclosure_open.png +0 -0
- data/vendor/Pods/Documentation/hpple/html/img/library_background.png +0 -0
- data/vendor/Pods/Documentation/hpple/html/img/title_background.png +0 -0
- data/vendor/Pods/Documentation/hpple/html/index.html +135 -0
- data/vendor/Pods/Headers/hpple/TFHpple.h +54 -0
- data/vendor/Pods/Headers/hpple/TFHppleElement.h +68 -0
- data/vendor/Pods/Headers/hpple/XPathQuery.h +10 -0
- data/vendor/Pods/Pods-Acknowledgements.markdown +29 -0
- data/vendor/Pods/Pods-Acknowledgements.plist +59 -0
- data/vendor/Pods/Pods-prefix.pch +3 -0
- data/vendor/Pods/Pods-resources.sh +19 -0
- data/vendor/Pods/Pods.bridgesupport +104 -0
- data/vendor/Pods/Pods.xcconfig +7 -0
- data/vendor/Pods/Pods.xcodeproj/project.pbxproj +633 -0
- data/vendor/Pods/PodsDummy_Pods.m +4 -0
- data/vendor/Pods/build-iPhoneSimulator/libPods.a +0 -0
- data/vendor/Pods/hpple/LICENSE.txt +22 -0
- data/vendor/Pods/hpple/README.markdown +50 -0
- data/vendor/Pods/hpple/TFHpple.h +54 -0
- data/vendor/Pods/hpple/TFHpple.m +102 -0
- data/vendor/Pods/hpple/TFHppleElement.h +68 -0
- data/vendor/Pods/hpple/TFHppleElement.m +114 -0
- data/vendor/Pods/hpple/XPathQuery.h +10 -0
- data/vendor/Pods/hpple/XPathQuery.m +197 -0
- metadata +86 -8
- data/vendor/hpple/hpple.bridgesupport +0 -78
- data/vendor/hpple/libHpple.a +0 -0
Binary file
|
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2009 Topfunky Corporation, http://topfunky.com
|
2
|
+
|
3
|
+
MIT LICENSE
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# DESCRIPTION
|
2
|
+
|
3
|
+
EXPERIMENTAL! Only a few hours old.
|
4
|
+
|
5
|
+
Hpple: A nice Objective-C wrapper on the XPathQuery library for parsing HTML.
|
6
|
+
|
7
|
+
Inspired by why the lucky stiff's [Hpricot](http://github.com/why/hpricot/tree/master).
|
8
|
+
|
9
|
+
# AUTHOR
|
10
|
+
|
11
|
+
Geoffrey Grosenbach, [Topfunky Corporation](http://topfunky.com) and [PeepCode Screencasts](http://peepcode.com).
|
12
|
+
|
13
|
+
# FEATURES
|
14
|
+
|
15
|
+
* Easy searching by XPath (CSS selectors are planned)
|
16
|
+
* Parses HTML (XML coming soon)
|
17
|
+
* Easy access to tag content, name, and attributes.
|
18
|
+
|
19
|
+
# INSTALLATION
|
20
|
+
|
21
|
+
* Open your XCode project and the Hpple project.
|
22
|
+
* Drag the "Hpple" directory to your project.
|
23
|
+
* Add the libxml2.2.dylib framework to your project and search paths as described at [Cocoa with Love](http://cocoawithlove.com/2008/10/using-libxml2-for-parsing-and-xpath.html)
|
24
|
+
|
25
|
+
More documentation and short screencast coming soon...
|
26
|
+
|
27
|
+
# USAGE
|
28
|
+
|
29
|
+
See TFHppleHTMLTest.m in the Hpple project for samples.
|
30
|
+
|
31
|
+
<pre>
|
32
|
+
#import "TFHpple.h"
|
33
|
+
|
34
|
+
NSData * data = [NSData dataWithContentsOfFile:@"index.html"];
|
35
|
+
|
36
|
+
TFHpple * doc = [[TFHpple alloc] initWithHTMLData:data];
|
37
|
+
NSArray * elements = [doc search:@"//a[@class='sponsor']"];
|
38
|
+
|
39
|
+
TFHppleElement * element = [elements objectAtIndex:0];
|
40
|
+
[e content]; // Tag's innerHTML
|
41
|
+
[e tagName]; // "a"
|
42
|
+
[e attributes]; // NSDictionary of href, class, id, etc.
|
43
|
+
[e objectForKey:@"href"]; // Easy access to single attribute
|
44
|
+
|
45
|
+
</pre>
|
46
|
+
|
47
|
+
# TODO
|
48
|
+
|
49
|
+
* Internal error catching and messages
|
50
|
+
* CSS3 selectors in addition to XPath
|
@@ -0,0 +1,54 @@
|
|
1
|
+
//
|
2
|
+
// TFHpple.h
|
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
|
+
|
30
|
+
|
31
|
+
#import <Foundation/Foundation.h>
|
32
|
+
|
33
|
+
#import "TFHppleElement.h"
|
34
|
+
|
35
|
+
@interface TFHpple : NSObject {
|
36
|
+
@private
|
37
|
+
NSData * data;
|
38
|
+
BOOL isXML;
|
39
|
+
}
|
40
|
+
|
41
|
+
- (id) initWithData:(NSData *)theData isXML:(BOOL)isDataXML;
|
42
|
+
- (id) initWithXMLData:(NSData *)theData;
|
43
|
+
- (id) initWithHTMLData:(NSData *)theData;
|
44
|
+
|
45
|
+
+ (TFHpple *) hppleWithData:(NSData *)theData isXML:(BOOL)isDataXML;
|
46
|
+
+ (TFHpple *) hppleWithXMLData:(NSData *)theData;
|
47
|
+
+ (TFHpple *) hppleWithHTMLData:(NSData *)theData;
|
48
|
+
|
49
|
+
- (NSArray *) searchWithXPathQuery:(NSString *)xPathOrCSS;
|
50
|
+
- (TFHppleElement *) peekAtSearchWithXPathQuery:(NSString *)xPathOrCSS;
|
51
|
+
|
52
|
+
@property (nonatomic, strong, readonly) NSData * data;
|
53
|
+
|
54
|
+
@end
|
@@ -0,0 +1,102 @@
|
|
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
|
+
|
30
|
+
#import "TFHpple.h"
|
31
|
+
#import "XPathQuery.h"
|
32
|
+
|
33
|
+
@implementation TFHpple
|
34
|
+
|
35
|
+
@synthesize data;
|
36
|
+
|
37
|
+
|
38
|
+
- (id) initWithData:(NSData *)theData isXML:(BOOL)isDataXML
|
39
|
+
{
|
40
|
+
if (!(self = [super init])) {
|
41
|
+
return nil;
|
42
|
+
}
|
43
|
+
|
44
|
+
data = theData;
|
45
|
+
isXML = isDataXML;
|
46
|
+
|
47
|
+
return self;
|
48
|
+
}
|
49
|
+
|
50
|
+
- (id) initWithXMLData:(NSData *)theData
|
51
|
+
{
|
52
|
+
return [self initWithData:theData isXML:YES];
|
53
|
+
}
|
54
|
+
|
55
|
+
- (id) initWithHTMLData:(NSData *)theData
|
56
|
+
{
|
57
|
+
return [self initWithData:theData isXML:NO];
|
58
|
+
}
|
59
|
+
|
60
|
+
+ (TFHpple *) hppleWithData:(NSData *)theData isXML:(BOOL)isDataXML {
|
61
|
+
return [[[self class] alloc] initWithData:theData isXML:isDataXML];
|
62
|
+
}
|
63
|
+
|
64
|
+
+ (TFHpple *) hppleWithHTMLData:(NSData *)theData {
|
65
|
+
return [[self class] hppleWithData:theData isXML:NO];
|
66
|
+
}
|
67
|
+
|
68
|
+
+ (TFHpple *) hppleWithXMLData:(NSData *)theData {
|
69
|
+
return [[self class] hppleWithData:theData isXML:YES];
|
70
|
+
}
|
71
|
+
|
72
|
+
#pragma mark -
|
73
|
+
|
74
|
+
// Returns all elements at xPath.
|
75
|
+
- (NSArray *) searchWithXPathQuery:(NSString *)xPathOrCSS
|
76
|
+
{
|
77
|
+
NSArray * detailNodes = nil;
|
78
|
+
if (isXML) {
|
79
|
+
detailNodes = PerformXMLXPathQuery(data, xPathOrCSS);
|
80
|
+
} else {
|
81
|
+
detailNodes = PerformHTMLXPathQuery(data, xPathOrCSS);
|
82
|
+
}
|
83
|
+
|
84
|
+
NSMutableArray * hppleElements = [NSMutableArray array];
|
85
|
+
for (id node in detailNodes) {
|
86
|
+
[hppleElements addObject:[TFHppleElement hppleElementWithNode:node]];
|
87
|
+
}
|
88
|
+
return hppleElements;
|
89
|
+
}
|
90
|
+
|
91
|
+
// Returns first element at xPath
|
92
|
+
- (TFHppleElement *) peekAtSearchWithXPathQuery:(NSString *)xPathOrCSS
|
93
|
+
{
|
94
|
+
NSArray * elements = [self searchWithXPathQuery:xPathOrCSS];
|
95
|
+
if ([elements count] >= 1) {
|
96
|
+
return [elements objectAtIndex:0];
|
97
|
+
}
|
98
|
+
|
99
|
+
return nil;
|
100
|
+
}
|
101
|
+
|
102
|
+
@end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
//
|
2
|
+
// TFHppleElement.h
|
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
|
+
|
30
|
+
#import <Foundation/Foundation.h>
|
31
|
+
|
32
|
+
|
33
|
+
@interface TFHppleElement : NSObject {
|
34
|
+
@private
|
35
|
+
|
36
|
+
NSDictionary * node;
|
37
|
+
__unsafe_unretained TFHppleElement *parent;
|
38
|
+
}
|
39
|
+
|
40
|
+
- (id) initWithNode:(NSDictionary *) theNode;
|
41
|
+
|
42
|
+
+ (TFHppleElement *) hppleElementWithNode:(NSDictionary *) theNode;
|
43
|
+
|
44
|
+
// Returns this tag's innerHTML content.
|
45
|
+
@property (nonatomic, copy, readonly) NSString *content;
|
46
|
+
|
47
|
+
// Returns the name of the current tag, such as "h3".
|
48
|
+
@property (nonatomic, copy, readonly) NSString *tagName;
|
49
|
+
|
50
|
+
// Returns tag attributes with name as key and content as value.
|
51
|
+
// href = 'http://peepcode.com'
|
52
|
+
// class = 'highlight'
|
53
|
+
@property (nonatomic, strong, readonly) NSDictionary *attributes;
|
54
|
+
|
55
|
+
// Returns the children of a given node
|
56
|
+
@property (nonatomic, strong, readonly) NSArray *children;
|
57
|
+
|
58
|
+
// Returns the first child of a given node
|
59
|
+
@property (nonatomic, strong, readonly) TFHppleElement *firstChild;
|
60
|
+
|
61
|
+
// the parent of a node
|
62
|
+
@property (nonatomic, unsafe_unretained, readonly) TFHppleElement *parent;
|
63
|
+
|
64
|
+
// Provides easy access to the content of a specific attribute,
|
65
|
+
// such as 'href' or 'class'.
|
66
|
+
- (NSString *) objectForKey:(NSString *) theKey;
|
67
|
+
|
68
|
+
@end
|
@@ -0,0 +1,114 @@
|
|
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
|
+
|
30
|
+
|
31
|
+
#import "TFHppleElement.h"
|
32
|
+
|
33
|
+
static NSString * const TFHppleNodeContentKey = @"nodeContent";
|
34
|
+
static NSString * const TFHppleNodeNameKey = @"nodeName";
|
35
|
+
static NSString * const TFHppleNodeChildrenKey = @"nodeChildArray";
|
36
|
+
static NSString * const TFHppleNodeAttributeArrayKey = @"nodeAttributeArray";
|
37
|
+
static NSString * const TFHppleNodeAttributeNameKey = @"attributeName";
|
38
|
+
|
39
|
+
@interface TFHppleElement ()
|
40
|
+
@property (nonatomic, unsafe_unretained, readwrite) TFHppleElement *parent;
|
41
|
+
@end
|
42
|
+
|
43
|
+
@implementation TFHppleElement
|
44
|
+
@synthesize parent;
|
45
|
+
|
46
|
+
|
47
|
+
- (id) initWithNode:(NSDictionary *) theNode
|
48
|
+
{
|
49
|
+
if (!(self = [super init]))
|
50
|
+
return nil;
|
51
|
+
|
52
|
+
node = theNode;
|
53
|
+
|
54
|
+
return self;
|
55
|
+
}
|
56
|
+
|
57
|
+
+ (TFHppleElement *) hppleElementWithNode:(NSDictionary *) theNode {
|
58
|
+
return [[[self class] alloc] initWithNode:theNode];
|
59
|
+
}
|
60
|
+
|
61
|
+
#pragma mark -
|
62
|
+
|
63
|
+
- (NSString *) content
|
64
|
+
{
|
65
|
+
return [node objectForKey:TFHppleNodeContentKey];
|
66
|
+
}
|
67
|
+
|
68
|
+
|
69
|
+
- (NSString *) tagName
|
70
|
+
{
|
71
|
+
return [node objectForKey:TFHppleNodeNameKey];
|
72
|
+
}
|
73
|
+
|
74
|
+
- (NSArray *) children
|
75
|
+
{
|
76
|
+
NSMutableArray *children = [NSMutableArray array];
|
77
|
+
for (NSDictionary *child in [node objectForKey:TFHppleNodeChildrenKey]) {
|
78
|
+
TFHppleElement *element = [TFHppleElement hppleElementWithNode:child];
|
79
|
+
element.parent = self;
|
80
|
+
[children addObject:element];
|
81
|
+
}
|
82
|
+
return children;
|
83
|
+
}
|
84
|
+
|
85
|
+
- (TFHppleElement *) firstChild
|
86
|
+
{
|
87
|
+
NSArray * children = self.children;
|
88
|
+
if (children.count)
|
89
|
+
return [children objectAtIndex:0];
|
90
|
+
return nil;
|
91
|
+
}
|
92
|
+
|
93
|
+
|
94
|
+
- (NSDictionary *) attributes
|
95
|
+
{
|
96
|
+
NSMutableDictionary * translatedAttributes = [NSMutableDictionary dictionary];
|
97
|
+
for (NSDictionary * attributeDict in [node objectForKey:TFHppleNodeAttributeArrayKey]) {
|
98
|
+
[translatedAttributes setObject:[attributeDict objectForKey:TFHppleNodeContentKey]
|
99
|
+
forKey:[attributeDict objectForKey:TFHppleNodeAttributeNameKey]];
|
100
|
+
}
|
101
|
+
return translatedAttributes;
|
102
|
+
}
|
103
|
+
|
104
|
+
- (NSString *) objectForKey:(NSString *) theKey
|
105
|
+
{
|
106
|
+
return [[self attributes] objectForKey:theKey];
|
107
|
+
}
|
108
|
+
|
109
|
+
- (id) description
|
110
|
+
{
|
111
|
+
return [node description];
|
112
|
+
}
|
113
|
+
|
114
|
+
@end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
//
|
2
|
+
// XPathQuery.h
|
3
|
+
// FuelFinder
|
4
|
+
//
|
5
|
+
// Created by Matt Gallagher on 4/08/08.
|
6
|
+
// Copyright 2008 __MyCompanyName__. All rights reserved.
|
7
|
+
//
|
8
|
+
|
9
|
+
NSArray *PerformHTMLXPathQuery(NSData *document, NSString *query);
|
10
|
+
NSArray *PerformXMLXPathQuery(NSData *document, NSString *query);
|
@@ -0,0 +1,197 @@
|
|
1
|
+
//
|
2
|
+
// XPathQuery.m
|
3
|
+
// FuelFinder
|
4
|
+
//
|
5
|
+
// Created by Matt Gallagher on 4/08/08.
|
6
|
+
// Copyright 2008 __MyCompanyName__. All rights reserved.
|
7
|
+
//
|
8
|
+
|
9
|
+
#import "XPathQuery.h"
|
10
|
+
|
11
|
+
#import <libxml/tree.h>
|
12
|
+
#import <libxml/parser.h>
|
13
|
+
#import <libxml/HTMLparser.h>
|
14
|
+
#import <libxml/xpath.h>
|
15
|
+
#import <libxml/xpathInternals.h>
|
16
|
+
|
17
|
+
NSDictionary *DictionaryForNode(xmlNodePtr currentNode, NSMutableDictionary *parentResult,BOOL parentContent);
|
18
|
+
NSArray *PerformXPathQuery(xmlDocPtr doc, NSString *query);
|
19
|
+
|
20
|
+
NSDictionary *DictionaryForNode(xmlNodePtr currentNode, NSMutableDictionary *parentResult,BOOL parentContent)
|
21
|
+
{
|
22
|
+
NSMutableDictionary *resultForNode = [NSMutableDictionary dictionary];
|
23
|
+
|
24
|
+
if (currentNode->name)
|
25
|
+
{
|
26
|
+
NSString *currentNodeContent =
|
27
|
+
[NSString stringWithCString:(const char *)currentNode->name encoding:NSUTF8StringEncoding];
|
28
|
+
[resultForNode setObject:currentNodeContent forKey:@"nodeName"];
|
29
|
+
}
|
30
|
+
|
31
|
+
if (currentNode->content && currentNode->content != (xmlChar *)-1)
|
32
|
+
{
|
33
|
+
NSString *currentNodeContent =
|
34
|
+
[NSString stringWithCString:(const char *)currentNode->content encoding:NSUTF8StringEncoding];
|
35
|
+
|
36
|
+
if ([[resultForNode objectForKey:@"nodeName"] isEqual:@"text"] && parentResult)
|
37
|
+
{
|
38
|
+
if(parentContent)
|
39
|
+
{
|
40
|
+
[parentResult setObject:[currentNodeContent stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] forKey:@"nodeContent"];
|
41
|
+
return nil;
|
42
|
+
}
|
43
|
+
[resultForNode setObject:currentNodeContent forKey:@"nodeContent"];
|
44
|
+
// NSLog(@"content: %@",currentNodeContent);
|
45
|
+
return resultForNode;
|
46
|
+
|
47
|
+
}
|
48
|
+
else {
|
49
|
+
[resultForNode setObject:currentNodeContent forKey:@"nodeContent"];
|
50
|
+
}
|
51
|
+
|
52
|
+
|
53
|
+
}
|
54
|
+
|
55
|
+
xmlAttr *attribute = currentNode->properties;
|
56
|
+
if (attribute)
|
57
|
+
{
|
58
|
+
NSMutableArray *attributeArray = [NSMutableArray array];
|
59
|
+
while (attribute)
|
60
|
+
{
|
61
|
+
NSMutableDictionary *attributeDictionary = [NSMutableDictionary dictionary];
|
62
|
+
NSString *attributeName =
|
63
|
+
[NSString stringWithCString:(const char *)attribute->name encoding:NSUTF8StringEncoding];
|
64
|
+
if (attributeName)
|
65
|
+
{
|
66
|
+
// NSLog(@"Attribute Name Set: %@",attributeName);
|
67
|
+
[attributeDictionary setObject:attributeName forKey:@"attributeName"];
|
68
|
+
}
|
69
|
+
|
70
|
+
if (attribute->children)
|
71
|
+
{
|
72
|
+
NSDictionary *childDictionary = DictionaryForNode(attribute->children, attributeDictionary,true);
|
73
|
+
if (childDictionary)
|
74
|
+
{
|
75
|
+
[attributeDictionary setObject:childDictionary forKey:@"attributeContent"];
|
76
|
+
}
|
77
|
+
}
|
78
|
+
|
79
|
+
if ([attributeDictionary count] > 0)
|
80
|
+
{
|
81
|
+
[attributeArray addObject:attributeDictionary];
|
82
|
+
}
|
83
|
+
attribute = attribute->next;
|
84
|
+
}
|
85
|
+
|
86
|
+
if ([attributeArray count] > 0)
|
87
|
+
{
|
88
|
+
[resultForNode setObject:attributeArray forKey:@"nodeAttributeArray"];
|
89
|
+
}
|
90
|
+
}
|
91
|
+
|
92
|
+
xmlNodePtr childNode = currentNode->children;
|
93
|
+
if (childNode)
|
94
|
+
{
|
95
|
+
NSMutableArray *childContentArray = [NSMutableArray array];
|
96
|
+
while (childNode)
|
97
|
+
{
|
98
|
+
NSDictionary *childDictionary = DictionaryForNode(childNode, resultForNode,false);
|
99
|
+
if (childDictionary)
|
100
|
+
{
|
101
|
+
[childContentArray addObject:childDictionary];
|
102
|
+
}
|
103
|
+
childNode = childNode->next;
|
104
|
+
}
|
105
|
+
if ([childContentArray count] > 0)
|
106
|
+
{
|
107
|
+
[resultForNode setObject:childContentArray forKey:@"nodeChildArray"];
|
108
|
+
}
|
109
|
+
}
|
110
|
+
|
111
|
+
return resultForNode;
|
112
|
+
}
|
113
|
+
|
114
|
+
NSArray *PerformXPathQuery(xmlDocPtr doc, NSString *query)
|
115
|
+
{
|
116
|
+
xmlXPathContextPtr xpathCtx;
|
117
|
+
xmlXPathObjectPtr xpathObj;
|
118
|
+
|
119
|
+
/* Create xpath evaluation context */
|
120
|
+
xpathCtx = xmlXPathNewContext(doc);
|
121
|
+
if(xpathCtx == NULL)
|
122
|
+
{
|
123
|
+
NSLog(@"Unable to create XPath context.");
|
124
|
+
return nil;
|
125
|
+
}
|
126
|
+
|
127
|
+
/* Evaluate xpath expression */
|
128
|
+
xpathObj = xmlXPathEvalExpression((xmlChar *)[query cStringUsingEncoding:NSUTF8StringEncoding], xpathCtx);
|
129
|
+
if(xpathObj == NULL) {
|
130
|
+
NSLog(@"Unable to evaluate XPath.");
|
131
|
+
xmlXPathFreeContext(xpathCtx);
|
132
|
+
return nil;
|
133
|
+
}
|
134
|
+
|
135
|
+
xmlNodeSetPtr nodes = xpathObj->nodesetval;
|
136
|
+
if (!nodes)
|
137
|
+
{
|
138
|
+
NSLog(@"Nodes was nil.");
|
139
|
+
xmlXPathFreeObject(xpathObj);
|
140
|
+
xmlXPathFreeContext(xpathCtx);
|
141
|
+
return nil;
|
142
|
+
}
|
143
|
+
|
144
|
+
NSMutableArray *resultNodes = [NSMutableArray array];
|
145
|
+
for (NSInteger i = 0; i < nodes->nodeNr; i++)
|
146
|
+
{
|
147
|
+
NSDictionary *nodeDictionary = DictionaryForNode(nodes->nodeTab[i], nil,false);
|
148
|
+
if (nodeDictionary)
|
149
|
+
{
|
150
|
+
[resultNodes addObject:nodeDictionary];
|
151
|
+
}
|
152
|
+
}
|
153
|
+
|
154
|
+
/* Cleanup */
|
155
|
+
xmlXPathFreeObject(xpathObj);
|
156
|
+
xmlXPathFreeContext(xpathCtx);
|
157
|
+
|
158
|
+
return resultNodes;
|
159
|
+
}
|
160
|
+
|
161
|
+
NSArray *PerformHTMLXPathQuery(NSData *document, NSString *query)
|
162
|
+
{
|
163
|
+
xmlDocPtr doc;
|
164
|
+
|
165
|
+
/* Load XML document */
|
166
|
+
doc = htmlReadMemory([document bytes], (int)[document length], "", NULL, HTML_PARSE_NOWARNING | HTML_PARSE_NOERROR);
|
167
|
+
|
168
|
+
if (doc == NULL)
|
169
|
+
{
|
170
|
+
NSLog(@"Unable to parse.");
|
171
|
+
return nil;
|
172
|
+
}
|
173
|
+
|
174
|
+
NSArray *result = PerformXPathQuery(doc, query);
|
175
|
+
xmlFreeDoc(doc);
|
176
|
+
|
177
|
+
return result;
|
178
|
+
}
|
179
|
+
|
180
|
+
NSArray *PerformXMLXPathQuery(NSData *document, NSString *query)
|
181
|
+
{
|
182
|
+
xmlDocPtr doc;
|
183
|
+
|
184
|
+
/* Load XML document */
|
185
|
+
doc = xmlReadMemory([document bytes], (int)[document length], "", NULL, XML_PARSE_RECOVER);
|
186
|
+
|
187
|
+
if (doc == NULL)
|
188
|
+
{
|
189
|
+
NSLog(@"Unable to parse.");
|
190
|
+
return nil;
|
191
|
+
}
|
192
|
+
|
193
|
+
NSArray *result = PerformXPathQuery(doc, query);
|
194
|
+
xmlFreeDoc(doc);
|
195
|
+
|
196
|
+
return result;
|
197
|
+
}
|