gov.llnl.babel.xml
Class XMLUtilities

java.lang.Object
  extended by gov.llnl.babel.xml.XMLUtilities

public class XMLUtilities
extends java.lang.Object

Utility class XMLUtiliites is a collection of common XML utility functions. All methods are declared as static. This class is the primary interface to the XML parsers. The current implementation uses the Xerces parsers from the Apache project.


Constructor Summary
XMLUtilities()
           
 
Method Summary
static org.w3c.dom.Document checkXML(java.lang.String frag)
          Check whether the XML fragment is well-formed.
static org.w3c.dom.Node cloneDOM(org.w3c.dom.Node node, org.w3c.dom.Document document)
          Recursively copy the DOM tree using the specified document as the root document factory.
static javax.xml.parsers.DocumentBuilder createDocBuilder()
           
static java.lang.String decodeXMLString(java.lang.String s)
          Transform &, < > " back into &,<,>,".
static java.lang.String encodeXMLString(java.lang.String s)
          Normalize an XML string by replacing less than, greater than, ampersand, and double quote by their XML entity representatives.
static java.lang.String formatChildren(org.w3c.dom.Node root)
          Return a string representation of the XML DOM document under the specified element root.
static java.lang.String getXMLString(org.w3c.dom.Node node)
          Return a string representation of the XML DOM document.
static boolean isWhitespace(java.lang.String s)
          Return whether the string contains only whitespace.
static org.w3c.dom.Element lookupElement(org.w3c.dom.Element e, java.lang.String name)
          Search the children of the specified parent element and return the first element that matches the specified element name.
static org.w3c.dom.Document parse(org.xml.sax.InputSource is, org.xml.sax.EntityResolver er)
          Parse and validate the XML document represented by the specified input source.
static org.w3c.dom.Document parse(java.lang.String uri)
          Parse and validate the XML document represented by the string URI.
static java.lang.String trimWhiteSpaceLines(java.lang.String s)
          Remove white space lines from the beginning and end of the string.
static org.w3c.dom.Document validateXML(java.lang.String dtdPublic, java.lang.String dtdFile, org.xml.sax.EntityResolver er, java.lang.String start, java.lang.String frag)
          Validate the provided XML fragment with the specified start element against the DTD public identifier.
static void writeDOM(org.w3c.dom.Node node, java.io.PrintWriter writer)
          Recursively write the XML DOM representation into a print writer output stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLUtilities

public XMLUtilities()
Method Detail

createDocBuilder

public static javax.xml.parsers.DocumentBuilder createDocBuilder()

parse

public static org.w3c.dom.Document parse(java.lang.String uri)
                                  throws java.io.IOException,
                                         org.xml.sax.SAXException
Parse and validate the XML document represented by the string URI. An IOException will be thrown if the supplied URI is not valid. A SAXException will be thrown if there was an error parsing the XML. The return value is the DOM representation of the parsed document.

Throws:
java.io.IOException
org.xml.sax.SAXException

parse

public static org.w3c.dom.Document parse(org.xml.sax.InputSource is,
                                         org.xml.sax.EntityResolver er)
                                  throws java.io.IOException,
                                         org.xml.sax.SAXException
Parse and validate the XML document represented by the specified input source. The entity resolver is used to redirect public and system identifiers. An IOException will be thrown if the input source is not valid and a SAXException will be thrown if there is an error parsing the XML. The return value is the DOM representation of the parsed document.

Throws:
java.io.IOException
org.xml.sax.SAXException

checkXML

public static org.w3c.dom.Document checkXML(java.lang.String frag)
Check whether the XML fragment is well-formed. No validation is done using a DTD. If no error is detected, then the DOM tree is returned. Null is returned if any error or warning is detected during the parse.


validateXML

public static org.w3c.dom.Document validateXML(java.lang.String dtdPublic,
                                               java.lang.String dtdFile,
                                               org.xml.sax.EntityResolver er,
                                               java.lang.String start,
                                               java.lang.String frag)
Validate the provided XML fragment with the specified start element against the DTD public identifier. If no error is detected, then the DOM tree is returned. Null is returned any error or warning is detected during the parsing and validation.


encodeXMLString

public static java.lang.String encodeXMLString(java.lang.String s)
Normalize an XML string by replacing less than, greater than, ampersand, and double quote by their XML entity representatives.


decodeXMLString

public static java.lang.String decodeXMLString(java.lang.String s)
Transform &, < > " back into &,<,>,".


lookupElement

public static org.w3c.dom.Element lookupElement(org.w3c.dom.Element e,
                                                java.lang.String name)
Search the children of the specified parent element and return the first element that matches the specified element name. If no such matching element can be found, then return null.


cloneDOM

public static org.w3c.dom.Node cloneDOM(org.w3c.dom.Node node,
                                        org.w3c.dom.Document document)
Recursively copy the DOM tree using the specified document as the root document factory. Only element and text nodes are supported.


writeDOM

public static void writeDOM(org.w3c.dom.Node node,
                            java.io.PrintWriter writer)
Recursively write the XML DOM representation into a print writer output stream. The DOM tree is output without any formatting.


getXMLString

public static java.lang.String getXMLString(org.w3c.dom.Node node)
Return a string representation of the XML DOM document.


formatChildren

public static java.lang.String formatChildren(org.w3c.dom.Node root)
Return a string representation of the XML DOM document under the specified element root. This string will have both starting and ending newlines removed. If the generated string consists of only whitespace, then a null string is returned. A common use of this method is to constuct the HTML text associated with a comment element.


trimWhiteSpaceLines

public static java.lang.String trimWhiteSpaceLines(java.lang.String s)
Remove white space lines from the beginning and end of the string. The returned string does not end with an end-of-line terminator. A null string is returned if the string contains only whitespace.


isWhitespace

public static boolean isWhitespace(java.lang.String s)
Return whether the string contains only whitespace. Whitespace is defined as any character whose encoding value is less than or equal to a space.