Class DOMUtils
- java.lang.Object
-
- org.apache.jcp.xml.dsig.internal.dom.DOMUtils
-
public final class DOMUtils extends java.lang.Object
Useful static DOM utility methods.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
DOMUtils.NodeSet
-
Constructor Summary
Constructors Modifier Constructor Description private
DOMUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static void
appendChild(org.w3c.dom.Node parent, org.w3c.dom.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 org.w3c.dom.Element
createElement(org.w3c.dom.Document doc, java.lang.String tag, java.lang.String nsURI, java.lang.String prefix)
Creates an element in the specified namespace, with the specified tag and namespace prefix.static java.lang.String
getAttributeValue(org.w3c.dom.Element elem, java.lang.String name)
Returns the attribute value for the attribute with the specified name.static org.w3c.dom.Element
getFirstChildElement(org.w3c.dom.Node node)
Returns the first child element of the specified node, or null if there is no such element.static org.w3c.dom.Element
getFirstChildElement(org.w3c.dom.Node node, java.lang.String localName)
Deprecated.static org.w3c.dom.Element
getFirstChildElement(org.w3c.dom.Node node, java.lang.String localName, java.lang.String namespaceURI)
Returns the first child element of the specified node and checks that the local name is equal tolocalName
and the namespace is equal tonamespaceURI
static <N> java.lang.String
getIdAttributeValue(org.w3c.dom.Element elem, java.lang.String name)
Returns the attribute value for the attribute with the specified name.static org.w3c.dom.Element
getLastChildElement(org.w3c.dom.Node node)
Returns the last child element of the specified node, or null if there is no such element.static org.w3c.dom.Element
getNextSiblingElement(org.w3c.dom.Node node)
Returns the next sibling element of the specified node, or null if there is no such element.static org.w3c.dom.Element
getNextSiblingElement(org.w3c.dom.Node node, java.lang.String localName)
Deprecated.static org.w3c.dom.Element
getNextSiblingElement(org.w3c.dom.Node node, java.lang.String localName, java.lang.String namespaceURI)
Returns the next sibling element of the specified node and checks that the local name is equal tolocalName
and the namespace is equal tonamespaceURI
static java.lang.String
getNSPrefix(javax.xml.crypto.XMLCryptoContext context, java.lang.String nsURI)
Returns the prefix associated with the specified namespace URIstatic org.w3c.dom.Document
getOwnerDocument(org.w3c.dom.Node node)
Returns the owner document of the specified node.static java.lang.String
getQNameString(java.lang.String prefix, java.lang.String localName)
Create a QName string from a prefix and local name.static java.lang.String
getSignaturePrefix(javax.xml.crypto.XMLCryptoContext context)
Returns the prefix associated with the XML Signature namespace URIstatic boolean
isNamespace(org.w3c.dom.Node node)
static boolean
nodesEqual(org.w3c.dom.Node thisNode, org.w3c.dom.Node otherNode)
Compares 2 nodes for equality.static java.util.Set<org.w3c.dom.Node>
nodeSet(org.w3c.dom.NodeList nl)
Returns a Set ofNode
s, backed by the specifiedNodeList
.static boolean
paramsEqual(java.security.spec.AlgorithmParameterSpec spec1, java.security.spec.AlgorithmParameterSpec spec2)
private static boolean
paramsEqual(javax.xml.crypto.dsig.spec.ExcC14NParameterSpec spec1, javax.xml.crypto.dsig.spec.ExcC14NParameterSpec spec2)
private static boolean
paramsEqual(javax.xml.crypto.dsig.spec.XPathFilter2ParameterSpec spec1, javax.xml.crypto.dsig.spec.XPathFilter2ParameterSpec spec2)
private static boolean
paramsEqual(javax.xml.crypto.dsig.spec.XPathFilterParameterSpec spec1, javax.xml.crypto.dsig.spec.XPathFilterParameterSpec spec2)
private static boolean
paramsEqual(javax.xml.crypto.dsig.spec.XSLTTransformParameterSpec spec1, javax.xml.crypto.dsig.spec.XSLTTransformParameterSpec spec2)
static void
removeAllChildren(org.w3c.dom.Node node)
Removes all children nodes from the specified node.static void
setAttribute(org.w3c.dom.Element elem, java.lang.String name, java.lang.String value)
Sets an element's attribute (using DOM level 2) with the specified value and namespace prefix.static void
setAttributeID(org.w3c.dom.Element elem, java.lang.String name, java.lang.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.private static org.w3c.dom.Element
verifyElement(org.w3c.dom.Element elem, java.lang.String localName)
private static org.w3c.dom.Element
verifyElement(org.w3c.dom.Element elem, java.lang.String localName, java.lang.String namespaceURI)
-
-
-
Method Detail
-
getOwnerDocument
public static org.w3c.dom.Document getOwnerDocument(org.w3c.dom.Node node)
Returns the owner document of the specified node.- Parameters:
node
- the node- Returns:
- the owner document
-
getQNameString
public static java.lang.String getQNameString(java.lang.String prefix, java.lang.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 org.w3c.dom.Element createElement(org.w3c.dom.Document doc, java.lang.String tag, java.lang.String nsURI, java.lang.String prefix)
Creates an element in the specified namespace, with the specified tag and namespace prefix.- Parameters:
doc
- the owner documenttag
- the tagnsURI
- the namespace URIprefix
- the namespace prefix- Returns:
- the newly created element
-
setAttribute
public static void setAttribute(org.w3c.dom.Element elem, java.lang.String name, java.lang.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 onname
- the name of the attributevalue
- the attribute value. If null, no attribute is set.
-
setAttributeID
public static void setAttributeID(org.w3c.dom.Element elem, java.lang.String name, java.lang.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 onname
- the name of the attributevalue
- the attribute value. If null, no attribute is set.
-
getFirstChildElement
public static org.w3c.dom.Element getFirstChildElement(org.w3c.dom.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:
java.lang.NullPointerException
- ifnode == null
-
getFirstChildElement
@Deprecated public static org.w3c.dom.Element getFirstChildElement(org.w3c.dom.Node node, java.lang.String localName) throws javax.xml.crypto.MarshalException
Deprecated.Returns the first child element of the specified node and checks that the local name is equal tolocalName
.- Parameters:
node
- the node- Returns:
- the first child element of the specified node
- Throws:
java.lang.NullPointerException
- ifnode == null
javax.xml.crypto.MarshalException
- if no such element or the local name is not equal tolocalName
-
getFirstChildElement
public static org.w3c.dom.Element getFirstChildElement(org.w3c.dom.Node node, java.lang.String localName, java.lang.String namespaceURI) throws javax.xml.crypto.MarshalException
Returns the first child element of the specified node and checks that the local name is equal tolocalName
and the namespace is equal tonamespaceURI
- Parameters:
node
- the node- Returns:
- the first child element of the specified node
- Throws:
java.lang.NullPointerException
- ifnode == null
javax.xml.crypto.MarshalException
- if no such element or the local name is not equal tolocalName
-
verifyElement
private static org.w3c.dom.Element verifyElement(org.w3c.dom.Element elem, java.lang.String localName) throws javax.xml.crypto.MarshalException
- Throws:
javax.xml.crypto.MarshalException
-
verifyElement
private static org.w3c.dom.Element verifyElement(org.w3c.dom.Element elem, java.lang.String localName, java.lang.String namespaceURI) throws javax.xml.crypto.MarshalException
- Throws:
javax.xml.crypto.MarshalException
-
getLastChildElement
public static org.w3c.dom.Element getLastChildElement(org.w3c.dom.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:
java.lang.NullPointerException
- ifnode == null
-
getNextSiblingElement
public static org.w3c.dom.Element getNextSiblingElement(org.w3c.dom.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:
java.lang.NullPointerException
- ifnode == null
-
getNextSiblingElement
@Deprecated public static org.w3c.dom.Element getNextSiblingElement(org.w3c.dom.Node node, java.lang.String localName) throws javax.xml.crypto.MarshalException
Deprecated.Returns the next sibling element of the specified node and checks that the local name is equal tolocalName
.- Parameters:
node
- the node- Returns:
- the next sibling element of the specified node
- Throws:
java.lang.NullPointerException
- ifnode == null
javax.xml.crypto.MarshalException
- if no such element or the local name is not equal tolocalName
-
getNextSiblingElement
public static org.w3c.dom.Element getNextSiblingElement(org.w3c.dom.Node node, java.lang.String localName, java.lang.String namespaceURI) throws javax.xml.crypto.MarshalException
Returns the next sibling element of the specified node and checks that the local name is equal tolocalName
and the namespace is equal tonamespaceURI
- Parameters:
node
- the node- Returns:
- the next sibling element of the specified node
- Throws:
java.lang.NullPointerException
- ifnode == null
javax.xml.crypto.MarshalException
- if no such element or the local name is not equal tolocalName
-
getAttributeValue
public static java.lang.String getAttributeValue(org.w3c.dom.Element elem, java.lang.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 attributename
- the name of the attribute- Returns:
- the attribute value (may be null if unspecified)
-
getIdAttributeValue
public static <N> java.lang.String getIdAttributeValue(org.w3c.dom.Element elem, java.lang.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 attributename
- the name of the attribute- Returns:
- the attribute value (may be null if unspecified)
-
nodeSet
public static java.util.Set<org.w3c.dom.Node> nodeSet(org.w3c.dom.NodeList nl)
Returns a Set ofNode
s, backed by the specifiedNodeList
.- Parameters:
nl
- the NodeList- Returns:
- a Set of Nodes
-
getNSPrefix
public static java.lang.String getNSPrefix(javax.xml.crypto.XMLCryptoContext context, java.lang.String nsURI)
Returns the prefix associated with the specified namespace URI- Parameters:
context
- contains the namespace mapnsURI
- the namespace URI- Returns:
- the prefix associated with the specified namespace URI, or null if not set
-
getSignaturePrefix
public static java.lang.String getSignaturePrefix(javax.xml.crypto.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(org.w3c.dom.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(org.w3c.dom.Node thisNode, org.w3c.dom.Node otherNode)
Compares 2 nodes for equality. Implementation is not complete.
-
appendChild
public static void appendChild(org.w3c.dom.Node parent, org.w3c.dom.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(java.security.spec.AlgorithmParameterSpec spec1, java.security.spec.AlgorithmParameterSpec spec2)
-
paramsEqual
private static boolean paramsEqual(javax.xml.crypto.dsig.spec.XPathFilter2ParameterSpec spec1, javax.xml.crypto.dsig.spec.XPathFilter2ParameterSpec spec2)
-
paramsEqual
private static boolean paramsEqual(javax.xml.crypto.dsig.spec.ExcC14NParameterSpec spec1, javax.xml.crypto.dsig.spec.ExcC14NParameterSpec spec2)
-
paramsEqual
private static boolean paramsEqual(javax.xml.crypto.dsig.spec.XPathFilterParameterSpec spec1, javax.xml.crypto.dsig.spec.XPathFilterParameterSpec spec2)
-
paramsEqual
private static boolean paramsEqual(javax.xml.crypto.dsig.spec.XSLTTransformParameterSpec spec1, javax.xml.crypto.dsig.spec.XSLTTransformParameterSpec spec2)
-
isNamespace
public static boolean isNamespace(org.w3c.dom.Node node)
-
-