at.gv.egovernment.moa.util
Class DOMUtils

java.lang.Object
  |
  +--at.gv.egovernment.moa.util.DOMUtils

public class DOMUtils
extends Object

Various utility functions for handling XML DOM trees. The parsing methods in this class make use of some features internal to the Xerces DOM parser, mainly for performance reasons. As soon as JAXP (currently at version 1.2) is better at schema handling, it should be used as the parser interface.

Version:
$Id$
Author:
Patrick Peck

Constructor Summary
DOMUtils()
           
 
Method Summary
static void addSchemaToPool(InputStream inputStream, String systemId)
          Preparse a schema and add it to the schema pool.
static boolean checkAttributeParentsInNodeList(org.w3c.dom.NodeList nodes)
          Check, that each attribute node in the given NodeList has its parent in the NodeList as well.
static Set collectNamespaceURIs(org.w3c.dom.Element context)
          Collect all the namespace URIs used in the subtree of a given element.
static Map getNamespaceDeclarations(org.w3c.dom.Node node)
          Build the namespace prefix to namespace URL mapping in effect for a given node.
static String getText(org.w3c.dom.Node node)
          Return the text that a node contains.
static boolean isAncestor(org.w3c.dom.Element candAnc, org.w3c.dom.Node cand)
          Check, if a given DOM element is an ancestor of a given node.
static void localizeNamespaceDeclarations(org.w3c.dom.Element context)
          Add all namespace declarations declared in the parent(s) of a given element and used in the subtree of the given element to the given element.
static org.w3c.dom.DocumentFragment nodeList2DocumentFragment(org.w3c.dom.NodeList nodeList)
          Convert an unstructured NodeList into a DocumentFragment.
static org.w3c.dom.Document parseDocument(InputStream inputStream, boolean validating, String externalSchemaLocations, String externalNoNamespaceSchemaLocation)
          Parse an XML document from an InputStream.
static org.w3c.dom.Document parseDocument(InputStream inputStream, boolean validating, String externalSchemaLocations, String externalNoNamespaceSchemaLocation, org.xml.sax.EntityResolver entityResolver, org.xml.sax.ErrorHandler errorHandler)
          Parse an XML document from an InputStream.
static org.w3c.dom.Document parseDocument(String xmlString, boolean validating, String externalSchemaLocations, String externalNoNamespaceSchemaLocation)
          Parse an UTF-8 encoded XML document from a String.
static org.w3c.dom.Document parseDocument(String xmlString, String encoding, boolean validating, String externalSchemaLocations, String externalNoNamespaceSchemaLocation)
          Parse an XML document from a String.
static org.w3c.dom.Element parseXmlValidating(InputStream inputStream)
          A convenience method to parse an XML document validating.
static String serializeNode(org.w3c.dom.Node node)
          Serialize the given DOM node.
static byte[] serializeNode(org.w3c.dom.Node node, String xmlEncoding)
          Serialize the given DOM node to a byte array.
static boolean validateElement(org.w3c.dom.Element element, String externalSchemaLocations, String externalNoNamespaceSchemaLocation)
          Schema validate a given DOM element.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DOMUtils

public DOMUtils()
Method Detail

addSchemaToPool

public static void addSchemaToPool(InputStream inputStream,
                                   String systemId)
                            throws IOException
Preparse a schema and add it to the schema pool.
Parameters:
inputStream - An InputStream providing the contents of the schema.
systemId - The systemId to use for the schema.
Throws:
IOException - An error occurred reading the schema.

parseDocument

public static org.w3c.dom.Document parseDocument(InputStream inputStream,
                                                 boolean validating,
                                                 String externalSchemaLocations,
                                                 String externalNoNamespaceSchemaLocation,
                                                 org.xml.sax.EntityResolver entityResolver,
                                                 org.xml.sax.ErrorHandler errorHandler)
                                          throws org.xml.sax.SAXException,
                                                 IOException,
                                                 javax.xml.parsers.ParserConfigurationException
Parse an XML document from an InputStream.
Parameters:
inputStream - The InputStream containing the XML document.
validating - If true, parse validating.
externalSchemaLocations - A String containing namespace URI to schema location pairs, the same way it is accepted by the xsi: schemaLocation attribute.
externalNoNamespaceSchemaLocation - The schema location of the schema for elements without a namespace, the same way it is accepted by the xsi:noNamespaceSchemaLocation attribute.
entityResolver - An EntityResolver to resolve external entities (schemas and DTDs). If null, it will not be set.
errorHandler - An ErrorHandler to decide what to do with parsing errors. If null, it will not be set.
Returns:
The parsed XML document as a DOM tree.
Throws:
org.xml.sax.SAXException - An error occurred parsing the document.
IOException - An error occurred reading the document.
javax.xml.parsers.ParserConfigurationException - An error occurred configuring the XML parser.

parseDocument

public static org.w3c.dom.Document parseDocument(InputStream inputStream,
                                                 boolean validating,
                                                 String externalSchemaLocations,
                                                 String externalNoNamespaceSchemaLocation)
                                          throws org.xml.sax.SAXException,
                                                 IOException,
                                                 javax.xml.parsers.ParserConfigurationException
Parse an XML document from an InputStream. It uses a MOAEntityResolver as the EntityResolver and a MOAErrorHandler as the ErrorHandler.
Parameters:
inputStream - The InputStream containing the XML document.
validating - If true, parse validating.
externalSchemaLocations - A String containing namespace URI to schema location pairs, the same way it is accepted by the xsi: schemaLocation attribute.
externalNoNamespaceSchemaLocation - The schema location of the schema for elements without a namespace, the same way it is accepted by the xsi:noNamespaceSchemaLocation attribute.
Returns:
The parsed XML document as a DOM tree.
Throws:
org.xml.sax.SAXException - An error occurred parsing the document.
IOException - An error occurred reading the document.
javax.xml.parsers.ParserConfigurationException - An error occurred configuring the XML parser.

parseDocument

public static org.w3c.dom.Document parseDocument(String xmlString,
                                                 String encoding,
                                                 boolean validating,
                                                 String externalSchemaLocations,
                                                 String externalNoNamespaceSchemaLocation)
                                          throws org.xml.sax.SAXException,
                                                 IOException,
                                                 javax.xml.parsers.ParserConfigurationException
Parse an XML document from a String. It uses a MOAEntityResolver as the EntityResolver and a MOAErrorHandler as the ErrorHandler.
Parameters:
xmlString - The String containing the XML document.
encoding - The encoding of the XML document.
validating - If true, parse validating.
externalSchemaLocations - A String containing namespace URI to schema location pairs, the same way it is accepted by the xsi: schemaLocation attribute.
externalNoNamespaceSchemaLocation - The schema location of the schema for elements without a namespace, the same way it is accepted by the xsi:noNamespaceSchemaLocation attribute.
Returns:
The parsed XML document as a DOM tree.
Throws:
org.xml.sax.SAXException - An error occurred parsing the document.
IOException - An error occurred reading the document.
javax.xml.parsers.ParserConfigurationException - An error occurred configuring the XML parser.

parseDocument

public static org.w3c.dom.Document parseDocument(String xmlString,
                                                 boolean validating,
                                                 String externalSchemaLocations,
                                                 String externalNoNamespaceSchemaLocation)
                                          throws org.xml.sax.SAXException,
                                                 IOException,
                                                 javax.xml.parsers.ParserConfigurationException
Parse an UTF-8 encoded XML document from a String.
Parameters:
xmlString - The String containing the XML document.
validating - If true, parse validating.
externalSchemaLocations - A String containing namespace URI to schema location pairs, the same way it is accepted by the xsi: schemaLocation attribute.
externalNoNamespaceSchemaLocation - The schema location of the schema for elements without a namespace, the same way it is accepted by the xsi:noNamespaceSchemaLocation attribute.
Returns:
The parsed XML document as a DOM tree.
Throws:
org.xml.sax.SAXException - An error occurred parsing the document.
IOException - An error occurred reading the document.
javax.xml.parsers.ParserConfigurationException - An error occurred configuring the XML parser.

parseXmlValidating

public static org.w3c.dom.Element parseXmlValidating(InputStream inputStream)
                                              throws javax.xml.parsers.ParserConfigurationException,
                                                     org.xml.sax.SAXException,
                                                     IOException
A convenience method to parse an XML document validating.
Parameters:
inputStream - The InputStream containing the XML document.
Returns:
The root element of the parsed XML document.
Throws:
org.xml.sax.SAXException - An error occurred parsing the document.
IOException - An error occurred reading the document.
javax.xml.parsers.ParserConfigurationException - An error occurred configuring the XML parser.

validateElement

public static boolean validateElement(org.w3c.dom.Element element,
                                      String externalSchemaLocations,
                                      String externalNoNamespaceSchemaLocation)
                               throws javax.xml.parsers.ParserConfigurationException,
                                      IOException,
                                      org.xml.sax.SAXException,
                                      javax.xml.transform.TransformerException
Schema validate a given DOM element.
Parameters:
element - The element to validate.
externalSchemaLocations - A String containing namespace URI to schema location pairs, the same way it is accepted by the xsi: schemaLocation attribute.
externalNoNamespaceSchemaLocation - The schema location of the schema for elements without a namespace, the same way it is accepted by the xsi:noNamespaceSchemaLocation attribute.
Returns:
true, if the element validates against the schemas declared in it.
Throws:
org.xml.sax.SAXException - An error occurred parsing the document.
IOException - An error occurred reading the document from its serialized representation.
javax.xml.parsers.ParserConfigurationException - An error occurred configuring the XML
javax.xml.transform.TransformerException - An error occurred serializing the element.

serializeNode

public static String serializeNode(org.w3c.dom.Node node)
                            throws javax.xml.transform.TransformerException,
                                   IOException
Serialize the given DOM node. The node will be serialized using the UTF-8 encoding.
Parameters:
node - The node to serialize.
Returns:
String The String representation of the given DOM node.
Throws:
javax.xml.transform.TransformerException - An error occurred transforming the node to a String.
IOException - An IO error occurred writing the node to a byte array.

serializeNode

public static byte[] serializeNode(org.w3c.dom.Node node,
                                   String xmlEncoding)
                            throws javax.xml.transform.TransformerException,
                                   IOException
Serialize the given DOM node to a byte array.
Parameters:
node - The node to serialize.
xmlEncoding - The XML encoding to use.
Returns:
The serialized node, as a byte array. Using a compatible encoding this can easily be converted into a String.
Throws:
javax.xml.transform.TransformerException - An error occurred transforming the node to a byte array.
IOException - An IO error occurred writing the node to a byte array.

getText

public static String getText(org.w3c.dom.Node node)
Return the text that a node contains. This routine:
Parameters:
node - A DOM node from which to extract text.
Returns:
A String representing its contents.

getNamespaceDeclarations

public static Map getNamespaceDeclarations(org.w3c.dom.Node node)
Build the namespace prefix to namespace URL mapping in effect for a given node.
Parameters:
node - The context node for which build the map.
Returns:
The namespace prefix to namespace URL mapping ( a String value to String value mapping).

localizeNamespaceDeclarations

public static void localizeNamespaceDeclarations(org.w3c.dom.Element context)
Add all namespace declarations declared in the parent(s) of a given element and used in the subtree of the given element to the given element.
Parameters:
context - The element to which to add the namespaces.

collectNamespaceURIs

public static Set collectNamespaceURIs(org.w3c.dom.Element context)
Collect all the namespace URIs used in the subtree of a given element.
Parameters:
context - The element that should be searched for namespace URIs.
Returns:
All namespace URIs used in the subtree of context, including the ones used in context itself.

checkAttributeParentsInNodeList

public static boolean checkAttributeParentsInNodeList(org.w3c.dom.NodeList nodes)
Check, that each attribute node in the given NodeList has its parent in the NodeList as well.
Parameters:
nodes - The NodeList to check.
Returns:
true, if each attribute node in nodes has its parent in nodes as well.

nodeList2DocumentFragment

public static org.w3c.dom.DocumentFragment nodeList2DocumentFragment(org.w3c.dom.NodeList nodeList)
                                                              throws javax.xml.parsers.ParserConfigurationException
Convert an unstructured NodeList into a DocumentFragment.
Parameters:
nodeList - Contains the node list to be converted into a DOM DocumentFragment.
Returns:
the resulting DocumentFragment. The DocumentFragment will be backed by a new DOM Document, i.e. all noded of the node list will be cloned.
Throws:
javax.xml.parsers.ParserConfigurationException - An error occurred creating the DocumentFragment.

isAncestor

public static boolean isAncestor(org.w3c.dom.Element candAnc,
                                 org.w3c.dom.Node cand)
Check, if a given DOM element is an ancestor of a given node.
Parameters:
candAnc - The DOM element to check for being the ancestor.
cand - The node to check for being the child.
Returns:
true, if candAnc is an (indirect) ancestor of cand; false otherwise.