jimm.twice.xml.dom
Class Node

java.lang.Object
  extended byjimm.twice.xml.dom.Node

public class Node
extends java.lang.Object

A document contains a (possibly empty) tree of nodes. Each node knows its parent and children and holds an XML element.

Author:
Jim Menard, jimm@io.com

Field Summary
protected  java.util.ArrayList children
           
protected  Element element
           
protected  int indentLevel
           
protected  Node parent
           
protected static int SPACES_PER_INDENT_LEVEL
           
 
Constructor Summary
Node(Element element)
          Constructor.
Node(Element element, Node parent)
          Constructor.
 
Method Summary
 Node addChild(Element element)
          Given an XML element, creates a node with this element, adds it to the list of children of this node, and returns the new node.
 Node addChild(Node node)
          Adds node as a child of this node and return it.
 java.util.Iterator children()
           
 Node findStartsWith(java.lang.String prefix)
          Performs a depth-first search to find the first node whose element's name starts with prefix.
 Element getElement()
           
 Node getParent()
           
protected  byte[] indentation()
          Returns a byte array containing whitespace that may be used to indent this node's element when creating its XML representation.
 void removeAllChildren()
           
 void removeChild(Node node)
           
 void setParent(Node p)
           
 java.lang.String toString()
           
 void writeTo(java.io.OutputStream out)
          Writes this document as XML to the specified OutputStream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

SPACES_PER_INDENT_LEVEL

protected static final int SPACES_PER_INDENT_LEVEL
See Also:
Constant Field Values

element

protected Element element

children

protected java.util.ArrayList children

parent

protected Node parent

indentLevel

protected int indentLevel
Constructor Detail

Node

public Node(Element element)
Constructor. Parent is set to null.

Parameters:
element - the XML element

Node

public Node(Element element,
            Node parent)
Constructor.

Parameters:
element - the XML element
parent - the parent of this node; ma be null
Method Detail

getElement

public Element getElement()

children

public java.util.Iterator children()

setParent

public void setParent(Node p)

getParent

public Node getParent()

addChild

public Node addChild(Element element)
Given an XML element, creates a node with this element, adds it to the list of children of this node, and returns the new node.

Parameters:
element - an XML element
Returns:
a new node that references the specified element

addChild

public Node addChild(Node node)
Adds node as a child of this node and return it.

Parameters:
node - a document node
Returns:
the node

removeChild

public void removeChild(Node node)

removeAllChildren

public void removeAllChildren()

findStartsWith

public Node findStartsWith(java.lang.String prefix)
Performs a depth-first search to find the first node whose element's name starts with prefix. Returns null if no such node is found or if prefix is null or the empty string.

Parameters:
prefix - the string to search for
Returns:
the first node (using a depth-first search) whose element's name starts with prefix, or null if no such child is found or prefix is null or the empty string

indentation

protected byte[] indentation()
Returns a byte array containing whitespace that may be used to indent this node's element when creating its XML representation.

Returns:
a byte array containing tabs and spaces

writeTo

public void writeTo(java.io.OutputStream out)
             throws java.io.IOException
Writes this document as XML to the specified OutputStream.

Parameters:
out - a OutputStream
Throws:
java.io.IOException

toString

public java.lang.String toString()