Class DOMUtils

java.lang.Object
org.jvnet.basicjaxb.dom.DOMUtils

public class DOMUtils extends Object
A utility class to provide DOM helper methods.
  • Method Details

    • getLogger

      public static org.slf4j.Logger getLogger()
    • toElement

      public static Element toElement(QName qname, String value)
      Convert a QName instance into a W3C DOM Element instance.
      Parameters:
      qname - The instance to be converted.
      Returns:
      A W3C DOM Element representaion.
    • toElement

      public static Element toElement(String namespaceURI, String localPart, String prefix, String value)
      Create a W3C DOM Element instance.
      Parameters:
      namespaceURI - The node's namespace URI.
      localPart - The node's local part (name).
      prefix - The namespace prefix for this node.
      value - The elements's value.
      Returns:
      A W3C DOM Node representaion.
    • toNode

      public static Node toNode(QName qname, String value)
      Convert a QName instance into a W3C DOM Node instance.
      Parameters:
      qname - The instance to be converted.
      Returns:
      A W3C DOM Node representaion.
    • toNode

      public static Node toNode(jakarta.xml.bind.JAXBElement<Object> jaxbElement, boolean prefix)
      Convert a JAXBElement instance into a W3C DOM Node instance.
      Parameters:
      jaxbElement - The instance to be converted.
      prefix - A flag to assign the prefix.
      Returns:
      A W3C DOM Node representaion.
    • toNode

      public static Node toNode(jakarta.xml.bind.JAXBElement<Object> jaxbElement)
      Convert a JAXBElement instance into a W3C DOM Node instance. Ignore the prefix.
      Parameters:
      jaxbElement - The instance to be converted.
      Returns:
      A W3C DOM Node representaion.
    • toNode

      public static Node toNode(String namespaceURI, String localPart, String prefix, String value)
      Create a W3C DOM Node instance.
      Parameters:
      namespaceURI - The node's namespace URI.
      localPart - The node's local part (name).
      prefix - The namespace prefix for this node.
      value - The node's value.
      Returns:
      A W3C DOM Node representaion.
    • areEqualNodes

      public static boolean areEqualNodes(Node node1, Node node2, boolean ignorePrefix)

      Tests whether two nodes are equal.

      This method tests for equality of nodes, not sameness (i.e., whether the two nodes are references to the same object), which can be tested with Node.isSameNode. All nodes that are the same will also be equal, though the reverse may not be true.

      Two nodes are equal if and only if the following conditions are satisfied:

      • The two nodes are of the same type.
      • The following string attributes are equal (they are both null, or they have the same length and are character for character identical):
        • namespaceURI
        • localName
        • Optional:
          • prefix: The prefix xmlns is used only to declare namespace bindings.
          • nodeName: Concatenation of prefix, ":" and localName.
        • nodeValue
        • baseURI
      • The attributes NamedNodeMaps are equal (they are both null, or they have the same map size and for each node that exists in one map there is a node that exists in the other map and is equal, although not necessarily at the same index).
      • The childNodes NodeLists are equal (they are both null, or they have the same length and contain equal nodes at the same index).

      Note: Normalization can affect equality; to avoid this, nodes should be normalized before being compared.

      For two DocumentType nodes to be equal, the following conditions must also be satisfied:

      • The following string attributes are equal:
        • publicId
        • systemId
        • internalSubset
      • The entities NamedNodeMaps are equal.
      • The notations NamedNodeMaps are equal.

      On the other hand, the following do not affect equality:

      • The ownerDocument attribute.
      • The specified attribute for Attr nodes.
      • The isWhitespaceInElementContent attribute for Text nodes.
      • As well as any user data or event listeners registered on the nodes.

      Note: This method is derived from com.sun.org.apache.xerces.internal.dom.NodeImpl.

      Parameters:
      node1 - The first node to compare equality with.
      node2 - The second node to compare equality with.
      ignorePrefix - When true, ignore the prefix related values.
      Returns:
      If the nodes are equal, true; otherwise false.
    • areEqualParentNodes

      public static boolean areEqualParentNodes(Node node1, Node node2, boolean ignorePrefix)

      Override inherited behavior from areEqualNodes to support deep equal.

      Reference: DOM Level 3 Working Draft - Experimental.

      Note: This method is derived from com.sun.org.apache.xerces.internal.dom.ParentNode.

      Parameters:
      node1 - The first node to compare equality with.
      node2 - The second node to compare equality with.
      ignorePrefix - When true, ignore the prefix related values.
      Returns:
      If the nodes (and any subtrees) are equal, true; otherwise false.
    • areEqualElements

      public static boolean areEqualElements(Element node1, Element node2, boolean ignorePrefix)

      Override inherited behavior from areEqualNodes and areEqualParentNodes to check on attributes.

      Reference: DOM Level 3 Working Draft - Experimental.

      Note: This method is derived from com.sun.org.apache.xerces.internal.dom.ElementImpl

      Parameters:
      node1 - The first element to compare equality with.
      node2 - The second element to compare equality with.
      ignorePrefix - When true, ignore the prefix related values.
      Returns:
      If the elements (and any subtrees) are equal, true; otherwise false.
    • saxParseDocument

      public static Document saxParseDocument(InputStream is, String systemId, String locatorPrefix) throws IOException, SAXException
      Use SAX to parse an XML document with line numbers.
      Parameters:
      is - An input stream for the source XML file.
      systemId - The systemId which is needed for resolving relative URIs.
      locatorPrefix - Custom prefix for publicId, systemId, lineNumber and columnNumber.
      Returns:
      A W3C Document instance enhanced with line numbers per element.
      Throws:
      IOException - When SAX cannot parse the input stream.
      SAXException - When SAXParserFactory cannot create a new SAX parser.
      See Also:
    • getLocator

      public static LocatorBean getLocator(Element element, String locatorPrefix)
      Get a LocatorBean from a Element.
      Parameters:
      element - A DOM element with optional locator attributes.
      locatorPrefix - Custom prefix for publicId, systemId, lineNumber and columnNumber.
      Returns:
      A Locator with optional values or null.
    • transformToString

      public static String transformToString(Node node) throws TransformerException
      Transform a Node instance into a formatted XML string.
      Parameters:
      node - The Node instance to be transformed.
      Returns:
      A Node instance into a formatted XML string.
      Throws:
      TransformerException - When the Node instance cannot be transformed.
    • buildToString

      public static String buildToString(String tab, Node node)
      Build a Node instance into a string representation.
      Parameters:
      tab - The string for indentation.
      node - The Node instance to build into a string.
      Returns:
      A Node instance into a formatted XML string.