Apache CXF API

org.apache.cxf.helpers
Class DOMUtils

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

public final class DOMUtils
extends Object

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


Nested Class Summary
static class DOMUtils.NullResolver
           
 
Method Summary
static void addNamespacePrefix(Element element, String namespaceUri, String prefix)
          Add a namespace prefix definition to an element.
static Set<QName> convertStringsToQNames(List<String> expandedQNames)
           
static QName convertStringToQName(String expandedQName)
           
static QName convertStringToQName(String expandedQName, String prefix)
           
static Document createDocument()
           
static String createNamespace(Element el, String ns)
           
static QName createQName(String qualifiedName, Node node)
          Creates a QName object based on the qualified name and using the Node as a base to lookup the namespace for the prefix
static List<Element> findAllElementsByTagName(Element elem, String tagName)
           
static List<Element> findAllElementsByTagNameNS(Element elem, String nameSpaceURI, String localName)
           
static Element findChildWithAtt(Node parent, String elemName, String attName, String attVal)
          Find the first direct child with a given attribute.
static String getAllContent(Node n)
          Get the text content of a node and all it's children or null if there is no text
static String getAttribute(Element element, QName attName)
           
static String getAttribute(Node element, String attName)
           
static String getAttributeValueEmptyNull(Element e, String attributeName)
          This function is much like getAttribute, but returns null, not "", for a nonexistent attribute.
static Node getChild(Node parent, int type)
          Get the first child of the specified type.
static Node getChild(Node parent, String name)
          Get the first element child.
static String getChildContent(Node parent, String name)
          Get the first child's content ( ie it's included TEXT node ).
static List<Element> getChildrenWithName(Element parent, String ns, String localName)
          Return child elements with specified name.
static List<Element> getChildrenWithNamespace(Element parent, String ns)
          Returns all child elements with specified namespace.
static String getContent(Node n)
          Get the trimmed text content of a node or null if there is no text
static QName getElementQName(Element el)
           
static Element getFirstChildWithName(Element parent, QName q)
          Return the first element child with the specified qualified name.
static Element getFirstChildWithName(Element parent, String ns, String lp)
          Return the first element child with the specified qualified name.
static Element getFirstElement(Node parent)
          Get the first direct child with a given type
static String getNamespace(Node node, String searchPrefix)
          Starting from a node, find the namespace declaration for a prefix.
static Node getNext(Node current)
          Get the next sibling with the same name and type
static Node getNext(Node current, String name, int type)
          Return the next sibling with a given name and type
static Element getNextElement(Element el)
           
static String getPrefix(Element el, String ns)
           
static void getPrefixes(Element element, String namespaceUri, List<String> prefixes)
          Get all prefixes defined on this element for the specified namespace.
static void getPrefixesRecursive(Element element, String namespaceUri, List<String> prefixes)
          Get all prefixes defined, up to the root, for a namespace URI.
static String getPrefixRecursive(Element el, String ns)
           
static String getRawContent(Node n)
          Get the raw text content of a node or null if there is no text
static boolean hasAttribute(Element element, String value)
           
static boolean hasElementInNS(Element el, String namespace)
           
static boolean hasElementWithName(Element el, String nameSpaceURI, String localName)
           
static Document newDocument()
          Creates a new Document object
static void removeAttribute(Node node, String attName)
           
static void setAttribute(Node node, String attName, String val)
           
static String setNamespace(Element element, String namespace, String prefix)
          Set a namespace/prefix on an element if it is not set already.
static void setText(Node node, String val)
          Set or replace the text value
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

newDocument

public static Document newDocument()
Creates a new Document object

Throws:
ParserConfigurationException

createDocument

public static Document createDocument()

getAttributeValueEmptyNull

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

Parameters:
e -
attributeName -

getAllContent

public static String getAllContent(Node n)
Get the text content of a node and all it's children or null if there is no text


getContent

public static String getContent(Node n)
Get the trimmed text content of a node or null if there is no text


getRawContent

public static String getRawContent(Node n)
Get the raw text content of a node or null if there is no text


getChild

public static Node getChild(Node parent,
                            String name)
Get the first element child.

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

hasAttribute

public static boolean hasAttribute(Element element,
                                   String value)

getAttribute

public static String getAttribute(Node element,
                                  String attName)

getAttribute

public static String getAttribute(Element element,
                                  QName attName)

setAttribute

public static void setAttribute(Node node,
                                String attName,
                                String val)

removeAttribute

public static void removeAttribute(Node node,
                                   String attName)

setText

public static void setText(Node node,
                           String val)
Set or replace the text value


findChildWithAtt

public static Element findChildWithAtt(Node parent,
                                       String elemName,
                                       String attName,
                                       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 String getChildContent(Node parent,
                                     String name)
Get the first child's content ( ie it's included TEXT node ).


getElementQName

public static QName getElementQName(Element el)

createQName

public static QName createQName(String qualifiedName,
                                Node node)
Creates a QName object based on the qualified name and using the Node as a base to lookup the namespace for the prefix

Parameters:
qualifiedName -
node -

convertStringToQName

public static QName convertStringToQName(String expandedQName)

convertStringToQName

public static QName convertStringToQName(String expandedQName,
                                         String prefix)

convertStringsToQNames

public static Set<QName> convertStringsToQNames(List<String> expandedQNames)

getFirstElement

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


getNextElement

public static Element getNextElement(Element el)

getFirstChildWithName

public static Element getFirstChildWithName(Element parent,
                                            QName q)
Return the first element child with the specified qualified name.

Parameters:
parent -
q -

getFirstChildWithName

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

Parameters:
parent -
ns -
lp -

getChildrenWithName

public static List<Element> getChildrenWithName(Element parent,
                                                String ns,
                                                String localName)
Return child elements with specified name.

Parameters:
parent -
ns -
localName -

getChildrenWithNamespace

public static List<Element> getChildrenWithNamespace(Element parent,
                                                     String ns)
Returns all child elements with specified namespace.

Parameters:
parent - the element to search under
ns - the namespace to find elements in
Returns:
all child elements with specified namespace

getChild

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

Parameters:
parent -
type -

getNext

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


getNext

public static Node getNext(Node current,
                           String name,
                           int type)
Return the next sibling with a given name and type


getPrefixRecursive

public static String getPrefixRecursive(Element el,
                                        String ns)

getPrefix

public static String getPrefix(Element el,
                               String ns)

getPrefixesRecursive

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

Parameters:
element -
namespaceUri -
prefixes -

getPrefixes

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

Parameters:
element -
namespaceUri -
prefixes -

createNamespace

public static String createNamespace(Element el,
                                     String ns)

getNamespace

public static String getNamespace(Node node,
                                  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 List<Element> findAllElementsByTagNameNS(Element elem,
                                                       String nameSpaceURI,
                                                       String localName)

findAllElementsByTagName

public static List<Element> findAllElementsByTagName(Element elem,
                                                     String tagName)

hasElementWithName

public static boolean hasElementWithName(Element el,
                                         String nameSpaceURI,
                                         String localName)

hasElementInNS

public static boolean hasElementInNS(Element el,
                                     String namespace)

setNamespace

public static String setNamespace(Element element,
                                  String namespace,
                                  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(Element element,
                                      String namespaceUri,
                                      String prefix)
Add a namespace prefix definition to an element.

Parameters:
element -
namespaceUri -
prefix -

Apache CXF API

Apache CXF