APIdia Launch Partner
Top Description Fields Constructors Methods
com.sun.org.apache.xml.internal.security.utils

public final Class XMLUtils

extends Object
Class Inheritance
Imports
java.io.BufferedInputStream, .BufferedOutputStream, .File, .IOException, .InputStream, .OutputStream, java.lang.reflect.InvocationTargetException, java.math.BigInteger, java.nio.file.Files, java.security.AccessController, .PrivilegedAction, java.util.ArrayList, .Base64, .HashSet, .List, .Set, java.util.stream.Collectors, com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException, .Canonicalizer, .InvalidCanonicalizerException, com.sun.org.apache.xml.internal.security.parser.XMLParser, .XMLParserException, .XMLParserImpl, com.sun.org.slf4j.internal.Logger, .LoggerFactory, org.w3c.dom.Attr, .Document, .Element, .NamedNodeMap, .Node, .NodeList, .Text

DOM and XML accessibility and comfort functions.

Field Summary

Modifier and TypeField and Description
private static volatile String
private static volatile String
private static boolean
private static final Logger
private static volatile String
private static volatile String
private static XMLParser

Constructor Summary

AccessConstructor and Description
private
XMLUtils()

Constructor XMLUtils

Method Summary

Modifier and TypeMethod and Description
public static void
public static void
addReturnToElement(Element e)

Method addReturnToElement

public static void
public static void
circumventBug2650(Document doc)

This method spreads all namespace attributes in a DOM document to their children.

private static void
circumventBug2650internal(Node node)

This is the work horse for circumventBug2650.

public static Set<Node>

Returns:

the set with the nodelist
convertNodelistToSet
(NodeList xpathNodeSet)

Method convertNodelistToSet

public static Element

Returns:

the Element
createElementInEncryption11Space
(Document
the factory Document
doc
,
String
the local name of the Element
elementName
)

Creates an Element in the XML Encryption 1.1 specification namespace.

public static Element

Returns:

the Element
createElementInEncryptionSpace
(Document
the factory Document
doc
,
String
the local name of the Element
elementName
)

Creates an Element in the XML Encryption specification namespace.

public static Element

Returns:

the Element
createElementInSignature11Space
(Document
the factory Document
doc
,
String
the local name of the Element
elementName
)

Creates an Element in the XML Signature 1.1 specification namespace.

public static Element

Returns:

the Element
createElementInSignatureSpace
(Document
the factory Document
doc
,
String
the local name of the Element
elementName
)

Creates an Element in the XML Signature specification namespace.

public static byte[]
decode(String encodedString)

public static byte[]
decode(byte[] encodedBytes)

public static boolean

Returns:

true if the element is in XML Encryption 1.1 namespace and the local name equals the supplied one
elementIsInEncryption11Space
(Element element, String localName)

Returns true if the element is in XML Encryption 1.1 namespace and the local name equals the supplied one.

public static boolean

Returns:

true if the element is in XML Encryption namespace and the local name equals the supplied one
elementIsInEncryptionSpace
(Element element, String localName)

Returns true if the element is in XML Encryption namespace and the local name equals the supplied one.

public static boolean

Returns:

true if the element is in XML Signature namespace and the local name equals the supplied one
elementIsInSignature11Space
(Element element, String localName)

Returns true if the element is in XML Signature 1.1 namespace and the local name equals the supplied one.

public static boolean

Returns:

true if the element is in XML Signature namespace and the local name equals the supplied one
elementIsInSignatureSpace
(Element element, String localName)

Returns true if the element is in XML Signature namespace and the local name equals the supplied one.

public static String
encodeToString(byte[] bytes)

public static Set<Node>
excludeNodeFromSet(Node signatureElement, Set<Node> inputSet)

Returns: nodes with the constrain

public static byte[]

Returns:

a byte array with bitlen bits of big
getBytes
(BigInteger
BigInteger to be converted
big
,
int
int the desired length in bits of the representation
bitlen
)

Returns a byte-array representation of a {@link BigInteger}.

public static String

Returns:

the string of children
getFullTextChildrenFromNode
(Node node)

Method getFullTextChildrenFromNode

public static Element
public static Document

Returns:

the owner document of the node
getOwnerDocument
(Node node)

This method returns the owner document of a particular node.

public static Document

Returns:

the owner document
getOwnerDocument
(Set<Node> xpathNodeSet)

This method returns the first non-null owner document of the Nodes in this Set.

public static void
getSet(Node rootNode, Set<Node> result, Node exclude, boolean
whether comments or not
comments
)

private static void
getSetRec(final Node rootNode, final Set<Node> result, final Node exclude, final boolean comments)

public static String

Returns:

the string for the node.
getStrFromNode
(Node xpathnode)

Method getStrFromNode

public static boolean
public static boolean

Returns:

true if the node is descendant
isDescendantOrSelf
(Node ctx, Node descendantOrSelf)

Returns true if the descendantOrSelf is on the descendant-or-self axis of the context node.

public static boolean
public static void
outputDOM(Node
root node of the DOM tree
contextNode
,
File
the file to write to
outputFile
)

Outputs a DOM tree to a File.

public static void
outputDOM(Node
root node of the DOM tree
contextNode
,
OutputStream os)

Outputs a DOM tree to an OutputStream.

public static void
outputDOM(Node
root node of the DOM tree
contextNode
,
OutputStream os, boolean addPreamble)

Outputs a DOM tree to an OutputStream.

public static void
outputDOMc14nWithComments(Node contextNode, OutputStream os)

Serializes the contextNode into the OutputStream, but suppresses all Exceptions.

public static boolean
protectAgainstWrappingAttack(Node startNode, String value)

This method is a tree-search to help prevent against wrapping attacks.

public static boolean
protectAgainstWrappingAttack(Node startNode, Element knownElement, String value)

This method is a tree-search to help prevent against wrapping attacks.

public static Document

Returns:

Document
read
(File file, boolean disallowDocTypeDeclarations)

Reads a document from the input stream.

public static Document

Returns:

Document
read
(InputStream inputStream, boolean disallowDocTypeDeclarations)

Reads a document from the input stream.

public static Document

Returns:

Document
readResource
(String
- resource name to be opened by the class loader
name
,
ClassLoader loader, boolean disallowDocTypeDeclarations)

Reads a document from the input stream and closes it.

public static Element
selectDs11Node(Node sibling, String nodeName, int number)

Returns: nodes with the constraint

public static Element[]
selectDs11Nodes(Node sibling, String nodeName)

Returns: nodes with the constrain

public static Element
selectDsNode(Node sibling, String nodeName, int number)

Returns: nodes with the constraint

public static Element[]
selectDsNodes(Node sibling, String nodeName)

Returns: nodes with the constrain

public static Element
selectNode(Node sibling, String uri, String nodeName, int number)

Returns: nodes with the constrain

public static Element[]
selectNodes(Node sibling, String uri, String nodeName)

Returns: nodes with the constraint

public static Element
selectXencNode(Node sibling, String nodeName, int number)

Returns: nodes with the constrain

public static void
setDs11Prefix(String
the new prefix for the digital signature 1.1 namespace
prefix
)

Set the prefix for the digital signature 1.1 namespace

public static void
setDsPrefix(String
the new prefix for the digital signature namespace
prefix
)

Set the prefix for the digital signature namespace

public static void
setXenc11Prefix(String
the new prefix for the encryption namespace 1.1
prefix
)

Set the prefix for the encryption namespace 1.1

public static void
setXencPrefix(String
the new prefix for the encryption namespace
prefix
)

Set the prefix for the encryption namespace

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ds11Prefixback to summary
private static volatile String ds11Prefix
dsPrefixback to summary
private static volatile String dsPrefix
ignoreLineBreaksback to summary
private static boolean ignoreLineBreaks
Annotations
@SuppressWarnings:removal
LOGback to summary
private static final Logger LOG
xenc11Prefixback to summary
private static volatile String xenc11Prefix
xencPrefixback to summary
private static volatile String xencPrefix
xmlParserImplback to summary
private static XMLParser xmlParserImpl
Annotations
@SuppressWarnings:removal

Constructor Detail

XMLUtilsback to summary
private XMLUtils()

Constructor XMLUtils

Method Detail

addReturnBeforeChildback to summary
public static void addReturnBeforeChild(Element e, Node child)
addReturnToElementback to summary
public static void addReturnToElement(Element e)

Method addReturnToElement

addReturnToElementback to summary
public static void addReturnToElement(Document doc, HelperNodeList nl)
circumventBug2650back to summary
public static void circumventBug2650(Document doc)

This method spreads all namespace attributes in a DOM document to their children. This is needed because the XML Signature XPath transform must evaluate the XPath against all nodes in the input, even against XPath namespace nodes. Through a bug in XalanJ2, the namespace nodes are not fully visible in the Xalan XPath model, so we have to do this by hand in DOM spaces so that the nodes become visible in XPath space.

See Also
Namespace axis resolution is not XPath compliant
circumventBug2650internalback to summary
private static void circumventBug2650internal(Node node)

This is the work horse for circumventBug2650.

Annotations
@SuppressWarnings:fallthrough
See Also
Namespace axis resolution is not XPath compliant
convertNodelistToSetback to summary
public static Set<Node> convertNodelistToSet(NodeList xpathNodeSet)

Method convertNodelistToSet

Returns:Set<Node>

the set with the nodelist

createElementInEncryption11Spaceback to summary
public static Element createElementInEncryption11Space(Document doc, String elementName)

Creates an Element in the XML Encryption 1.1 specification namespace.

Parameters
doc:Document

the factory Document

elementName:String

the local name of the Element

Returns:Element

the Element

createElementInEncryptionSpaceback to summary
public static Element createElementInEncryptionSpace(Document doc, String elementName)

Creates an Element in the XML Encryption specification namespace.

Parameters
doc:Document

the factory Document

elementName:String

the local name of the Element

Returns:Element

the Element

createElementInSignature11Spaceback to summary
public static Element createElementInSignature11Space(Document doc, String elementName)

Creates an Element in the XML Signature 1.1 specification namespace.

Parameters
doc:Document

the factory Document

elementName:String

the local name of the Element

Returns:Element

the Element

createElementInSignatureSpaceback to summary
public static Element createElementInSignatureSpace(Document doc, String elementName)

Creates an Element in the XML Signature specification namespace.

Parameters
doc:Document

the factory Document

elementName:String

the local name of the Element

Returns:Element

the Element

decodeback to summary
public static byte[] decode(String encodedString)
decodeback to summary
public static byte[] decode(byte[] encodedBytes)
elementIsInEncryption11Spaceback to summary
public static boolean elementIsInEncryption11Space(Element element, String localName)

Returns true if the element is in XML Encryption 1.1 namespace and the local name equals the supplied one.

Returns:boolean

true if the element is in XML Encryption 1.1 namespace and the local name equals the supplied one

elementIsInEncryptionSpaceback to summary
public static boolean elementIsInEncryptionSpace(Element element, String localName)

Returns true if the element is in XML Encryption namespace and the local name equals the supplied one.

Returns:boolean

true if the element is in XML Encryption namespace and the local name equals the supplied one

elementIsInSignature11Spaceback to summary
public static boolean elementIsInSignature11Space(Element element, String localName)

Returns true if the element is in XML Signature 1.1 namespace and the local name equals the supplied one.

Returns:boolean

true if the element is in XML Signature namespace and the local name equals the supplied one

elementIsInSignatureSpaceback to summary
public static boolean elementIsInSignatureSpace(Element element, String localName)

Returns true if the element is in XML Signature namespace and the local name equals the supplied one.

Returns:boolean

true if the element is in XML Signature namespace and the local name equals the supplied one

encodeToStringback to summary
public static String encodeToString(byte[] bytes)
excludeNodeFromSetback to summary
public static Set<Node> excludeNodeFromSet(Node signatureElement, Set<Node> inputSet)
Returns:Set<Node>

nodes with the constrain

getBytesback to summary
public static byte[] getBytes(BigInteger big, int bitlen)

Returns a byte-array representation of a {@link BigInteger}. No sign-bit is output

Note

{@link BigInteger}'s toByteArray returns eventually longer arrays because of the leading sign-bit.

Parameters
big:BigInteger

BigInteger to be converted

bitlen:int

int the desired length in bits of the representation

Returns:byte[]

a byte array with bitlen bits of big

getFullTextChildrenFromNodeback to summary
public static String getFullTextChildrenFromNode(Node node)

Method getFullTextChildrenFromNode

Returns:String

the string of children

getNextElementback to summary
public static Element getNextElement(Node el)
getOwnerDocumentback to summary
public static Document getOwnerDocument(Node node)

This method returns the owner document of a particular node. This method is necessary because it always returns a Document. Node#getOwnerDocument returns null if the Node is a Document.

Returns:Document

the owner document of the node

getOwnerDocumentback to summary
public static Document getOwnerDocument(Set<Node> xpathNodeSet)

This method returns the first non-null owner document of the Nodes in this Set. This method is necessary because it always returns a Document. Node#getOwnerDocument returns null if the Node is a Document.

Returns:Document

the owner document

getSetback to summary
public static void getSet(Node rootNode, Set<Node> result, Node exclude, boolean comments)
Parameters
comments:boolean

whether comments or not

getSetRecback to summary
private static void getSetRec(final Node rootNode, final Set<Node> result, final Node exclude, final boolean comments)
Annotations
@SuppressWarnings:fallthrough
getStrFromNodeback to summary
public static String getStrFromNode(Node xpathnode)

Method getStrFromNode

Returns:String

the string for the node.

ignoreLineBreaksback to summary
public static boolean ignoreLineBreaks()
isDescendantOrSelfback to summary
public static boolean isDescendantOrSelf(Node ctx, Node descendantOrSelf)

Returns true if the descendantOrSelf is on the descendant-or-self axis of the context node.

Returns:boolean

true if the node is descendant

isIgnoreLineBreaksback to summary
public static boolean isIgnoreLineBreaks()
outputDOMback to summary
public static void outputDOM(Node contextNode, File outputFile) throws IOException

Outputs a DOM tree to a File.

Parameters
contextNode:Node

root node of the DOM tree

outputFile:File

the file to write to

outputDOMback to summary
public static void outputDOM(Node contextNode, OutputStream os)

Outputs a DOM tree to an OutputStream.

Parameters
contextNode:Node

root node of the DOM tree

os:OutputStream

the OutputStream

outputDOMback to summary
public static void outputDOM(Node contextNode, OutputStream os, boolean addPreamble)

Outputs a DOM tree to an OutputStream. If an Exception is thrown during execution, it's StackTrace is output to System.out, but the Exception is not re-thrown.

Parameters
contextNode:Node

root node of the DOM tree

os:OutputStream

the OutputStream

outputDOMc14nWithCommentsback to summary
public static void outputDOMc14nWithComments(Node contextNode, OutputStream os)

Serializes the contextNode into the OutputStream, but suppresses all Exceptions.

NOTE: This should only be used for debugging purposes, NOT in a production environment; this method ignores all exceptions, so you won't notice if something goes wrong. If you're asking what is to be used in a production environment, simply use the code inside the try{} statement, but handle the Exceptions appropriately.
protectAgainstWrappingAttackback to summary
public static boolean protectAgainstWrappingAttack(Node startNode, String value)

This method is a tree-search to help prevent against wrapping attacks. It checks that no two Elements have ID Attributes that match the "value" argument, if this is the case then "false" is returned. Note that a return value of "true" does not necessarily mean that a matching Element has been found, just that no wrapping attack has been detected.

protectAgainstWrappingAttackback to summary
public static boolean protectAgainstWrappingAttack(Node startNode, Element knownElement, String value)

This method is a tree-search to help prevent against wrapping attacks. It checks that no other Element than the given "knownElement" argument has an ID attribute that matches the "value" argument, which is the ID value of "knownElement". If this is the case then "false" is returned.

readback to summary
public static Document read(File file, boolean disallowDocTypeDeclarations) throws XMLParserException, IOException

Reads a document from the input stream.

Returns:Document

Document

readback to summary
public static Document read(InputStream inputStream, boolean disallowDocTypeDeclarations) throws XMLParserException

Reads a document from the input stream. Caller is responsible for closing the stream.

Returns:Document

Document

readResourceback to summary
public static Document readResource(String name, ClassLoader loader, boolean disallowDocTypeDeclarations) throws XMLParserException, IOException

Reads a document from the input stream and closes it.

Parameters
name:String

- resource name to be opened by the class loader

Returns:Document

Document

Exceptions
IOException:
inputStream.close() failed.
selectDs11Nodeback to summary
public static Element selectDs11Node(Node sibling, String nodeName, int number)
Returns:Element

nodes with the constraint

selectDs11Nodesback to summary
public static Element[] selectDs11Nodes(Node sibling, String nodeName)
Returns:Element[]

nodes with the constrain

selectDsNodeback to summary
public static Element selectDsNode(Node sibling, String nodeName, int number)
Returns:Element

nodes with the constraint

selectDsNodesback to summary
public static Element[] selectDsNodes(Node sibling, String nodeName)
Returns:Element[]

nodes with the constrain

selectNodeback to summary
public static Element selectNode(Node sibling, String uri, String nodeName, int number)
Returns:Element

nodes with the constrain

selectNodesback to summary
public static Element[] selectNodes(Node sibling, String uri, String nodeName)
Returns:Element[]

nodes with the constraint

selectXencNodeback to summary
public static Element selectXencNode(Node sibling, String nodeName, int number)
Returns:Element

nodes with the constrain

setDs11Prefixback to summary
public static void setDs11Prefix(String prefix)

Set the prefix for the digital signature 1.1 namespace

Parameters
prefix:String

the new prefix for the digital signature 1.1 namespace

Exceptions
SecurityException:
if a security manager is installed and the caller does not have permission to set the prefix
setDsPrefixback to summary
public static void setDsPrefix(String prefix)

Set the prefix for the digital signature namespace

Parameters
prefix:String

the new prefix for the digital signature namespace

Exceptions
SecurityException:
if a security manager is installed and the caller does not have permission to set the prefix
setXenc11Prefixback to summary
public static void setXenc11Prefix(String prefix)

Set the prefix for the encryption namespace 1.1

Parameters
prefix:String

the new prefix for the encryption namespace 1.1

Exceptions
SecurityException:
if a security manager is installed and the caller does not have permission to set the prefix
setXencPrefixback to summary
public static void setXencPrefix(String prefix)

Set the prefix for the encryption namespace

Parameters
prefix:String

the new prefix for the encryption namespace

Exceptions
SecurityException:
if a security manager is installed and the caller does not have permission to set the prefix