Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / javax / swing / text / ElementIterator.java
bloba6a5ff618bd1471cd141e3e2267cb15a7f663725
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)
9 any later version.
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
19 02110-1301 USA.
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
24 combination.
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;
40 /**
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
49 // The root element.
50 private Element root;
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
58 // examining.
59 private int[] state;
61 // The previous item.
62 private Element previousItem;
64 /**
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];
75 /**
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)
81 this.root = root;
82 this.currentElement = root;
83 this.state = new int[5];
86 /**
87 * Returns a new ElementIterator which is a clone of this
88 * ElementIterator.
90 public Object clone()
92 try
94 return super.clone();
96 catch (CloneNotSupportedException _)
98 // Can't happen.
99 return null;
104 * Returns the current element.
106 public Element current()
108 return currentElement;
112 * Returns the depth to which we have descended in the tree.
114 public int depth()
116 return currentDepth;
120 * Returns the first element in the tree.
122 public Element first()
124 // Reset the iterator.
125 currentElement = root;
126 currentDepth = 0;
127 previousItem = null;
128 return 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)
139 return null;
140 if (! currentElement.isLeaf())
142 ++currentDepth;
143 if (currentDepth > state.length)
145 int[] newState = new int[state.length * 2];
146 System.arraycopy(state, 0, newState, 0, state.length);
147 state = newState;
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
157 // level.
158 --currentDepth;
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)
178 return null;
179 return previousItem;