]> err.no Git - sope/blob - sope-core/NGExtensions/XmlExt.subproj/DOMNode+EOQualifier.m
minor cleanups
[sope] / sope-core / NGExtensions / XmlExt.subproj / DOMNode+EOQualifier.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
5
6   OGo 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   OGo 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 OGo; 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 // $Id$
22
23 #include "DOMNode+EOQualifier.h"
24 #include <EOControl/EOQualifier.h>
25 #include "common.h"
26
27 @interface NSObject(DOMNodeEOQualifier)
28 - (NSArray *)_domChildrenMatchingQualifier:(EOQualifier *)_qualifier;
29 - (NSArray *)_domDescendantsMatchingQualifier:(EOQualifier *)_qualifier
30   includeSelf:(BOOL)_includeSelf;
31 @end
32
33 @implementation NSObject(DOMNodeEOQualifier)
34 /* this category is used to support DOM ops on any object */
35
36 static NSArray *emptyArray = nil;
37
38 - (NSArray *)_domChildrenMatchingQualifier:(EOQualifier *)_qualifier {
39   id       children;
40   unsigned count;
41   
42   if (![(DOMNode *)self hasChildNodes])
43     return nil;
44
45   if ((children = [(DOMNode *)self childNodes]) == nil)
46     return nil;
47
48   if ((count = [children count]) == 0) {
49     if (emptyArray == nil) emptyArray = [[NSArray alloc] init];
50     return emptyArray;
51   }
52   else {
53     NSMutableArray *marray;
54     unsigned i;
55     
56     marray = [NSMutableArray arrayWithCapacity:(count + 1)];
57     
58     for (i = 0; i < count; i++) {
59       id childNode;
60       
61       if ((childNode = [children objectAtIndex:i])) {
62         if ((_qualifier == nil) ||
63             [(id<EOQualifierEvaluation>)_qualifier evaluateWithObject:childNode])
64           [marray addObject:childNode];
65       }
66     }
67     
68     return [[marray copy] autorelease];
69   }
70 }
71
72 - (void)_addDOMDescendantsMatchingQualifier:(EOQualifier *)_qualifier
73   toMutableArray:(NSMutableArray *)_array
74   includeSelf:(BOOL)_includeSelf
75 {
76   id       children;
77   unsigned i, count;
78
79   if (_includeSelf) {
80     if ([(id<EOQualifierEvaluation>)_qualifier evaluateWithObject:self])
81       [_array addObject:self];
82   }
83   
84   if (![(DOMNode *)self hasChildNodes])
85     return;
86
87   children = [(DOMNode *)self childNodes];
88   for (i = 0, count = [children count]; i < count; i++) {
89     [[children objectAtIndex:i]
90                _addDOMDescendantsMatchingQualifier:_qualifier
91                toMutableArray:_array
92                includeSelf:YES];
93   }
94 }
95 - (NSArray *)_domDescendantsMatchingQualifier:(EOQualifier *)_qualifier
96   includeSelf:(BOOL)_includeSelf
97 {
98   NSMutableArray *marray;
99   
100   marray = [NSMutableArray arrayWithCapacity:16];
101   
102   [self _addDOMDescendantsMatchingQualifier:_qualifier
103         toMutableArray:marray
104         includeSelf:_includeSelf];
105   
106   return [[marray copy] autorelease];
107 }
108
109 @end /* NSObject(DOMNodeEOQualifier) */
110
111 @implementation DOMNode(EOQualifier)
112
113 - (NSArray *)childrenMatchingQualifier:(EOQualifier *)_qualifier {
114   return [self _domChildrenMatchingQualifier:_qualifier];
115 }
116
117 - (NSArray *)descendantsMatchingQualifier:(EOQualifier *)_qualifier {
118   return [self _domDescendantsMatchingQualifier:_qualifier
119                includeSelf:NO];
120 }
121
122 @end /* DOMNode(EOQualifier) */