-
Notifications
You must be signed in to change notification settings - Fork 0
/
CXMLNode.m
executable file
·376 lines (296 loc) · 9.88 KB
/
CXMLNode.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
//
// CXMLNode.m
// TouchCode
//
// Created by Jonathan Wight on 03/07/08.
// Copyright 2008 toxicsoftware.com. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
#import "CXMLNode.h"
#import "CXMLNode_PrivateExtensions.h"
#import "CXMLDocument.h"
#import "CXMLElement.h"
#import "CXMLNode_CreationExtensions.h"
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
#include <libxml/xmlIO.h>
static int MyXmlOutputWriteCallback(void * context, const char * buffer, int len);
static int MyXmlOutputCloseCallback(void * context);
@implementation CXMLNode
- (void)dealloc
{
if (_node)
{
if (_node->_private == self)
_node->_private = NULL;
if (_freeNodeOnRelease)
{
xmlFreeNode(_node);
}
_node = NULL;
}
//
[super dealloc];
}
- (id)copyWithZone:(NSZone *)zone;
{
#pragma unused (zone)
xmlNodePtr theNewNode = xmlCopyNode(_node, 1);
CXMLNode *theNode = [[[self class] alloc] initWithLibXMLNode:theNewNode freeOnDealloc:YES];
theNewNode->_private = theNode;
return(theNode);
}
#pragma mark -
- (CXMLNodeKind)kind
{
NSAssert(_node != NULL, @"CXMLNode does not have attached libxml2 _node.");
return((CXMLNodeKind)_node->type); // TODO this isn't 100% accurate!
}
- (NSString *)name
{
NSAssert(_node != NULL, @"CXMLNode does not have attached libxml2 _node.");
// TODO use xmlCheckUTF8 to check name
if (_node->name == NULL)
return(NULL);
NSString *localName = [NSString stringWithUTF8String:(const char *)_node->name];
if (_node->ns == NULL || _node->ns->prefix == NULL)
return localName;
return [NSString stringWithFormat:@"%@:%@", [NSString stringWithUTF8String:(const char *)_node->ns->prefix], localName];
}
- (NSString *)stringValue
{
NSAssert(_node != NULL, @"CXMLNode does not have attached libxml2 _node.");
if (_node->type == XML_TEXT_NODE || _node->type == XML_CDATA_SECTION_NODE)
return [NSString stringWithUTF8String:(const char *)_node->content];
if (_node->type == XML_ATTRIBUTE_NODE)
return [NSString stringWithUTF8String:(const char *)_node->children->content];
NSMutableString *theStringValue = [[[NSMutableString alloc] init] autorelease];
for (CXMLNode *child in [self children])
{
[theStringValue appendString:[child stringValue]];
}
return theStringValue;
}
- (NSUInteger)index
{
NSAssert(_node != NULL, @"CXMLNode does not have attached libxml2 _node.");
xmlNodePtr theCurrentNode = _node->prev;
NSUInteger N;
for (N = 0; theCurrentNode != NULL; ++N, theCurrentNode = theCurrentNode->prev)
;
return(N);
}
- (NSUInteger)level
{
NSAssert(_node != NULL, @"CXMLNode does not have attached libxml2 _node.");
xmlNodePtr theCurrentNode = _node->parent;
NSUInteger N;
for (N = 0; theCurrentNode != NULL; ++N, theCurrentNode = theCurrentNode->parent)
;
return(N);
}
- (CXMLDocument *)rootDocument
{
NSAssert(_node != NULL, @"CXMLNode does not have attached libxml2 _node.");
return(_node->doc->_private);
}
- (CXMLNode *)parent
{
NSAssert(_node != NULL, @"CXMLNode does not have attached libxml2 _node.");
if (_node->parent == NULL)
return(NULL);
else
return (_node->parent->_private);
}
- (NSUInteger)childCount
{
NSAssert(_node != NULL, @"CXMLNode does not have attached libxml2 _node.");
if (_node->type == CXMLAttributeKind)
return 0; // NSXMLNodes of type NSXMLAttributeKind can't have children
xmlNodePtr theCurrentNode = _node->children;
NSUInteger N;
for (N = 0; theCurrentNode != NULL; ++N, theCurrentNode = theCurrentNode->next)
;
return(N);
}
- (NSArray *)children
{
NSAssert(_node != NULL, @"CXMLNode does not have attached libxml2 _node.");
NSMutableArray *theChildren = [NSMutableArray array];
if (_node->type != CXMLAttributeKind) // NSXML Attribs don't have children.
{
xmlNodePtr theCurrentNode = _node->children;
while (theCurrentNode != NULL)
{
CXMLNode *theNode = [CXMLNode nodeWithLibXMLNode:theCurrentNode freeOnDealloc:NO];
[theChildren addObject:theNode];
theCurrentNode = theCurrentNode->next;
}
}
return(theChildren);
}
- (CXMLNode *)childAtIndex:(NSUInteger)index
{
NSAssert(_node != NULL, @"CXMLNode does not have attached libxml2 _node.");
xmlNodePtr theCurrentNode = _node->children;
NSUInteger N;
for (N = 0; theCurrentNode != NULL && N != index; ++N, theCurrentNode = theCurrentNode->next)
;
if (theCurrentNode)
return([CXMLNode nodeWithLibXMLNode:theCurrentNode freeOnDealloc:NO]);
return(NULL);
}
- (CXMLNode *)previousSibling
{
NSAssert(_node != NULL, @"CXMLNode does not have attached libxml2 _node.");
if (_node->prev == NULL)
return(NULL);
else
return([CXMLNode nodeWithLibXMLNode:_node->prev freeOnDealloc:NO]);
}
- (CXMLNode *)nextSibling
{
NSAssert(_node != NULL, @"CXMLNode does not have attached libxml2 _node.");
if (_node->next == NULL)
return(NULL);
else
return([CXMLNode nodeWithLibXMLNode:_node->next freeOnDealloc:NO]);
}
//- (CXMLNode *)previousNode;
//- (CXMLNode *)nextNode;
//- (NSString *)XPath;
- (NSString *)localName
{
NSAssert(_node != NULL, @"CXMLNode does not have attached libxml2 _node.");
// TODO use xmlCheckUTF8 to check name
if (_node->name == NULL)
return(NULL);
else
return([NSString stringWithUTF8String:(const char *)_node->name]);
}
- (NSString *)prefix
{
if (_node->ns && _node->ns->prefix)
return([NSString stringWithUTF8String:(const char *)_node->ns->prefix]);
else
return(@"");
}
- (NSString *)URI
{
if (_node->ns)
return([NSString stringWithUTF8String:(const char *)_node->ns->href]);
else
return(NULL);
}
+ (NSString *)localNameForName:(NSString *)name
{
NSRange split = [name rangeOfString:@":"];
if (split.length > 0)
return [name substringFromIndex:split.location + 1];
return name;
}
+ (NSString *)prefixForName:(NSString *)name
{
NSRange split = [name rangeOfString:@":"];
if (split.length > 0)
return [name substringToIndex:split.location];
return @"";
}
+ (CXMLNode *)predefinedNamespaceForPrefix:(NSString *)name
{
if ([name isEqualToString:@"xml"])
return [CXMLNode namespaceWithName:@"xml" stringValue:@"http://www.w3.org/XML/1998/namespace"];
if ([name isEqualToString:@"xs"])
return [CXMLNode namespaceWithName:@"xs" stringValue:@"http://www.w3.org/2001/XMLSchema"];
if ([name isEqualToString:@"xsi"])
return [CXMLNode namespaceWithName:@"xsi" stringValue:@"http://www.w3.org/2001/XMLSchema-instance"];
if ([name isEqualToString:@"xmlns"]) // Not in Cocoa, but should be as it's reserved by W3C
return [CXMLNode namespaceWithName:@"xmlns" stringValue:@"http://www.w3.org/2000/xmlns/"];
return nil;
}
- (NSString *)description
{
NSAssert(_node != NULL, @"CXMLNode does not have attached libxml2 _node.");
return([NSString stringWithFormat:@"<%@ %p [%p] %@ %@>", NSStringFromClass([self class]), self, self->_node, [self name], [self XMLStringWithOptions:0]]);
}
- (NSString *)XMLString
{
return([self XMLStringWithOptions:0]);
}
- (NSString *)XMLStringWithOptions:(NSUInteger)options
{
#pragma unused (options)
NSMutableData *theData = [[[NSMutableData alloc] init] autorelease];
xmlOutputBufferPtr theOutputBuffer = xmlOutputBufferCreateIO(MyXmlOutputWriteCallback, MyXmlOutputCloseCallback, theData, NULL);
xmlNodeDumpOutput(theOutputBuffer, _node->doc, _node, 0, 0, "utf-8");
xmlOutputBufferFlush(theOutputBuffer);
NSString *theString = [[[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding] autorelease];
xmlOutputBufferClose(theOutputBuffer);
return(theString);
}
//- (NSString *)canonicalXMLStringPreservingComments:(BOOL)comments;
- (NSArray *)nodesForXPath:(NSString *)xpath error:(NSError **)error
{
#pragma unused (error)
NSAssert(_node != NULL, @"CXMLNode does not have attached libxml2 _node.");
NSArray *theResult = NULL;
xmlXPathContextPtr theXPathContext = xmlXPathNewContext(_node->doc);
theXPathContext->node = _node;
// TODO considering putting xmlChar <-> UTF8 into a NSString category
xmlXPathObjectPtr theXPathObject = xmlXPathEvalExpression((const xmlChar *)[xpath UTF8String], theXPathContext);
if (theXPathObject == NULL)
{
if (error)
*error = [NSError errorWithDomain:@"TODO_DOMAIN" code:-1 userInfo:NULL];
return(NULL);
}
if (xmlXPathNodeSetIsEmpty(theXPathObject->nodesetval))
theResult = [NSArray array]; // TODO better to return NULL?
else
{
NSMutableArray *theArray = [NSMutableArray array];
int N;
for (N = 0; N < theXPathObject->nodesetval->nodeNr; N++)
{
xmlNodePtr theNode = theXPathObject->nodesetval->nodeTab[N];
[theArray addObject:[CXMLNode nodeWithLibXMLNode:theNode freeOnDealloc:NO]];
}
theResult = theArray;
}
xmlXPathFreeObject(theXPathObject);
xmlXPathFreeContext(theXPathContext);
return(theResult);
}
//- (NSArray *)objectsForXQuery:(NSString *)xquery constants:(NSDictionary *)constants error:(NSError **)error;
//- (NSArray *)objectsForXQuery:(NSString *)xquery error:(NSError **)error;
@end
static int MyXmlOutputWriteCallback(void * context, const char * buffer, int len)
{
NSMutableData *theData = context;
[theData appendBytes:buffer length:len];
return(len);
}
static int MyXmlOutputCloseCallback(void * context)
{
return(0);
}