org.apache.cxf.helpers
Class DOMUtils

java.lang.Object
  extended by org.apache.cxf.helpers.DOMUtils

public final class DOMUtils
extends java.lang.Object

Few simple utils to read DOM. This is originally from the Jakarta Commons Modeler.

Author:
Costin Manolache

Nested Class Summary
static class DOMUtils.NullResolver
           
 
Method Summary
static void addNamespacePrefix(org.w3c.dom.Element element, java.lang.String namespaceUri, java.lang.String prefix)
          Add a namespace prefix definition to an element.
static org.w3c.dom.Document createDocument()
           
static javax.xml.parsers.DocumentBuilder createDocumentBuilder()
           
static java.lang.String createNamespace(org.w3c.dom.Element el, java.lang.String ns)
           
static java.util.List<org.w3c.dom.Element> findAllElementsByTagName(org.w3c.dom.Element elem, java.lang.String tagName)
           
static java.util.List<org.w3c.dom.Element> findAllElementsByTagNameNS(org.w3c.dom.Element elem, java.lang.String nameSpaceURI, java.lang.String localName)
           
static org.w3c.dom.Node findChildWithAtt(org.w3c.dom.Node parent, java.lang.String elemName, java.lang.String attName, java.lang.String attVal)
          Find the first direct child with a given attribute.
static java.lang.String getAttribute(org.w3c.dom.Element element, javax.xml.namespace.QName attName)
           
static java.lang.String getAttribute(org.w3c.dom.Node element, java.lang.String attName)
           
static java.lang.String getAttributeValueEmptyNull(org.w3c.dom.Element e, java.lang.String attributeName)
          This function is much like getAttribute, but returns null, not "", for a nonexistent attribute.
static org.w3c.dom.Node getChild(org.w3c.dom.Node parent, int type)
          Get the first child of the specified type.
static org.w3c.dom.Node getChild(org.w3c.dom.Node parent, java.lang.String name)
          Get the first element child.
static java.lang.String getChildContent(org.w3c.dom.Node parent, java.lang.String name)
          Get the first child's content ( ie it's included TEXT node ).
static java.util.List<org.w3c.dom.Element> getChildrenWithName(org.w3c.dom.Element parent, java.lang.String ns, java.lang.String localName)
          Return child elements with specified name.
static java.lang.String getContent(org.w3c.dom.Node n)
          Get the trimmed text content of a node or null if there is no text
static javax.xml.namespace.QName getElementQName(org.w3c.dom.Element el)
           
static org.w3c.dom.Element getFirstChildWithName(org.w3c.dom.Element parent, javax.xml.namespace.QName q)
          Return the first element child with the specified qualified name.
static org.w3c.dom.Element getFirstChildWithName(org.w3c.dom.Element parent, java.lang.String ns, java.lang.String lp)
          Return the first element child with the specified qualified name.
static org.w3c.dom.Element getFirstElement(org.w3c.dom.Node parent)
          Get the first direct child with a given type
static java.lang.String getNamespace(org.w3c.dom.Node node, java.lang.String searchPrefix)
          Starting from a node, find the namespace declaration for a prefix.
static org.w3c.dom.Node getNext(org.w3c.dom.Node current)
          Get the next sibling with the same name and type
static org.w3c.dom.Node getNext(org.w3c.dom.Node current, java.lang.String name, int type)
          Return the next sibling with a given name and type
static org.w3c.dom.Element getNextElement(org.w3c.dom.Element el)
           
static java.lang.String getPrefix(org.w3c.dom.Element el, java.lang.String ns)
           
static void getPrefixes(org.w3c.dom.Element element, java.lang.String namespaceUri, java.util.List<java.lang.String> prefixes)
          Get all prefixes defined on this element for the specified namespace.
static void getPrefixesRecursive(org.w3c.dom.Element element, java.lang.String namespaceUri, java.util.List<java.lang.String> prefixes)
          Get all prefixes defined, up to the root, for a namespace URI.
static java.lang.String getPrefixRecursive(org.w3c.dom.Element el, java.lang.String ns)
           
static java.lang.String getRawContent(org.w3c.dom.Node n)
          Get the raw text content of a node or null if there is no text
static org.w3c.dom.Document readXml(java.io.InputStream is)
          Read XML as DOM.
static org.w3c.dom.Document readXml(java.io.Reader is)
           
static org.w3c.dom.Document readXml(javax.xml.transform.stream.StreamSource is)
           
static void removeAttribute(org.w3c.dom.Node node, java.lang.String attName)
           
static void setAttribute(org.w3c.dom.Node node, java.lang.String attName, java.lang.String val)
           
static java.lang.String setNamespace(org.w3c.dom.Element element, java.lang.String namespace, java.lang.String prefix)
          Set a namespace/prefix on an element if it is not set already.
static void setText(org.w3c.dom.Node node, java.lang.String val)
          Set or replace the text value
static void writeXml(org.w3c.dom.Node n, java.io.OutputStream os)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getAttributeValueEmptyNull

public static java.lang.String getAttributeValueEmptyNull(org.w3c.dom.Element e,
                                                          java.lang.String attributeName)
This function is much like getAttribute, but returns null, not "", for a nonexistent attribute.

Parameters:
e -
attributeName -
Returns:

getContent

public static java.lang.String getContent(org.w3c.dom.Node n)
Get the trimmed text content of a node or null if there is no text


getRawContent

public static java.lang.String getRawContent(org.w3c.dom.Node n)
Get the raw text content of a node or null if there is no text


getChild

public static org.w3c.dom.Node getChild(org.w3c.dom.Node parent,
                                        java.lang.String name)
Get the first element child.

Parameters:
parent - lookup direct childs
name - name of the element. If null return the first element.

getAttribute

public static java.lang.String getAttribute(org.w3c.dom.Node element,
                                            java.lang.String attName)

getAttribute

public static java.lang.String getAttribute(org.w3c.dom.Element element,
                                            javax.xml.namespace.QName attName)

setAttribute

public static void setAttribute(org.w3c.dom.Node node,
                                java.lang.String attName,
                                java.lang.String val)

removeAttribute

public static void removeAttribute(org.w3c.dom.Node node,
                                   java.lang.String attName)

setText

public static void setText(org.w3c.dom.Node node,
                           java.lang.String val)
Set or replace the text value


findChildWithAtt

public static org.w3c.dom.Node findChildWithAtt(org.w3c.dom.Node parent,
                                                java.lang.String elemName,
                                                java.lang.String attName,
                                                java.lang.String attVal)
Find the first direct child with a given attribute.

Parameters:
parent -
elemName - name of the element, or null for any
attName - attribute we're looking for
attVal - attribute value or null if we just want any

getChildContent

public static java.lang.String getChildContent(org.w3c.dom.Node parent,
                                               java.lang.String name)
Get the first child's content ( ie it's included TEXT node ).


getElementQName

public static javax.xml.namespace.QName getElementQName(org.w3c.dom.Element el)

getFirstElement

public static org.w3c.dom.Element getFirstElement(org.w3c.dom.Node parent)
Get the first direct child with a given type


getNextElement

public static org.w3c.dom.Element getNextElement(org.w3c.dom.Element el)

getFirstChildWithName

public static org.w3c.dom.Element getFirstChildWithName(org.w3c.dom.Element parent,
                                                        javax.xml.namespace.QName q)
Return the first element child with the specified qualified name.

Parameters:
parent -
q -
Returns:

getFirstChildWithName

public static org.w3c.dom.Element getFirstChildWithName(org.w3c.dom.Element parent,
                                                        java.lang.String ns,
                                                        java.lang.String lp)
Return the first element child with the specified qualified name.

Parameters:
parent -
ns -
lp -
Returns:

getChildrenWithName

public static java.util.List<org.w3c.dom.Element> getChildrenWithName(org.w3c.dom.Element parent,
                                                                      java.lang.String ns,
                                                                      java.lang.String localName)
Return child elements with specified name.

Parameters:
parent -
ns -
localName -
Returns:

getChild

public static org.w3c.dom.Node getChild(org.w3c.dom.Node parent,
                                        int type)
Get the first child of the specified type.

Parameters:
parent -
type -
Returns:

getNext

public static org.w3c.dom.Node getNext(org.w3c.dom.Node current)
Get the next sibling with the same name and type


getNext

public static org.w3c.dom.Node getNext(org.w3c.dom.Node current,
                                       java.lang.String name,
                                       int type)
Return the next sibling with a given name and type


readXml

public static org.w3c.dom.Document readXml(java.io.InputStream is)
                                    throws org.xml.sax.SAXException,
                                           java.io.IOException,
                                           javax.xml.parsers.ParserConfigurationException
Read XML as DOM.

Throws:
org.xml.sax.SAXException
java.io.IOException
javax.xml.parsers.ParserConfigurationException

readXml

public static org.w3c.dom.Document readXml(java.io.Reader is)
                                    throws org.xml.sax.SAXException,
                                           java.io.IOException,
                                           javax.xml.parsers.ParserConfigurationException
Throws:
org.xml.sax.SAXException
java.io.IOException
javax.xml.parsers.ParserConfigurationException

readXml

public static org.w3c.dom.Document readXml(javax.xml.transform.stream.StreamSource is)
                                    throws org.xml.sax.SAXException,
                                           java.io.IOException,
                                           javax.xml.parsers.ParserConfigurationException
Throws:
org.xml.sax.SAXException
java.io.IOException
javax.xml.parsers.ParserConfigurationException

writeXml

public static void writeXml(org.w3c.dom.Node n,
                            java.io.OutputStream os)
                     throws javax.xml.transform.TransformerException
Throws:
javax.xml.transform.TransformerException

createDocumentBuilder

public static javax.xml.parsers.DocumentBuilder createDocumentBuilder()

createDocument

public static org.w3c.dom.Document createDocument()

getPrefixRecursive

public static java.lang.String getPrefixRecursive(org.w3c.dom.Element el,
                                                  java.lang.String ns)

getPrefix

public static java.lang.String getPrefix(org.w3c.dom.Element el,
                                         java.lang.String ns)

getPrefixesRecursive

public static void getPrefixesRecursive(org.w3c.dom.Element element,
                                        java.lang.String namespaceUri,
                                        java.util.List<java.lang.String> prefixes)
Get all prefixes defined, up to the root, for a namespace URI.

Parameters:
element -
namespaceUri -
prefixes -

getPrefixes

public static void getPrefixes(org.w3c.dom.Element element,
                               java.lang.String namespaceUri,
                               java.util.List<java.lang.String> prefixes)
Get all prefixes defined on this element for the specified namespace.

Parameters:
element -
namespaceUri -
prefixes -

createNamespace

public static java.lang.String createNamespace(org.w3c.dom.Element el,
                                               java.lang.String ns)

getNamespace

public static java.lang.String getNamespace(org.w3c.dom.Node node,
                                            java.lang.String searchPrefix)
Starting from a node, find the namespace declaration for a prefix. for a matching namespace declaration.

Parameters:
node - search up from here to search for namespace definitions
searchPrefix - the prefix we are searching for
Returns:
the namespace if found.

findAllElementsByTagNameNS

public static java.util.List<org.w3c.dom.Element> findAllElementsByTagNameNS(org.w3c.dom.Element elem,
                                                                             java.lang.String nameSpaceURI,
                                                                             java.lang.String localName)

findAllElementsByTagName

public static java.util.List<org.w3c.dom.Element> findAllElementsByTagName(org.w3c.dom.Element elem,
                                                                           java.lang.String tagName)

setNamespace

public static java.lang.String setNamespace(org.w3c.dom.Element element,
                                            java.lang.String namespace,
                                            java.lang.String prefix)
Set a namespace/prefix on an element if it is not set already. First off, it searches for the element for the prefix associated with the specified namespace. If the prefix isn't null, then this is returned. Otherwise, it creates a new attribute using the namespace/prefix passed as parameters.

Parameters:
element -
namespace -
prefix -
Returns:
the prefix associated with the set namespace

addNamespacePrefix

public static void addNamespacePrefix(org.w3c.dom.Element element,
                                      java.lang.String namespaceUri,
                                      java.lang.String prefix)
Add a namespace prefix definition to an element.

Parameters:
element -
namespaceUri -
prefix -


Apache CXF