/
DescendantIterator.java
209 lines (180 loc) · 6.47 KB
/
DescendantIterator.java
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
/*--
Copyright (C) 2000-2012 Jason Hunter & Brett McLaughlin.
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 disclaimer that follows
these conditions in the documentation and/or other materials
provided with the distribution.
3. The name "JDOM" must not be used to endorse or promote products
derived from this software without prior written permission. For
written permission, please contact <request_AT_jdom_DOT_org>.
4. Products derived from this software may not be called "JDOM", nor
may "JDOM" appear in their name, without prior written permission
from the JDOM Project Management <request_AT_jdom_DOT_org>.
In addition, we request (but do not require) that you include in the
end-user documentation provided with the redistribution and/or in the
software itself an acknowledgement equivalent to the following:
"This product includes software developed by the
JDOM Project (http://www.jdom.org/)."
Alternatively, the acknowledgment may be graphical using the logos
available at http://www.jdom.org/images/logos.
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 JDOM AUTHORS OR THE PROJECT
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 JDOM Project and was originally
created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
on the JDOM Project, please see <http://www.jdom.org/>.
*/
package org.jdom2;
import java.util.*;
import org.jdom2.Content;
import org.jdom2.Parent;
import org.jdom2.util.IteratorIterable;
/**
* Traverse all a parent's descendants (all children at any level below
* the parent - excludes the parent itself).
*
* @author Bradley S. Huffman
* @author Jason Hunter
* @author Rolf Lear
*/
final class DescendantIterator implements IteratorIterable<Content> {
/** Needed to be Iterable! */
private final Parent parent;
/*
* Note, we use an Array of Object here, even through
* List<Iterator<Content>> would look neater, etc.
* Fact is, for 'hamlet', using a list for the stack takes about
* twice as long as using the Object[] array.
*/
private Object[] stack = new Object[16];
private int ssize = 0;
/** The iterator that supplied to most recent next() */
private Iterator<Content> current = null;
/** The iterator going down the tree, null unless next() returned Parent */
private Iterator<Content> descending = null;
/** The iterator going up the tree, null unless next() returned dead-end */
private Iterator<Content> ascending = null;
/** what it says... */
private boolean hasnext = true;
/**
* Iterator for the descendants of the supplied object.
*
* @param parent document or element whose descendants will be iterated
*/
DescendantIterator(Parent parent) {
this.parent = parent;
// can trust that parent is not null, DescendantIterator is package-private.
current = parent.getContent().iterator();
hasnext = current.hasNext();
}
@Override
public DescendantIterator iterator() {
// Implement the Iterable stuff.
return new DescendantIterator(parent);
}
/**
* Returns <b>true</b> if the iteration has more {@link Content} descendants.
*
* @return true is the iterator has more descendants
*/
@Override
public boolean hasNext() {
return hasnext;
}
/**
* Returns the next {@link Content} descendant.
*
* @return the next descendant
*/
@Override
public Content next() {
// set the 'current' if it needs changing.
if (descending != null) {
current = descending;
descending = null;
} else if (ascending != null) {
current = ascending;
ascending = null;
}
final Content ret = current.next();
// got an item to return.
// sort out the next state....
if ((ret instanceof Element) && ((Element)ret).getContentSize() > 0) {
// there is another descendant, and it has values.
// our next will be down....
descending = ((Element)ret).getContent().iterator();
if (ssize >= stack.length) {
stack = Arrays.copyOf(stack, ssize + 16);
}
stack[ssize++] = current;
return ret;
}
if (current.hasNext()) {
// our next will be along....
return ret;
}
// our next will be up.
while (ssize > 0) {
// if the stack was generic, this would not be needed, but,
// the java.uti.* stack codes are too slow.
@SuppressWarnings("unchecked")
final Iterator<Content> subit = (Iterator<Content>)stack[--ssize];
ascending = subit;
stack[ssize] = null;
if (ascending.hasNext()) {
return ret;
}
}
ascending = null;
hasnext = false;
return ret;
}
/**
* Detaches the last {@link org.jdom2.Content} returned by the last call to
* next from it's parent. <b>Note</b>: this <b>does not</b> affect
* iteration and all children, siblings, and any node following the
* removed node (in document order) will be visited.
*/
@Override
public void remove() {
current.remove();
// if our next move was to go down, we can't.
// we can go along, or up.
descending = null;
if (current.hasNext() || ascending != null) {
// we have a next element, or our next move was up anyway.
return;
}
// our next move was going to be down, or accross, but those are not
// possible any more, need to check up.
// our next will be up.
while (ssize > 0) {
@SuppressWarnings("unchecked")
final Iterator<Content> subit = (Iterator<Content>)stack[--ssize];
stack[ssize] = null;
ascending = subit;
if (ascending.hasNext()) {
return;
}
}
ascending = null;
hasnext = false;
}
}