Class DOMUtils

java.lang.Object
at.gv.egovernment.moaspss.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.
  • Constructor Details

    • DOMUtils

      public DOMUtils()
  • Method Details

    • addSchemaToPool

      public static void addSchemaToPool(InputStream inputStream, String systemId) throws IOException
      Preparse a schema and add it to the schema pool. The method only adds the schema to the pool if a schema having the same systemId (namespace URI) is not already present in the pool.
      Parameters:
      inputStream - An InputStream providing the contents of the schema.
      systemId - The systemId (namespace URI) to use for the schema.
      Throws:
      IOException - An error occurred reading the schema.
    • parseDocument

      public static Document parseDocument(InputStream inputStream, boolean validating, String externalSchemaLocations, String externalNoNamespaceSchemaLocation, EntityResolver entityResolver, ErrorHandler errorHandler) throws SAXException, IOException, 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:
      SAXException - An error occurred parsing the document.
      IOException - An error occurred reading the document.
      ParserConfigurationException - An error occurred configuring the XML parser.
    • parseDocumentSimple

      public static Document parseDocumentSimple(InputStream inputStream) throws SAXException, IOException, ParserConfigurationException
      Parse an XML document from an InputStream.
      Parameters:
      inputStream - The InputStream containing the XML document.
      Returns:
      The parsed XML document as a DOM tree.
      Throws:
      SAXException - An error occurred parsing the document.
      IOException - An error occurred reading the document.
      ParserConfigurationException - An error occurred configuring the XML parser.
    • parseDocument

      public static Document parseDocument(InputStream inputStream, boolean validating, String externalSchemaLocations, String externalNoNamespaceSchemaLocation) throws SAXException, IOException, 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:
      SAXException - An error occurred parsing the document.
      IOException - An error occurred reading the document.
      ParserConfigurationException - An error occurred configuring the XML parser.
    • parseDocument

      public static Document parseDocument(String xmlString, String encoding, boolean validating, String externalSchemaLocations, String externalNoNamespaceSchemaLocation) throws SAXException, IOException, 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:
      SAXException - An error occurred parsing the document.
      IOException - An error occurred reading the document.
      ParserConfigurationException - An error occurred configuring the XML parser.
    • parseDocument

      public static Document parseDocument(String xmlString, boolean validating, String externalSchemaLocations, String externalNoNamespaceSchemaLocation) throws SAXException, IOException, 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:
      SAXException - An error occurred parsing the document.
      IOException - An error occurred reading the document.
      ParserConfigurationException - An error occurred configuring the XML parser.
    • parseXmlValidating

      public static Element parseXmlValidating(InputStream inputStream) throws ParserConfigurationException, 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:
      SAXException - An error occurred parsing the document.
      IOException - An error occurred reading the document.
      ParserConfigurationException - An error occurred configuring the XML parser.
    • parseXmlNonValidating

      public static Element parseXmlNonValidating(InputStream inputStream) throws ParserConfigurationException, SAXException, IOException
      A convenience method to parse an XML document non validating.
      Parameters:
      inputStream - The InputStream containing the XML document.
      Returns:
      The root element of the parsed XML document.
      Throws:
      SAXException - An error occurred parsing the document.
      IOException - An error occurred reading the document.
      ParserConfigurationException - An error occurred configuring the XML parser.
    • validateElement

      public static boolean validateElement(Element element, String externalSchemaLocations, String externalNoNamespaceSchemaLocation) throws ParserConfigurationException, IOException, SAXException, 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:
      SAXException - An error occurred parsing the document.
      IOException - An error occurred reading the document from its serialized representation.
      ParserConfigurationException - An error occurred configuring the XML
      TransformerException - An error occurred serializing the element.
    • validateElement

      public static boolean validateElement(Element element, String externalSchemaLocations, String externalNoNamespaceSchemaLocation, EntityResolver entityResolver) throws ParserConfigurationException, IOException, SAXException, 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:
      SAXException - An error occurred parsing the document.
      IOException - An error occurred reading the document from its serialized representation.
      ParserConfigurationException - An error occurred configuring the XML
      TransformerException - An error occurred serializing the element.
    • serializeNode

      public static String serializeNode(Node node) throws 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:
      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 String serializeNode(Node node, boolean omitXmlDeclaration) throws TransformerException, IOException
      Serialize the given DOM node. The node will be serialized using the UTF-8 encoding.
      Parameters:
      node - The node to serialize.
      omitXmlDeclaration - The boolean value for omitting the XML Declaration.
      Returns:
      String The String representation of the given DOM node.
      Throws:
      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 String serializeNode(Node node, boolean omitXmlDeclaration, String lineSeperator) throws TransformerException, IOException
      Serialize the given DOM node. The node will be serialized using the UTF-8 encoding.
      Parameters:
      node - The node to serialize.
      omitXmlDeclaration - The boolean value for omitting the XML Declaration.
      lineSeperator - Sets the line seperator String of the parser
      Returns:
      String The String representation of the given DOM node.
      Throws:
      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(Node node, String xmlEncoding) throws 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:
      TransformerException - An error occurred transforming the node to a byte array.
      IOException - An IO error occurred writing the node to a byte array.
    • serializeNode

      public static byte[] serializeNode(Node node, String xmlEncoding, boolean omitDeclaration) throws TransformerException, IOException
      Serialize the given DOM node to a byte array.
      Parameters:
      node - The node to serialize.
      xmlEncoding - The XML encoding to use.
      omitDeclaration - The boolean value for omitting the XML Declaration.
      Returns:
      The serialized node, as a byte array. Using a compatible encoding this can easily be converted into a String.
      Throws:
      TransformerException - An error occurred transforming the node to a byte array.
      IOException - An IO error occurred writing the node to a byte array.
    • serializeNode

      public static byte[] serializeNode(Node node, String xmlEncoding, boolean omitDeclaration, String lineSeperator) throws TransformerException, IOException
      Serialize the given DOM node to a byte array.
      Parameters:
      node - The node to serialize.
      xmlEncoding - The XML encoding to use.
      omitDeclaration - The boolean value for omitting the XML Declaration.
      lineSeperator - Sets the line seperator String of the parser
      Returns:
      The serialized node, as a byte array. Using a compatible encoding this can easily be converted into a String.
      Throws:
      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(Node node)
      Return the text that a node contains. This routine:
      • Ignores comments and processing instructions.
      • Concatenates TEXT nodes, CDATA nodes, and the results recursively processing EntityRef nodes.
      • Ignores any element nodes in the sublist. (Other possible options are to recurse into element sublists or throw an exception.)
      Parameters:
      node - A DOM node from which to extract text.
      Returns:
      A String representing its contents.
    • getNamespaceDeclarations

      public static Map getNamespaceDeclarations(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(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(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(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 DocumentFragment nodeList2DocumentFragment(NodeList nodeList) throws 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:
      ParserConfigurationException - An error occurred creating the DocumentFragment.
    • isAncestor

      public static boolean isAncestor(Element candAnc, 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.
    • getElementFromNodeList

      public static Element getElementFromNodeList(NodeList nl)
      Selects the (first) element from a node list and returns it.
      Parameters:
      nl - The NodeList to get the element from.
      Returns:
      The (first) element included in the node list or null if the node list is null or empty or no element is included in the list.
    • getChildElements

      public static List getChildElements(Element parent)
      Returns all child elements of the given element.
      Parameters:
      parent - The element to get the child elements from.
      Returns:
      A list including all child elements of the given element. Maybe empty if the parent element has no child elements.
    • nodeToByteArray

      public static byte[] nodeToByteArray(Node node) throws TransformerException
      Returns a byte array from given node.
      Parameters:
      node -
      Returns:
      Throws:
      TransformerException