Class DomUtil


  • public class DomUtil
    extends java.lang.Object
    DomUtil provides some common utility methods related to w3c-DOM.
    • Constructor Summary

      Constructors 
      Constructor Description
      DomUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static org.w3c.dom.Element addChildElement​(org.w3c.dom.Element parent, java.lang.String localName, Namespace namespace)
      Add a new child element with the given local name and namespace to the specified parent.
      static org.w3c.dom.Element addChildElement​(org.w3c.dom.Element parent, java.lang.String localName, Namespace namespace, java.lang.String text)
      Add a new child element with the given local name and namespace to the specified parent.
      static org.w3c.dom.Element addChildElement​(org.w3c.dom.Node parent, java.lang.String localName, Namespace namespace)
      Add a new child element with the given local name and namespace to the specified parent.
      static org.w3c.dom.Document createDocument()
      Creates and returns a new empty DOM document.
      static org.w3c.dom.Element createElement​(org.w3c.dom.Document factory, java.lang.String localName, Namespace namespace)
      Create a new DOM element with the specified local name and namespace.
      static org.w3c.dom.Element createElement​(org.w3c.dom.Document factory, java.lang.String localName, Namespace namespace, java.lang.String text)
      Create a new DOM element with the specified local name and namespace and add the specified text as Text node to it.
      static org.w3c.dom.Element depthToXml​(boolean isDeep, org.w3c.dom.Document factory)
      Returns the Xml representation of a boolean isDeep, where false presents a depth value of '0', true a depth value of 'infinity'.
      static org.w3c.dom.Element depthToXml​(java.lang.String depth, org.w3c.dom.Document factory)
      Returns the Xml representation of a depth String.
      static java.lang.String getAttribute​(org.w3c.dom.Element parent, java.lang.String localName, Namespace namespace)
      Returns the value of the named attribute of the current element.
      static org.w3c.dom.Element getChildElement​(org.w3c.dom.Node parent, java.lang.String childLocalName, Namespace childNamespace)
      Returns the first child element that matches the given local name and namespace.
      static org.w3c.dom.Element getChildElement​(org.w3c.dom.Node parent, javax.xml.namespace.QName childName)
      Returns the first child element that matches the given QName.
      static ElementIterator getChildren​(org.w3c.dom.Element parent)
      Return an ElementIterator over all child elements.
      static ElementIterator getChildren​(org.w3c.dom.Element parent, java.lang.String childLocalName, Namespace childNamespace)
      Returns a ElementIterator containing all child elements of the given parent node that match the given local name and namespace.
      static ElementIterator getChildren​(org.w3c.dom.Element parent, javax.xml.namespace.QName childName)
      Returns a ElementIterator containing all child elements of the given parent node that match the given QName.
      static java.lang.String getChildText​(org.w3c.dom.Element parent, java.lang.String childLocalName, Namespace childNamespace)
      Calls getText(Element) on the first child element that matches the given local name and namespace.
      static java.lang.String getChildTextTrim​(org.w3c.dom.Element parent, java.lang.String childLocalName, Namespace childNamespace)
      Calls getTextTrim(Element) on the first child element that matches the given local name and namespace.
      static java.lang.String getChildTextTrim​(org.w3c.dom.Element parent, javax.xml.namespace.QName childName)
      Calls getTextTrim(Element) on the first child element that matches the given name.
      static java.util.List<org.w3c.dom.Node> getContent​(org.w3c.dom.Node parent)
      Return a list of all child nodes that are either Element, Text or CDATA.
      static java.lang.String getExpandedName​(java.lang.String localName, Namespace namespace)
      Returns a string representation of the name of a DOM node consisting of "{" + namespace uri + "}" + localName.
      static org.w3c.dom.Element getFirstChildElement​(org.w3c.dom.Node parent)
      Return the first child element
      static Namespace getNamespace​(org.w3c.dom.Element element)
      Build a Namespace from the prefix and uri retrieved from the given element.
      static org.w3c.dom.Attr[] getNamespaceAttributes​(org.w3c.dom.Element element)
      Returns the namespace attributes of the given element.
      static java.lang.String getPrefixedName​(java.lang.String localName, Namespace namespace)
      Return the qualified name of a DOM node consisting of namespace prefix + ":" + local name.
      static java.lang.String getQualifiedName​(java.lang.String localName, Namespace namespace)
      Deprecated.
      As of 2.0.
      static java.lang.String getText​(org.w3c.dom.Element element)
      Concatenates the values of all child nodes of type 'Text' or 'CDATA'/
      static java.lang.String getText​(org.w3c.dom.Element element, java.lang.String defaultValue)
      Same as getText(Element) except that 'defaultValue' is returned instead of null, if the element does not contain any text.
      static java.lang.String getTextTrim​(org.w3c.dom.Element element)
      Removes leading and trailing whitespace after calling getText(Element).
      static boolean hasChildElement​(org.w3c.dom.Node parent, java.lang.String childLocalName, Namespace childNamespace)
      Returns true if the given parent node has a child element that matches the specified local name and namespace.
      static boolean hasContent​(org.w3c.dom.Node parent)
      Return true if the given parent contains any child that is either an Element, Text or CDATA.
      static org.w3c.dom.Element hrefToXml​(java.lang.String href, org.w3c.dom.Document factory)
      Builds a 'DAV:href' Xml element from the given href.
      static boolean matches​(org.w3c.dom.Node node, java.lang.String requiredLocalName, Namespace requiredNamespace)
      Returns true if the specified node matches the required names.
      static boolean matches​(org.w3c.dom.Node node, javax.xml.namespace.QName requiredName)
      Returns true if the specified node matches the required QName.
      static org.w3c.dom.Document parseDocument​(java.io.InputStream stream)
      Parses the given input stream and returns the resulting DOM document.
      static void setAttribute​(org.w3c.dom.Element element, java.lang.String attrLocalName, Namespace attrNamespace, java.lang.String attrValue)
      Add an attribute node to the given element.
      static void setBuilderFactory​(javax.xml.parsers.DocumentBuilderFactory documentBuilderFactory)
      Support the replacement of BUILDER_FACTORY.
      static void setNamespaceAttribute​(org.w3c.dom.Element element, java.lang.String prefix, java.lang.String uri)
      Adds a namespace attribute on the given element.
      static void setText​(org.w3c.dom.Element element, java.lang.String text)
      Create a new text node and add it as child to the given element.
      static org.w3c.dom.Element timeoutToXml​(long timeout, org.w3c.dom.Document factory)
      Converts the given timeout (long value defining the number of milli- second until timeout is reached) to its Xml representation as defined by RFC 4918.
      static void transformDocument​(org.w3c.dom.Document xmlDoc, java.io.OutputStream out)
      Uses a new Transformer instance to transform the specified xml document to the specified writer output target.
      static void transformDocument​(org.w3c.dom.Document xmlDoc, java.io.Writer writer)
      Uses a new Transformer instance to transform the specified xml document to the specified writer output target.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • DomUtil

        public DomUtil()
    • Method Detail

      • setBuilderFactory

        public static void setBuilderFactory​(javax.xml.parsers.DocumentBuilderFactory documentBuilderFactory)
        Support the replacement of BUILDER_FACTORY. This is useful for injecting a customized BuilderFactory, for example with one that uses a local catalog resolver. This is one technique for addressing this issue: http://www.w3.org/blog/systeam/2008/02/08/w3c_s_excessive_dtd_traffic
        Parameters:
        documentBuilderFactory -
      • createDocument

        public static org.w3c.dom.Document createDocument()
                                                   throws javax.xml.parsers.ParserConfigurationException
        Creates and returns a new empty DOM document.
        Returns:
        new DOM document
        Throws:
        javax.xml.parsers.ParserConfigurationException - if the document can not be created
      • parseDocument

        public static org.w3c.dom.Document parseDocument​(java.io.InputStream stream)
                                                  throws javax.xml.parsers.ParserConfigurationException,
                                                         org.xml.sax.SAXException,
                                                         java.io.IOException
        Parses the given input stream and returns the resulting DOM document.
        Parameters:
        stream - XML input stream
        Returns:
        parsed DOM document
        Throws:
        javax.xml.parsers.ParserConfigurationException - if the document can not be created
        org.xml.sax.SAXException - if the document can not be parsed
        java.io.IOException - if the input stream can not be read
      • getAttribute

        public static java.lang.String getAttribute​(org.w3c.dom.Element parent,
                                                    java.lang.String localName,
                                                    Namespace namespace)
        Returns the value of the named attribute of the current element.
        Parameters:
        parent -
        localName - attribute local name or 'nodeName' if no namespace is specified.
        namespace - or null
        Returns:
        attribute value, or null if not found
      • getNamespaceAttributes

        public static org.w3c.dom.Attr[] getNamespaceAttributes​(org.w3c.dom.Element element)
        Returns the namespace attributes of the given element.
        Parameters:
        element -
        Returns:
        the namespace attributes.
      • getText

        public static java.lang.String getText​(org.w3c.dom.Element element)
        Concatenates the values of all child nodes of type 'Text' or 'CDATA'/
        Parameters:
        element -
        Returns:
        String representing the value of all Text and CDATA child nodes or null if the length of the resulting String is 0.
        See Also:
        isText(Node)
      • getText

        public static java.lang.String getText​(org.w3c.dom.Element element,
                                               java.lang.String defaultValue)
        Same as getText(Element) except that 'defaultValue' is returned instead of null, if the element does not contain any text.
        Parameters:
        element -
        defaultValue -
        Returns:
        the text contained in the specified element or defaultValue if the element does not contain any text.
      • getTextTrim

        public static java.lang.String getTextTrim​(org.w3c.dom.Element element)
        Removes leading and trailing whitespace after calling getText(Element).
        Parameters:
        element -
        Returns:
        Trimmed text or null
      • getChildText

        public static java.lang.String getChildText​(org.w3c.dom.Element parent,
                                                    java.lang.String childLocalName,
                                                    Namespace childNamespace)
        Calls getText(Element) on the first child element that matches the given local name and namespace.
        Parameters:
        parent -
        childLocalName -
        childNamespace -
        Returns:
        text contained in the first child that matches the given local name and namespace or null.
        See Also:
        getText(Element)
      • getChildTextTrim

        public static java.lang.String getChildTextTrim​(org.w3c.dom.Element parent,
                                                        java.lang.String childLocalName,
                                                        Namespace childNamespace)
        Calls getTextTrim(Element) on the first child element that matches the given local name and namespace.
        Parameters:
        parent -
        childLocalName -
        childNamespace -
        Returns:
        text contained in the first child that matches the given local name and namespace or null. Note, that leading and trailing whitespace is removed from the text.
        See Also:
        getTextTrim(Element)
      • getChildTextTrim

        public static java.lang.String getChildTextTrim​(org.w3c.dom.Element parent,
                                                        javax.xml.namespace.QName childName)
        Calls getTextTrim(Element) on the first child element that matches the given name.
        Parameters:
        parent -
        childName -
        Returns:
        text contained in the first child that matches the given name or null. Note, that leading and trailing whitespace is removed from the text.
        See Also:
        getTextTrim(Element)
      • hasChildElement

        public static boolean hasChildElement​(org.w3c.dom.Node parent,
                                              java.lang.String childLocalName,
                                              Namespace childNamespace)
        Returns true if the given parent node has a child element that matches the specified local name and namespace.
        Parameters:
        parent -
        childLocalName -
        childNamespace -
        Returns:
        returns true if a child element exists that matches the specified local name and namespace.
      • getChildElement

        public static org.w3c.dom.Element getChildElement​(org.w3c.dom.Node parent,
                                                          java.lang.String childLocalName,
                                                          Namespace childNamespace)
        Returns the first child element that matches the given local name and namespace. If no child element is present or no child element matches, null is returned.
        Parameters:
        parent -
        childLocalName -
        childNamespace -
        Returns:
        first child element matching the specified names or null.
      • getChildElement

        public static org.w3c.dom.Element getChildElement​(org.w3c.dom.Node parent,
                                                          javax.xml.namespace.QName childName)
        Returns the first child element that matches the given QName. If no child element is present or no child element matches, null is returned.
        Parameters:
        parent -
        childName -
        Returns:
        first child element matching the specified name or null.
      • getChildren

        public static ElementIterator getChildren​(org.w3c.dom.Element parent,
                                                  java.lang.String childLocalName,
                                                  Namespace childNamespace)
        Returns a ElementIterator containing all child elements of the given parent node that match the given local name and namespace. If the namespace is null only the localName is compared.
        Parameters:
        parent - the node the children elements should be retrieved from
        childLocalName -
        childNamespace -
        Returns:
        an ElementIterator giving access to all child elements that match the specified localName and namespace.
      • getChildren

        public static ElementIterator getChildren​(org.w3c.dom.Element parent,
                                                  javax.xml.namespace.QName childName)
        Returns a ElementIterator containing all child elements of the given parent node that match the given QName.
        Parameters:
        parent - the node the children elements should be retrieved from
        childName -
        Returns:
        an ElementIterator giving access to all child elements that match the specified name.
      • getFirstChildElement

        public static org.w3c.dom.Element getFirstChildElement​(org.w3c.dom.Node parent)
        Return the first child element
        Returns:
        the first child element or null if the given node has no child elements.
      • hasContent

        public static boolean hasContent​(org.w3c.dom.Node parent)
        Return true if the given parent contains any child that is either an Element, Text or CDATA.
        Parameters:
        parent -
        Returns:
        true if the given parent contains any child that is either an Element, Text or CDATA.
      • getContent

        public static java.util.List<org.w3c.dom.Node> getContent​(org.w3c.dom.Node parent)
        Return a list of all child nodes that are either Element, Text or CDATA.
        Parameters:
        parent -
        Returns:
        a list of all child nodes that are either Element, Text or CDATA.
      • getNamespace

        public static Namespace getNamespace​(org.w3c.dom.Element element)
        Build a Namespace from the prefix and uri retrieved from the given element.
        Returns:
        the Namespace of the given element.
      • matches

        public static boolean matches​(org.w3c.dom.Node node,
                                      java.lang.String requiredLocalName,
                                      Namespace requiredNamespace)
        Returns true if the specified node matches the required names. Note, that that tests return true if the required name is null.
        Parameters:
        node -
        requiredLocalName -
        requiredNamespace -
        Returns:
        true if local name and namespace match the corresponding properties of the given DOM node.
      • matches

        public static boolean matches​(org.w3c.dom.Node node,
                                      javax.xml.namespace.QName requiredName)
        Returns true if the specified node matches the required QName.
        Parameters:
        node -
        requiredName -
        Returns:
        true if local name and namespace match the corresponding properties of the given DOM node.
      • createElement

        public static org.w3c.dom.Element createElement​(org.w3c.dom.Document factory,
                                                        java.lang.String localName,
                                                        Namespace namespace)
        Create a new DOM element with the specified local name and namespace.
        Parameters:
        factory -
        localName -
        namespace -
        Returns:
        a new DOM element
        See Also:
        Document.createElement(String), Document.createElementNS(String, String)
      • createElement

        public static org.w3c.dom.Element createElement​(org.w3c.dom.Document factory,
                                                        java.lang.String localName,
                                                        Namespace namespace,
                                                        java.lang.String text)
        Create a new DOM element with the specified local name and namespace and add the specified text as Text node to it.
        Parameters:
        factory -
        localName -
        namespace -
        text -
        Returns:
        a new DOM element
        See Also:
        Document.createElement(String), Document.createElementNS(String, String), Document.createTextNode(String), Node.appendChild(org.w3c.dom.Node)
      • addChildElement

        public static org.w3c.dom.Element addChildElement​(org.w3c.dom.Element parent,
                                                          java.lang.String localName,
                                                          Namespace namespace)
        Add a new child element with the given local name and namespace to the specified parent.
        Parameters:
        parent -
        localName -
        namespace -
        Returns:
        the new element that was attached to the given parent.
      • addChildElement

        public static org.w3c.dom.Element addChildElement​(org.w3c.dom.Node parent,
                                                          java.lang.String localName,
                                                          Namespace namespace)
        Add a new child element with the given local name and namespace to the specified parent.
        Parameters:
        parent -
        localName -
        namespace -
        Returns:
        the new element that was attached to the given parent.
      • addChildElement

        public static org.w3c.dom.Element addChildElement​(org.w3c.dom.Element parent,
                                                          java.lang.String localName,
                                                          Namespace namespace,
                                                          java.lang.String text)
        Add a new child element with the given local name and namespace to the specified parent. The specified text is added as Text node to the created child element.
        Parameters:
        parent -
        localName -
        namespace -
        text -
        Returns:
        child element that was added to the specified parent
        See Also:
        Document.createElement(String), Document.createElementNS(String, String), Document.createTextNode(String), Node.appendChild(org.w3c.dom.Node)
      • setText

        public static void setText​(org.w3c.dom.Element element,
                                   java.lang.String text)
        Create a new text node and add it as child to the given element.
        Parameters:
        element -
        text -
      • setAttribute

        public static void setAttribute​(org.w3c.dom.Element element,
                                        java.lang.String attrLocalName,
                                        Namespace attrNamespace,
                                        java.lang.String attrValue)
        Add an attribute node to the given element.
        Parameters:
        element -
        attrLocalName -
        attrNamespace -
        attrValue -
      • setNamespaceAttribute

        public static void setNamespaceAttribute​(org.w3c.dom.Element element,
                                                 java.lang.String prefix,
                                                 java.lang.String uri)
        Adds a namespace attribute on the given element.
        Parameters:
        element -
        prefix -
        uri -
      • timeoutToXml

        public static org.w3c.dom.Element timeoutToXml​(long timeout,
                                                       org.w3c.dom.Document factory)
        Converts the given timeout (long value defining the number of milli- second until timeout is reached) to its Xml representation as defined by RFC 4918.
        Parameters:
        timeout - number of milli-seconds until timeout is reached.
        Returns:
        'timeout' Xml element
      • depthToXml

        public static org.w3c.dom.Element depthToXml​(boolean isDeep,
                                                     org.w3c.dom.Document factory)
        Returns the Xml representation of a boolean isDeep, where false presents a depth value of '0', true a depth value of 'infinity'.
        Parameters:
        isDeep -
        Returns:
        Xml representation
      • depthToXml

        public static org.w3c.dom.Element depthToXml​(java.lang.String depth,
                                                     org.w3c.dom.Document factory)
        Returns the Xml representation of a depth String. Webdav defines the following valid values for depths: 0, 1, infinity
        Parameters:
        depth -
        Returns:
        'deep' XML element
      • hrefToXml

        public static org.w3c.dom.Element hrefToXml​(java.lang.String href,
                                                    org.w3c.dom.Document factory)
        Builds a 'DAV:href' Xml element from the given href.

        Note that the path present needs to be a valid URI or URI reference.

        Parameters:
        href - String representing the text of the 'href' Xml element
        factory - the Document used as factory
        Returns:
        Xml representation of a 'href' according to RFC 2518.
      • getQualifiedName

        public static java.lang.String getQualifiedName​(java.lang.String localName,
                                                        Namespace namespace)
        Deprecated.
        As of 2.0. Please use getExpandedName(String, Namespace) instead. This method was named according to usage of 'qualified name' in JSR 170 that conflicts with the terminology used in XMLNS. As of JCR 2.0 the String consisting of "{" + namespace uri + "}" + localName is called Expanded Name.
        Parameters:
        localName -
        namespace -
        Returns:
        the expanded name of a DOM node consisting of "{" + namespace uri + "}" + localName. If the specified namespace is null or represents the empty namespace, the local name is returned.
      • getExpandedName

        public static java.lang.String getExpandedName​(java.lang.String localName,
                                                       Namespace namespace)
        Returns a string representation of the name of a DOM node consisting of "{" + namespace uri + "}" + localName. If the specified namespace is null or represents the empty namespace, the local name is returned.
        Parameters:
        localName -
        namespace -
        Returns:
        String representation of the name of a DOM node consisting of "{" + namespace uri + "}" + localName. If the specified namespace is null or represents the empty namespace, the local name is returned.
        Since:
        2.0 Replaces the deprecated method getQualifiedName(String, Namespace).
      • getPrefixedName

        public static java.lang.String getPrefixedName​(java.lang.String localName,
                                                       Namespace namespace)
        Return the qualified name of a DOM node consisting of namespace prefix + ":" + local name. If the specified namespace is null or contains an empty prefix, the local name is returned.
        NOTE, that this is the value to be used for the 'qualified Name' parameter expected with the namespace sensitive factory methods.
        Parameters:
        localName -
        namespace -
        Returns:
        qualified name consisting of prefix, ':' and local name.
        See Also:
        Document.createAttributeNS(String, String), Document.createElementNS(String, String)
      • transformDocument

        public static void transformDocument​(org.w3c.dom.Document xmlDoc,
                                             java.io.Writer writer)
                                      throws javax.xml.transform.TransformerException,
                                             org.xml.sax.SAXException
        Uses a new Transformer instance to transform the specified xml document to the specified writer output target.
        Parameters:
        xmlDoc - XML document to create the transformation Source for.
        writer - The writer used to create a new transformation Result for.
        Throws:
        javax.xml.transform.TransformerException
        org.xml.sax.SAXException
      • transformDocument

        public static void transformDocument​(org.w3c.dom.Document xmlDoc,
                                             java.io.OutputStream out)
                                      throws javax.xml.transform.TransformerException,
                                             org.xml.sax.SAXException
        Uses a new Transformer instance to transform the specified xml document to the specified writer output target.
        Parameters:
        xmlDoc - XML document to create the transformation Source for.
        out - The stream used to create a new transformation Result for.
        Throws:
        javax.xml.transform.TransformerException
        org.xml.sax.SAXException