File: DOMQueryPathExpression.m

package info (click to toggle)
sope 5.12.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,592 kB
  • sloc: objc: 169,229; sh: 3,823; ansic: 3,409; javascript: 446; python: 318; makefile: 185
file content (878 lines) | stat: -rw-r--r-- 22,235 bytes parent folder | download | duplicates (7)
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
/*
  Copyright (C) 2000-2005 SKYRIX Software AG

  This file is part of SOPE.

  SOPE is free software; you can redistribute it and/or modify it under
  the terms of the GNU Lesser General Public License as published by the
  Free Software Foundation; either version 2, or (at your option) any
  later version.

  SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
  License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with SOPE; see the file COPYING.  If not, write to the
  Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  02111-1307, USA.
*/

#include <DOM/DOMQueryPathExpression.h>
#include "DOMDocument.h"
#include "DOMAttribute.h"
#include "DOMNamedNodeMap.h"
#include "common.h"

@interface NSString(QP)
- (NSArray *)queryPathComponents;
@end

#define QUERY_CURRENT 1
#define QUERY_PARENT  2
#define QUERY_ROOT    3

/*
  QueryPathExpression classes:
    NSObject
      _DOMQPPredicateExpression
      _DOMQPPredicateQPExpression
      DOMQueryPathExpression
        _DOMQueryPathSequence
        _DOMQueryPathPredicates
        _DOMQueryPathAttribute
        _DOMQueryPathNodeQuery
        _DOMQueryPathChildNodes
        _DOMQueryPathKeyPath
*/

@interface DOMQueryPathExpression(Privates)

+ (id)makeQueryPathExpression:(NSString *)_expr;

- (id)_deepChildNodesWithName:(id)_name type:(int)_type node:(id)_domNode;
- (id)_flatChildNodesWithName:(id)_name type:(int)_type node:(id)_domNode;
- (id)_rootSearchNodeForNode:(id)_domNode;

- (id)evaluateWithNode:(id)_node      inContext:(id)_ctx;
- (id)evaluateWithNodeList:(id)_nodes inContext:(id)_ctx;

@end

@interface _DOMQPPredicateExpression : NSObject
- (BOOL)matchesNode:(id)_node inContext:(id)_ctx;
@end

@interface _DOMQPPredicateQPExpression : NSObject
{
  DOMQueryPathExpression *qpexpr;
}
- (id)initWithQueryPathExpression:(DOMQueryPathExpression *)_expr;
@end

@interface _DOMQueryPathSequence : DOMQueryPathExpression
{
  NSArray *queryPaths;
}
+ (id)sequenceWithArray:(NSArray *)_array;
@end

@interface _DOMQueryPathPredicates : DOMQueryPathExpression
{
  DOMQueryPathExpression *expr;
  NSArray *predicates;
}
- (id)initWithQueryPathExpression:(DOMQueryPathExpression *)_expr
  predicates:(NSArray *)_predicates;
@end

@interface _DOMQueryPathAttribute : DOMQueryPathExpression
{
  NSString *attrSpec;
  NSString *uri;
  struct {
    int getAll:1;
    int hasColon:1;
  } aflags;
}
- (id)initWithString:(NSString *)_spec;
@end

@interface _DOMQueryPathNodeQuery : DOMQueryPathExpression
{
  int  queryOp;
}
- (id)initWithQueryOp:(int)_op;
@end

@interface _DOMQueryPathChildNodes : DOMQueryPathExpression
{
  NSString *elementName;
  BOOL     deep;
  int      nodeType;
}
- (id)initWithName:(NSString *)_name deep:(BOOL)_flag type:(int)_type;
@end

@interface _DOMQueryPathKeyPath : DOMQueryPathExpression
{
  NSString *keyPath;
}
- (id)initWithKeyPath:(NSString *)_path;
@end

@implementation DOMQueryPathExpression

- (id)_deepChildNodesWithName:(id)_name type:(int)_type node:(id)_domNode {
  NSMutableArray *array;
  id       children;
  unsigned i, count;
  NSString *fname, *furi;
  
  if (![_domNode hasChildNodes])
    return [NSArray array];
  
  children = [_domNode childNodes];
  if ((count = [children count]) == 0)
    return [NSArray array];

  if (_name) {
    NSRange r;

    r = [_name rangeOfString:@"}"];
    if (r.length != 0) {
      fname = [_name substringFromIndex:(r.location + r.length)];
      furi  = [[_name substringToIndex:r.location] substringFromIndex:1];
    }
    else {
      fname = _name;
      furi  = nil;
    }
  }
  else {
    fname = nil;
    furi  = nil;
  }
  
  array = [NSMutableArray arrayWithCapacity:count];
  for (i = 0; i < count; i++) {
    id child;
    
    if ((child = [children objectAtIndex:i]) == nil)
      continue;
    
    if (([child nodeType] == _type) || (_type == -1)) {

      if (_name == nil) {
        /* wildcard query */
        [array addObject:child];
      }
      else {
        NSString *nname;

        nname = [child nodeName];

        if ([nname isEqualToString:_name]) {
          /* FQ name matches */
          [array addObject:child];
        }
        else if ([nname isEqualToString:fname]) {
          /* name matches */
          if (furi) {
            /* check URI */
            if ([[child namespaceURI] isEqualToString:furi])
              [array addObject:child];
          }
          else {
            [array addObject:child];
          }
        }
      }
    }
    
    [array addObjectsFromArray:
             [self _deepChildNodesWithName:_name type:_type node:child]];
  }
  return array;
}

- (id)_flatChildNodesWithName:(id)_name type:(int)_type node:(id)_domNode {
  NSMutableArray *array;
  id       children;
  unsigned i, count;
  
  if (![_domNode hasChildNodes])
    return [NSArray array];
  
  children = [_domNode childNodes];
  if ((count = [children count]) == 0)
    return [NSArray array];
  
  array = [NSMutableArray arrayWithCapacity:count];
  for (i = 0; i < count; i++) {
    id child;
    
    child = [children objectAtIndex:i];

    if (([child nodeType] != _type) && (_type != -1))
      continue;
    
    if (_name) {
      if (![[child nodeName] isEqualToString:_name])
        continue;
    }
    
    if (child)
      [array addObject:child];
  }
  return [[array copy] autorelease];
}

- (id)_rootSearchNodeForNode:(id)_domNode {
  id root;
  
  switch ([_domNode nodeType]) {
    case DOM_DOCUMENT_NODE:
    case DOM_DOCUMENT_FRAGMENT_NODE:
      root = [(id<DOMDocument>)_domNode documentElement];
      if (root == nil) {
        NSLog(@"WARNING(%s): document node %@ has no root element !",
              __PRETTY_FUNCTION__, _domNode);
      }
      break;
      
    case DOM_COMMENT_NODE:
    case DOM_PROCESSING_INSTRUCTION_NODE:
    case DOM_ELEMENT_NODE:
      root = [_domNode ownerDocument];
      if (root == nil) {
        NSLog(@"WARNING(%s): node %@ has no owner document !",
              __PRETTY_FUNCTION__, _domNode);
      }
      root = [self _rootSearchNodeForNode:root];
      break;
      
    case DOM_ATTRIBUTE_NODE:
      root = [(id<DOMAttr>)_domNode ownerElement];
      if (root == nil) {
        NSLog(@"WARNING(%s): attribute node %@ has no owner element !",
              __PRETTY_FUNCTION__, _domNode);
      }
      root = [self _rootSearchNodeForNode:root];
      break;

    default:
      root = [self _rootSearchNodeForNode:[_domNode parentNode]];
      break;
  }
  return root;
}

+ (id)queryPathWithComponents:(NSArray *)_array {
  NSMutableArray *a;
  unsigned i, count;
  
  if ((count = [_array count]) == 0)
    return nil;
  
  if (count == 1)
    return [[self makeQueryPathExpression:[_array objectAtIndex:0]]
                  autorelease];
  
  a = [NSMutableArray arrayWithCapacity:count];
  for (i = 0; i < count; i++) {
    DOMQueryPathExpression *c;
    
    if ((c = [self makeQueryPathExpression:[_array objectAtIndex:i]])) {
      [a addObject:c];
      [c release]; c = nil;
    }
    else {
      NSLog(@"%s: couldn't make query-path expression ..", 
	    __PRETTY_FUNCTION__);
    }
  }
  
  return [_DOMQueryPathSequence sequenceWithArray:a];
}

+ (NSArray *)queryPathComponentsOfString:(NSString *)_path {
  return [_path queryPathComponents];
}

+ (id)queryPathWithString:(NSString *)_string {
  return [self queryPathWithComponents:[_string queryPathComponents]];
}

+ (_DOMQPPredicateExpression *)parsePredicateExpression:(NSString *)_expr {
  DOMQueryPathExpression    *qpexpr;
  _DOMQPPredicateExpression *pred;
#if 0
  NSLog(@"%s: can't parse predicates yet '%@'",
        __PRETTY_FUNCTION__, _expr);
#endif

  _expr = [@"-" stringByAppendingString:_expr];
  
  qpexpr = [DOMQueryPathExpression queryPathWithString:_expr];
  //NSLog(@"Expr: %@", qpexpr);
  
  pred = (_DOMQPPredicateExpression*)
    [[_DOMQPPredicateQPExpression alloc] initWithQueryPathExpression:qpexpr];
  
  return [pred autorelease];
}

+ (NSArray *)parseNodeQueryDetailExpressions:(NSString *)_path {
  unsigned       i, len, s;
  NSMutableArray *predicates;
  
  if ([_path length] == 0) return nil;
  
  predicates = nil;
  for (i = 0, s = 0, len = [_path length]; i < len; i++) {
    unichar c;
    
    c = [_path characterAtIndex:i];
    
    if ((c == ']') && (s != 0)) {
      /* finished a predicate */
      NSString *ps;
      id predicate;
      
      ps = ((i - s) > 0)
        ? [_path substringWithRange:NSMakeRange(s, (i - s))]
        : (NSString *)@"";
      
      if ((predicate = [self parsePredicateExpression:ps])) {
        if (predicates == nil)
          predicates = [NSMutableArray arrayWithCapacity:4];
        [predicates addObject:predicate];
      }
      
      s = 0;
    }
    else if (c == '[') {
      /* start a predicate */

      if (s != 0) {
        NSLog(@"%s: syntax error, predicate not properly closed ('%@') !",
              __PRETTY_FUNCTION__, _path);
      }
      
      s = (i + 1);
    }
  }
  return predicates;
}

+ (id)makeQueryPathExpression:(NSString *)_path {
  DOMQueryPathExpression *result;
  BOOL     isDeep = NO;
  NSString *predicateString;
  NSRange  r;
  
  if (([_path rangeOfString:@")"].length) != 0) {
    //[self doesNotRecognizeSelector:_cmd];
    NSLog(@"%s: unsupported querypath '%@'", __PRETTY_FUNCTION__, _path);
  }

  if ([_path hasPrefix:@"-"]) {
    isDeep = NO;
    _path = [_path substringFromIndex:1];
  }
  else
    isDeep = YES;

  r = [_path rangeOfString:@"["];
  if (r.length != 0) {
    predicateString = [_path substringFromIndex:r.location];
    _path           = [_path substringToIndex:r.location];
  }
  else
    predicateString = nil;
  
  if ([_path length] == 0) {
    /* empty path, returns current node */
    result = [[_DOMQueryPathNodeQuery alloc] initWithQueryOp:QUERY_CURRENT];
  }
  else if ([_path isEqualToString:@"/"]) {
    /* lookup root element */
    result = [[_DOMQueryPathNodeQuery alloc] initWithQueryOp:QUERY_ROOT];
  }
  else if ([_path isEqualToString:@"."]) {
    /* lookup current element */
    result = [[_DOMQueryPathNodeQuery alloc] initWithQueryOp:QUERY_CURRENT];
  }
  else if ([_path isEqualToString:@".."]) {
    /* lookup parent element */
    result = [[_DOMQueryPathNodeQuery alloc] initWithQueryOp:QUERY_PARENT];
  }
  else if ([_path isEqualToString:@"*"]) {
    result =
      [[_DOMQueryPathChildNodes alloc] initWithName:nil
				       deep:isDeep
				       type:DOM_ELEMENT_NODE];
  }
  else if ([_path isEqualToString:@"#"]) {
    result =
      [[_DOMQueryPathChildNodes alloc] initWithName:nil
				       deep:isDeep
				       type:-1];
  }
  else if ([_path hasPrefix:@"!"]) {
    /* perform key-value call */
    _path  = [_path substringFromIndex:1];
    result = [[_DOMQueryPathKeyPath alloc] initWithKeyPath:_path];
  }
  else if ([_path hasPrefix:@"?"]) {
    /* lookup processing instruction */
    _path = [_path substringFromIndex:1]; // target of PI
    result = [[_DOMQueryPathChildNodes alloc] 
	       initWithName:_path
	       deep:isDeep
	       type:DOM_PROCESSING_INSTRUCTION_NODE];
  }
  else if ([_path hasPrefix:@"@"]) {
    /* lookup attribute */
    _path  = [_path substringFromIndex:1];
    result = [[_DOMQueryPathAttribute alloc] initWithString:_path];
  }
  else {
    /* deep lookup child element */
    result =
      [[_DOMQueryPathChildNodes alloc] 
                                initWithName:_path deep:isDeep
                                type:DOM_ELEMENT_NODE];
  }
  
  /* attach predicates ... */
  
  if (([predicateString length] > 0) && (result != nil)) {
    NSArray *predicates;
    
    if ((predicates = [self parseNodeQueryDetailExpressions:predicateString])){
#if 0
      NSLog(@"%s: can't yet handle predicates %@",
            __PRETTY_FUNCTION__, predicates);
#endif
      
      result = [result autorelease];
      result = [[_DOMQueryPathPredicates alloc]
                                         initWithQueryPathExpression:result
                                         predicates:predicates];
    }
  }
  
  return result;
}

- (id)evaluateWithNode:(id)_node inContext:(id)_ctx {
  /* override in subclasses ! */
  [self doesNotRecognizeSelector:_cmd];
  return nil;
}

- (id)evaluateWithNodeList:(id)_nodes inContext:(id)_ctx {
  unsigned       i, count;
  NSMutableArray *ma;
  NSArray        *results;
  
  if ((count = [_nodes count]) == 0)
    return _nodes;
  
  ma = [[NSMutableArray alloc] init];
  for (i = 0; i < count; i++) {
    id node;
    
    node = [_nodes objectAtIndex:i];
    node = [self evaluateWithNode:node inContext:_ctx];
    
    if (node)
      [ma addObject:node];
  }
  results = [ma copy];
  [ma release];
  return [results autorelease];
}

- (id)evaluateWithNode:(id)_node {
  return [self evaluateWithNode:_node inContext:nil];
}
- (id)evaluateWithNodeList:(id)_nodes {
  return [self evaluateWithNodeList:_nodes inContext:nil];
}

@end /* DOMQueryPathExpression */

@implementation _DOMQueryPathChildNodes

- (id)initWithName:(NSString *)_name deep:(BOOL)_flag type:(int)_type {
  self->elementName = [_name copy];
  self->deep        = _flag;
  self->nodeType    = _type;
  return self;
}

- (void)dealloc {
  [self->elementName release];
  [super dealloc];
}

- (id)evaluateWithNode:(id)_node inContext:(id)_ctx {
  id   result;
  BOOL _forceList = NO;
  
  result = self->deep
    ? [self _deepChildNodesWithName:self->elementName
            type:self->nodeType node:_node]
    : [self _flatChildNodesWithName:self->elementName
            type:self->nodeType node:_node];
  
  if (!_forceList) {
    if ([result count] == 0)
      result = nil;
    else if ([result count] == 1)
      result = [result objectAtIndex:0];
  }
  
  return result;
}

- (id)evaluateWithNodeList:(id)_nodes inContext:(id)_ctx {
  unsigned       i, count;
  NSMutableArray *ma;
  NSArray        *results;
  
  if ((count = [_nodes count]) == 0)
    return _nodes;
  
  ma = [[NSMutableArray alloc] init];
  for (i = 0; i < count; i++) {
    id node;

    node = [_nodes objectAtIndex:i];
    
    node = self->deep
      ? [self _deepChildNodesWithName:self->elementName
              type:self->nodeType node:node]
      : [self _flatChildNodesWithName:self->elementName
              type:self->nodeType node:node];
    
    [ma addObjectsFromArray:node];
  }
  results = [ma copy];
  [ma release];
  return [results autorelease];
}

- (NSString *)description {
  NSMutableString *ms;

  ms = [NSMutableString stringWithCapacity:128];
  [ms appendFormat:@"<%@[0x%p]:", NSStringFromClass([self class]), self];
  [ms appendFormat:@" element='%@'", self->elementName];
  [ms appendString:self->deep ? @" deep" : @" shallow"];
  [ms appendFormat:@" nodeType=%i", self->nodeType];
  [ms appendString:@">"];
  return ms;
}

@end /* _DOMQueryPathChildNodes */

@implementation _DOMQueryPathKeyPath

- (id)initWithKeyPath:(NSString *)_path {
  self->keyPath = [_path copy];
  return self;
}
- (void)dealloc {
  [self->keyPath release];
  [super dealloc];
}

- (id)evaluateWithNode:(id)_node inContext:(id)_ctx {
  return [_node valueForKeyPath:self->keyPath];
}

- (NSString *)description {
  return [NSString stringWithFormat:@"<0x%p[%@]: keypath='%@'>",
                     self, NSStringFromClass([self class]), self->keyPath];
}

@end /* _DOMQueryPathKeyPath */

@implementation _DOMQueryPathNodeQuery

- (id)initWithQueryOp:(int)_op {
  self->queryOp = _op;
  return self;
}

- (id)evaluateWithNode:(id)_node inContext:(id)_ctx {
  switch (self->queryOp) {
    case QUERY_ROOT:
      return [self _rootSearchNodeForNode:_node];
    case QUERY_PARENT:
      return [_node parentNode];
    case QUERY_CURRENT:
      return _node;
      
    default:
      [NSException raise:@"DOMQueryPathException"
                   format:@"unknown node operation %i on node %@",
                     self->queryOp, _node];
      return nil;
  }
}

- (NSString *)description {
  return [NSString stringWithFormat:@"<0x%p[%@]: op=%i>",
                     self, NSStringFromClass([self class]), self->queryOp];
}

@end /* _DOMQueryPathNodeQuery */

@implementation _DOMQueryPathPredicates

- (id)initWithQueryPathExpression:(DOMQueryPathExpression *)_expr
  predicates:(NSArray *)_predicates
{
  self->expr       = [_expr retain];
  self->predicates = [_predicates copy];
  return self;
}

- (void)dealloc {
  [self->expr       release];
  [self->predicates release];
  [super dealloc];
}

- (id)evaluateWithNodeList:(id)_nodes inContext:(id)_ctx {
  NSArray *list;
  
  if ((list = [self->expr evaluateWithNodeList:_nodes inContext:_ctx]) &&
      [self->predicates count] > 0) {
    NSMutableArray *result;
    unsigned i, count;

    result = [NSMutableArray arrayWithCapacity:16];
    
    for (i = 0, count = [list count]; i < count; i++) {
      NSEnumerator *e;
      _DOMQPPredicateExpression *pred;
      id node;
      
      if ((node = [list objectAtIndex:i]) == nil)
        continue;
      
      e = [self->predicates objectEnumerator];
      while ((pred = [e nextObject])) {
        if (![pred matchesNode:node inContext:nil]) {
          node = nil;
          break;
        }
      }

      if (node) [result addObject:node];
    }
    
    list = [[result copy] autorelease];
  }
  return list;
}
- (id)evaluateWithNode:(id)_node inContext:(id)_ctx {
  NSLog(@"WARNING(%s): called -evaluateWithNode: ...", __PRETTY_FUNCTION__);
  return [self evaluateWithNodeList:[NSArray arrayWithObject:_node]
               inContext:_ctx];
}

@end /* _DOMQueryPathPredicates */

@implementation _DOMQueryPathSequence

- (id)initWithArray:(NSArray *)_array {
  self->queryPaths = [_array retain];
  return self;
}
+ (id)sequenceWithArray:(NSArray *)_array {
  return [[[self alloc] initWithArray:_array] autorelease];
}

- (void)dealloc {
  [self->queryPaths release];
  [super dealloc];
}

- (id)evaluateWithNode:(id)_node inContext:(id)_ctx {
  NSEnumerator          *e;
  DOMQueryPathExpression *queryPathComponent;
  id                    activeNode;

  activeNode = _node;
  e = [self->queryPaths objectEnumerator];
  while ((queryPathComponent = [e nextObject]) && (activeNode != nil)) {
    activeNode =
      [queryPathComponent evaluateWithNode:activeNode inContext:_ctx];
  }
  
  return activeNode;
}

- (NSString *)description {
  NSMutableString *ms;

  ms = [NSMutableString stringWithCapacity:128];
  [ms appendFormat:@"<%@[0x%p]: ", NSStringFromClass([self class]), self];
  [ms appendString:@"sequence="];
  [ms appendString:[self->queryPaths description]];
  [ms appendString:@">"];
  return ms;
}

@end /* _DOMQueryPathSequence */

@implementation _DOMQueryPathAttribute

- (id)initWithString:(NSString *)_spec {
  if ([_spec isEqualToString:@"*"]) {
    /* select all attributes */
    self->aflags.getAll = 1;
  }
  else if ([_spec hasPrefix:@"{"]) {
    /* fully qualified name */
    NSRange r;

    r = [_spec rangeOfString:@"}"];
    if (r.length == 0) {
      /* syntax error, missing closing '}' */
      self->attrSpec = [_spec copy];
    }
    else {
      self->attrSpec =
        [[_spec substringFromIndex:(r.location + r.length)] copy];
      self->uri =
        [[[_spec substringToIndex:r.location] substringFromIndex:1] copy];
    }
  }
  else {
    NSRange r;

    r = [_spec rangeOfString:@":"];
    if (r.length != 0) {
      /* found colon (namespaces), eg 'html:blah' */
      self->aflags.hasColon = 1;
      self->attrSpec =
        [[_spec substringFromIndex:(r.location + r.length)] copy];
      
      self->uri = [_spec substringToIndex:r.location];
      self->uri = ([self->uri length] > 1)
        ? [self->uri copy]
        : (id)@"*";
    }
    else {
      /* usual 'blah' */
      self->attrSpec = [_spec copy];
    }
  }  
  return self;
}

- (void)dealloc {
  [self->uri      release];
  [self->attrSpec release];
  [super dealloc];
}

- (id)evaluateWithNode:(id)_node inContext:(id)_ctx {
  /* lookup attribute element */
  id   attributes;
  id   result;
  BOOL _forceList = NO;
  
  attributes = [(id<DOMNode>)_node attributes];

#if DEBUG
  if (attributes == nil)
    NSLog(@"%s: node %@ has no attributes ..", __PRETTY_FUNCTION__, _node);
#endif
  
  if (self->aflags.getAll) {
    /* all attribute elements */
    result = attributes;
    _forceList = NO; // attributes behave already like a list  ..
  }
  else if (self->uri) {
    result = [attributes namedItem:self->attrSpec namespaceURI:self->uri];
  }
  else if (self->aflags.hasColon) {
    result = [attributes namedItem:self->attrSpec namespaceURI:self->uri];
    //result = [result value];
  }
  else {
    result = [attributes namedItem:self->attrSpec];
  }
  
  if (_forceList)
    result = [NSArray arrayWithObject:result];
  
  return result;
}

- (NSString *)description {
  NSMutableString *ms;

  ms = [NSMutableString stringWithCapacity:64];
  [ms appendFormat:@"<%@[0x%p]:", NSStringFromClass([self class]), self];
  [ms appendFormat:@" attrSpec='%@'", self->attrSpec];
  if (self->uri)
    [ms appendFormat:@" uri='%@'", self->uri];
  if (self->aflags.getAll)
    [ms appendString:@" getall"];
  if (self->aflags.hasColon)
    [ms appendString:@" colon"];
  [ms appendString:@">"];
  return ms;
}

@end /* _DOMQueryPathAttribute */

@implementation _DOMQPPredicateExpression

- (BOOL)matchesNode:(id)_node inContext:(id)_ctx {
  /* override in subclasses ! */
  [self doesNotRecognizeSelector:_cmd];
  return NO;
}

@end /* _DOMQPPredicateExpression */

@implementation _DOMQPPredicateQPExpression

- (id)initWithQueryPathExpression:(DOMQueryPathExpression *)_expr {
  self->qpexpr = [_expr retain];
  return self;
}
- (id)init {
  return [self initWithQueryPathExpression:nil];
}

- (void)dealloc {
  [self->qpexpr release];
  [super dealloc];
}

- (BOOL)matchesNode:(id)_node inContext:(id)_ctx {
  id nodeList;
  
  //NSLog(@"check match of node %@, qpexpr %@ ...", _node, qpexpr);
  
  nodeList = [NSArray arrayWithObject:_node];
  nodeList = [self->qpexpr evaluateWithNodeList:nodeList inContext:_ctx];
  
  return ([nodeList count] > 0) ? YES : NO;
}

@end /* _DOMQPPredicateQPExpression */