APIdia Launch Partner
Top Description Inners Fields Constructors Methods
javax.swing.text

public Class ElementIterator

extends Object
implements Cloneable
Class Inheritance
All Implemented Interfaces
java.lang.Cloneable
Imports
java.util.Enumeration, .Stack

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:

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 = it.next()) != null) {
             // process element
             System.out.println("elem: " + elem.getName());
         } else {
             break;
         }
     }
}
Author
Sunita Mani

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
ElementIterator.StackItem

The StackItem class stores the element as well as a child index.

Field Summary

Modifier and TypeField and Description
private Stack<ElementIterator.StackItem>
private Element

Constructor Summary

AccessConstructor and Description
public
ElementIterator(Document
a Document
document
)

Creates a new ElementIterator.

public
ElementIterator(Element
the root Element
root
)

Creates a new ElementIterator.

Method Summary

Modifier and TypeMethod and Description
public synchronized Object

Returns:

a cloned ElementIterator Object
clone
()

Overrides java.lang.Object.clone.

Clones the ElementIterator.

public Element

Returns:

element on top of the stack or null if the root element is null
current
()

Fetches the current Element.

public int

Returns:

the depth
depth
()

Fetches the current depth of element tree.

private void
dumpTree()

Iterates through the element tree and prints out each element and its attributes.

public Element

Returns:

an Element
first
()

Fetches the first element.

private Element
getDeepestLeaf(Element parent)

Returns the last child of parent that is a leaf.

public Element

Returns:

the next element or null at the end of the list
next
()

Fetches the next Element.

public Element

Returns:

previous Element if available
previous
()

Fetches the previous Element.

Inherited from java.lang.Object:
equalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

elementStackback to summary
private Stack<ElementIterator.StackItem> elementStack
rootback to summary
private Element root

Constructor Detail

ElementIteratorback to summary
public ElementIterator(Document document)

Creates a new ElementIterator. The root element is taken to get the default root element of the document.

Parameters
document:Document

a Document

ElementIteratorback to summary
public ElementIterator(Element root)

Creates a new ElementIterator.

Parameters
root:Element

the root Element

Method Detail

cloneback to summary
public synchronized Object clone()

Overrides java.lang.Object.clone.

Clones the ElementIterator.

Returns:Object

a cloned ElementIterator Object

currentback to summary
public Element current()

Fetches the current Element.

Returns:Element

element on top of the stack or null if the root element is null

depthback to summary
public int depth()

Fetches the current depth of element tree.

Returns:int

the depth

dumpTreeback to summary
private void dumpTree()

Iterates through the element tree and prints out each element and its attributes.

firstback to summary
public Element first()

Fetches the first element.

Returns:Element

an Element

getDeepestLeafback to summary
private Element getDeepestLeaf(Element parent)

Returns the last child of parent that is a leaf. If the last child is a not a leaf, this method is called with the last child.

nextback to summary
public Element next()

Fetches the next Element. The strategy used to locate the next element is a depth-first search.

Returns:Element

the next element or null at the end of the list

previousback to summary
public Element previous()

Fetches the previous Element. If however the current element is the last element, or the current element is null, then null is returned.

Returns:Element

previous Element if available

javax.swing.text back to summary

private Class ElementIterator.StackItem

extends Object
implements Cloneable
Class Inheritance
All Implemented Interfaces
java.lang.Cloneable

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 an 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.

Field Summary

Modifier and TypeField and Description
pack-priv int
pack-priv Element

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
protected Object
clone()

Overrides java.lang.Object.clone.

Creates and returns a copy of this object.

private Element
private int
private void
Inherited from java.lang.Object:
equalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

childIndexback to summary
pack-priv int childIndex
itemback to summary
pack-priv Element item

Constructor Detail

StackItemback to summary
private StackItem(Element elem)

Method Detail

cloneback to summary
protected Object clone() throws CloneNotSupportedException

Overrides java.lang.Object.clone.

Doc from java.lang.Object.clone.

Creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object. The general intent is that, for any object x, the expression:

x.clone() != x
will be true, and that the expression:
x.clone().getClass() == x.getClass()
will be true, but these are not absolute requirements. While it is typically the case that:
x.clone().equals(x)
will be true, this is not an absolute requirement.

By convention, the returned object should be obtained by calling super.clone. If a class and all of its superclasses (except Object) obey this convention, it will be the case that x.clone().getClass() == x.getClass().

By convention, the object returned by this method should be independent of this object (which is being cloned). To achieve this independence, it may be necessary to modify one or more fields of the object returned by super.clone before returning it. Typically, this means copying any mutable objects that comprise the internal "deep structure" of the object being cloned and replacing the references to these objects with references to the copies. If a class contains only primitive fields or references to immutable objects, then it is usually the case that no fields in the object returned by super.clone need to be modified.

Exceptions
CloneNotSupportedException:
if the object's class does not support the Cloneable interface. Subclasses that override the clone method can also throw this exception to indicate that an instance cannot be cloned.
getElementback to summary
private Element getElement()
getIndexback to summary
private int getIndex()
incrementIndexback to summary
private void incrementIndex()