Frames | No Frames |
1: /* ElementIterator.java -- 2: Copyright (C) 2005 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 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. 10: 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. 15: 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. 20: 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. 25: 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. */ 37: 38: package javax.swing.text; 39: 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. 46: */ 47: public class ElementIterator implements Cloneable 48: { 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; 55: 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; 60: 61: // The previous item. 62: private Element previousItem; 63: 64: /** 65: * Create a new ElementIterator to iterate over the given document. 66: * @param document the Document over which we iterate 67: */ 68: public ElementIterator(Document document) 69: { 70: this.root = document.getDefaultRootElement(); 71: this.currentElement = root; 72: this.state = new int[5]; 73: } 74: 75: /** 76: * Create a new ElementIterator to iterate over the given document. 77: * @param root the Document over which we iterate 78: */ 79: public ElementIterator(Element root) 80: { 81: this.root = root; 82: this.currentElement = root; 83: this.state = new int[5]; 84: } 85: 86: /** 87: * Returns a new ElementIterator which is a clone of this 88: * ElementIterator. 89: */ 90: public Object clone() 91: { 92: try 93: { 94: return super.clone(); 95: } 96: catch (CloneNotSupportedException _) 97: { 98: // Can't happen. 99: return null; 100: } 101: } 102: 103: /** 104: * Returns the current element. 105: */ 106: public Element current() 107: { 108: return currentElement; 109: } 110: 111: /** 112: * Returns the depth to which we have descended in the tree. 113: */ 114: public int depth() 115: { 116: return currentDepth; 117: } 118: 119: /** 120: * Returns the first element in the tree. 121: */ 122: public Element first() 123: { 124: // Reset the iterator. 125: currentElement = root; 126: currentDepth = 0; 127: previousItem = null; 128: return root; 129: } 130: 131: /** 132: * Advance the iterator and return the next element of the tree, 133: * performing an "in-order" traversal. 134: */ 135: public Element next() 136: { 137: previousItem = currentElement; 138: if (currentElement == null) 139: return null; 140: if (! currentElement.isLeaf()) 141: { 142: ++currentDepth; 143: if (currentDepth > state.length) 144: { 145: int[] newState = new int[state.length * 2]; 146: System.arraycopy(state, 0, newState, 0, state.length); 147: state = newState; 148: } 149: state[currentDepth] = 0; 150: currentElement = currentElement.getElement(0); 151: return currentElement; 152: } 153: 154: while (currentDepth > 0) 155: { 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()) 162: { 163: currentElement = currentElement.getElement(state[currentDepth]); 164: return currentElement; 165: } 166: } 167: 168: currentElement = null; 169: return currentElement; 170: } 171: 172: /** 173: * Returns the previous item. Does not modify the iterator state. 174: */ 175: public Element previous() 176: { 177: if (currentElement == null || currentElement == root) 178: return null; 179: return previousItem; 180: } 181: }