Package org.apache.wss4j.common.util
Class XMLUtils
- java.lang.Object
-
- org.apache.wss4j.common.util.XMLUtils
-
public final class XMLUtils extends Object
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
elementToStream(Element element, OutputStream out)
static Element
findElement(Node startNode, String name, String namespace)
Returns the first element that matchesname
andnamespace
.static Element
findElementById(Node startNode, String value, boolean checkMultipleElements)
Returns the single element that contains an Id with valueuri
andnamespace
.static List<Element>
findElements(Node startNode, String name, String namespace)
Returns all elements that matchname
andnamespace
.static Element
findSAMLAssertionElementById(Node startNode, String value)
Returns the single SAMLAssertion element that contains an AssertionID/ID that matches the supplied parameter.static Element
getDirectChildElement(Node parentNode, String localName, String namespace)
Gets a direct child with specified localname and namespace.static String
getElementText(Element e)
Return the text content of an Element, or null if no such text content existsstatic String
getIDFromReference(String ref)
Turn a reference (eg "#5") into an ID (eg "5").static InputSource
getInputSourceFromURI(String uri)
Utility to get the bytes uri.static String
getNamespace(String prefix, Node e)
static String
getPrefixNS(String uri, Node e)
static String
prettyDocumentToString(Document doc)
static String
setNamespace(Element element, String namespace, String prefix)
Set a namespace/prefix on an element if it is not set already.static InputSource
sourceToInputSource(Source source)
Utility to get the bytes uri
-
-
-
Field Detail
-
XMLNS_NS
public static final String XMLNS_NS
- See Also:
- Constant Field Values
-
XML_NS
public static final String XML_NS
- See Also:
- Constant Field Values
-
WSU_NS
public static final String WSU_NS
- See Also:
- Constant Field Values
-
-
Method Detail
-
getDirectChildElement
public static Element getDirectChildElement(Node parentNode, String localName, String namespace)
Gets a direct child with specified localname and namespace.- Parameters:
parentNode
- the node where to start the searchlocalName
- local name of the child to getnamespace
- the namespace of the child to get- Returns:
- the node or
null
if not such node found
-
getElementText
public static String getElementText(Element e)
Return the text content of an Element, or null if no such text content exists
-
prettyDocumentToString
public static String prettyDocumentToString(Document doc) throws IOException, TransformerException
- Throws:
IOException
TransformerException
-
elementToStream
public static void elementToStream(Element element, OutputStream out) throws TransformerException
- Throws:
TransformerException
-
sourceToInputSource
public static InputSource sourceToInputSource(Source source) throws IOException, TransformerException
Utility to get the bytes uri- Parameters:
source
- the resource to get- Throws:
IOException
TransformerException
-
getInputSourceFromURI
public static InputSource getInputSourceFromURI(String uri)
Utility to get the bytes uri. Does NOT handle authenticated URLs, use getInputSourceFromURI(uri, username, password)- Parameters:
uri
- the resource to get
-
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
-
getIDFromReference
public static String getIDFromReference(String ref)
Turn a reference (eg "#5") into an ID (eg "5").- Parameters:
ref
-- Returns:
- ref trimmed and with the leading "#" removed, or null if not correctly formed
-
findElementById
public static Element findElementById(Node startNode, String value, boolean checkMultipleElements)
Returns the single element that contains an Id with valueuri
andnamespace
. The Id can be either a wsu:Id or an Id with no namespace. This is a replacement for a XPath Id lookup with the given namespace. It's somewhat faster than XPath, and we do not deal with prefixes, just with the real namespace URI If checkMultipleElements is true and there are multiple elements, we LOG.a warning and return null as this can be used to get around the signature checking.- Parameters:
startNode
- Where to start the searchvalue
- Value of the Id attributecheckMultipleElements
- If true then go through the entire tree and return null if there are multiple elements with the same Id- Returns:
- The found element if there was exactly one match, or
null
otherwise
-
findElement
public static Element findElement(Node startNode, String name, String namespace)
Returns the first element that matchesname
andnamespace
. This is a replacement for a XPath lookup//name
with the given namespace. It's somewhat faster than XPath, and we do not deal with prefixes, just with the real namespace URI- Parameters:
startNode
- Where to start the searchname
- Local name of the elementnamespace
- Namespace URI of the element- Returns:
- The found element or
null
-
findElements
public static List<Element> findElements(Node startNode, String name, String namespace)
Returns all elements that matchname
andnamespace
. This is a replacement for a XPath lookup//name
with the given namespace. It's somewhat faster than XPath, and we do not deal with prefixes, just with the real namespace URI- Parameters:
startNode
- Where to start the searchname
- Local name of the elementnamespace
- Namespace URI of the element- Returns:
- The found elements (or an empty list)
-
findSAMLAssertionElementById
public static Element findSAMLAssertionElementById(Node startNode, String value)
Returns the single SAMLAssertion element that contains an AssertionID/ID that matches the supplied parameter.- Parameters:
startNode
- Where to start the searchvalue
- Value of the AssertionID/ID attribute- Returns:
- The found element if there was exactly one match, or
null
otherwise
-
-