org.opengts.util
Class XMLTools

java.lang.Object
  extended by org.opengts.util.XMLTools

public class XMLTools
extends java.lang.Object

A set of tools for simplifying the reading and validating of XML documents


Nested Class Summary
static class XMLTools.XMLErrorHandler
          XMLErrorHandler class
 
Constructor Summary
XMLTools()
           
 
Method Summary
static java.lang.String ATTR(java.lang.String key, boolean value)
          Create Attribute
static java.lang.String ATTR(java.lang.String key, int value)
          Create Attribute
static java.lang.String ATTR(java.lang.String key, long value)
          Create Attribute
static java.lang.String ATTR(java.lang.String key, java.lang.Object value)
          Create Attribute
static java.lang.String ATTR(java.lang.String key, java.lang.String value)
          Create Attribute
static java.lang.String CDATA(boolean isSoapReq, java.lang.String content)
          Wrap content in CDATA
static java.lang.String endTAG(boolean isSoapReq, java.lang.String tag, boolean newLine)
          End tag
static java.lang.String escapeXML(java.lang.String val)
          Escape the specified String, suitable for an XML attribute value
static java.lang.String getAttribute(org.w3c.dom.Element elem, java.lang.String key)
          Returns the value for the specified attribute key, or null if the key is not defined in the specified Element
static java.lang.String getAttribute(org.w3c.dom.Element elem, java.lang.String key, java.lang.String dft)
          Returns the value for the specified attribute key, or the default value if the key is not defined in the specified Element
static java.lang.String getAttribute(org.w3c.dom.Element elem, java.lang.String key, java.lang.String dft, boolean resolveRT)
          Returns the value for the specified attribute key, or the default value if the key is not defined in the specified Element
static boolean getAttributeBoolean(org.w3c.dom.Element elem, java.lang.String key, boolean dft)
          Returns the boolean value for the specified attribute key, or the default value if the key is not defined in the specified Element
static boolean getAttributeBoolean(org.w3c.dom.Element elem, java.lang.String key, boolean dft, boolean resolveRT)
          Returns the boolean value for the specified attribute key, or the default value if the key is not defined in the specified Element
static double getAttributeDouble(org.w3c.dom.Element elem, java.lang.String key, double dft)
          Returns the double value for the specified attribute key, or the default value if the key is not defined in the specified Element
static double getAttributeDouble(org.w3c.dom.Element elem, java.lang.String key, double dft, boolean resolveRT)
          Returns the double value for the specified attribute key, or the default value if the key is not defined in the specified Element
static int getAttributeInt(org.w3c.dom.Element elem, java.lang.String key, int dft)
          Returns the int value for the specified attribute key, or the default value if the key is not defined in the specified Element
static int getAttributeInt(org.w3c.dom.Element elem, java.lang.String key, int dft, boolean resolveRT)
          Returns the int value for the specified attribute key, or the default value if the key is not defined in the specified Element
static long getAttributeLong(org.w3c.dom.Element elem, java.lang.String key, long dft)
          Returns the long value for the specified attribute key, or the default value if the key is not defined in the specified Element
static long getAttributeLong(org.w3c.dom.Element elem, java.lang.String key, long dft, boolean resolveRT)
          Returns the long value for the specified attribute key, or the default value if the key is not defined in the specified Element
static org.w3c.dom.Element getChildElement(org.w3c.dom.Node root, java.lang.String name)
          Gets the first child element of the specified Node having the specified name
static org.w3c.dom.NodeList getChildElements(org.w3c.dom.Node root, java.lang.String name)
          Gets all child elements of the specified Node having the specified name
static org.w3c.dom.Document getDocument(byte[] xml)
          Load and parse a XML document from an array of bytes
static org.w3c.dom.Document getDocument(byte[] xml, boolean checkErrors)
          Load and parse a XML document from an array of bytes
static org.w3c.dom.Document getDocument(java.io.File xmlFile)
          Load and parses a XML document from a file
static org.w3c.dom.Document getDocument(java.io.File xmlFile, boolean checkErrors)
          Load and parses a XML document from a file
static org.w3c.dom.Document getDocument(java.io.InputStream input)
          Load and parse a XML document from an input stream
static org.w3c.dom.Document getDocument(java.io.InputStream input, boolean checkErrors)
          Load and parse a XML document from an input stream
static org.w3c.dom.Document getDocument(java.lang.String xml)
          Load and parse a XML document from a String
static org.w3c.dom.Document getDocument(java.lang.String xml, boolean checkErrors)
          Load and parse a XML document from a String
static java.lang.String getNodeText(org.w3c.dom.Node root)
          Parse text from node.
static java.lang.String getNodeText(org.w3c.dom.Node root, java.lang.String repNewline)
          Parse text from node.
static java.lang.String getNodeText(org.w3c.dom.Node root, java.lang.String repNewline, boolean resolveRT)
          Parse text from node.
static java.lang.String getNodeText(org.w3c.dom.Node root, java.lang.String repNewline, boolean resolveRT, java.lang.String dft)
          Parse text from node.
static org.w3c.dom.Element getPathElement(org.w3c.dom.Element root, java.lang.String nodes)
          Gets an element by a relative path from root
static org.w3c.dom.Element getPathElement(org.w3c.dom.Element root, java.util.StringTokenizer nodes)
          Gets an element by a relative path from root
static java.lang.String getPathText(org.w3c.dom.Element root, java.lang.String nodes)
          Gets gets the path of an element found at a relative path from root
static void main(java.lang.String[] argv)
          Main entry point for testing/debugging
static java.lang.String nodeToString(org.w3c.dom.Node n)
           
static java.lang.String[] parseLines(java.lang.String text)
          Parse String into an array terminated by CR, NL, or CRNL
static java.lang.String PREFIX(boolean isSoapReq, int indent)
          Create prefix spaces
static void printNodeTree(java.lang.String indent, org.w3c.dom.Node n)
           
static java.lang.String startTAG(boolean isSoapReq, java.lang.String tag, java.lang.String attr, boolean endTag, boolean newLine)
          Start tag
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLTools

public XMLTools()
Method Detail

escapeXML

public static java.lang.String escapeXML(java.lang.String val)
Escape the specified String, suitable for an XML attribute value


startTAG

public static java.lang.String startTAG(boolean isSoapReq,
                                        java.lang.String tag,
                                        java.lang.String attr,
                                        boolean endTag,
                                        boolean newLine)
Start tag


endTAG

public static java.lang.String endTAG(boolean isSoapReq,
                                      java.lang.String tag,
                                      boolean newLine)
End tag


CDATA

public static java.lang.String CDATA(boolean isSoapReq,
                                     java.lang.String content)
Wrap content in CDATA


PREFIX

public static java.lang.String PREFIX(boolean isSoapReq,
                                      int indent)
Create prefix spaces


ATTR

public static java.lang.String ATTR(java.lang.String key,
                                    java.lang.Object value)
Create Attribute


ATTR

public static java.lang.String ATTR(java.lang.String key,
                                    java.lang.String value)
Create Attribute


ATTR

public static java.lang.String ATTR(java.lang.String key,
                                    int value)
Create Attribute


ATTR

public static java.lang.String ATTR(java.lang.String key,
                                    long value)
Create Attribute


ATTR

public static java.lang.String ATTR(java.lang.String key,
                                    boolean value)
Create Attribute


getDocument

public static org.w3c.dom.Document getDocument(java.io.File xmlFile)
Load and parses a XML document from a file

Parameters:
xmlFile - The file to load the document from
Returns:
The pasrsed XML document

getDocument

public static org.w3c.dom.Document getDocument(java.io.File xmlFile,
                                               boolean checkErrors)
Load and parses a XML document from a file

Parameters:
xmlFile - The file to load the document from
checkErrors - True if the XML document should be validated against it's DTD
Returns:
The pasrsed XML document

getDocument

public static org.w3c.dom.Document getDocument(java.io.InputStream input)
Load and parse a XML document from an input stream

Parameters:
input - The input stream to read the document from
Returns:
The parsed XML document

getDocument

public static org.w3c.dom.Document getDocument(java.io.InputStream input,
                                               boolean checkErrors)
Load and parse a XML document from an input stream

Parameters:
input - The input stream to read the document from
checkErrors - True if the XML document should be validated against it's DTD
Returns:
The parsed XML document

getDocument

public static org.w3c.dom.Document getDocument(byte[] xml)
Load and parse a XML document from an array of bytes

Parameters:
xml - The byte array to read the document from
Returns:
The parsed XML document

getDocument

public static org.w3c.dom.Document getDocument(byte[] xml,
                                               boolean checkErrors)
Load and parse a XML document from an array of bytes

Parameters:
xml - The byte array to read the document from
checkErrors - True if the XML document should be validated against it's DTD
Returns:
The parsed XML document

getDocument

public static org.w3c.dom.Document getDocument(java.lang.String xml)
Load and parse a XML document from a String

Parameters:
xml - The String to read the document from
Returns:
The parsed XML document

getDocument

public static org.w3c.dom.Document getDocument(java.lang.String xml,
                                               boolean checkErrors)
Load and parse a XML document from a String

Parameters:
xml - The String to read the document from
checkErrors - True if the XML document should be validated against it's DTD
Returns:
The parsed XML document

getNodeText

public static java.lang.String getNodeText(org.w3c.dom.Node root)
Parse text from node. Combines and returns the values of all immediate child Text/CData Nodes of the given root Node. This method does not return null.

Parameters:
root - The node to get the text of
Returns:
The parsed text

getNodeText

public static java.lang.String getNodeText(org.w3c.dom.Node root,
                                           java.lang.String repNewline)
Parse text from node. Combines and returns the values of all immediate child Text/CData Nodes of the given root Node. This method does not return null.

Parameters:
root - The node to get the text of
repNewline - The string to replace any newlines with
Returns:
The parsed text

getNodeText

public static java.lang.String getNodeText(org.w3c.dom.Node root,
                                           java.lang.String repNewline,
                                           boolean resolveRT)
Parse text from node. Combines and returns the values of all immediate child Text/CData Nodes of the given root Node. This method does not return null.

Parameters:
root - The node to get the text of
repNewline - The string to replace any newlines with
resolveRT - If true, resolve any runtime config variables
Returns:
The parsed text

getNodeText

public static java.lang.String getNodeText(org.w3c.dom.Node root,
                                           java.lang.String repNewline,
                                           boolean resolveRT,
                                           java.lang.String dft)
Parse text from node. Combines and returns the values of all immediate child Text/CData Nodes of the given root Node. This method may return the specified default value, dft.

Parameters:
root - The node to get the text of
repNewline - The string to replace any newlines with
resolveRT - If true, resolve any runtime config variables
dft - The default value to return if no text was found
Returns:
The parsed text

parseLines

public static java.lang.String[] parseLines(java.lang.String text)
Parse String into an array terminated by CR, NL, or CRNL

Parameters:
text - The String text to parse
Returns:
An array of String lines

getAttribute

public static java.lang.String getAttribute(org.w3c.dom.Element elem,
                                            java.lang.String key)
Returns the value for the specified attribute key, or null if the key is not defined in the specified Element

Parameters:
elem - The Element from which the key attribute will be returned
key - The attribute key
Returns:
The value of the key, or null if the key is not defined.

getAttribute

public static java.lang.String getAttribute(org.w3c.dom.Element elem,
                                            java.lang.String key,
                                            java.lang.String dft)
Returns the value for the specified attribute key, or the default value if the key is not defined in the specified Element

Parameters:
elem - The Element from which the key attribute will be returned
key - The attribute key
dft - The default value to return if the key is not defined
Returns:
The value of the key, or the default value if the key is not defined.

getAttribute

public static java.lang.String getAttribute(org.w3c.dom.Element elem,
                                            java.lang.String key,
                                            java.lang.String dft,
                                            boolean resolveRT)
Returns the value for the specified attribute key, or the default value if the key is not defined in the specified Element

Parameters:
elem - The Element from which the key attribute will be returned
key - The attribute key
dft - The default value to return if the key is not defined
resolveRT - If true, resolve any runtime config variables
Returns:
The value of the key, or the default value if the key is not defined.

getAttributeBoolean

public static boolean getAttributeBoolean(org.w3c.dom.Element elem,
                                          java.lang.String key,
                                          boolean dft)
Returns the boolean value for the specified attribute key, or the default value if the key is not defined in the specified Element

Parameters:
elem - The Element from which the key attribute will be returned
key - The attribute key
dft - The default boolean value to return if the key is not defined
Returns:
The boolean value of the key, or the default value if the key is not defined.

getAttributeBoolean

public static boolean getAttributeBoolean(org.w3c.dom.Element elem,
                                          java.lang.String key,
                                          boolean dft,
                                          boolean resolveRT)
Returns the boolean value for the specified attribute key, or the default value if the key is not defined in the specified Element

Parameters:
elem - The Element from which the key attribute will be returned
key - The attribute key
dft - The default boolean value to return if the key is not defined
resolveRT - If true, resolve any runtime config variables
Returns:
The boolean value of the key, or the default value if the key is not defined.

getAttributeInt

public static int getAttributeInt(org.w3c.dom.Element elem,
                                  java.lang.String key,
                                  int dft)
Returns the int value for the specified attribute key, or the default value if the key is not defined in the specified Element

Parameters:
elem - The Element from which the key attribute will be returned
key - The attribute key
dft - The default int value to return if the key is not defined
Returns:
The int value of the key, or the default value if the key is not defined.

getAttributeInt

public static int getAttributeInt(org.w3c.dom.Element elem,
                                  java.lang.String key,
                                  int dft,
                                  boolean resolveRT)
Returns the int value for the specified attribute key, or the default value if the key is not defined in the specified Element

Parameters:
elem - The Element from which the key attribute will be returned
key - The attribute key
dft - The default int value to return if the key is not defined
resolveRT - If true, resolve any runtime config variables
Returns:
The int value of the key, or the default value if the key is not defined.

getAttributeLong

public static long getAttributeLong(org.w3c.dom.Element elem,
                                    java.lang.String key,
                                    long dft)
Returns the long value for the specified attribute key, or the default value if the key is not defined in the specified Element

Parameters:
elem - The Element from which the key attribute will be returned
key - The attribute key
dft - The default long value to return if the key is not defined
Returns:
The long value of the key, or the default value if the key is not defined.

getAttributeLong

public static long getAttributeLong(org.w3c.dom.Element elem,
                                    java.lang.String key,
                                    long dft,
                                    boolean resolveRT)
Returns the long value for the specified attribute key, or the default value if the key is not defined in the specified Element

Parameters:
elem - The Element from which the key attribute will be returned
key - The attribute key
dft - The default long value to return if the key is not defined
resolveRT - If true, resolve any runtime config variables
Returns:
The long value of the key, or the default value if the key is not defined.

getAttributeDouble

public static double getAttributeDouble(org.w3c.dom.Element elem,
                                        java.lang.String key,
                                        double dft)
Returns the double value for the specified attribute key, or the default value if the key is not defined in the specified Element

Parameters:
elem - The Element from which the key attribute will be returned
key - The attribute key
dft - The default double value to return if the key is not defined
Returns:
The double value of the key, or the default value if the key is not defined.

getAttributeDouble

public static double getAttributeDouble(org.w3c.dom.Element elem,
                                        java.lang.String key,
                                        double dft,
                                        boolean resolveRT)
Returns the double value for the specified attribute key, or the default value if the key is not defined in the specified Element

Parameters:
elem - The Element from which the key attribute will be returned
key - The attribute key
dft - The default double value to return if the key is not defined
resolveRT - If true, resolve any runtime config variables
Returns:
The double value of the key, or the default value if the key is not defined.

getChildElement

public static org.w3c.dom.Element getChildElement(org.w3c.dom.Node root,
                                                  java.lang.String name)
Gets the first child element of the specified Node having the specified name

Parameters:
root - The Node to look for the child element in
name - The name of the child element
Returns:
The child element

getChildElements

public static org.w3c.dom.NodeList getChildElements(org.w3c.dom.Node root,
                                                    java.lang.String name)
Gets all child elements of the specified Node having the specified name

Parameters:
root - The Node to look for the child element in
name - The name of the child element
Returns:
The child element array

getPathText

public static java.lang.String getPathText(org.w3c.dom.Element root,
                                           java.lang.String nodes)
Gets gets the path of an element found at a relative path from root

Parameters:
root - The root node
nodes - The path to the child element, delimited with '/'
Returns:
The destination child element, or null

getPathElement

public static org.w3c.dom.Element getPathElement(org.w3c.dom.Element root,
                                                 java.lang.String nodes)
Gets an element by a relative path from root

Parameters:
root - The root node
nodes - The path to the child element, delimited with '/'
Returns:
The destination child element, or null

getPathElement

public static org.w3c.dom.Element getPathElement(org.w3c.dom.Element root,
                                                 java.util.StringTokenizer nodes)
Gets an element by a relative path from root

Parameters:
root - The root node
nodes - The path to the child element, delimited with '/'
Returns:
The destination child element, or null

nodeToString

public static java.lang.String nodeToString(org.w3c.dom.Node n)

printNodeTree

public static void printNodeTree(java.lang.String indent,
                                 org.w3c.dom.Node n)

main

public static void main(java.lang.String[] argv)
Main entry point for testing/debugging

Parameters:
argv - Comand-line arguments