-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathXPNSXMLNodeImpl.m
532 lines (406 loc) · 13.8 KB
/
XPNSXMLNodeImpl.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
//
// XPNSXMLNodeImpl.m
// Panthro
//
// Created by Todd Ditchendorf on 4/27/14.
//
//
#import "XPNSXMLNodeImpl.h"
#import "XPNSXMLDocumentImpl.h"
#import "XPAxis.h"
#import "XPNodeSetValueEnumeration.h"
#import "XPNodeTest.h"
#import "XPNodeSetExtent.h"
#import "XPEmptyNodeSet.h"
@interface XPNSXMLNodeImpl ()
@property (nonatomic, retain) id <XPNodeInfo>parent;
@property (nonatomic, assign) NSRange range;
@property (nonatomic, assign) NSInteger lineNumber;
@end
@implementation XPNSXMLNodeImpl
+ (id <XPNodeInfo>)nodeInfoWithNode:(void *)inNode {
NSXMLNode *node = (id)inNode;
Class cls = (NSXMLDocumentKind == [node kind]) ? [XPNSXMLDocumentImpl class] : [XPNSXMLNodeImpl class];
id <XPNodeInfo>nodeInfo = [[[cls alloc] initWithNode:node] autorelease];
return nodeInfo;
}
- (id <XPNodeInfo>)initWithNode:(void *)node {
NSParameterAssert(node);
self = [super init];
if (self) {
self.node = node;
}
return self;
}
- (void)dealloc {
self.node = nil;
self.parent = nil;
[super dealloc];
}
- (NSString *)description {
return [NSString stringWithFormat:@"<%@ %p %@ %@>", [self class], self, XPNodeTypeName[self.nodeType], self.name];
}
- (NSInteger)compareOrderTo:(id <XPNodeInfo>)other {
XPAssert([other isKindOfClass:[XPNSXMLNodeImpl class]]);
NSComparisonResult result = NSOrderedSame;
// are they the same node?
if ([self isSameNodeInfo:other]) {
return result;
}
XPNSXMLNodeImpl *that = (id)other;
// are they siblings (common case)
if ([self.parent isSameNodeInfo:other.parent]) {
return [self.node index] - [that.node index];
}
// find the depths of both nodes in the tree
NSUInteger depth1 = [self.node level];
NSUInteger depth2 = [that.node level];
id <XPNodeInfo>p1 = nil;
id <XPNodeInfo>p2 = nil;
// move up one branch of the tree so we have two nodes on the same level
p1 = self;
while (depth1>depth2) {
p1 = p1.parent;
if ([p1 isSameNodeInfo:that]) {
return NSOrderedDescending;
}
depth1--;
}
p2 = that;
while (depth2>depth1) {
p2 = p2.parent;
if ([p2 isSameNodeInfo:self]) {
return NSOrderedAscending;
}
depth2--;
}
// now move up both branches in sync until we find a common parent
while (1) {
id <XPNodeInfo>par1 = p1.parent;
id <XPNodeInfo>par2 = p2.parent;
if (!par1 || !par2) {
[NSException raise:@"NullPointerException" format:@"NSXML Tree Compare - internal error"];
}
if ([par1 isSameNodeInfo:par2]) {
return [((XPNSXMLNodeImpl *)p1).node index] - [((XPNSXMLNodeImpl *)p2).node index];
}
p1 = par1;
p2 = par2;
}
return result;
}
- (BOOL)isSameNodeInfo:(id <XPNodeInfo>)other {
XPAssert(!other || [other isKindOfClass:[XPNSXMLNodeImpl class]]);
return other == self || [(id)other node] == self.node;
}
- (XPNodeType)nodeType {
XPAssert(self.node);
XPNodeType type = XPNodeTypeNone;
switch ([(NSXMLNode *)self.node kind]) {
case NSXMLDocumentKind:
type = XPNodeTypeRoot;
break;
case NSXMLElementKind:
type = XPNodeTypeElement;
break;
case NSXMLAttributeKind:
type = XPNodeTypeAttribute;
break;
case NSXMLNamespaceKind:
type = XPNodeTypeNamespace;
break;
case NSXMLProcessingInstructionKind:
type = XPNodeTypePI;
break;
case NSXMLCommentKind:
type = XPNodeTypeComment;
break;
case NSXMLTextKind:
type = XPNodeTypeText;
break;
case NSXMLInvalidKind:
case NSXMLDTDKind:
case NSXMLEntityDeclarationKind:
case NSXMLAttributeDeclarationKind:
case NSXMLNotationDeclarationKind:
case NSXMLElementDeclarationKind:
default:
type = XPNodeTypeNone;
break;
}
return type;
}
- (NSString *)stringValue {
XPAssert(self.node);
NSString *str = [self.node stringValue];
return str ? str : @"";
}
- (NSString *)name {
XPAssert(self.node);
NSString *str = [self.node name];
return str ? str : @"";
}
- (NSString *)localName {
XPAssert(self.node);
NSString *str = [self.node localName];
return str ? str : @"";
}
- (NSString *)prefix {
XPAssert(self.node);
NSString *str = [self.node prefix];
return str ? str : @"";
}
- (NSString *)namespaceURI {
XPAssert(self.node);
NSString *str = [self.node URI];
return str ? str : @"";
}
- (NSRange)range {
return NSMakeRange(0, [[self.node XMLString] length]);
}
- (id <XPNodeInfo>)parent {
XPAssert(self.node);
if (!_parent) {
NSXMLNode *parent = [self.node parent];
if (parent) {
self.parent = [[self class] nodeInfoWithNode:parent];
}
}
return _parent;
}
- (id <XPDocumentInfo>)documentRoot {
XPAssert(self.node);
return [[[XPNSXMLDocumentImpl alloc] initWithNode:[self.node rootDocument]] autorelease];
}
- (NSString *)attributeValueForURI:(NSString *)uri localName:(NSString *)localName {
XPAssert(XPNodeTypeElement == self.nodeType);
NSXMLNode *attrNode = [(NSXMLElement *)self.node attributeForLocalName:localName URI:uri];
return [attrNode stringValue];
}
- (NSString *)namespaceURIForPrefix:(NSString *)prefix {
XPAssert(self.node);
NSString *res = @"";
if ([prefix length]) {
// 2nd arg to namespace-uri-for-prefix() must be an element node. so make sure we start with one.
NSXMLNode *node = self.node;
if (XPNodeTypeElement != [self nodeType] && XPNodeTypeRoot != [self nodeType]) {
node = [node parent];
}
NSString *xquery = nil;
NSError *err = nil;
// xquery = @"in-scope-prefixes(.)";
// err = nil;
// NSArray *prefixes = [node objectsForXQuery:xquery error:&err];
// NSLog(@"%@", prefixes);
xquery = [NSString stringWithFormat:@"namespace-uri-for-prefix('%@', .)", prefix];
err = nil;
NSArray *uris = [node objectsForXQuery:xquery error:&err];
if ([uris count]) {
res = [uris[0] absoluteString];
} else {
if (err) NSLog(@"%@", err);
}
}
return res;
}
- (id <XPAxisEnumeration>)enumerationForAxis:(XPAxis)axis nodeTest:(XPNodeTest *)nodeTest {
NSArray *nodes = nil;
BOOL sorted = NO;
switch (axis) {
case XPAxisAncestor:
sorted = NO;
nodes = [self nodesForAncestorAxis:nodeTest];
break;
case XPAxisAncestorOrSelf:
sorted = NO;
nodes = [self nodesForAncestorOrSelfAxis:nodeTest];
break;
case XPAxisAttribute:
sorted = YES;
nodes = [self nodesForAttributeAxis:nodeTest];
break;
case XPAxisChild:
sorted = YES;
nodes = [self nodesForChildAxis:nodeTest];
break;
case XPAxisDescendant:
sorted = YES;
nodes = [self nodesForDescendantAxis:nodeTest];
break;
case XPAxisDescendantOrSelf:
sorted = YES;
nodes = [self nodesForDescendantOrSelfAxis:nodeTest];
break;
case XPAxisFollowing:
sorted = YES;
nodes = [self nodesForFollowingSiblingAxis:nodeTest includeDescendants:YES];
break;
case XPAxisFollowingSibling:
sorted = YES;
nodes = [self nodesForFollowingSiblingAxis:nodeTest includeDescendants:NO];
break;
case XPAxisNamespace:
sorted = YES;
[NSException raise:@"XPathException" format:@"Namespace Axis not yet implemented."];
break;
case XPAxisParent:
sorted = YES;
nodes = [self nodesForParentAxis:nodeTest];
break;
case XPAxisPreceding:
sorted = NO;
nodes = [self nodesForPrecedingSiblingAxis:nodeTest includeDescendants:YES];
break;
case XPAxisPrecedingSibling:
sorted = NO;
nodes = [self nodesForPrecedingSiblingAxis:nodeTest includeDescendants:NO];
break;
case XPAxisSelf:
sorted = YES;
nodes = [self nodesForSelfAxis:nodeTest];
break;
default:
XPAssert(0);
break;
}
XPNodeSetValue *nodeSet = nil;
if ([nodes count]) {
XPNodeSetExtent *ext = [[[XPNodeSetExtent alloc] initWithNodes:nodes comparer:nil] autorelease];
ext.sorted = sorted;
ext.reverseSorted = !sorted;
nodeSet = ext;
} else {
nodeSet = [XPEmptyNodeSet instance];
}
id <XPAxisEnumeration>enm = (id <XPAxisEnumeration>)[nodeSet enumerate];
return enm;
}
- (NSArray *)nodesForSelfAxis:(XPNodeTest *)nodeTest {
NSArray *result = nil;
if ([nodeTest matches:self]) {
result = @[self];
}
return result;
}
- (NSArray *)descendantNodesFromParent:(NSXMLNode *)parent nodeTest:(XPNodeTest *)nodeTest {
NSMutableArray *result = [NSMutableArray array];
for (NSXMLNode *child in [parent children]) {
id <XPNodeInfo>node = [[self class] nodeInfoWithNode:child];
if ([nodeTest matches:node]) {
[result addObject:node];
}
[result addObjectsFromArray:[self descendantNodesFromParent:child nodeTest:nodeTest]];
}
return result;
}
- (NSArray *)nodesForDescendantOrSelfAxis:(XPNodeTest *)nodeTest {
NSMutableArray *result = [NSMutableArray array];
if ([nodeTest matches:self]) {
[result addObject:self];
}
[result addObjectsFromArray:[self descendantNodesFromParent:self.node nodeTest:nodeTest]];
return result;
}
- (NSArray *)nodesForDescendantAxis:(XPNodeTest *)nodeTest {
NSMutableArray *nodes = [NSMutableArray array];
[nodes addObjectsFromArray:[self descendantNodesFromParent:self.node nodeTest:nodeTest]];
return nodes;
}
- (NSArray *)nodesForAncestorOrSelfAxis:(XPNodeTest *)nodeTest {
NSMutableArray *result = [NSMutableArray array];
if ([nodeTest matches:self]) {
[result addObject:self];
}
[result addObjectsFromArray:[self nodesForAncestorAxis:nodeTest]];
return result;
}
- (NSArray *)nodesForAncestorAxis:(XPNodeTest *)nodeTest {
NSMutableArray *result = nil;
NSXMLNode *parent = [self.node parent];
while (parent) {
id <XPNodeInfo>node = [[self class] nodeInfoWithNode:parent];
if ([nodeTest matches:node]) {
if (!result) {
result = [NSMutableArray array];
}
[result addObject:node];
}
parent = [parent parent];
}
return result;
}
- (NSArray *)nodesForParentAxis:(XPNodeTest *)nodeTest {
NSXMLNode *parent = [self.node parent];
id <XPNodeInfo>node = [[self class] nodeInfoWithNode:parent];
NSArray *result = nil;
if ([nodeTest matches:node]) {
result = @[node];
}
return result;
}
- (NSArray *)nodesForChildAxis:(XPNodeTest *)nodeTest {
NSArray *children = [self.node children];
NSMutableArray *result = nil;
for (NSXMLNode *child in children) {
id <XPNodeInfo>node = [[self class] nodeInfoWithNode:child];
if ([nodeTest matches:node]) {
if (!result) {
result = [NSMutableArray array];
}
[result addObject:node];
}
}
return result;
}
- (NSArray *)nodesForAttributeAxis:(XPNodeTest *)nodeTest {
NSMutableArray *result = nil;
if (XPNodeTypeElement == self.nodeType) {
NSArray *attrs = [(NSXMLElement *)self.node attributes];
for (NSXMLNode *attr in attrs) {
id <XPNodeInfo>node = [[self class] nodeInfoWithNode:attr];
if ([nodeTest matches:node]) {
if (!result) {
result = [NSMutableArray array];
}
[result addObject:node];
}
}
}
return result;
}
- (NSArray *)nodesForFollowingSiblingAxis:(XPNodeTest *)nodeTest includeDescendants:(BOOL)includeDescendants {
NSMutableArray *result = [NSMutableArray array];
NSXMLNode *parent = [self.node parent];
NSArray *children = [parent children];
NSUInteger c = [children count];
NSUInteger i = [self.node index] + 1;
children = [children subarrayWithRange:NSMakeRange(i, c - i)];
for (NSXMLNode *child in children) {
id <XPNodeInfo>node = [[self class] nodeInfoWithNode:child];
if ([nodeTest matches:node]) {
[result addObject:node];
}
if (includeDescendants) {
[result addObjectsFromArray:[self descendantNodesFromParent:child nodeTest:nodeTest]];
}
}
return result;
}
- (NSArray *)nodesForPrecedingSiblingAxis:(XPNodeTest *)nodeTest includeDescendants:(BOOL)includeDescendants {
NSMutableArray *result = [NSMutableArray array];
NSUInteger i = [self.node index];
NSArray *children = [[[self.node parent] children] subarrayWithRange:NSMakeRange(0, i)];
for (NSXMLNode *child in [children reverseObjectEnumerator]) {
id <XPNodeInfo>node = [[self class] nodeInfoWithNode:child];
if ([nodeTest matches:node]) {
[result addObject:node];
}
if (includeDescendants) {
[result addObjectsFromArray:[self descendantNodesFromParent:child nodeTest:nodeTest]];
}
}
return result;
}
@synthesize range = _range;
@synthesize lineNumber = _lineNumber;
@end