File: DOMTreeWalker.m

package info (click to toggle)
sope 5.12.6-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 16,592 kB
  • sloc: objc: 169,229; sh: 3,823; ansic: 3,409; javascript: 446; python: 318; makefile: 185
file content (296 lines) | stat: -rw-r--r-- 7,269 bytes parent folder | download | duplicates (11)
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
/*
  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/DOMTreeWalker.h>
#include <DOM/DOMNodeFilter.h>
#include <DOM/DOMNode.h>
#include "common.h"

@implementation NGDOMTreeWalker

- (id)initWithRootNode:(id)_rootNode
  whatToShow:(unsigned long)_whatToShow
  filter:(id)_filter
  expandEntityReferences:(BOOL)_flag
{
  NSAssert(_rootNode, @"missing root-node !");

  if ((self = [super init])) {
    self->root       = [_rootNode retain];
    self->whatToShow = _whatToShow;
    self->filter     = [_filter retain];
    self->expandEntityReferences = _flag;
    
    [self setCurrentNode:_rootNode];
  }
  return self;
}

- (void)dealloc {
  [self->visibleChildren release];
  [self->currentNode release];
  [self->root        release];
  [self->filter      release];
  [super dealloc];
}
                 
/* attributes */

- (id)root {
  return self->root;
}
- (unsigned long)whatToShow {
  return self->whatToShow;
}
- (id)filter {
  return self->filter;
}
- (BOOL)expandEntityReferences {
  return self->expandEntityReferences;
}

- (void)setCurrentNode:(id)_node {
  if (_node == self->currentNode)
    /* same node */
    return;
  
  ASSIGN(self->currentNode, _node);

  /* clear state caches */
  [self->visibleChildren release]; self->visibleChildren = nil;
}
- (id)currentNode {
  return self->currentNode;
}

/* internals */

- (BOOL)_shouldShowNode:(id)_node {
  if (self->whatToShow == DOM_SHOW_ALL)
    return YES;
  
  switch([_node nodeType]) {
    case DOM_ATTRIBUTE_NODE:
      return (self->whatToShow & DOM_SHOW_ATTRIBUTE) != 0 ? YES : NO;
    case DOM_CDATA_SECTION_NODE:
      return (self->whatToShow & DOM_SHOW_CDATA_SECTION) != 0 ? YES : NO;
    case DOM_COMMENT_NODE:
      return (self->whatToShow & DOM_SHOW_COMMENT) != 0 ? YES : NO;
    case DOM_DOCUMENT_NODE:
      return (self->whatToShow & DOM_SHOW_DOCUMENT) != 0 ? YES : NO;
    case DOM_DOCUMENT_FRAGMENT_NODE:
      return (self->whatToShow & DOM_SHOW_DOCUMENT_FRAGMENT) != 0 ? YES : NO;
    case DOM_ELEMENT_NODE:
      return (self->whatToShow & DOM_SHOW_ELEMENT) != 0 ? YES : NO;
    case DOM_PROCESSING_INSTRUCTION_NODE:
      return (self->whatToShow & DOM_SHOW_PROCESSING_INSTRUCTION) != 0 ? YES:NO;
    case DOM_TEXT_NODE:
      return (self->whatToShow & DOM_SHOW_TEXT) != 0 ? YES : NO;
    case DOM_DOCUMENT_TYPE_NODE:
      return (self->whatToShow & DOM_SHOW_DOCUMENT_TYPE) != 0 ? YES : NO;
    case DOM_ENTITY_NODE:
      return (self->whatToShow & DOM_SHOW_ENTITY) != 0 ? YES : NO;
    case DOM_ENTITY_REFERENCE_NODE:
      return (self->whatToShow & DOM_SHOW_ENTITY_REFERENCE) != 0 ? YES : NO;
    case DOM_NOTATION_NODE:
      return (self->whatToShow & DOM_SHOW_NOTATION) != 0 ? YES : NO;
    default:
      return YES;
  }
}

- (BOOL)_isVisibleNode:(id)_node {
  if (![self _shouldShowNode:_node])
    return NO;
  if (self->filter)
    return [self->filter acceptNode:_node] == DOM_FILTER_ACCEPT ? YES : NO;
  return YES;
}
- (unsigned short)_navTypeOfNode:(id)_node {
  if (![self _shouldShowNode:_node])
    return DOM_FILTER_SKIP;
  if (self->filter)
    return [self->filter acceptNode:_node] == DOM_FILTER_ACCEPT ? YES : NO;
  return DOM_FILTER_ACCEPT;
}

- (NSArray *)_ensureVisibleChildren {
  static NSArray *emptyArray = nil;
  id children;
  unsigned count;

  if (self->visibleChildren)
    return self->visibleChildren;
  
  children = [[self currentNode] childNodes];

  if ((count = [children count]) > 0) {
    unsigned i;
    NSMutableArray *ma;
    
    ma = [[NSMutableArray alloc] initWithCapacity:(count + 1)];
    
    for (i = 0; i < count; i++) {
      id childNode;
      
      childNode = [children objectAtIndex:i];
      
      if ([self _isVisibleNode:childNode])
        [ma addObject:childNode];
    }
    
    self->visibleChildren = [ma copy];
    [ma release]; ma = nil;
  }
  else {
    if (emptyArray == nil) emptyArray = [[NSArray alloc] init];
    self->visibleChildren = [emptyArray retain];
  }
  return self->visibleChildren;
}

- (BOOL)_hasVisibleChildren {
  return [[self _ensureVisibleChildren] count] > 0 ? YES : NO;
}
- (id)_visibleChildren {
  return [self _ensureVisibleChildren];
}
- (id)_firstVisibleChild {
  NSArray *a = [self _ensureVisibleChildren];
  if ([a count] == 0)
    return nil;
  return [a objectAtIndex:0];
}
- (id)_lastVisibleChild {
  NSArray *a = [self _ensureVisibleChildren];
  unsigned count;
  
  if ((count = [a count]) == 0)
    return nil;
  return [a objectAtIndex:(count - 1)];
}

- (id<NSObject,DOMNode>)_visibleParentNode {
  id<NSObject,DOMNode> node;

  for (node = [[self currentNode] parentNode]; node; node =[node parentNode]) {
    if ([self _isVisibleNode:node])
      return node;
    
    if (node == [self root])
      /* do not step above root */
      break;
  }
  return nil;
}

- (id<NSObject,DOMNode>)_nextVisibleSibling {
  id<NSObject,DOMNode> node;

  for (node = [(id<NSObject,DOMNode>)[self currentNode] nextSibling];
       node != nil;
       node = [node nextSibling]) {
    if ([self _isVisibleNode:node])
      return node;
  }
  return nil;
}
- (id<NSObject,DOMNode>)_previousVisibleSibling {
  id<NSObject,DOMNode> node;

  for (node = [(id<NSObject,DOMNode>)[self currentNode] previousSibling];
       node != nil;
       node = [node previousSibling]) {
    if ([self _isVisibleNode:node])
      return node;
  }
  return nil;
}

/* operations */

- (id<NSObject,DOMNode>)parentNode {
  id parent;

  if ((parent = [self _visibleParentNode])) {
    [self setCurrentNode:parent];
    return parent;
  }
  else
    return nil;
}

- (id<NSObject,DOMNode>)firstChild {
  if ([self _hasVisibleChildren]) {
    id child;

    child = [self _firstVisibleChild];
    [self setCurrentNode:child];
    return child;
  }
  else
    return nil;
}

- (id<NSObject,DOMNode>)lastChild {
  if ([self _hasVisibleChildren]) {
    id child;

    child = [self _lastVisibleChild];
    [self setCurrentNode:child];
    return child;
  }
  else
    return nil;
}

- (id<NSObject,DOMNode>)previousSibling {
  id node;

  if ((node = [self _previousVisibleSibling])) {
    [self setCurrentNode:node];
    return node;
  }
  else
    return nil;
}

- (id<NSObject,DOMNode>)nextSibling {
  id node;

  if ((node = [self _nextVisibleSibling])) {
    [self setCurrentNode:node];
    return node;
  }
  else
    return nil;
}

- (id<NSObject,DOMNode>)previousNode {
  [self doesNotRecognizeSelector:_cmd];
  return nil;
}
- (id<NSObject,DOMNode>)nextNode {
  [self doesNotRecognizeSelector:_cmd];
  return nil;
}

@end /* NGDOMTreeWalker */