1 /* ElementIterator.java --
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package javax
.swing
.text
;
41 * This class can be used to iterate over the {@link Element} tree of
42 * a {@link Document} or an {@link Element}. This iterator performs
43 * an "in-order" traversal -- first it visits a node, then each of the
44 * node's children in order. No locking is performed during the
45 * iteration; that is up to the caller.
47 public class ElementIterator
implements Cloneable
51 // The current element.
52 private Element currentElement
;
53 // The depth to which we have descended in the tree.
54 private int currentDepth
;
56 // This is at least as big as the current depth, and at index N
57 // contains the index of the child element we're currently
62 private Element previousItem
;
65 * Create a new ElementIterator to iterate over the given document.
66 * @param document the Document over which we iterate
68 public ElementIterator(Document document
)
70 this.root
= document
.getDefaultRootElement();
71 this.currentElement
= root
;
72 this.state
= new int[5];
76 * Create a new ElementIterator to iterate over the given document.
77 * @param root the Document over which we iterate
79 public ElementIterator(Element root
)
82 this.currentElement
= root
;
83 this.state
= new int[5];
87 * Returns a new ElementIterator which is a clone of this
96 catch (CloneNotSupportedException _
)
104 * Returns the current element.
106 public Element
current()
108 return currentElement
;
112 * Returns the depth to which we have descended in the tree.
120 * Returns the first element in the tree.
122 public Element
first()
124 // Reset the iterator.
125 currentElement
= root
;
132 * Advance the iterator and return the next element of the tree,
133 * performing an "in-order" traversal.
135 public Element
next()
137 previousItem
= currentElement
;
138 if (currentElement
== null)
140 if (! currentElement
.isLeaf())
143 if (currentDepth
> state
.length
)
145 int[] newState
= new int[state
.length
* 2];
146 System
.arraycopy(state
, 0, newState
, 0, state
.length
);
149 state
[currentDepth
] = 0;
150 currentElement
= currentElement
.getElement(0);
151 return currentElement
;
154 while (currentDepth
> 0)
156 // At a leaf, or done with a non-leaf's children, so go up a
159 currentElement
= currentElement
.getParentElement();
160 ++state
[currentDepth
];
161 if (state
[currentDepth
] < currentElement
.getElementCount())
163 currentElement
= currentElement
.getElement(state
[currentDepth
]);
164 return currentElement
;
168 currentElement
= null;
169 return currentElement
;
173 * Returns the previous item. Does not modify the iterator state.
175 public Element
previous()
177 if (currentElement
== null || currentElement
== root
)