comparison NGSrich_0.5.5/src/org/jdom/DescendantIterator.java @ 0:89ad0a9cca52 default tip

Uploaded
author pfrommolt
date Mon, 21 Nov 2011 08:12:19 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:89ad0a9cca52
1 /*--
2
3 $Id: DescendantIterator.java,v 1.6 2007/11/10 05:28:58 jhunter Exp $
4
5 Copyright (C) 2000-2007 Jason Hunter & Brett McLaughlin.
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions
10 are met:
11
12 1. Redistributions of source code must retain the above copyright
13 notice, this list of conditions, and the following disclaimer.
14
15 2. Redistributions in binary form must reproduce the above copyright
16 notice, this list of conditions, and the disclaimer that follows
17 these conditions in the documentation and/or other materials
18 provided with the distribution.
19
20 3. The name "JDOM" must not be used to endorse or promote products
21 derived from this software without prior written permission. For
22 written permission, please contact <request_AT_jdom_DOT_org>.
23
24 4. Products derived from this software may not be called "JDOM", nor
25 may "JDOM" appear in their name, without prior written permission
26 from the JDOM Project Management <request_AT_jdom_DOT_org>.
27
28 In addition, we request (but do not require) that you include in the
29 end-user documentation provided with the redistribution and/or in the
30 software itself an acknowledgement equivalent to the following:
31 "This product includes software developed by the
32 JDOM Project (http://www.jdom.org/)."
33 Alternatively, the acknowledgment may be graphical using the logos
34 available at http://www.jdom.org/images/logos.
35
36 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39 DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
40 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 SUCH DAMAGE.
48
49 This software consists of voluntary contributions made by many
50 individuals on behalf of the JDOM Project and was originally
51 created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
52 Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
53 on the JDOM Project, please see <http://www.jdom.org/>.
54
55 */
56
57 package org.jdom;
58
59 import java.util.*;
60 import org.jdom.Content;
61 import org.jdom.Element;
62 import org.jdom.Parent;
63
64 /**
65 * Traverse all a parent's descendants (all children at any level below
66 * the parent).
67 *
68 * @author Bradley S. Huffman
69 * @author Jason Hunter
70 * @version $Revision: 1.6 $, $Date: 2007/11/10 05:28:58 $
71 */
72 class DescendantIterator implements Iterator {
73
74 private Iterator iterator;
75 private Iterator nextIterator;
76 private List stack = new ArrayList();
77
78 private static final String CVS_ID =
79 "@(#) $RCSfile: DescendantIterator.java,v $ $Revision: 1.6 $ $Date: 2007/11/10 05:28:58 $ $Name: jdom_1_1_1 $";
80
81 /**
82 * Iterator for the descendants of the supplied object.
83 *
84 * @param parent document or element whose descendants will be iterated
85 */
86 DescendantIterator(Parent parent) {
87 if (parent == null) {
88 throw new IllegalArgumentException("parent parameter was null");
89 }
90 this.iterator = parent.getContent().iterator();
91 }
92
93 /**
94 * Returns true> if the iteration has more {@link Content} descendants.
95 *
96 * @return true is the iterator has more descendants
97 */
98 public boolean hasNext() {
99 if (iterator != null && iterator.hasNext()) return true;
100 if (nextIterator != null && nextIterator.hasNext()) return true;
101 if (stackHasAnyNext()) return true;
102 return false;
103 }
104
105 /**
106 * Returns the next {@link Content} descendant.
107 *
108 * @return the next descendant
109 */
110 public Object next() {
111 if (!hasNext()) {
112 throw new NoSuchElementException();
113 }
114
115 // If we need to descend, go for it and record where we are.
116 // We do the shuffle here on the next next() call so remove() is easy
117 // to code up.
118 if (nextIterator != null) {
119 push(iterator);
120 iterator = nextIterator;
121 nextIterator = null;
122 }
123
124 // If this iterator is finished, try moving up the stack
125 while (!iterator.hasNext()) {
126 if (stack.size() > 0) {
127 iterator = pop();
128 }
129 else {
130 throw new NoSuchElementException("Somehow we lost our iterator");
131 }
132 }
133
134 Content child = (Content) iterator.next();
135 if (child instanceof Element) {
136 nextIterator = ((Element)child).getContent().iterator();
137 }
138 return child;
139 }
140
141 /**
142 * Detaches the last {@link org.jdom.Content} returned by the last call to
143 * next from it's parent. <b>Note</b>: this <b>does not</b> affect
144 * iteration and all children, siblings, and any node following the
145 * removed node (in document order) will be visited.
146 */
147 public void remove() {
148 iterator.remove();
149 }
150
151 private Iterator pop() {
152 int stackSize = stack.size();
153 if (stackSize == 0) {
154 throw new NoSuchElementException("empty stack");
155 }
156 return (Iterator) stack.remove(stackSize - 1);
157 }
158
159 private void push(Iterator itr) {
160 stack.add(itr);
161 }
162
163 private boolean stackHasAnyNext() {
164 int size = stack.size();
165 for (int i = 0; i < size; i++) {
166 Iterator itr = (Iterator) stack.get(i);
167 if (itr.hasNext()) {
168 return true;
169 }
170 }
171 return false;
172 }
173 }