com.sibvisions.util.xml
Class XmlNode

java.lang.Object
  extended by com.sibvisions.util.xml.XmlNode
All Implemented Interfaces:
Cloneable

public final class XmlNode
extends Object
implements Cloneable

The XmlNode encapsulates the information of a xml tag. A node can be a comment, tag or xml. The xml type is the first node of a xml file. The xml node contains all other sub xml nodes.


Field Summary
static String NAME_COMMENT
          Constant value for the name of comment nodes.
static String NAME_DOCTYPE
          Constant value the name of the doctype node (<?
static String NAME_TEXTPART
          Constant value for the name of comment nodes.
static String NAME_XMLDECLARATION
          Constant value the name of the declaration node (<?
static short TYPE_ATTRIBUTE
          Constant value for the type of attributes.
static short TYPE_COMMENT
          Constant value for the type of comment nodes.
static short TYPE_DECLARATION
          Constant value for the type of declaration elements.
static short TYPE_DOCTYPE
          Constant value for the doctype.
static short TYPE_TEXT
          Constant value for the type of standard nodes.
static short TYPE_TEXTPART
          Constant value for the type of standard nodes.
 
Constructor Summary
XmlNode(short pType, String pName)
          Creates a new instance of XmlNode without a parent.
XmlNode(short pType, String pName, String pValue)
          Creates a new instance of XmlNode without a parent.
XmlNode(short pType, String pName, String pValue, XmlNode pParentNode)
          Creates a new instance of XmlNode.
XmlNode(short pType, String pName, XmlNode pParentNode)
          Creates a new instance of XmlNode with a parent.
XmlNode(String pName)
          Creates a new instance of XmlNode without a parent.
 
Method Summary
 void add(XmlNode xmnSub)
          Adds a XmlNode as a known sub node, at the end of the known sub nodes list.
 void clearSubNodes()
          Clears the list of known sub elements.
 Object clone()
          
static XmlNode createDoctype(String pName, String pPublicId, String pSystemId)
          Creates the xml doctype tag <!
 void createXml(OutputStream pOut, int pIndent, boolean pInsertNewLines)
          Create the xml structure based on the current node.
static XmlNode createXmlDeclaration()
          Creates the xml declaration tag <?
 int getColumnNumber()
          Gets the column number behind the start-tag tag.
 int getLineNumber()
          Gets the line number of the tag occurence.
 String getName()
          Gets the node name.
 XmlNode getNode(String pNodePath)
          Gets a specific (sub) XmlNode based on the current XmlNode.
 int getNodeCount(String pNodePath)
          Count the elements of a node list.
 List<XmlNode> getNodes(String pNodePath)
          Gets a list of XmlNodes based on the current XmlNode.
 String getNodeValue(String pNodePath)
          Gets the value of a specific (sub) XmlNode based on the current XmlNode.
 XmlNode getParent()
          Gets the parent XmlNode.
 List<XmlNode> getSubNodes()
          Gets the list of sub XmlNode elements.
 short getType()
          Gets the node type.
 String getValue()
          Gets the node value.
static String getXmlValue(String pValue)
          Returns the given value as XML value.
 int indexOf(String pNodePath, String pValue)
          Returns the index of the first occurrence of the specified node with the given value, or -1 if this node does not contain the specified node with the given value.
 int indexOf(String pNodePath, String pValue, int pStartPos)
          Returns the index of the first occurrence of the specified node with the given value, or -1 if this node does not contain the specified node with the given value.
 void insert(int iPosition, XmlNode xmnSub)
          Inserts a XmlNode as a known sub node at a defined position, in the sub node list.
 XmlNode insertNode(String pNodePath, Object pValue)
          Inserts a specific XmlNode at a defined position based on the current XmlNode.
 boolean remove(XmlNode xmnSub)
          Remove a sub XmlNode from the known sub nodes.
 XmlNode removeNode(String pNodePath)
          Removes a specific (sub) XmlNode based on the current XmlNode.
 XmlNode setNode(String pNodePath, Object pValue)
          Sets a specific (sub) XmlNode based on the current XmlNode.
 void setValue(String pValue)
          Sets the node value.
 int size()
          Returns the number of sub elements.
 int size(short pType)
          Returns the number of sub elements of the defined type.
 String toString()
          
 String toString(boolean pInsertNewLines)
          Returns the string representation of this node.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

TYPE_DECLARATION

public static final short TYPE_DECLARATION
Constant value for the type of declaration elements.

See Also:
Constant Field Values

TYPE_DOCTYPE

public static final short TYPE_DOCTYPE
Constant value for the doctype.

See Also:
Constant Field Values

TYPE_COMMENT

public static final short TYPE_COMMENT
Constant value for the type of comment nodes.

See Also:
Constant Field Values

TYPE_TEXT

public static final short TYPE_TEXT
Constant value for the type of standard nodes.

See Also:
Constant Field Values

TYPE_ATTRIBUTE

public static final short TYPE_ATTRIBUTE
Constant value for the type of attributes.

See Also:
Constant Field Values

TYPE_TEXTPART

public static final short TYPE_TEXTPART
Constant value for the type of standard nodes.

See Also:
Constant Field Values

NAME_COMMENT

public static final String NAME_COMMENT
Constant value for the name of comment nodes.

See Also:
Constant Field Values

NAME_TEXTPART

public static final String NAME_TEXTPART
Constant value for the name of comment nodes.

See Also:
Constant Field Values

NAME_XMLDECLARATION

public static final String NAME_XMLDECLARATION
Constant value the name of the declaration node (<?xml?>).

See Also:
Constant Field Values

NAME_DOCTYPE

public static final String NAME_DOCTYPE
Constant value the name of the doctype node (<?xml?>).

See Also:
Constant Field Values
Constructor Detail

XmlNode

public XmlNode(String pName)
Creates a new instance of XmlNode without a parent. The node type is TYPE_TEXT.

Parameters:
pName - name of the node

XmlNode

public XmlNode(short pType,
               String pName)
Creates a new instance of XmlNode without a parent.

Parameters:
pType - node type
pName - name of the node

XmlNode

public XmlNode(short pType,
               String pName,
               String pValue)
Creates a new instance of XmlNode without a parent.

Parameters:
pType - node type
pValue - value of the node
pName - name of the node

XmlNode

public XmlNode(short pType,
               String pName,
               XmlNode pParentNode)
Creates a new instance of XmlNode with a parent.

Parameters:
pType - node type
pName - name of the node
pParentNode - parent XmlNode or null

XmlNode

public XmlNode(short pType,
               String pName,
               String pValue,
               XmlNode pParentNode)
Creates a new instance of XmlNode.

Parameters:
pType - node type
pName - name of the node
pValue - value of the node
pParentNode - parent XmlNode or null
Method Detail

toString

public String toString()

Overrides:
toString in class Object

clone

public Object clone()
             throws CloneNotSupportedException

Overrides:
clone in class Object
Throws:
CloneNotSupportedException

add

public void add(XmlNode xmnSub)
Adds a XmlNode as a known sub node, at the end of the known sub nodes list.

Parameters:
xmnSub - instance of the sub node

insert

public void insert(int iPosition,
                   XmlNode xmnSub)
Inserts a XmlNode as a known sub node at a defined position, in the sub node list.

Parameters:
iPosition - position at which the specified element is to be inserted
xmnSub - instance of the sub node

getSubNodes

public List<XmlNode> getSubNodes()
Gets the list of sub XmlNode elements.

Returns:
list of sub elements

remove

public boolean remove(XmlNode xmnSub)
Remove a sub XmlNode from the known sub nodes.

Parameters:
xmnSub - instance of the sub node
Returns:
true if the node was removed otherwise false

clearSubNodes

public void clearSubNodes()
Clears the list of known sub elements.


getParent

public XmlNode getParent()
Gets the parent XmlNode.

Returns:
parent XmlNode or null if the node has no parent

getType

public short getType()
Gets the node type.

Returns:
type of the node (TYPE_ATTRIBUTE, TYPE_COMMENT, TYPE_TEXT, TYPE_DECLARATION), TYPE_DOCTYPE)

getName

public String getName()
Gets the node name.

Returns:
the node name

setValue

public void setValue(String pValue)
Sets the node value.

Parameters:
pValue - node value

getValue

public String getValue()
Gets the node value.

Returns:
node value

getXmlValue

public static String getXmlValue(String pValue)
Returns the given value as XML value.

Parameters:
pValue - the value
Returns:
the value as XML value

getNode

public XmlNode getNode(String pNodePath)
Gets a specific (sub) XmlNode based on the current XmlNode. To get a special element of a node list, use brackets to define the element position e.g. /document/nodes(2)

Parameters:
pNodePath - Node path e.g. /document/singlenode
Returns:
null if the XmlNode is not present or the position is invalid, otherwise the XmlNode
Throws:
IllegalArgumentException - if the path contains an incomplete or an invalid index
IndexOutOfBoundsException - if the path contains an index which is smaller or larger than the allowed length

getNodeValue

public String getNodeValue(String pNodePath)
Gets the value of a specific (sub) XmlNode based on the current XmlNode. To get a special element of a node list, use brackets to define the element position e.g. /document/nodes(2)

Parameters:
pNodePath - Node path e.g. /document/singlenode
Returns:
null if the XmlNode is not present or the position is invalid, otherwise the value of the XmlNode
Throws:
IllegalArgumentException - if the path contains an incomplete or an invalid index
IndexOutOfBoundsException - if the path contains an index which is smaller or larger than the allowed length

getNodes

public List<XmlNode> getNodes(String pNodePath)
Gets a list of XmlNodes based on the current XmlNode. If you define a special list element, you will get a list with only one element instead of all list elements.

Parameters:
pNodePath - Node path e.g. /document/nodelist or null to get all sub nodes
Returns:
null if the XmlNode is not present or the position is invalid, otherwise the list with XmlNodes
Throws:
IllegalArgumentException - if the path contains an incomplete or an invalid index
IndexOutOfBoundsException - if the path contains an index which is smaller or larger than the allowed length
See Also:
getSubNodes()

getNodeCount

public int getNodeCount(String pNodePath)
Count the elements of a node list.

Parameters:
pNodePath - Node path e.g. /document/nodelist
Returns:
0 if the list is empty or the element does not exists, otherwise the element count
Throws:
IllegalArgumentException - if the path contains an incomplete or an invalid index
IndexOutOfBoundsException - if the path contains an index which is smaller or larger than the allowed length

size

public int size()
Returns the number of sub elements. The size() includes all element types (comment, attribute, ...).

Returns:
the number of sub elements

size

public int size(short pType)
Returns the number of sub elements of the defined type.

Parameters:
pType - type of sub elements to count
Returns:
the number of sub elements

removeNode

public XmlNode removeNode(String pNodePath)
Removes a specific (sub) XmlNode based on the current XmlNode.

Parameters:
pNodePath - Node path e.g. /document/singlenode
Returns:
null if the XmlNode is not present or the position is invalid, otherwise the removed XmlNode
Throws:
IllegalArgumentException - if the path contains an incomplete or an invalid index
IndexOutOfBoundsException - if the path contains an index which is smaller or larger than the allowed length
See Also:
getNode(String)

setNode

public XmlNode setNode(String pNodePath,
                       Object pValue)
Sets a specific (sub) XmlNode based on the current XmlNode. If the hierarchy is not present, this method will create the necessary elements.

Parameters:
pNodePath - Node path e.g. /document/singlenode
pValue - the pValue can be an instance of XmlNode or any other object. If the pValue is not an instance of XmlNode then the node value will be set to pValue.toString(). Otherwise, if pValue is an instance of XmlNode, the pValue will be used as replacement for the specified node (the name of the node will be used instead of the name defined in the path). The node value will be set to null if pValue == null.
Returns:
null if the XmlNode is not present or the position is invalid, otherwise the new XmlNode
Throws:
IllegalArgumentException - if the path contains an incomplete or an invalid index
IndexOutOfBoundsException - if the path contains an index which is smaller or larger than the allowed length

insertNode

public XmlNode insertNode(String pNodePath,
                          Object pValue)
Inserts a specific XmlNode at a defined position based on the current XmlNode. If the hierarchy is not present, this method will create the necessary elements.

Parameters:
pNodePath - Node path with or without position e.g. /document/singlenode(2). The pValue will be inserted before the found node or after the last occurence of the pNodePath, if the position is size() + 1 or not defined.
pValue - the value can be an instance of XmlNode or any other object. If pValue is not an instance of XmlNode, the node value will be pValue.toString(). If it is an instance of XmlNode then the pValue will be used as node (the name of the node will be used instead of the name defined in the path). The node value will be set to null if pValue == null.
Returns:
null if the position, if needed, is invalid or pValue is null, otherwise the inserted XmlNode
Throws:
IllegalArgumentException - if the path contains an incomplete or an invalid index
IndexOutOfBoundsException - if the path contains an index which is smaller or larger than the allowed length

createXml

public void createXml(OutputStream pOut,
                      int pIndent,
                      boolean pInsertNewLines)
               throws IOException
Create the xml structure based on the current node.

Parameters:
pOut - stream for writing the xml structure
pIndent - indentation for sub nodes
pInsertNewLines - true to insert new lines
Throws:
IOException - if the output operation to pOut fails or the encoding UTF-8 is not usable

createXmlDeclaration

public static XmlNode createXmlDeclaration()
Creates the xml declaration tag <?xml version=\"1.0\" encoding=\"utf-8\">.

Returns:
xml declaration node

createDoctype

public static XmlNode createDoctype(String pName,
                                    String pPublicId,
                                    String pSystemId)
Creates the xml doctype tag <!DOCTYPE name PUBLIC \"publicId\" \"systemId\">.

Parameters:
pName - the doctype name
pPublicId - the public id
pSystemId - the system id
Returns:
xml doctype node

getLineNumber

public int getLineNumber()
Gets the line number of the tag occurence.

Returns:
the line number

getColumnNumber

public int getColumnNumber()
Gets the column number behind the start-tag tag.

Returns:
the column number

indexOf

public int indexOf(String pNodePath,
                   String pValue)
Returns the index of the first occurrence of the specified node with the given value, or -1 if this node does not contain the specified node with the given value.

Parameters:
pNodePath - the node path
pValue - the search value
Returns:
the position or -1 if the node was not found
Throws:
IndexOutOfBoundsException - if the start position is invalid

indexOf

public int indexOf(String pNodePath,
                   String pValue,
                   int pStartPos)
Returns the index of the first occurrence of the specified node with the given value, or -1 if this node does not contain the specified node with the given value.

Parameters:
pNodePath - the node path
pValue - the search value
pStartPos - the start position of the search
Returns:
the position or -1 if the node was not found
Throws:
IndexOutOfBoundsException - if the start position is invalid

toString

public String toString(boolean pInsertNewLines)
Returns the string representation of this node.

Parameters:
pInsertNewLines - true to insert new lines
Returns:
the string representation of this node


Copyright © 2009 SIB Visions GmbH. All Rights Reserved.