Newer
Older
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
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Xerces" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache\@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation, and was
* originally based on software copyright (c) 2001, International
* Business Machines, Inc., http://www.ibm.com . For more information
* on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
/*
* $Id$
*/
//////////////////////////////////////////////////////////////////////
// DOMNodeIteratorImpl.cpp: implementation of the DOMNodeIteratorImpl class.
//
//////////////////////////////////////////////////////////////////////
#include "DOMNodeIteratorImpl.hpp"
#include "DOMDocumentImpl.hpp"
#include <xercesc/dom/DOMDocument.hpp>
#include <xercesc/dom/DOMException.hpp>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
DOMNodeIteratorImpl::DOMNodeIteratorImpl (DOMDocument* doc,
DOMNode* root,
unsigned long whatToShow,
DOMNodeFilter* nodeFilter,
bool expandEntityRef)
David Abram Cargill
committed
: fRoot(root),
fDocument(doc),
fWhatToShow(whatToShow),
fNodeFilter(nodeFilter),
fExpandEntityReferences(expandEntityRef),
David Abram Cargill
committed
fDetached(false),
fCurrentNode(0),
fForward(true)
{
}
DOMNodeIteratorImpl::DOMNodeIteratorImpl ( const DOMNodeIteratorImpl& toCopy)
David Abram Cargill
committed
: fRoot(toCopy.fRoot),
fDocument(toCopy.fDocument),
fWhatToShow(toCopy.fWhatToShow),
fNodeFilter(toCopy.fNodeFilter),
fExpandEntityReferences(toCopy.fExpandEntityReferences),
David Abram Cargill
committed
fDetached(toCopy.fDetached),
fCurrentNode(toCopy.fCurrentNode),
fForward(toCopy.fForward)
{
}
DOMNodeIteratorImpl& DOMNodeIteratorImpl::operator= (const DOMNodeIteratorImpl& other) {
fRoot = other.fRoot;
fCurrentNode = other.fRoot;
fWhatToShow = other.fWhatToShow;
fNodeFilter = other.fNodeFilter;
fForward = other.fForward;
fDetached = other.fDetached;
fExpandEntityReferences = other.fExpandEntityReferences;
fDocument = other.fDocument;
return *this;
}
DOMNodeIteratorImpl::~DOMNodeIteratorImpl ()
{
fDetached = false;
}
void DOMNodeIteratorImpl::detach ()
{
fDetached = true;
((DOMDocumentImpl *)fDocument)->removeNodeIterator(this);
}
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
DOMNode* DOMNodeIteratorImpl::getRoot() {
return fRoot;
}
// Implementation Note: Note that the iterator looks at whatToShow
// and filter values at each call, and therefore one _could_ add
// setters for these values and alter them while iterating!
/** Return the whatToShow value */
unsigned long DOMNodeIteratorImpl::getWhatToShow () {
return fWhatToShow;
}
/** Return the filter */
DOMNodeFilter* DOMNodeIteratorImpl::getFilter () {
return fNodeFilter;
}
/** Get the expandEntity reference flag. */
bool DOMNodeIteratorImpl::getExpandEntityReferences()
{
return fExpandEntityReferences;
}
/** Return the next DOMNode* in the Iterator. The node is the next node in
* depth-first order which also passes the filter, and whatToShow.
* A 0 return means either that
*/
DOMNode* DOMNodeIteratorImpl::nextNode () {
if (fDetached)
throw DOMException(DOMException::INVALID_STATE_ERR, 0);
// if root is 0 there is no next node->
if (!fRoot)
return 0;
DOMNode* aNextNode = fCurrentNode;
bool accepted = false; // the next node has not been accepted.
while (!accepted) {
// if last direction is not forward, repeat node->
if (!fForward && (aNextNode != 0)) {
//System.out.println("nextNode():!fForward:"+fCurrentNode.getNodeName());
aNextNode = fCurrentNode;
} else {
// else get the next node via depth-first
aNextNode = nextNode(aNextNode, true);
}
fForward = true; //REVIST: should direction be set forward before 0 check?
// nothing in the list. return 0.
if (!aNextNode) return 0;
// does node pass the filters and whatToShow?
accepted = acceptNode(aNextNode);
if (accepted) {
// if so, then the node is the current node->
fCurrentNode = aNextNode;
return fCurrentNode;
}
}
// no nodes, or no accepted nodes.
return 0;
}
/** Return the previous Node in the Iterator. The node is the next node in
* _backwards_ depth-first order which also passes the filter, and whatToShow.
*/
DOMNode* DOMNodeIteratorImpl::previousNode () {
if (fDetached)
throw DOMException(DOMException::INVALID_STATE_ERR, 0);
// if the root is 0, or the current node is 0, return 0.
if (!fRoot || !fCurrentNode) return 0;
DOMNode* aPreviousNode = fCurrentNode;
bool accepted = false;
while (!accepted) {
if (fForward && (aPreviousNode != 0)) {
//repeat last node->
aPreviousNode = fCurrentNode;
} else {
// get previous node in backwards depth first order.
aPreviousNode = previousNode(aPreviousNode);
}
// we are going backwards
fForward = false;
// if the new previous node is 0, we're at head or past the root,
// so return 0.
if (!aPreviousNode) return 0;
// check if node passes filters and whatToShow.
accepted = acceptNode(aPreviousNode);
if (accepted) {
// if accepted, update the current node, and return it.
fCurrentNode = aPreviousNode;
return fCurrentNode;
}
}
// there are no nodes?
return 0;
}
/** The node is accepted if it passes the whatToShow and the filter. */
bool DOMNodeIteratorImpl::acceptNode (DOMNode* node) {
if (fDetached)
throw DOMException(DOMException::INVALID_STATE_ERR, 0);
if (fNodeFilter == 0) {
return ((fWhatToShow & (1 << (node->getNodeType() - 1))) != 0);
} else {
return ((fWhatToShow & (1 << (node->getNodeType() - 1))) != 0)
&& fNodeFilter->acceptNode(node) == DOMNodeFilter::FILTER_ACCEPT;
}
}
/** Return node, if matches or any parent if matches. */
DOMNode* DOMNodeIteratorImpl::matchNodeOrParent (DOMNode* node) {
for (DOMNode* n = fCurrentNode; n != fRoot; n = n->getParentNode()) {
if (node == n) return n;
}
return 0;
}
/** The method nextNode(DOMNode, bool) returns the next node
* from the actual DOM tree.
*
* The bool visitChildren determines whether to visit the children.
* The result is the nextNode.
*/
DOMNode* DOMNodeIteratorImpl::nextNode (DOMNode* node, bool visitChildren) {
if (fDetached)
throw DOMException(DOMException::INVALID_STATE_ERR, 0);
if (!node) return fRoot;
DOMNode* result = 0;
// only check children if we visit children.
if (visitChildren) {
//if hasChildren, return 1st child.
if (node->hasChildNodes()) {
result = node->getFirstChild();
return result;
}
}
// if hasSibling, return sibling
if (node != fRoot) {
result = node->getNextSibling();
if (result != 0) return result;
// return parent's 1st sibling.
DOMNode* parent = node->getParentNode();
while ((parent != 0) && parent != fRoot) {
result = parent->getNextSibling();
if (result != 0) {
return result;
} else {
parent = parent->getParentNode();
}
} // while (parent != 0 && parent != fRoot) {
}
// end of list, return 0
return 0;
}
/** The method previousNode(DOMNode) returns the previous node
* from the actual DOM tree.
*/
DOMNode* DOMNodeIteratorImpl::previousNode (DOMNode* node) {
if (fDetached)
throw DOMException(DOMException::INVALID_STATE_ERR, 0);
DOMNode* result = 0;
// if we're at the root, return 0.
if (node == fRoot)
return 0;
// get sibling
result = node->getPreviousSibling();
if (!result) {
//if 1st sibling, return parent
result = node->getParentNode();
return result;
}
// if sibling has children, keep getting last child of child.
if (result->hasChildNodes()) {
while (result->hasChildNodes()) {
result = result->getLastChild();
}
}
return result;
}
/** Fix-up the iterator on a remove. Called by DOM or otherwise,
* before an actual DOM remove.
*/
void DOMNodeIteratorImpl::removeNode (DOMNode* node) {
if (fDetached)
throw DOMException(DOMException::INVALID_STATE_ERR, 0);
// Implementation note: Fix-up means setting the current node properly
// after a remove.
if (!node) return;
DOMNode* deleted = matchNodeOrParent(node);
if (!deleted) return;
if (fForward) {
fCurrentNode = previousNode(deleted);
} else
// if (!fForward)
{
DOMNode* next = nextNode(deleted, false);
if (next != 0) {
// normal case: there _are_ nodes following this in the iterator.
fCurrentNode = next;
} else {
// the last node in the iterator is to be removed,
// so we set the current node to be the previous one.
fCurrentNode = previousNode(deleted);
fForward = true;
}
}
}
void DOMNodeIteratorImpl::release()
{
detach();
// for performance reason, do not recycle pointer
// chance that this is allocated again and again is not usual