|
Java example source code file (ElementIterator.java)
The ElementIterator.java Java example source code
/*
* Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.swing.text;
import java.util.Stack;
import java.util.Enumeration;
/**
* <p>
* ElementIterator, as the name suggests, iterates over the Element
* tree. The constructor can be invoked with either Document or an Element
* as an argument. If the constructor is invoked with a Document as an
* argument then the root of the iteration is the return value of
* document.getDefaultRootElement().
*
* The iteration happens in a depth-first manner. In terms of how
* boundary conditions are handled:
* a) if next() is called before first() or current(), the
* root will be returned.
* b) next() returns null to indicate the end of the list.
* c) previous() returns null when the current element is the root
* or next() has returned null.
*
* The ElementIterator does no locking of the Element tree. This means
* that it does not track any changes. It is the responsibility of the
* user of this class, to ensure that no changes happen during element
* iteration.
*
* Simple usage example:
*
* public void iterate() {
* ElementIterator it = new ElementIterator(root);
* Element elem;
* while (true) {
* if ((elem = next()) != null) {
* // process element
* System.out.println("elem: " + elem.getName());
* } else {
* break;
* }
* }
* }
*
* @author Sunita Mani
*
*/
public class ElementIterator implements Cloneable {
private Element root;
private Stack<StackItem> elementStack = null;
/**
* The StackItem class stores the element
* as well as a child index. If the
* index is -1, then the element represented
* on the stack is the element itself.
* Otherwise, the index functions as as index
* into the vector of children of the element.
* In this case, the item on the stack
* represents the "index"th child of the element
*
*/
private class StackItem implements Cloneable {
Element item;
int childIndex;
private StackItem(Element elem) {
/**
* -1 index implies a self reference,
* as opposed to an index into its
* list of children.
*/
this.item = elem;
this.childIndex = -1;
}
private void incrementIndex() {
childIndex++;
}
private Element getElement() {
return item;
}
private int getIndex() {
return childIndex;
}
protected Object clone() throws java.lang.CloneNotSupportedException {
return super.clone();
}
}
/**
* Creates a new ElementIterator. The
* root element is taken to get the
* default root element of the document.
*
* @param document a Document.
*/
public ElementIterator(Document document) {
root = document.getDefaultRootElement();
}
/**
* Creates a new ElementIterator.
*
* @param root the root Element.
*/
public ElementIterator(Element root) {
this.root = root;
}
/**
* Clones the ElementIterator.
*
* @return a cloned ElementIterator Object.
*/
public synchronized Object clone() {
try {
ElementIterator it = new ElementIterator(root);
if (elementStack != null) {
it.elementStack = new Stack<StackItem>();
for (int i = 0; i < elementStack.size(); i++) {
StackItem item = elementStack.elementAt(i);
StackItem clonee = (StackItem)item.clone();
it.elementStack.push(clonee);
}
}
return it;
} catch (CloneNotSupportedException e) {
throw new InternalError(e);
}
}
/**
* Fetches the first element.
*
* @return an Element.
*/
public Element first() {
// just in case...
if (root == null) {
return null;
}
elementStack = new Stack<StackItem>();
if (root.getElementCount() != 0) {
elementStack.push(new StackItem(root));
}
return root;
}
/**
* Fetches the current depth of element tree.
*
* @return the depth.
*/
public int depth() {
if (elementStack == null) {
return 0;
}
return elementStack.size();
}
/**
* Fetches the current Element.
*
* @return element on top of the stack or
* <code>null if the root element is
Other Java examples (source code examples)Here is a short list of links related to this Java ElementIterator.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.