001: /*--
002:
003: $Id: DescendantIterator.java,v 1.1 2005/04/27 09:32:37 wittek Exp $
004:
005: Copyright (C) 2000-2004 Jason Hunter & Brett McLaughlin.
006: All rights reserved.
007:
008: Redistribution and use in source and binary forms, with or without
009: modification, are permitted provided that the following conditions
010: are met:
011:
012: 1. Redistributions of source code must retain the above copyright
013: notice, this list of conditions, and the following disclaimer.
014:
015: 2. Redistributions in binary form must reproduce the above copyright
016: notice, this list of conditions, and the disclaimer that follows
017: these conditions in the documentation and/or other materials
018: provided with the distribution.
019:
020: 3. The name "JDOM" must not be used to endorse or promote products
021: derived from this software without prior written permission. For
022: written permission, please contact <request_AT_jdom_DOT_org>.
023:
024: 4. Products derived from this software may not be called "JDOM", nor
025: may "JDOM" appear in their name, without prior written permission
026: from the JDOM Project Management <request_AT_jdom_DOT_org>.
027:
028: In addition, we request (but do not require) that you include in the
029: end-user documentation provided with the redistribution and/or in the
030: software itself an acknowledgement equivalent to the following:
031: "This product includes software developed by the
032: JDOM Project (http://www.jdom.org/)."
033: Alternatively, the acknowledgment may be graphical using the logos
034: available at http://www.jdom.org/images/logos.
035:
036: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
040: CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: SUCH DAMAGE.
048:
049: This software consists of voluntary contributions made by many
050: individuals on behalf of the JDOM Project and was originally
051: created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
052: Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
053: on the JDOM Project, please see <http://www.jdom.org/>.
054:
055: */
056:
057: package org.jdom;
058:
059: import java.util.*;
060: import org.jdom.Content;
061: import org.jdom.Element;
062: import org.jdom.Parent;
063:
064: /**
065: * Traverse all a parent's descendants (all children at any level below
066: * the parent).
067: *
068: * @author Bradley S. Huffman
069: * @author Jason Hunter
070: * @version $Revision: 1.1 $, $Date: 2005/04/27 09:32:37 $
071: */
072: class DescendantIterator implements Iterator {
073:
074: private Iterator iterator;
075: private Iterator nextIterator;
076: private List stack = new ArrayList();
077:
078: private static final String CVS_ID = "@(#) $RCSfile: DescendantIterator.java,v $ $Revision: 1.1 $ $Date: 2005/04/27 09:32:37 $ $Name: $";
079:
080: /**
081: * Iterator for the descendants of the supplied object.
082: *
083: * @param parent document or element whose descendants will be iterated
084: */
085: DescendantIterator(Parent parent) {
086: if (parent == null) {
087: throw new IllegalArgumentException(
088: "parent parameter was null");
089: }
090: this .iterator = parent.getContent().iterator();
091: }
092:
093: /**
094: * Returns true> if the iteration has more {@link Content} descendants.
095: *
096: * @return true is the iterator has more descendants
097: */
098: public boolean hasNext() {
099: if (iterator != null && iterator.hasNext())
100: return true;
101: if (nextIterator != null && nextIterator.hasNext())
102: return true;
103: if (stackHasAnyNext())
104: return true;
105: return false;
106: }
107:
108: /**
109: * Returns the next {@link Content} descendant.
110: *
111: * @return the next descendant
112: */
113: public Object next() {
114: if (!hasNext()) {
115: throw new NoSuchElementException();
116: }
117:
118: // If we need to descend, go for it and record where we are.
119: // We do the shuffle here on the next next() call so remove() is easy
120: // to code up.
121: if (nextIterator != null) {
122: push(iterator);
123: iterator = nextIterator;
124: nextIterator = null;
125: }
126:
127: // If this iterator is finished, try moving up the stack
128: while (!iterator.hasNext()) {
129: if (stack.size() > 0) {
130: iterator = pop();
131: } else {
132: throw new NoSuchElementException(
133: "Somehow we lost our iterator");
134: }
135: }
136:
137: Content child = (Content) iterator.next();
138: if (child instanceof Element) {
139: nextIterator = ((Element) child).getContent().iterator();
140: }
141: return child;
142: }
143:
144: /**
145: * Detaches the last {@link org.jdom.Content} returned by the last call to
146: * next from it's parent. <b>Note</b>: this <b>does not</b> affect
147: * iteration and all children, siblings, and any node following the
148: * removed node (in document order) will be visited.
149: */
150: public void remove() {
151: iterator.remove();
152: }
153:
154: private Iterator pop() {
155: int stackSize = stack.size();
156: if (stackSize == 0) {
157: throw new NoSuchElementException("empty stack");
158: }
159: return (Iterator) stack.remove(stackSize - 1);
160: }
161:
162: private void push(Iterator itr) {
163: stack.add(itr);
164: }
165:
166: private boolean stackHasAnyNext() {
167: int size = stack.size();
168: for (int i = 0; i < size; i++) {
169: Iterator itr = (Iterator) stack.get(i);
170: if (itr.hasNext()) {
171: return true;
172: }
173: }
174: return false;
175: }
176: }
|