gov.llnl.babel.xml

Class 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.

Method Summary

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

Method Details

checkXML

public static Document checkXML(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.

cloneDOM

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

createDocBuilder

public static DocumentBuilder createDocBuilder()

decodeXMLString

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

encodeXMLString

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

formatChildren

public static String formatChildren(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.

getXMLString

public static String getXMLString(Node node)
Return a string representation of the XML DOM document.

isWhitespace

public static boolean isWhitespace(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.

lookupElement

public static Element lookupElement(Element e,
                                    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.

parse

public static Document parse(InputSource is,
                             EntityResolver er)
            throws IOException,
                   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.

parse

public static Document parse(String uri)
            throws IOException,
                   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.

trimWhiteSpaceLines

public static String trimWhiteSpaceLines(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.

validateXML

public static Document validateXML(String dtdPublic,
                                   String dtdFile,
                                   EntityResolver er,
                                   String start,
                                   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.

writeDOM

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