public class XmlUtil
extends java.lang.Object
| Modifier and Type | Class and Description |
|---|---|
static class |
XmlUtil.SailPointEntityResolver
Custom entity resolver that handles different locations.
|
static class |
XmlUtil.SailPointErrorHandler
Custom error handler that just throws exceptions.
|
| Modifier and Type | Field and Description |
|---|---|
static char |
DOUBLE_QUOTE |
static char |
SINGLE_QUOTE |
| Constructor and Description |
|---|
XmlUtil() |
| Modifier and Type | Method and Description |
|---|---|
static void |
addAttribute(java.lang.StringBuilder b,
java.lang.String name,
java.lang.String value)
Adds an attribute name and value to a string buffer.
|
static void |
addContent(java.lang.StringBuilder b,
java.lang.Object o)
Replaces special characters in a string with XML character entities.
|
static java.lang.String |
DocumentToString(org.w3c.dom.Document doc)
Converts (@code
Document) to XML string |
static java.lang.String |
escapeAttribute(java.lang.String s)
Escape the given attribute.
|
static void |
escapeAttribute(java.lang.StringBuilder b,
java.lang.String s)
Escape the given attribute and store it in the given StringBuffer.
|
static void |
escapeAttribute(java.io.Writer f,
java.lang.String s)
Variant for Writers.
|
static java.lang.String |
escapeContent(java.lang.String s)
Escape the given element content.
|
static void |
escapeContent(java.lang.StringBuilder b,
java.lang.String s)
Differs from escapeAttribute in that we don't escape quotes.
|
static void |
escapeContent(java.io.Writer f,
java.lang.String s)
Escape the given element content and stream it to a Writer.
|
static java.lang.Object |
evaluateXpath(org.w3c.dom.Node xmlDocument,
java.lang.String xpath,
java.util.Map<java.lang.String,java.lang.String> namespaceMappings,
javax.xml.namespace.QName qname) |
static java.lang.Object |
evaluateXpath(org.w3c.dom.Node xmlDocument,
java.lang.String xpath,
java.util.Map<java.lang.String,java.lang.String> namespaceMappings,
javax.xml.namespace.QName qname,
boolean isOptionalNameSpacePrefix) |
static java.util.Map<java.lang.String,java.lang.String> |
extractNamespaces(java.lang.String xmlContent)
Extracts and prepares namespace prefix to namespace URI mappings
|
static java.util.List<java.lang.String> |
extractNodes(java.lang.String rawResponse,
java.lang.String rootPath,
java.util.Map<java.lang.String,java.lang.String> namespaceMappings)
Extracts all the child nodes of a given rooth path and prepares the child node list
|
static org.w3c.dom.Text |
findText(org.w3c.dom.Node node,
boolean ignoreEmpty)
Search for the first Text node beneath a node.
|
static java.lang.String |
getAttribute(org.w3c.dom.Element e,
java.lang.String name)
Get an attribute, collapsing empty strings to null.
|
static org.w3c.dom.Element |
getChildElement(org.w3c.dom.Node node)
Dig out the first child element under a node.
|
static org.w3c.dom.Element |
getChildElement(org.w3c.dom.Node node,
java.lang.String elementName)
Dig out the child element with the given name under a node.
|
static java.lang.String |
getContent(org.w3c.dom.Element e)
Return the content of an element as a string.
|
static org.w3c.dom.Document |
getDocument(java.lang.String xml)
Prepares a
Document object using the supplied XML string. |
static org.w3c.dom.Document |
getDocument(java.lang.String xml,
boolean isNamespaceAware)
Prepares a
Document object using the supplied XML string. |
static org.w3c.dom.Element |
getNextElement(org.w3c.dom.Node node)
Get the next right sibling that is an element.
|
static javax.xml.xpath.XPath |
getXpath(java.util.Map<java.lang.String,java.lang.String> namespaceMappings)
Creates a
XPath object using the supplied map containing
namespace mappings. |
static javax.xml.xpath.XPath |
getXpath(java.util.Map<java.lang.String,java.lang.String> namespaceMappings,
boolean isOptionalNameSpacePrefix)
Creates a
XPath object using the supplied map containing
namespace mappings. |
static javax.xml.xpath.XPathExpression |
getXpathExpression(java.lang.String xpathExpr,
java.util.Map<java.lang.String,java.lang.String> namespaceMappings)
Prepares an
XPathExpression object using the supplied XPath
expression and the namespace mappings. |
static javax.xml.xpath.XPathExpression |
getXpathExpression(java.lang.String xpathExpr,
java.util.Map<java.lang.String,java.lang.String> namespaceMappings,
boolean isOptionalNameSpacePrefix)
Prepares an
XPathExpression object using the supplied XPath
expression and the namespace mappings. |
static boolean |
isXmlChar(char ch)
Return whether the given character is an XML char.
|
static org.w3c.dom.Element |
parseOld(java.lang.String xml,
java.lang.String dtd,
boolean validating)
Parse an XML string and return the document element.
|
static void |
println(java.lang.Object o)
exclude
|
static void |
serialize(java.lang.StringBuilder b,
org.w3c.dom.Node node)
Serialize the given Node into the given StringBuilder.
|
static java.lang.String |
unescapeAttribute(java.lang.String s)
Unescape an XML attribute - this is the inverse of escapeAttribute().
|
public static final char DOUBLE_QUOTE
public static final char SINGLE_QUOTE
public static void println(java.lang.Object o)
public static org.w3c.dom.Document getDocument(java.lang.String xml)
throws org.xml.sax.SAXException,
java.io.IOException,
javax.xml.parsers.ParserConfigurationException
Document object using the supplied XML string.xml - string value of XML contentDocument objectorg.xml.sax.SAXExceptionjava.io.IOExceptionjavax.xml.parsers.ParserConfigurationExceptionpublic static org.w3c.dom.Document getDocument(java.lang.String xml,
boolean isNamespaceAware)
throws org.xml.sax.SAXException,
java.io.IOException,
javax.xml.parsers.ParserConfigurationException
Document object using the supplied XML string.xml - string value of XML contentisNamespaceAware - boolean value to indicate whether the generated
Document is namespace awareDocument objectorg.xml.sax.SAXExceptionjava.io.IOExceptionjavax.xml.parsers.ParserConfigurationExceptionpublic static javax.xml.xpath.XPathExpression getXpathExpression(java.lang.String xpathExpr,
java.util.Map<java.lang.String,java.lang.String> namespaceMappings)
throws javax.xml.xpath.XPathExpressionException
XPathExpression object using the supplied XPath
expression and the namespace mappings.
namespaceMappings should contain all non-standard XML namespaces
which may exist in the XML for correct parsing using XPath.
xpathExpr - string representation of XPath expressionnamespaceMappings - namespace mappings, if any, can be nullXPathExpression objectjavax.xml.xpath.XPathExpressionExceptionpublic static javax.xml.xpath.XPathExpression getXpathExpression(java.lang.String xpathExpr,
java.util.Map<java.lang.String,java.lang.String> namespaceMappings,
boolean isOptionalNameSpacePrefix)
throws javax.xml.xpath.XPathExpressionException
XPathExpression object using the supplied XPath
expression and the namespace mappings.
namespaceMappings should contain all non-standard XML namespaces
which may exist in the XML for correct parsing using XPath.
xpathExpr - string representation of XPath expressionnamespaceMappings - namespace mappings, if any, can be nullisOptionalNameSpacePrefix - boolean value to determine that namespace prefix
is optional or not for XPATH evaluationXPathExpression objectjavax.xml.xpath.XPathExpressionExceptionpublic static javax.xml.xpath.XPath getXpath(java.util.Map<java.lang.String,java.lang.String> namespaceMappings)
XPath object using the supplied map containing
namespace mappings.
namespaceMappings should contain all non-standard XML namespaces
which may exist in the XML for correct parsing using XPath.
namespaceMappings - namespace mappings, if any, can be nullXPath objectpublic static javax.xml.xpath.XPath getXpath(java.util.Map<java.lang.String,java.lang.String> namespaceMappings,
boolean isOptionalNameSpacePrefix)
XPath object using the supplied map containing
namespace mappings.
namespaceMappings should contain all non-standard XML namespaces
which may exist in the XML for correct parsing using XPath.
namespaceMappings - namespace mappings, if any, can be nullisOptionalNameSpacePrefix - boolean value to determine that namespace prefix
* is optional or not for XPATH evaluationXPath objectpublic static java.lang.Object evaluateXpath(org.w3c.dom.Node xmlDocument,
java.lang.String xpath,
java.util.Map<java.lang.String,java.lang.String> namespaceMappings,
javax.xml.namespace.QName qname)
throws org.xml.sax.SAXException,
java.io.IOException,
javax.xml.parsers.ParserConfigurationException,
javax.xml.xpath.XPathExpressionException
xmlDocument - xpath - namespaceMappings - qname - org.xml.sax.SAXExceptionjava.io.IOExceptionjavax.xml.parsers.ParserConfigurationExceptionjavax.xml.xpath.XPathExpressionExceptionpublic static java.lang.Object evaluateXpath(org.w3c.dom.Node xmlDocument,
java.lang.String xpath,
java.util.Map<java.lang.String,java.lang.String> namespaceMappings,
javax.xml.namespace.QName qname,
boolean isOptionalNameSpacePrefix)
throws org.xml.sax.SAXException,
java.io.IOException,
javax.xml.parsers.ParserConfigurationException,
javax.xml.xpath.XPathExpressionException
xmlDocument - xpath - namespaceMappings - qname - org.xml.sax.SAXExceptionjava.io.IOExceptionjavax.xml.parsers.ParserConfigurationExceptionjavax.xml.xpath.XPathExpressionExceptionpublic static org.w3c.dom.Element parseOld(java.lang.String xml,
java.lang.String dtd,
boolean validating)
public static java.lang.String getAttribute(org.w3c.dom.Element e,
java.lang.String name)
public static org.w3c.dom.Element getChildElement(org.w3c.dom.Node node)
public static org.w3c.dom.Element getChildElement(org.w3c.dom.Node node,
java.lang.String elementName)
public static org.w3c.dom.Element getNextElement(org.w3c.dom.Node node)
public static org.w3c.dom.Text findText(org.w3c.dom.Node node,
boolean ignoreEmpty)
public static java.lang.String getContent(org.w3c.dom.Element e)
public static java.lang.String escapeAttribute(java.lang.String s)
public static void escapeAttribute(java.lang.StringBuilder b,
java.lang.String s)
public static void escapeAttribute(java.io.Writer f,
java.lang.String s)
throws java.io.IOException
ignore - As always happens, some zealous refactorer is going to look at this someday, declare "it sucks" and launch off into an over-engineered blowout that lets us stream XML into anything with a hole. Just remember that this is an "inner loop" utility, we should not be generating garbage in here. Due to the inconsistent java.io classes, this makes it almost impossible to do without duplication. If we do anything we define our own stream interface with implementations built on various shards of java.io.
java.io.IOExceptionpublic static java.lang.String unescapeAttribute(java.lang.String s)
public static void escapeContent(java.lang.StringBuilder b,
java.lang.String s)
ignore - NOTE: Originally we escaped newlines, returns, and tabs, but this made it impossible to edit Rule source because all formatting was lost. These characters are all valid in element content so I'm not sure why we escaped them. If you think you need to put the escaping back, discuss with the team, we may need a way for XMLProperty to declare ecaping style.
public static java.lang.String escapeContent(java.lang.String s)
public static void escapeContent(java.io.Writer f,
java.lang.String s)
throws java.io.IOException
ignore - A variant that streams to a file. It's too bad that we have to duplicate this but it seems silly to create wrapper garbage around StringBuilder just so we can share the guts.
As always happens, some zealous refactorer is going to look at this someday, declare "it sucks" and launch off into an over-engineered blowout that lets us stream XML into anything with a hole. Just remember that this is an "inner loop" utility, we should not be generating garbage in here. Due to the inconsistent java.io classes, this makes it almost impossible to do without duplication. If we do anything we define our own stream interface with implementations built on various shards of java.io.
See escapeContent(StringBuilder, String) for more information about what we need to escape.
java.io.IOExceptionpublic static boolean isXmlChar(char ch)
public static void serialize(java.lang.StringBuilder b,
org.w3c.dom.Node node)
public static void addContent(java.lang.StringBuilder b,
java.lang.Object o)
public static void addAttribute(java.lang.StringBuilder b,
java.lang.String name,
java.lang.String value)
Performs any necessary escaping on the value. This should be used when you're building the XML for something, and its possible for an attribute value to have any of the characters &, ', or "
public static java.util.Map<java.lang.String,java.lang.String> extractNamespaces(java.lang.String xmlContent)
xmlContent - XML content to extract namespace information fromMap containing keys as namespace prefix and namespace URI
as corresponding valuespublic static java.util.List<java.lang.String> extractNodes(java.lang.String rawResponse,
java.lang.String rootPath,
java.util.Map<java.lang.String,java.lang.String> namespaceMappings)
throws javax.xml.xpath.XPathExpressionException,
org.xml.sax.SAXException,
java.io.IOException,
javax.xml.parsers.ParserConfigurationException
rawResponse - XML ResponserootPath - string representation of XPath expressionnamespaceMappings - namespace mappings, if anyList containing list of nodes in String formatjavax.xml.xpath.XPathExpressionExceptionorg.xml.sax.SAXExceptionjava.io.IOExceptionjavax.xml.parsers.ParserConfigurationExceptionpublic static java.lang.String DocumentToString(org.w3c.dom.Document doc)
throws java.io.IOException
Document) to XML stringdoc - Document objectString converted XML stringjava.io.IOException