forked from topfunky/hpple
-
Notifications
You must be signed in to change notification settings - Fork 1
/
XPathQuery.m
executable file
·222 lines (185 loc) · 6.92 KB
/
XPathQuery.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//
// XPathQuery.m
// FuelFinder
//
// Created by Matt Gallagher on 4/08/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
// Changed by Christian Ziegler
//
#import "XPathQuery.h"
#import "TFHppleElement.h"
#import <libxml/tree.h>
#import <libxml/parser.h>
#import <libxml/HTMLparser.h>
#import <libxml/xpath.h>
#import <libxml/xpathInternals.h>
TFHppleElement *HppleElementForNode(xmlNodePtr currentNode, TFHppleElement *parentElement) {
TFHppleElement *currentElement = [[TFHppleElement new] autorelease];
if (currentNode->name) {
currentElement.name = [NSString stringWithCString:(const char *)currentNode->name encoding:NSUTF8StringEncoding];
}
if (currentNode->content && currentNode->content != (xmlChar *)-1) {
NSString *content = [NSString stringWithCString:(const char *)currentNode->content encoding:NSUTF8StringEncoding];
/* If tag name is text and there is a parent then it actually is content of the parent.
* Otherwise it must be a childNode which is why this should always be true */
assert([currentElement.name isEqual:@"text"] && parentElement);
content = [content stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (content.length > 0) {
if (parentElement.content) {
NSString *toAppend = [NSString stringWithFormat:@"\n%@", content];
content = [parentElement.content stringByAppendingString:toAppend];
}
parentElement.content = content;
}
return nil;
}
xmlAttr *attribute = currentNode->properties;
while (attribute) {
// Create element and set its name
TFHppleElement *attributeElement = [TFHppleElement new];
attributeElement.name = [NSString stringWithCString:(const char *)attribute->name encoding:NSUTF8StringEncoding];
// Children can be as simple as text but also complex data structures (XML) I guess :)
if (attribute->children) {
TFHppleElement *attributeValue = HppleElementForNode(attribute->children, attributeElement);
if (attributeValue) {
/* TODO: Create childNodes dictionary and stuff but not sure whether this is
* neccessary. Probably only for XML. Usually 'children' is only text which is
* then saved into content of the attribute itself */
NSLog(@"OMG! There is a more complex structure!!!");
assert(NO);
}
// TODO: Is there always only one child??
xmlNode *child = attribute->children->next;
if (child) {
NSLog(@"OH OH!! Obviously there can be more than one child");
assert(NO);
}
}
if (!currentElement.attributes) {
currentElement.attributes = [NSMutableDictionary dictionary];
}
NSString *key;
id valueForTag = [currentElement.attributes valueForKey:attributeElement.name];
// Key taken
if (valueForTag) {
NSNumber *numberOfElements;
// Not a number means there is only one element with the given name so far
if (![valueForTag isKindOfClass:NSNumber.class]) {
numberOfElements = [NSNumber numberWithInteger:1];
[currentElement.attributes setValue:numberOfElements forKey:attributeElement.name];
key = [NSString stringWithFormat:@"%@[%d]", attributeElement.name, numberOfElements.integerValue];
[currentElement.attributes setValue:valueForTag forKey:key];
}
else {
numberOfElements = (NSNumber *) valueForTag;
}
numberOfElements = [NSNumber numberWithInteger:numberOfElements.integerValue + 1];
[currentElement.attributes setValue:numberOfElements forKey:attributeElement.name];
// Create key of new value with format <tagName>[#] and set the value
key = [NSString stringWithFormat:@"%@[%d]", attributeElement.name, numberOfElements.integerValue];
}
// Key not taken
else {
key = attributeElement.name;
}
[currentElement.attributes setValue:attributeElement forKey:key];
[attributeElement release];
attribute = attribute->next;
}
xmlNodePtr childNode = currentNode->children;
while (childNode) {
TFHppleElement *childElement = HppleElementForNode(childNode, currentElement);
if (childElement) {
if (!currentElement.childNodes) {
currentElement.childNodes = [NSMutableDictionary dictionary];
}
NSString *key;
id valueForTag = [currentElement.childNodes valueForKey:childElement.name];
// Key taken
if (valueForTag) {
NSNumber *numberOfElements;
// Not a number means there is only one element with the given name so far
if (![valueForTag isKindOfClass:NSNumber.class]) {
numberOfElements = [NSNumber numberWithInteger:1];
key = [NSString stringWithFormat:@"%@[%d]", childElement.name, numberOfElements.integerValue];
[currentElement.childNodes setValue:valueForTag forKey:key];
[currentElement.childNodes setValue:numberOfElements forKey:childElement.name];
}
else {
numberOfElements = (NSNumber *) valueForTag;
}
numberOfElements = [NSNumber numberWithInteger:(numberOfElements.integerValue + 1)];
[currentElement.childNodes setValue:numberOfElements forKey:childElement.name];
// Create key of new value with format <tagName>[#] and set the value
key = [NSString stringWithFormat:@"%@[%d]", childElement.name, numberOfElements.integerValue];
}
// Key not taken
else {
key = childElement.name;
}
[currentElement.childNodes setValue:childElement forKey:key];
}
childNode = childNode->next;
}
return currentElement;
}
NSArray *PerformXPathQuery(xmlDocPtr doc, NSString *query) {
xmlXPathContextPtr xpathCtx;
xmlXPathObjectPtr xpathObj;
/* Create xpath evaluation context */
xpathCtx = xmlXPathNewContext(doc);
if(xpathCtx == NULL) {
NSLog(@"Unable to create XPath context.");
return nil;
}
/* Evaluate xpath expression */
xpathObj = xmlXPathEvalExpression((xmlChar *)[query cStringUsingEncoding:NSUTF8StringEncoding], xpathCtx);
if(xpathObj == NULL) {
NSLog(@"Unable to evaluate XPath.");
return nil;
}
xmlNodeSetPtr nodes = xpathObj->nodesetval;
if (!nodes) {
NSLog(@"Nodes was nil.");
return nil;
}
NSMutableArray *resultNodes = [NSMutableArray array];
for (NSInteger i = 0; i < nodes->nodeNr; i++) {
TFHppleElement *nodeElement = HppleElementForNode(nodes->nodeTab[i], nil);
if (nodeElement) {
[resultNodes addObject:nodeElement];
}
}
/* Cleanup */
xmlXPathFreeObject(xpathObj);
xmlXPathFreeContext(xpathCtx);
return resultNodes;
}
NSArray *PerformHTMLXPathQuery(NSData *document, NSString *query) {
xmlDocPtr doc;
/* Load XML document */
doc = htmlReadMemory([document bytes], [document length], "", NULL, HTML_PARSE_NOWARNING | HTML_PARSE_NOERROR);
if (doc == NULL)
{
NSLog(@"Unable to parse.");
return nil;
}
NSArray *result = PerformXPathQuery(doc, query);
xmlFreeDoc(doc);
return result;
}
NSArray *PerformXMLXPathQuery(NSData *document, NSString *query)
{
xmlDocPtr doc;
/* Load XML document */
doc = xmlReadMemory([document bytes], [document length], "", NULL, XML_PARSE_RECOVER);
if (doc == NULL)
{
NSLog(@"Unable to parse.");
return nil;
}
NSArray *result = PerformXPathQuery(doc, query);
xmlFreeDoc(doc);
return result;
}