java.lang.Object
org.cybergarage.xml.Node

public class Nodeextends Object
Represents an XML node with name, value, attributes, and child nodes.

This class provides a tree-like structure for representing XML documents. Each node can have a name, value, attributes, and child nodes. Nodes can be created independently or as part of an XML document tree.

Key features:

  • Node name and value management
  • Attribute storage and retrieval
  • Parent-child node relationships
  • XML string output with proper escaping
  • User data attachment for application-specific information
  • Field Details

    • parentNode

      private Node parentNode
      /////////////////////////////////////////////
    • name

      private String name
      /////////////////////////////////////////////
    • value

      private String value
      /////////////////////////////////////////////
    • attrList

      private AttributeList attrList
      /////////////////////////////////////////////
    • nodeList

      private NodeList nodeList
      /////////////////////////////////////////////
    • userData

      private Object userData
      /////////////////////////////////////////////
  • Constructor Details

    • Node

      public Node()
      Creates a Node with empty UserData and no Parent Node.
    • Node

      public Node(String name)
      Creates a Node with specified name.
      Parameters:
      name - name to set for the node
    • Node

      public Node(String ns, String name)
      Creates a Node with namespace and name.
      Parameters:
      ns - the namespace prefix to prepend to name
      name - name to set for the node
    • Node

      public Node(Node otherNode)
      Creates a copy of an existing node.
      Parameters:
      otherNode - node to copy
  • Method Details

    • setParentNode

      public void setParentNode(Node node)
      Sets the parent node of this node.
      Parameters:
      node - the parent node, or null if this node has no parent
    • getParentNode

      public Node getParentNode()
      Gets the parent node of this node.
      Returns:
      the parent node, or null if this node has no parent
    • getRootNode

      public Node getRootNode()
      Gets the root node of the tree this node belongs to.
      Returns:
      the root node, or this node if it has no parent
    • setName

      public void setName(String name)
      Sets the name of this node.
      Parameters:
      name - name to set for the node
    • setName

      public void setName(String ns, String name)
      Sets the namespace and name of this node.
      Parameters:
      ns - the namespace prefix to prepend to name
      name - name to set for the node
    • getName

      public String getName()
      Gets the name of this node.
      Returns:
      name of the node
    • isName

      public boolean isName(String value)
      Checks if this node's name matches the specified value.
      Parameters:
      value - the name to compare against
      Returns:
      true if the names match, false otherwise
    • setValue

      public void setValue(String value)
      Sets the value of this node.
      Parameters:
      value - value to set for the node
    • setValue

      public void setValue(int value)
      Sets the value of this node as an integer.
      Parameters:
      value - integer value to set for the node
    • addValue

      public void addValue(String value)
      Adds a value to this node's existing value.
      Parameters:
      value - value to append to existing value
    • getValue

      public String getValue()
      Gets the value of this node.
      Returns:
      value of the node
    • getNAttributes

      public int getNAttributes()
      Gets the number of attributes this node has.
      Returns:
      the number of attributes
    • getAttribute

      public Attribute getAttribute(int index)
      Gets the attribute at the specified index.
      Parameters:
      index - the index of the attribute to retrieve
      Returns:
      the attribute at the specified index
    • getAttribute

      public Attribute getAttribute(String name)
      Gets the attribute with the specified name.
      Parameters:
      name - the name of the attribute to retrieve
      Returns:
      the attribute with the specified name, or null if not found
    • addAttribute

      public void addAttribute(Attribute attr)
      Adds an attribute to this node.
      Parameters:
      attr - the attribute to add
    • insertAttributeAt

      public void insertAttributeAt(Attribute attr, int index)
      Inserts an attribute at the specified index.
      Parameters:
      attr - the attribute to insert
      index - the index at which to insert the attribute
    • addAttribute

      public void addAttribute(String name, String value)
      Adds an attribute with the specified name and value.
      Parameters:
      name - the attribute name
      value - the attribute value
    • removeAttribute

      public boolean removeAttribute(Attribute attr)
      Removes the specified attribute from this node.
      Parameters:
      attr - the attribute to remove
      Returns:
      true if the attribute was removed, false if not found
    • removeAttribute

      public boolean removeAttribute(String name)
      Removes the attribute with the specified name from this node.
      Parameters:
      name - the name of the attribute to remove
      Returns:
      true if the attribute was removed, false if not found
    • removeAllAttributes

      public void removeAllAttributes()
      Removes all attributes from this node.
    • hasAttributes

      public boolean hasAttributes()
      Checks if this node has any attributes.
      Returns:
      true if this node has attributes, false otherwise
    • setAttribute

      public void setAttribute(String name, String value)
      Sets an attribute value. If the attribute already exists, its value is updated. If it doesn't exist, a new attribute is created.
      Parameters:
      name - the attribute name
      value - the attribute value
    • setAttribute

      public void setAttribute(String name, int value)
      Sets an attribute value as an integer. If the attribute already exists, its value is updated. If it doesn't exist, a new attribute is created.
      Parameters:
      name - the attribute name
      value - the integer attribute value
    • getAttributeValue

      public String getAttributeValue(String name)
      Gets the value of an attribute with the specified name.
      Parameters:
      name - the attribute name
      Returns:
      the attribute value, or empty string if not found
    • getAttributeIntegerValue

      public int getAttributeIntegerValue(String name)
      Gets the integer value of an attribute with the specified name.
      Parameters:
      name - the attribute name
      Returns:
      the attribute value as integer, or 0 if not found or invalid
    • setNameSpace

      public void setNameSpace(String ns, String value)
      Sets a namespace attribute for this node.
      Parameters:
      ns - the namespace prefix
      value - the namespace URI
    • set

      public boolean set(Node otherNode)
      Copies all properties from another node to this node. This includes name, value, attributes, and child nodes.
      Parameters:
      otherNode - the node to copy from
      Returns:
      true if the copy was successful, false if otherNode is null
    • equals

      public boolean equals(Object o)
      /////////////////////////////////////////////
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • getNNodes

      public int getNNodes()
      Gets the number of child nodes this node has.
      Returns:
      the number of child nodes
    • getNode

      public Node getNode(int index)
      Gets the child node at the specified index.
      Parameters:
      index - the index of the child node to retrieve
      Returns:
      the child node at the specified index
    • getNode

      public Node getNode(String name)
      Gets the first child node with the specified name.
      Parameters:
      name - the name of the child node to retrieve
      Returns:
      the child node with the specified name, or null if not found
    • getNodeEndsWith

      public Node getNodeEndsWith(String name)
      Gets the first child node whose name ends with the specified string.
      Parameters:
      name - the suffix to match
      Returns:
      the first child node with name ending with the specified string, or null if not found
    • addNode

      public void addNode(Node node)
      Adds a child node to this node.
      Parameters:
      node - child node to add
    • insertNode

      public void insertNode(Node node, int index)
      Inserts a child node at the specified index.
      Parameters:
      node - the child node to insert
      index - the index at which to insert the node
    • getIndex

      public int getIndex(String name)
      Gets the index of the first child node with the specified name.
      Parameters:
      name - the name of the child node to find
      Returns:
      the index of the child node, or -1 if not found
    • removeNode

      public boolean removeNode(Node node)
      Removes a child node from this node.
      Parameters:
      node - child node to remove
      Returns:
      true if node was removed, false if not found
    • removeNode

      public boolean removeNode(String name)
      Removes the first child node with the specified name.
      Parameters:
      name - the name of the child node to remove
      Returns:
      true if the node was removed, false if not found
    • removeAllNodes

      public void removeAllNodes()
      Removes all child nodes from this node.
    • hasNodes

      public boolean hasNodes()
      Checks if this node has any child nodes.
      Returns:
      true if this node has child nodes, false otherwise
    • hasNode

      public boolean hasNode(String name)
      Checks if this node has a child node with the specified name.
      Parameters:
      name - the name of the child node to check for
      Returns:
      true if a child node with the specified name exists, false otherwise
    • setNode

      public void setNode(String name)
      Ensures a child node with the specified name exists. If a child node with the name already exists, this method does nothing. If not, a new child node with the specified name is created and added.
      Parameters:
      name - the name of the child node to ensure exists
    • setNode

      public void setNode(String name, String value)
      Ensures a child node with the specified name exists and sets its value. If a child node with the name already exists, its value is updated. If not, a new child node with the specified name and value is created and added.
      Parameters:
      name - the name of the child node
      value - the value to set for the child node
    • getNodeValue

      public String getNodeValue(String name)
      Gets the value of the first child node with the specified name.
      Parameters:
      name - the name of the child node
      Returns:
      the value of the child node, or empty string if not found
    • setUserData

      public void setUserData(Object data)
      Sets user data for this node.
      Parameters:
      data - user data object to associate with this node
    • getUserData

      public Object getUserData()
      Gets the user data associated with this node.
      Returns:
      user data object, or null if not set
    • getIndentLevelString

      public String getIndentLevelString(int nIndentLevel)
      Invoke getIndentLevelString(int, String) with " " as String
      Parameters:
      nIndentLevel - how many times the three space indent is repeated
      Returns:
      the indent String, built from three spaces repeated nIndentLevel times
      See Also:
    • getIndentLevelString

      public String getIndentLevelString(int nIndentLevel, String space)
      Get the indent level string.
      Parameters:
      nIndentLevel - the level of indentation to produce
      space - the String to use for the intendation
      Returns:
      an indentation String
    • outputAttributes

      public void outputAttributes(PrintWriter ps)
      Outputs this node's attributes to the specified print writer.
      Parameters:
      ps - the print writer to output to
    • output

      public void output(PrintWriter ps, int indentLevel, boolean hasChildNode)
      Outputs this node as XML to the specified print writer with indentation.
      Parameters:
      ps - the print writer to output to
      indentLevel - the indentation level for this node
      hasChildNode - whether to treat this node as having child nodes
    • toString

      public String toString(String enc, boolean hasChildNode)
      Gets the XML string representation of this node with the specified encoding.
      Parameters:
      enc - the character encoding to use, or null for default
      hasChildNode - whether to treat this node as having child nodes
      Returns:
      the XML string representation
    • toString

      public String toString()
      Gets the XML string representation of this node.
      Overrides:
      toString in class Object
      Returns:
      XML string using UTF-8 encoding
    • toXMLString

      public String toXMLString(boolean hasChildNode)
      Gets the XML string representation with all XML characters escaped. This is useful for embedding XML within XML.
      Parameters:
      hasChildNode - whether to treat this node as having child nodes
      Returns:
      the escaped XML string
    • toXMLString

      public String toXMLString()
      Gets the XML string representation with all XML characters escaped. This is useful for embedding XML within XML.
      Returns:
      the escaped XML string
    • print

      public void print(boolean hasChildNode)
      Prints this node as XML to standard output.
      Parameters:
      hasChildNode - whether to treat this node as having child nodes
    • print

      public void print()
      Prints this node as XML to standard output.