]> err.no Git - sope/blob - sope-core/NGExtensions/XmlExt.subproj/DOMNode+EOQualifier.m
aaaf12261bbcb84376f20a12c197e56cff5c39a3
[sope] / sope-core / NGExtensions / XmlExt.subproj / DOMNode+EOQualifier.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
6   SOPE is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with SOPE; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #include "DOMNode+EOQualifier.h"
23 #include <EOControl/EOQualifier.h>
24 #include "common.h"
25
26 @interface NSObject(DOMNodeEOQualifier)
27 - (NSArray *)_domChildrenMatchingQualifier:(EOQualifier *)_qualifier;
28 - (NSArray *)_domDescendantsMatchingQualifier:(EOQualifier *)_qualifier
29   includeSelf:(BOOL)_includeSelf;
30 @end
31
32 @implementation NSObject(DOMNodeEOQualifier)
33 /* this category is used to support DOM ops on any object */
34
35 static NSArray *emptyArray = nil;
36
37 - (NSArray *)_domChildrenMatchingQualifier:(EOQualifier *)_qualifier {
38   id       children;
39   unsigned count;
40   
41   if (![(id<DOMNode>)self hasChildNodes])
42     return nil;
43
44   if ((children = [(id<DOMNode>)self childNodes]) == nil)
45     return nil;
46
47   if ((count = [children count]) == 0) {
48     if (emptyArray == nil) emptyArray = [[NSArray alloc] init];
49     return emptyArray;
50   }
51   else {
52     NSMutableArray *marray;
53     unsigned i;
54     
55     marray = [NSMutableArray arrayWithCapacity:(count + 1)];
56     
57     for (i = 0; i < count; i++) {
58       id childNode;
59       
60       if ((childNode = [children objectAtIndex:i])) {
61         if ((_qualifier == nil) ||
62             [(id<EOQualifierEvaluation>)_qualifier evaluateWithObject:childNode])
63           [marray addObject:childNode];
64       }
65     }
66     
67     return [[marray copy] autorelease];
68   }
69 }
70
71 - (void)_addDOMDescendantsMatchingQualifier:(EOQualifier *)_qualifier
72   toMutableArray:(NSMutableArray *)_array
73   includeSelf:(BOOL)_includeSelf
74 {
75   id       children;
76   unsigned i, count;
77
78   if (_includeSelf) {
79     if ([(id<EOQualifierEvaluation>)_qualifier evaluateWithObject:self])
80       [_array addObject:self];
81   }
82   
83   if (![(id<DOMNode>)self hasChildNodes])
84     return;
85
86   children = [(id<DOMNode>)self childNodes];
87   for (i = 0, count = [children count]; i < count; i++) {
88     [[children objectAtIndex:i]
89                _addDOMDescendantsMatchingQualifier:_qualifier
90                toMutableArray:_array
91                includeSelf:YES];
92   }
93 }
94 - (NSArray *)_domDescendantsMatchingQualifier:(EOQualifier *)_qualifier
95   includeSelf:(BOOL)_includeSelf
96 {
97   NSMutableArray *marray;
98   
99   marray = [NSMutableArray arrayWithCapacity:16];
100   
101   [self _addDOMDescendantsMatchingQualifier:_qualifier
102         toMutableArray:marray
103         includeSelf:_includeSelf];
104   
105   return [[marray copy] autorelease];
106 }
107
108 @end /* NSObject(DOMNodeEOQualifier) */
109
110 @implementation NGDOMNode(EOQualifier)
111
112 - (NSArray *)childrenMatchingQualifier:(EOQualifier *)_qualifier {
113   return [self _domChildrenMatchingQualifier:_qualifier];
114 }
115
116 - (NSArray *)descendantsMatchingQualifier:(EOQualifier *)_qualifier {
117   return [self _domDescendantsMatchingQualifier:_qualifier
118                includeSelf:NO];
119 }
120
121 @end /* NGDOMNode(EOQualifier) */