org.apache.jcp.xml.dsig.internal.dom
Class DOMUtils

java.lang.Object
  extended by org.apache.jcp.xml.dsig.internal.dom.DOMUtils

public final class DOMUtils
extends Object

Useful static DOM utility methods.

Author:
Sean Mullan

Method Summary
static void appendChild(Node parent, Node child)
          Checks if child element has same owner document before appending to the parent, and imports it to the parent's document if necessary.
static Element createElement(Document doc, String tag, String nsURI, String prefix)
          Creates an element in the specified namespace, with the specified tag and namespace prefix.
static String getAttributeValue(Element elem, String name)
          Returns the attribute value for the attribute with the specified name.
static Element getFirstChildElement(Node node)
          Returns the first child element of the specified node, or null if there is no such element.
static Element getFirstChildElement(Node node, String localName)
          Deprecated. 
static Element getFirstChildElement(Node node, String localName, String namespaceURI)
          Returns the first child element of the specified node and checks that the local name is equal to localName and the namespace is equal to namespaceURI
static
<N> String
getIdAttributeValue(Element elem, String name)
          Returns the attribute value for the attribute with the specified name.
static Element getLastChildElement(Node node)
          Returns the last child element of the specified node, or null if there is no such element.
static Element getNextSiblingElement(Node node)
          Returns the next sibling element of the specified node, or null if there is no such element.
static Element getNextSiblingElement(Node node, String localName)
          Deprecated. 
static Element getNextSiblingElement(Node node, String localName, String namespaceURI)
          Returns the next sibling element of the specified node and checks that the local name is equal to localName and the namespace is equal to namespaceURI
static String getNSPrefix(XMLCryptoContext context, String nsURI)
          Returns the prefix associated with the specified namespace URI
static Document getOwnerDocument(Node node)
          Returns the owner document of the specified node.
static String getQNameString(String prefix, String localName)
          Create a QName string from a prefix and local name.
static String getSignaturePrefix(XMLCryptoContext context)
          Returns the prefix associated with the XML Signature namespace URI
static boolean isNamespace(Node node)
           
static boolean nodesEqual(Node thisNode, Node otherNode)
          Compares 2 nodes for equality.
static Set<Node> nodeSet(NodeList nl)
          Returns a Set of Nodes, backed by the specified NodeList.
static boolean paramsEqual(AlgorithmParameterSpec spec1, AlgorithmParameterSpec spec2)
           
static void removeAllChildren(Node node)
          Removes all children nodes from the specified node.
static void setAttribute(Element elem, String name, String value)
          Sets an element's attribute (using DOM level 2) with the specified value and namespace prefix.
static void setAttributeID(Element elem, String name, String value)
          Sets an element's attribute (using DOM level 2) with the specified value and namespace prefix AND registers the ID value with the specified element.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getOwnerDocument

public static Document getOwnerDocument(Node node)
Returns the owner document of the specified node.

Parameters:
node - the node
Returns:
the owner document

getQNameString

public static String getQNameString(String prefix,
                                    String localName)
Create a QName string from a prefix and local name.

Parameters:
prefix - The prefix, if any. Can be either null or empty.
localName - The local name.
Returns:
The string for the qName, for example, "xsd:element".

createElement

public static Element createElement(Document doc,
                                    String tag,
                                    String nsURI,
                                    String prefix)
Creates an element in the specified namespace, with the specified tag and namespace prefix.

Parameters:
doc - the owner document
tag - the tag
nsURI - the namespace URI
prefix - the namespace prefix
Returns:
the newly created element

setAttribute

public static void setAttribute(Element elem,
                                String name,
                                String value)
Sets an element's attribute (using DOM level 2) with the specified value and namespace prefix.

Parameters:
elem - the element to set the attribute on
name - the name of the attribute
value - the attribute value. If null, no attribute is set.

setAttributeID

public static void setAttributeID(Element elem,
                                  String name,
                                  String value)
Sets an element's attribute (using DOM level 2) with the specified value and namespace prefix AND registers the ID value with the specified element. This is for resolving same-document ID references.

Parameters:
elem - the element to set the attribute on
name - the name of the attribute
value - the attribute value. If null, no attribute is set.

getFirstChildElement

public static Element getFirstChildElement(Node node)
Returns the first child element of the specified node, or null if there is no such element.

Parameters:
node - the node
Returns:
the first child element of the specified node, or null if there is no such element
Throws:
NullPointerException - if node == null

getFirstChildElement

@Deprecated
public static Element getFirstChildElement(Node node,
                                                      String localName)
                                    throws MarshalException
Deprecated. 

Returns the first child element of the specified node and checks that the local name is equal to localName.

Parameters:
node - the node
Returns:
the first child element of the specified node
Throws:
NullPointerException - if node == null
MarshalException - if no such element or the local name is not equal to localName

getFirstChildElement

public static Element getFirstChildElement(Node node,
                                           String localName,
                                           String namespaceURI)
                                    throws MarshalException
Returns the first child element of the specified node and checks that the local name is equal to localName and the namespace is equal to namespaceURI

Parameters:
node - the node
Returns:
the first child element of the specified node
Throws:
NullPointerException - if node == null
MarshalException - if no such element or the local name is not equal to localName

getLastChildElement

public static Element getLastChildElement(Node node)
Returns the last child element of the specified node, or null if there is no such element.

Parameters:
node - the node
Returns:
the last child element of the specified node, or null if there is no such element
Throws:
NullPointerException - if node == null

getNextSiblingElement

public static Element getNextSiblingElement(Node node)
Returns the next sibling element of the specified node, or null if there is no such element.

Parameters:
node - the node
Returns:
the next sibling element of the specified node, or null if there is no such element
Throws:
NullPointerException - if node == null

getNextSiblingElement

@Deprecated
public static Element getNextSiblingElement(Node node,
                                                       String localName)
                                     throws MarshalException
Deprecated. 

Returns the next sibling element of the specified node and checks that the local name is equal to localName.

Parameters:
node - the node
Returns:
the next sibling element of the specified node
Throws:
NullPointerException - if node == null
MarshalException - if no such element or the local name is not equal to localName

getNextSiblingElement

public static Element getNextSiblingElement(Node node,
                                            String localName,
                                            String namespaceURI)
                                     throws MarshalException
Returns the next sibling element of the specified node and checks that the local name is equal to localName and the namespace is equal to namespaceURI

Parameters:
node - the node
Returns:
the next sibling element of the specified node
Throws:
NullPointerException - if node == null
MarshalException - if no such element or the local name is not equal to localName

getAttributeValue

public static String getAttributeValue(Element elem,
                                       String name)
Returns the attribute value for the attribute with the specified name. Returns null if there is no such attribute, or the empty string if the attribute value is empty.

This works around a limitation of the DOM Element.getAttributeNode method, which does not distinguish between an unspecified attribute and an attribute with a value of "" (it returns "" for both cases).

Parameters:
elem - the element containing the attribute
name - the name of the attribute
Returns:
the attribute value (may be null if unspecified)

getIdAttributeValue

public static <N> String getIdAttributeValue(Element elem,
                                             String name)
Returns the attribute value for the attribute with the specified name. Returns null if there is no such attribute, or the empty string if the attribute value is empty.

This works around a limitation of the DOM Element.getAttributeNode method, which does not distinguish between an unspecified attribute and an attribute with a value of "" (it returns "" for both cases).

Parameters:
elem - the element containing the attribute
name - the name of the attribute
Returns:
the attribute value (may be null if unspecified)

nodeSet

public static Set<Node> nodeSet(NodeList nl)
Returns a Set of Nodes, backed by the specified NodeList.

Parameters:
nl - the NodeList
Returns:
a Set of Nodes

getNSPrefix

public static String getNSPrefix(XMLCryptoContext context,
                                 String nsURI)
Returns the prefix associated with the specified namespace URI

Parameters:
context - contains the namespace map
nsURI - the namespace URI
Returns:
the prefix associated with the specified namespace URI, or null if not set

getSignaturePrefix

public static String getSignaturePrefix(XMLCryptoContext context)
Returns the prefix associated with the XML Signature namespace URI

Parameters:
context - contains the namespace map
Returns:
the prefix associated with the specified namespace URI, or null if not set

removeAllChildren

public static void removeAllChildren(Node node)
Removes all children nodes from the specified node.

Parameters:
node - the parent node whose children are to be removed

nodesEqual

public static boolean nodesEqual(Node thisNode,
                                 Node otherNode)
Compares 2 nodes for equality. Implementation is not complete.


appendChild

public static void appendChild(Node parent,
                               Node child)
Checks if child element has same owner document before appending to the parent, and imports it to the parent's document if necessary.


paramsEqual

public static boolean paramsEqual(AlgorithmParameterSpec spec1,
                                  AlgorithmParameterSpec spec2)

isNamespace

public static boolean isNamespace(Node node)


Copyright © 2000–2014 The Apache Software Foundation. All rights reserved.