th.co.edge.jseq.util
Class XMLUtil

java.lang.Object
  extended by th.co.edge.jseq.util.XMLUtil

public class XMLUtil
extends java.lang.Object

A utility class to work with org.w3c.dom.Node objects.


Nested Class Summary
static class XMLUtil.NodePrinter
          An implementation of Visitor that creates a string representation of a Node and its children, or in other words, to a (part of) an XML document.
static interface XMLUtil.Visitor
          The Visitor interface is used when traversing XML documents.
 
Method Summary
static org.w3c.dom.Node find(org.w3c.dom.Node node, XMLUtil.Visitor visitor)
          Searches the given Node and its children depth-first, looking for the first Node that satisfies the given Visitor.
static org.w3c.dom.Node[] findAllTags(org.w3c.dom.Node node, java.lang.String tag)
          Searches a Node and its children depth-first, returning an array with all element nodes with a given name.
static org.w3c.dom.Node[] findAllTags(org.w3c.dom.Node node, java.lang.String tag, java.lang.String text)
          Searches a Node and its children depth-first, returning an array with all element nodes with a given name, and also containing a text sub-node with a given text.
static org.w3c.dom.Node findTag(org.w3c.dom.Node node, java.lang.String tag)
          Searches a Node and its children depth-first for an element node with a given name.
static org.w3c.dom.Node findTag(org.w3c.dom.Node node, java.lang.String tag, java.lang.String text)
          Searches a Node and its children depth-first for an element node with a given name, and also containing a text sub-node with a given text.
static org.w3c.dom.Node getAttribute(org.w3c.dom.Node node, java.lang.String attributeName)
          Returns the named attribute in a Node.
static java.lang.String getAttributeText(org.w3c.dom.Node node, java.lang.String attributeName)
          Returns the string value of a named attribute in a Node.
static java.lang.String getText(org.w3c.dom.Node node)
          Returns the first text sub-node of the given Node.
static java.lang.String getText(org.w3c.dom.Node node, int nth)
          Returns the nth text sub-node of the given Node.
static java.lang.String makeXMLSafe(java.lang.String original)
          Returns a copy of a string where all special XML characters have been replaced by the corresponding character entity reference, for example, "<" is replaced by "<".
static java.lang.String toString(org.w3c.dom.Node node)
          Returns a string representation of a Node and its children, pretty-printed so as to include new-lines and with each sub-node indented.
static java.lang.String toString(org.w3c.dom.Node node, boolean prettyPrint)
          Returns a string representation of a Node and its children.
static void traverse(org.w3c.dom.Node node, XMLUtil.Visitor visitor)
          Traverses the given Node and its children depth-first, calling the given Visitor for each node.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

traverse

public static void traverse(org.w3c.dom.Node node,
                            XMLUtil.Visitor visitor)
Traverses the given Node and its children depth-first, calling the given Visitor for each node.

If the traversal should be stopped at a certain node, the Visitor should return true from the visit method for that node. Obviously, if the entire tree should be traversed, the visit method should always return false.

Parameters:
node - the root Node at which to start the traversal
visitor - the Visitor
See Also:
XMLUtil.Visitor.visit(Node)

find

public static org.w3c.dom.Node find(org.w3c.dom.Node node,
                                    XMLUtil.Visitor visitor)
Searches the given Node and its children depth-first, looking for the first Node that satisfies the given Visitor.

Parameters:
node - the root Node at which to start the search
visitor - the Visitor that is called for every node, and determines if the right node has been found by returning true from the visit method
Returns:
the first Node in a depth-first search that satisfies visitor, or null if there is none.
See Also:
XMLUtil.Visitor.visit(Node)

findTag

public static org.w3c.dom.Node findTag(org.w3c.dom.Node node,
                                       java.lang.String tag)
Searches a Node and its children depth-first for an element node with a given name.

Parameters:
node - the root Node at which to start the search
tag - the name of the tag to search for
Returns:
the Node found, or null

findTag

public static org.w3c.dom.Node findTag(org.w3c.dom.Node node,
                                       java.lang.String tag,
                                       java.lang.String text)
Searches a Node and its children depth-first for an element node with a given name, and also containing a text sub-node with a given text.

Parameters:
node - the root Node at which to start the search
tag - the name of the tag to search for
text - a string that must be included in the first text sub-node of the found node, or null if only the tag name should be used to determine if a node matches
Returns:
the Node found, or null

findAllTags

public static org.w3c.dom.Node[] findAllTags(org.w3c.dom.Node node,
                                             java.lang.String tag)
Searches a Node and its children depth-first, returning an array with all element nodes with a given name.

Parameters:
node - the root Node at which to start the search
tag - the name of the tags to search for
Returns:
a Node array with all element nodes with the given name starting from node. If no such nodes exist, an empty array is returned

findAllTags

public static org.w3c.dom.Node[] findAllTags(org.w3c.dom.Node node,
                                             java.lang.String tag,
                                             java.lang.String text)
Searches a Node and its children depth-first, returning an array with all element nodes with a given name, and also containing a text sub-node with a given text.

Parameters:
node - the root Node at which to start the search
tag - the name of the tags to search for
text - a string that must be included in the first text sub-node of the found node, or null if only the tag name should be used to determine if a node matches
Returns:
a Node array with all element nodes with the given name and containing a text node with text, starting from node. If no such nodes exist, an empty array is returned

getText

public static java.lang.String getText(org.w3c.dom.Node node)
Returns the first text sub-node of the given Node.

Parameters:
node - the Node whose children to search for the first text node
Returns:
the first text sub-node of node, or null if there is none

getText

public static java.lang.String getText(org.w3c.dom.Node node,
                                       int nth)
Returns the nth text sub-node of the given Node.

Parameters:
node - the Node whose children to search for the nth text node
nth - the index of the text childe node to return
Returns:
the nth text sub-node of node, or null if node has fewer than nth text nodes as children

getAttribute

public static org.w3c.dom.Node getAttribute(org.w3c.dom.Node node,
                                            java.lang.String attributeName)
Returns the named attribute in a Node.

Parameters:
node - the Node in which to look for attributes
attributeName - the name of the attribute to look up
Returns:
the attribute node for the attribute named attributeName in Node, or null if there is no attribute with that name

getAttributeText

public static java.lang.String getAttributeText(org.w3c.dom.Node node,
                                                java.lang.String attributeName)
Returns the string value of a named attribute in a Node.

Parameters:
node - the Node in which to look for attributes
attributeName - the name of the attribute to look up
Returns:
the string value of the attribute named attributeName in Node, or null if there is no attribute with that name

toString

public static java.lang.String toString(org.w3c.dom.Node node)
Returns a string representation of a Node and its children, pretty-printed so as to include new-lines and with each sub-node indented.

Parameters:
node - the Node to represent as a string
Returns:
a pretty-printed string representation of Node

toString

public static java.lang.String toString(org.w3c.dom.Node node,
                                        boolean prettyPrint)
Returns a string representation of a Node and its children.

Parameters:
node - the Node to represent as a string
prettyPrint - if true, the string representation of Node will be pretty-printed, so as to include new-lines and with each sub-node indented
Returns:
a string representation of Node

makeXMLSafe

public static java.lang.String makeXMLSafe(java.lang.String original)
Returns a copy of a string where all special XML characters have been replaced by the corresponding character entity reference, for example, "<" is replaced by "<".

Parameters:
original - the string to make safe to use in an XML document
Returns:
a copy of original with all special XML characters replaced by the corresponding character entity reference


Copyright © 2008 Edge Software, Co., Ltd.. All Rights Reserved.