de.cbse.jeasy
Class JENode

java.lang.Object
  extended by de.cbse.jeasy.JENode
All Implemented Interfaces:
javax.swing.tree.TreeNode

public class JENode
extends java.lang.Object
implements javax.swing.tree.TreeNode

JENode implements a XML structure as a TreeNode.
A JENode may be created by the SAX parser JESaxParser.parse2tree(...) or may be created by initialization.
A JENode contains of JEElements as userObjects.
These JEElements contain the tag name, the attributes and the value of the XML tag.

See a demonstration in the program JEasyExample tab "TreeNode".

Author:
Harry Pfohl

Field Summary
protected  boolean allowsChildren
          true if the node is able to have children
static int CDATA
           
protected  java.util.Vector children
          array of children, may be null if this node has no children
static int COMMENT
           
static java.util.Enumeration EMPTY_ENUMERATION
          An enumeration that is always empty.
static java.lang.String nextline
          nextline Line separator xmlversion XML version
protected  JENode parent
          this node's parent, or null if this node has no parent
protected  java.lang.Object userObject
          optional user object
static java.lang.String xmlversion
           
 
Constructor Summary
JENode()
          Creates a tree node that has no parent and no children, but which allows children.
JENode(java.lang.Object userObject)
          Creates a tree node with no parent, no children, but which allows children, and initializes it with the specified user object.
JENode(java.lang.Object userObject, boolean allowsChildren)
          Creates a tree node with no parent, no children, initialized with the specified user object, and that allows children only if specified.
JENode(java.lang.String tagname)
          Creates a tree node that has an XML tag
JENode(java.lang.String tagname, java.util.HashMap hm)
          Creates a tree node that has an XML tag and attributes
JENode(java.lang.String tagname, java.lang.String data)
          Creates a tree node that has an XML tag and data
JENode(java.lang.String tagname, java.lang.String data, java.util.HashMap hm)
          Creates a tree node that has an XML tag with attributes and data
JENode(java.lang.String tagname, java.lang.String data, int typ)
          Creates a tree node that has an XML tag and data as CDATA or COMMENT
JENode(java.lang.String tagname, java.lang.String data, int typ, java.util.HashMap hm)
          Creates a tree node that has an XML tag with attributes and data
 
Method Summary
 void add(JENode newChild)
          Removes newChild from its parent and makes it a child of this node by adding it to the end of this node's child array.
 java.util.Enumeration children()
          Creates and returns a forward-order enumeration of this node's children.
 java.lang.Object clone()
          Creates a new JENode out of an existing JENode.
 java.util.Vector getAllNodes(java.lang.String tagName)
          Gets a Vector of all JENodes with the given tagName
 java.util.Vector getAllNodesWithAttribute(java.lang.String attributeName, java.lang.String attributeValue)
          Gets a Vector of all JENodes with the given Attribute
 boolean getAllowsChildren()
           
 javax.swing.tree.TreeNode getChildAt(int index)
          Returns the child at the specified index in this node's child array.
 int getChildCount()
          Returns the number of children of this node.
 JEElement getElement()
          Returns this node's user object JEElement.
 JEElement getElement(java.lang.String tagName)
          Gets the JEElement with the given tagName inside the given JENode.
 java.lang.String getElementData(java.lang.String tagName)
          Gets the text of the JEElement with the given tagName inside the given JENode.
 java.util.Vector getElementVector(java.lang.String tagName)
          Gets a Vector of all Sub-Element data with the given tagName
 int getIndex(javax.swing.tree.TreeNode aChild)
          Returns the index of the specified child in this node's child array.
 JENode getNode(java.lang.String tagName)
          Gets the JENode with the given tagName inside the given JENode.
 JENode getNode(java.lang.String child, java.lang.String subChild)
          Looks for a childnode with the name child and there for the child equals subChild
 JENode getNodeByTag(java.lang.String element, java.lang.String subelement, java.lang.String subelementText)
          Gets a node with name equals element and its subelement and its value is subelementText
 java.lang.String getNodeName()
          Gets the name of the JENoden, which is the name of the JEElement
 java.util.Vector getNodes(java.lang.String tagName)
          Gets a Vector of all children JENodes with the given tagName
 javax.swing.tree.TreeNode getParent()
          Returns this node's parent or null if this node has no parent.
 java.lang.Object getUserObject()
          Returns this node's user object.
 void insert(JENode newChild, int childIndex)
          Removes newChild from its present parent (if it has a parent), sets the child's parent to this node, and then adds the child to this node's child array at index childIndex.
 boolean isLeaf()
          Returns true if this node has no children.
 boolean isNodeAncestor(javax.swing.tree.TreeNode anotherNode)
          Returns true if anotherNode is an ancestor of this node -- if it is this node, this node's parent, or an ancestor of this node's parent.
 boolean isNodeChild(javax.swing.tree.TreeNode aNode)
          Returns true if aNode is a child of this node.
static void main(java.lang.String[] args)
           
protected  void recToString(java.lang.StringBuffer sb, JENode jen, int depth)
           
 void remove(int childIndex)
          Removes the child at the specified index from this node's children and sets that node's parent to null.
 void remove(JENode aChild)
          Removes aChild from this node's child array, giving it a null parent.
 void setElement(JEElement userObject)
          Sets the user object for this node to userObject.
 void setParent(JENode newParent)
          Sets this node's parent to newParent but does not change the parent's child array.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

CDATA

public static final int CDATA
See Also:
Constant Field Values

COMMENT

public static final int COMMENT
See Also:
Constant Field Values

EMPTY_ENUMERATION

public static final java.util.Enumeration EMPTY_ENUMERATION
An enumeration that is always empty. This is used when an enumeration of a leaf node's children is requested.


nextline

public static final java.lang.String nextline
nextline Line separator xmlversion XML version


xmlversion

public static final java.lang.String xmlversion
See Also:
Constant Field Values

allowsChildren

protected boolean allowsChildren
true if the node is able to have children


children

protected java.util.Vector children
array of children, may be null if this node has no children


parent

protected JENode parent
this node's parent, or null if this node has no parent


userObject

protected transient java.lang.Object userObject
optional user object

Constructor Detail

JENode

public JENode()
Creates a tree node that has no parent and no children, but which allows children.


JENode

public JENode(java.lang.Object userObject)
Creates a tree node with no parent, no children, but which allows children, and initializes it with the specified user object.

Parameters:
userObject - an Object provided by the user that constitutes the node's data

JENode

public JENode(java.lang.Object userObject,
              boolean allowsChildren)
Creates a tree node with no parent, no children, initialized with the specified user object, and that allows children only if specified.

Parameters:
userObject - an Object provided by the user that constitutes the node's data
allowsChildren - if true, the node is allowed to have child nodes -- otherwise, it is always a leaf node

JENode

public JENode(java.lang.String tagname)
Creates a tree node that has an XML tag


JENode

public JENode(java.lang.String tagname,
              java.util.HashMap hm)
Creates a tree node that has an XML tag and attributes


JENode

public JENode(java.lang.String tagname,
              java.lang.String data)
Creates a tree node that has an XML tag and data


JENode

public JENode(java.lang.String tagname,
              java.lang.String data,
              java.util.HashMap hm)
Creates a tree node that has an XML tag with attributes and data


JENode

public JENode(java.lang.String tagname,
              java.lang.String data,
              int typ)
Creates a tree node that has an XML tag and data as CDATA or COMMENT


JENode

public JENode(java.lang.String tagname,
              java.lang.String data,
              int typ,
              java.util.HashMap hm)
Creates a tree node that has an XML tag with attributes and data

Method Detail

main

public static void main(java.lang.String[] args)

clone

public java.lang.Object clone()
Creates a new JENode out of an existing JENode.

Overrides:
clone in class java.lang.Object
Parameters:
jen -
Returns:

add

public void add(JENode newChild)
Removes newChild from its parent and makes it a child of this node by adding it to the end of this node's child array.

Parameters:
newChild - node to add as a child of this node
Throws:
java.lang.IllegalArgumentException - if newChild is null
java.lang.IllegalStateException - if this node does not allow children
See Also:
insert(de.cbse.jeasy.JENode, int)

children

public java.util.Enumeration children()
Creates and returns a forward-order enumeration of this node's children. Modifying this node's child array invalidates any child enumerations created before the modification.

Specified by:
children in interface javax.swing.tree.TreeNode
Returns:
an Enumeration of this node's children

getAllowsChildren

public boolean getAllowsChildren()
Specified by:
getAllowsChildren in interface javax.swing.tree.TreeNode
Returns:

getChildAt

public javax.swing.tree.TreeNode getChildAt(int index)
Returns the child at the specified index in this node's child array.

Specified by:
getChildAt in interface javax.swing.tree.TreeNode
Parameters:
index - an index into this node's child array
Returns:
the TreeNode in this node's child array at the specified index
Throws:
java.lang.ArrayIndexOutOfBoundsException - if index is out of bounds

getChildCount

public int getChildCount()
Returns the number of children of this node.

Specified by:
getChildCount in interface javax.swing.tree.TreeNode
Returns:
an int giving the number of children of this node

getElement

public JEElement getElement()
Returns this node's user object JEElement.

Returns:
the JEElement stored at this node
See Also:
#setUserObject, toString()

getElement

public JEElement getElement(java.lang.String tagName)
Gets the JEElement with the given tagName inside the given JENode. The tagName may be a path like /bookstore/book/author. If no leading "/" is used the first occurence of the path in all subelemenst is given back

Parameters:
jn -
tagName -
Returns:
the text of the element

getElementData

public java.lang.String getElementData(java.lang.String tagName)
Gets the text of the JEElement with the given tagName inside the given JENode. The tagName may be a path like /bookstore/book/author. If no leading "/" is used the first occurence of the path in all subelements is given back

Parameters:
jn -
tagName -
Returns:
the text of the element

getElementVector

public java.util.Vector getElementVector(java.lang.String tagName)
Gets a Vector of all Sub-Element data with the given tagName

Parameters:
tagName -
Returns:
Vector of String

getIndex

public int getIndex(javax.swing.tree.TreeNode aChild)
Returns the index of the specified child in this node's child array. If the specified node is not a child of this node, returns -1. This method performs a linear search and is O(n) where n is the number of children.

Specified by:
getIndex in interface javax.swing.tree.TreeNode
Parameters:
aChild - the TreeNode to search for among this node's children
Returns:
an int giving the index of the node in this node's child array, or -1 if the specified node is a not a child of this node
Throws:
java.lang.IllegalArgumentException - if aChild is null

getNode

public JENode getNode(java.lang.String tagName)
Gets the JENode with the given tagName inside the given JENode. The tagName may be a path like /bookstore/book/author. If no leading "/" is used the first occurence of the path in all subelemenst is given back

Parameters:
jn -
tagName -
Returns:

getNodeByTag

public JENode getNodeByTag(java.lang.String element,
                           java.lang.String subelement,
                           java.lang.String subelementText)
Gets a node with name equals element and its subelement and its value is subelementText

Parameters:
element -
subelement -
subelementText -
Returns:

getNode

public JENode getNode(java.lang.String child,
                      java.lang.String subChild)
Looks for a childnode with the name child and there for the child equals subChild

Parameters:
child -
subChild -
Returns:

getNodeName

public java.lang.String getNodeName()
Gets the name of the JENoden, which is the name of the JEElement

Returns:
the name of the node

getNodes

public java.util.Vector getNodes(java.lang.String tagName)
Gets a Vector of all children JENodes with the given tagName

Parameters:
tagName -
Returns:
Vector of JENodes

getAllNodes

public java.util.Vector getAllNodes(java.lang.String tagName)
Gets a Vector of all JENodes with the given tagName

Parameters:
tagName -
Returns:
Vector of JENodes

getAllNodesWithAttribute

public java.util.Vector getAllNodesWithAttribute(java.lang.String attributeName,
                                                 java.lang.String attributeValue)
Gets a Vector of all JENodes with the given Attribute

Parameters:
v -
attributeName -
attributeValue -
Returns:

getParent

public javax.swing.tree.TreeNode getParent()
Returns this node's parent or null if this node has no parent.

Specified by:
getParent in interface javax.swing.tree.TreeNode
Returns:
this node's parent TreeNode, or null if this node has no parent

getUserObject

public java.lang.Object getUserObject()
Returns this node's user object.

Returns:
the Object stored at this node by the user
See Also:
#setUserObject, toString()

insert

public void insert(JENode newChild,
                   int childIndex)
Removes newChild from its present parent (if it has a parent), sets the child's parent to this node, and then adds the child to this node's child array at index childIndex. newChild must not be null and must not be an ancestor of this node.

Parameters:
newChild - the MutableTreeNode to insert under this node
childIndex - the index in this node's child array where this node is to be inserted
Throws:
java.lang.ArrayIndexOutOfBoundsException - if childIndex is out of bounds
java.lang.IllegalArgumentException - if newChild is null or is an ancestor of this node
java.lang.IllegalStateException - if this node does not allow children
See Also:
#isNodeDescendant

isLeaf

public boolean isLeaf()
Returns true if this node has no children. To distinguish between nodes that have no children and nodes that cannot have children (e.g. to distinguish files from empty directories), use this method in conjunction with getAllowsChildren

Specified by:
isLeaf in interface javax.swing.tree.TreeNode
Returns:
true if this node has no children
See Also:
getAllowsChildren()

isNodeAncestor

public boolean isNodeAncestor(javax.swing.tree.TreeNode anotherNode)
Returns true if anotherNode is an ancestor of this node -- if it is this node, this node's parent, or an ancestor of this node's parent. (Note that a node is considered an ancestor of itself.) If anotherNode is null, this method returns false. This operation is at worst O(h) where h is the distance from the root to this node.

Parameters:
anotherNode - node to test as an ancestor of this node
Returns:
true if this node is a descendant of anotherNode
See Also:
#isNodeDescendant, #getSharedAncestor

isNodeChild

public boolean isNodeChild(javax.swing.tree.TreeNode aNode)
Returns true if aNode is a child of this node. If aNode is null, this method returns false.

Returns:
true if aNode is a child of this node; false if aNode is null

remove

public void remove(int childIndex)
Removes the child at the specified index from this node's children and sets that node's parent to null. The child node to remove must be a MutableTreeNode.

Parameters:
childIndex - the index in this node's child array of the child to remove
Throws:
java.lang.ArrayIndexOutOfBoundsException - if childIndex is out of bounds

remove

public void remove(JENode aChild)
Removes aChild from this node's child array, giving it a null parent.

Parameters:
aChild - a child of this node to remove
Throws:
java.lang.IllegalArgumentException - if aChild is null or is not a child of this node

setElement

public void setElement(JEElement userObject)
Sets the user object for this node to userObject.

Parameters:
userObject - the JEElement that constitutes this node's user-specified data
See Also:
getUserObject(), toString()

setParent

public void setParent(JENode newParent)
Sets this node's parent to newParent but does not change the parent's child array. This method is called from insert() and remove() to reassign a child's parent, it should not be messaged from anywhere else.

Parameters:
newParent - this node's new parent

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

recToString

protected void recToString(java.lang.StringBuffer sb,
                           JENode jen,
                           int depth)