001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Alexander T. Simbirtsev
019: * @version $Revision$
020: */package javax.swing.text;
021:
022: public class ElementIterator implements Cloneable {
023:
024: private final Element root;
025: private Element cursor;
026: private int depth;
027:
028: public ElementIterator(final Element root) {
029: this .root = root;
030: }
031:
032: public ElementIterator(final Document doc) {
033: this (doc.getRootElements()[0]);
034: }
035:
036: public Element first() {
037: return initCursorWithRoot();
038: }
039:
040: public Element current() {
041: if (cursor == null) {
042: return initCursorWithRoot();
043: }
044: if (cursor == root && root.getElementCount() == 0) {
045: depth = 0;
046: return null;
047: }
048: return cursor;
049: }
050:
051: public Element next() {
052: if (cursor == null) {
053: return initCursorWithRoot();
054: }
055: if (cursor == root && root.getElementCount() == 0) {
056: depth = 0;
057: return null;
058: }
059: if (cursor.getElementCount() != 0) {
060: depth++;
061: return (cursor = cursor.getElement(0));
062: }
063:
064: while (cursor != root && cursor != null) {
065: final Element parent = cursor.getParentElement();
066: final int currentIndex = getElementIndex(parent, cursor);
067: if (currentIndex < parent.getElementCount() - 1) {
068: return (cursor = parent.getElement(currentIndex + 1));
069: }
070: depth--;
071: cursor = parent;
072: }
073: depth = 0;
074: return (cursor = null);
075: }
076:
077: public Element previous() {
078: if (cursor == null || cursor == root) {
079: return null;
080: }
081:
082: final Element parent = cursor.getParentElement();
083: if (parent == null) {
084: return null;
085: }
086: final int currentIndex = getElementIndex(parent, cursor);
087: if (currentIndex == 0) {
088: return parent;
089: }
090: Element result = parent.getElement(currentIndex - 1);
091: while (result.getElementCount() != 0) {
092: result = result.getElement(result.getElementCount() - 1);
093: }
094: return result;
095: }
096:
097: public int depth() {
098: return depth;
099: }
100:
101: public Object clone() {
102: final ElementIterator cloned = new ElementIterator(root);
103: cloned.cursor = cursor;
104: cloned.depth = depth;
105: return cloned;
106: }
107:
108: private int getElementIndex(final Element parent,
109: final Element child) {
110: final int numChildren = parent.getElementCount();
111: for (int i = 0; i < numChildren; i++) {
112: if (parent.getElement(i) == child) {
113: return i;
114: }
115: }
116:
117: return -1;
118: }
119:
120: private Element initCursorWithRoot() {
121: cursor = root;
122: depth = 1;
123: return cursor;
124: }
125: }
|