forked from robbiehanson/KissXML
/
DDXMLElement.m
590 lines (479 loc) · 15.5 KB
/
DDXMLElement.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
#import "DDXMLElement.h"
#import "NSStringAdditions.h"
#import "DDXMLPrivate.h"
@implementation DDXMLElement
- (id)initWithName:(NSString *)name
{
// Note: Make every guarantee that genericPtr is not null
xmlNodePtr node = xmlNewNode(NULL, [name xmlChar]);
return [self initWithPrimitive:(xmlKindPtr)node];
}
- (id)initWithName:(NSString *)name URI:(NSString *)URI
{
// Note: Make every guarantee that genericPtr is not null
xmlNodePtr node = xmlNewNode(NULL, [name xmlChar]);
id result = [self initWithPrimitive:(xmlKindPtr)node];
[result setURI:URI];
return result;
}
- (id)initWithName:(NSString *)name stringValue:(NSString *)string
{
// Note: Make every guarantee that genericPtr is not null
xmlNodePtr node = xmlNewNode(NULL, [name xmlChar]);
id result = [self initWithPrimitive:(xmlKindPtr)node];
[result setStringValue:string];
return result;
}
- (id)initWithXMLString:(NSString *)string error:(NSError **)error
{
DDXMLDocument *doc = [[DDXMLDocument alloc] initWithXMLString:string options:0 error:error];
if(doc == nil)
{
return nil;
}
DDXMLNode *result = [doc rootElement];
[result detach];
[doc release];
return [result retain];
}
+ (id)nodeWithPrimitive:(xmlKindPtr)nodePtr
{
return [[[DDXMLElement alloc] initWithPrimitive:nodePtr] autorelease];
}
- (id)initWithPrimitive:(xmlKindPtr)nodePtr
{
if(nodePtr == NULL || nodePtr->type != XML_ELEMENT_NODE)
{
[self release];
return nil;
}
self = [super initWithPrimitive:nodePtr];
return self;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Elements by name
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Returns the child element nodes (as DDXMLElement objects) of the receiver that have a specified name.
*
* If name is a qualified name, then this method invokes elementsForLocalName:URI: with the URI parameter set to
* the URI associated with the prefix. Otherwise comparison is based on string equality of the qualified or
* non-qualified name.
**/
- (NSArray *)elementsForName:(NSString *)name
{
if(name == nil) return [NSArray array];
// We need to check to see if name has a prefix.
// If it does have a prefix, we need to figure out what the corresponding URI is for that prefix,
// and then search for any elements that have the same name (including prefix) OR have the same URI.
// Otherwise we loop through the children as usual and do a string compare on the name
NSString *prefix = [[self class] prefixForName:name];
if([prefix length] > 0)
{
xmlNodePtr node = (xmlNodePtr)genericPtr;
xmlNsPtr ns = xmlSearchNs(node->doc, node, [prefix xmlChar]);
if(ns != NULL)
{
NSString *uri = [NSString stringWithUTF8String:((const char *)ns->href)];
return [self elementsWithName:name uri:uri];
}
// Note: We used xmlSearchNs instead of resolveNamespaceForName: - avoid creating wrapper objects when possible
}
return [self elementsWithName:name uri:nil];
}
- (NSArray *)elementsForLocalName:(NSString *)localName URI:(NSString *)URI
{
if(localName == nil) return [NSArray array];
// We need to figure out what the prefix is for this URI.
// Then we search for elements that are named prefix:localName OR (named localName AND have the given URI).
NSString *prefix = [self resolvePrefixForNamespaceURI:URI];
if(prefix != nil)
{
NSString *name = [NSString stringWithFormat:@"%@:%@", prefix, localName];
return [self elementsWithName:name uri:URI];
}
else
{
return [self elementsWithName:localName uri:URI];
}
}
/**
* Helper method elementsForName and elementsForLocalName:URI: so work isn't duplicated.
* The name parameter is required, URI is optional.
**/
- (NSArray *)elementsWithName:(NSString *)name uri:(NSString *)uri
{
// Supplied: name, !uri : match: name
// Supplied: p:name, uri : match: p:name || (name && uri)
// Supplied: name, uri : match: name && uri
NSMutableArray *result = [NSMutableArray array];
xmlNodePtr node = (xmlNodePtr)genericPtr;
BOOL hasPrefix = [[[self class] prefixForName:name] length] > 0;
NSString *localName = [[self class] localNameForName:name];
xmlNodePtr child = node->children;
while(child != NULL)
{
if(child->type == XML_ELEMENT_NODE)
{
BOOL match = NO;
if(uri == nil)
{
match = xmlStrEqual(child->name, [name xmlChar]);
}
else
{
BOOL nameMatch = xmlStrEqual(child->name, [name xmlChar]);
BOOL localNameMatch = xmlStrEqual(child->name, [localName xmlChar]);
BOOL uriMatch = NO;
if(child->ns != NULL)
{
uriMatch = xmlStrEqual(child->ns->href, [uri xmlChar]);
}
if(hasPrefix)
match = nameMatch || (localNameMatch && uriMatch);
else
match = nameMatch && uriMatch;
}
if(match)
{
DDXMLElement *childElement = [[DDXMLElement alloc] initWithPrimitive:(xmlKindPtr)child];
[result addObject:childElement];
[childElement release];
}
}
child = child->next;
}
return result;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Attributes
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)addAttribute:(DDXMLNode *)attribute
{
// NSXML version uses this same assertion
DDCheck([attribute hasParent] == NO, @"Cannot add an attribute with a parent; detach or copy first");
DDCheck([attribute isXmlAttrPtr], @"Not an attribute");
// xmlNodePtr xmlAddChild(xmlNodePtr parent, xmlNodePtr cur)
// Add a new node to @parent, at the end of the child (or property) list merging
// adjacent TEXT nodes (in which case @cur is freed). If the new node is ATTRIBUTE, it is added
// into properties instead of children. If there is an attribute with equal name, it is first destroyed.
[self removeAttributeForName:[attribute name]];
xmlAddChild((xmlNodePtr)genericPtr, (xmlNodePtr)attribute->genericPtr);
}
- (void)removeAttribute:(xmlAttrPtr)attr
{
[[self class] removeAttribute:attr fromNode:(xmlNodePtr)genericPtr];
}
- (void)removeAllAttributes
{
[[self class] removeAllAttributesFromNode:(xmlNodePtr)genericPtr];
}
- (void)removeAttributeForName:(NSString *)name
{
// If we use xmlUnsetProp, then the attribute will be automatically freed.
// We don't want this unless no other wrapper objects have a reference to the property.
xmlAttrPtr attr = ((xmlNodePtr)genericPtr)->properties;
while(attr != NULL)
{
if(xmlStrEqual(attr->name, [name xmlChar]))
{
[self removeAttribute:attr];
return;
}
attr = attr->next;
}
}
- (NSArray *)attributes
{
NSMutableArray *result = [NSMutableArray array];
xmlAttrPtr attr = ((xmlNodePtr)genericPtr)->properties;
while(attr != NULL)
{
DDXMLNode *attrNode = [[DDXMLNode alloc] initWithPrimitive:(xmlKindPtr)attr];
[result addObject:attrNode];
[attrNode release];
attr = attr->next;
}
return result;
}
- (DDXMLNode *)attributeForName:(NSString *)name
{
xmlAttrPtr attr = ((xmlNodePtr)genericPtr)->properties;
while(attr != NULL)
{
if(xmlStrEqual([name xmlChar], attr->name))
{
return [DDXMLNode nodeWithPrimitive:(xmlKindPtr)attr];
}
attr = attr->next;
}
return nil;
}
/**
* Sets the list of attributes for the element.
* Any previously set attributes are removed.
**/
- (void)setAttributes:(NSArray *)attributes
{
[self removeAllAttributes];
NSUInteger i;
for(i = 0; i < [attributes count]; i++)
{
DDXMLNode *attribute = [attributes objectAtIndex:i];
[self addAttribute:attribute];
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Namespaces
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)addNamespace:(DDXMLNode *)namespace
{
// NSXML version uses this same assertion
DDCheck([namespace hasParent] == NO, @"Cannot add a namespace with a parent; detach or copy first");
DDCheck([namespace isXmlNsPtr], @"Not a namespace");
// Beware: [namespace prefix] does NOT return what you might expect. Use [namespace name] instead.
[self removeNamespaceForPrefix:[namespace name]];
xmlNsPtr currentNs = ((xmlNodePtr)genericPtr)->nsDef;
if(currentNs == NULL)
{
((xmlNodePtr)genericPtr)->nsDef = (xmlNsPtr)namespace->genericPtr;
}
else
{
while(currentNs != NULL)
{
if(currentNs->next == NULL)
{
currentNs->next = (xmlNsPtr)namespace->genericPtr;
break; // Yes this break is needed
}
currentNs = currentNs->next;
}
}
// The xmlNs structure doesn't contain a reference to the parent, so we manage our own reference
namespace->nsParentPtr = (xmlNodePtr)genericPtr;
// Did we just add a default namespace
if([[namespace name] isEqualToString:@""])
{
((xmlNodePtr)genericPtr)->ns = (xmlNsPtr)namespace->genericPtr;
// Note: The removeNamespaceForPrefix method above properly handled removing any previous default namespace
}
}
- (void)removeNamespace:(xmlNsPtr)ns
{
[[self class] removeNamespace:ns fromNode:(xmlNodePtr)genericPtr];
}
- (void)removeAllNamespaces
{
[[self class] removeAllNamespacesFromNode:(xmlNodePtr)genericPtr];
}
- (void)removeNamespaceForPrefix:(NSString *)name
{
// If name is nil or the empty string, the user is wishing to remove the default namespace
const xmlChar *xmlName = [name length] > 0 ? [name xmlChar] : NULL;
xmlNsPtr ns = ((xmlNodePtr)genericPtr)->nsDef;
while(ns != NULL)
{
if(xmlStrEqual(ns->prefix, xmlName))
{
[self removeNamespace:ns];
break;
}
ns = ns->next;
}
// Note: The removeNamespace method properly handles the situation where the namespace is the default namespace
}
- (NSArray *)namespaces
{
NSMutableArray *result = [NSMutableArray array];
xmlNsPtr ns = ((xmlNodePtr)genericPtr)->nsDef;
while(ns != NULL)
{
DDXMLNode *nsNode = [[DDXMLNode alloc] initWithPrimitive:(xmlKindPtr)ns nsParent:(xmlNodePtr)genericPtr];
[result addObject:nsNode];
[nsNode release];
ns = ns->next;
}
return result;
}
- (DDXMLNode *)namespaceForPrefix:(NSString *)prefix
{
// If the prefix is nil or the empty string, the user is requesting the default namespace
if([prefix length] == 0)
{
// Requesting the default namespace
xmlNsPtr ns = ((xmlNodePtr)genericPtr)->ns;
if(ns != NULL)
{
return [DDXMLNode nodeWithPrimitive:(xmlKindPtr)ns nsParent:(xmlNodePtr)genericPtr];
}
}
else
{
xmlNsPtr ns = ((xmlNodePtr)genericPtr)->nsDef;
while(ns != NULL)
{
if(xmlStrEqual(ns->prefix, [prefix xmlChar]))
{
return [DDXMLNode nodeWithPrimitive:(xmlKindPtr)ns nsParent:(xmlNodePtr)genericPtr];
}
ns = ns->next;
}
}
return nil;
}
- (void)setNamespaces:(NSArray *)namespaces
{
[self removeAllNamespaces];
NSUInteger i;
for(i = 0; i < [namespaces count]; i++)
{
DDXMLNode *namespace = [namespaces objectAtIndex:i];
[self addNamespace:namespace];
}
}
/**
* Recursively searches the given node for the given namespace
**/
+ (DDXMLNode *)resolveNamespaceForPrefix:(NSString *)prefix atNode:(xmlNodePtr)nodePtr
{
if(nodePtr == NULL) return nil;
xmlNsPtr ns = nodePtr->nsDef;
while(ns != NULL)
{
if(xmlStrEqual(ns->prefix, [prefix xmlChar]))
{
return [DDXMLNode nodeWithPrimitive:(xmlKindPtr)ns nsParent:nodePtr];
}
ns = ns->next;
}
return [self resolveNamespaceForPrefix:prefix atNode:nodePtr->parent];
}
/**
* Returns the namespace node with the prefix matching the given qualified name.
* Eg: You pass it "a:dog", it returns the namespace (defined in this node or parent nodes) that has the "a" prefix.
**/
- (DDXMLNode *)resolveNamespaceForName:(NSString *)name
{
// If the user passes nil or an empty string for name, they're looking for the default namespace.
if([name length] == 0)
{
return [[self class] resolveNamespaceForPrefix:nil atNode:(xmlNodePtr)genericPtr];
}
NSString *prefix = [[self class] prefixForName:name];
if([prefix length] > 0)
{
// Unfortunately we can't use xmlSearchNs because it returns an xmlNsPtr.
// This gives us mostly what we want, except we also need to know the nsParent.
// So we do the recursive search ourselves.
return [[self class] resolveNamespaceForPrefix:prefix atNode:(xmlNodePtr)genericPtr];
}
return nil;
}
/**
* Recursively searches the given node for a namespace with the given URI, and a set prefix.
**/
+ (NSString *)resolvePrefixForURI:(NSString *)uri atNode:(xmlNodePtr)nodePtr
{
if(nodePtr == NULL) return nil;
xmlNsPtr ns = nodePtr->nsDef;
while(ns != NULL)
{
if(xmlStrEqual(ns->href, [uri xmlChar]))
{
if(ns->prefix != NULL)
{
return [NSString stringWithUTF8String:((const char *)ns->prefix)];
}
}
ns = ns->next;
}
return [self resolvePrefixForURI:uri atNode:nodePtr->parent];
}
/**
* Returns the prefix associated with the specified URI.
* Returns a string that is the matching prefix or nil if it finds no matching prefix.
**/
- (NSString *)resolvePrefixForNamespaceURI:(NSString *)namespaceURI
{
// We can't use xmlSearchNsByHref because it will return xmlNsPtr's with NULL prefixes.
// We're looking for a definitive prefix for the given URI.
return [[self class] resolvePrefixForURI:namespaceURI atNode:(xmlNodePtr)genericPtr];
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Children
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)removeChild:(xmlNodePtr)child
{
[[self class] removeChild:child fromNode:(xmlNodePtr)genericPtr];
}
- (void)removeAllChildren
{
[[self class] removeAllChildrenFromNode:(xmlNodePtr)genericPtr];
}
- (void)removeChildAtIndex:(NSUInteger)index
{
NSUInteger i = 0;
xmlNodePtr child = ((xmlNodePtr)genericPtr)->children;
while(child != NULL)
{
// Ignore all but element, comment, text, or processing instruction nodes
if([[self class] isXmlNodePtr:(xmlKindPtr)child])
{
if(i == index)
{
[self removeChild:child];
return;
}
i++;
}
child = child->next;
}
}
- (void)addChild:(DDXMLNode *)child
{
// NSXML version uses these same assertions
DDCheck([child hasParent] == NO, @"Cannot add a child that has a parent; detach or copy first");
DDCheck([child isXmlNodePtr], @"Elements can only have text, elements, processing instructions, and comments as children");
xmlAddChild((xmlNodePtr)genericPtr, (xmlNodePtr)child->genericPtr);
}
- (void)insertChild:(DDXMLNode *)child atIndex:(NSUInteger)index
{
// NSXML version uses these same assertions
DDCheck([child hasParent] == NO, @"Cannot add a child that has a parent; detach or copy first");
DDCheck([child isXmlNodePtr], @"Elements can only have text, elements, processing instructions, and comments as children");
NSUInteger i = 0;
xmlNodePtr childNodePtr = ((xmlNodePtr)genericPtr)->children;
while(childNodePtr != NULL)
{
// Ignore all but element, comment, text, or processing instruction nodes
if([[self class] isXmlNodePtr:(xmlKindPtr)childNodePtr])
{
if(i == index)
{
xmlAddPrevSibling(childNodePtr, (xmlNodePtr)child->genericPtr);
return;
}
i++;
}
childNodePtr = childNodePtr->next;
}
if(i == index)
{
xmlAddChild((xmlNodePtr)genericPtr, (xmlNodePtr)child->genericPtr);
return;
}
// NSXML version uses this same assertion
DDCheck(NO, @"index (%u) beyond bounds (%u)", (unsigned)index, (unsigned)++i);
}
- (void)setChildren:(NSArray *)children
{
[self removeAllChildren];
NSUInteger i;
for(i = 0; i < [children count]; i++)
{
DDXMLNode *child = [children objectAtIndex:i];
[self addChild:child];
}
}
@end