public class XmlUtils
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static char |
NS_SEPARATOR
Separator for xml namespace and localname
|
static java.lang.String |
XML_COMMENT_BEGIN |
static java.lang.String |
XML_COMMENT_END |
static java.lang.String |
XML_PROLOG |
Constructor and Description |
---|
XmlUtils() |
Modifier and Type | Method and Description |
---|---|
static void |
appendXmlAttributeValue(java.lang.StringBuilder sb,
java.lang.String attrValue)
Appends text to the given
StringBuilder and escapes it as required for a
DOM attribute node. |
static void |
appendXmlTextValue(java.lang.StringBuilder sb,
java.lang.String textValue)
Appends text to the given
StringBuilder and escapes it as required for a
DOM text node. |
static void |
appendXmlTextValue(java.lang.StringBuilder sb,
java.lang.String textValue,
int start,
int end)
Appends text to the given
StringBuilder and escapes it as required for a
DOM text node. |
static void |
attachSourceFile(org.w3c.dom.Node node,
SourceFile sourceFile) |
static java.lang.String |
formatFloatAttribute(double value)
Format the given floating value into an XML string, omitting decimals if
0
|
static java.lang.String |
fromXmlAttributeValue(java.lang.String escapedAttrValue)
Converts the given XML-attribute-safe value to a java string
|
static java.lang.String |
getRootTagName(java.io.File xmlFile)
Returns the name of the root element tag stored in the given file, or null if it can't be
determined.
|
static SourceFilePosition |
getSourceFilePosition(org.w3c.dom.Node node) |
static java.io.Reader |
getUtfReader(java.io.File file)
Returns a character reader for the given file, which must be a UTF encoded file.
|
static boolean |
hasElementChildren(org.w3c.dom.Node node)
Returns true if the given node has one or more element children
|
static java.lang.String |
lookupNamespacePrefix(org.w3c.dom.Node node,
java.lang.String nsUri)
Returns the namespace prefix matching the requested namespace URI.
|
static java.lang.String |
lookupNamespacePrefix(org.w3c.dom.Node node,
java.lang.String nsUri,
boolean create)
Returns the namespace prefix matching the requested namespace URI.
|
static java.lang.String |
lookupNamespacePrefix(org.w3c.dom.Node node,
java.lang.String nsUri,
java.lang.String defaultPrefix,
boolean create)
Returns the namespace prefix matching the requested namespace URI.
|
static org.w3c.dom.Document |
parseDocument(java.io.Reader xml,
boolean namespaceAware)
Parses the given
Reader as a DOM document, using the JDK parser. |
static org.w3c.dom.Document |
parseDocument(java.lang.String xml,
boolean namespaceAware)
Parses the given XML string as a DOM document, using the JDK parser.
|
static org.w3c.dom.Document |
parseDocumentSilently(java.lang.String xml,
boolean namespaceAware)
Parses the given XML string as a DOM document, using the JDK parser.
|
static org.w3c.dom.Document |
parseUtfXmlFile(java.io.File file,
boolean namespaceAware)
Parses the given UTF file as a DOM document, using the JDK parser.
|
static java.lang.String |
stripBom(java.lang.String xml)
Strips out a leading UTF byte order mark, if present
|
static java.lang.String |
toXml(org.w3c.dom.Node node)
Dump an XML tree to string.
|
static java.lang.String |
toXml(org.w3c.dom.Node node,
java.util.Map<SourcePosition,SourceFilePosition> blame) |
static java.lang.String |
toXmlAttributeValue(java.lang.String attrValue)
Converts the given attribute value to an XML-attribute-safe value, meaning that
single and double quotes are replaced with their corresponding XML entities.
|
static java.lang.String |
toXmlTextValue(java.lang.String textValue)
Converts the given attribute value to an XML-text-safe value, meaning that
less than and ampersand characters are escaped.
|
public static final java.lang.String XML_COMMENT_BEGIN
public static final java.lang.String XML_COMMENT_END
public static final java.lang.String XML_PROLOG
public static final char NS_SEPARATOR
@NonNull public static java.lang.String lookupNamespacePrefix(@NonNull org.w3c.dom.Node node, @NonNull java.lang.String nsUri)
lookupNamespacePrefix(Node, String, boolean)
instead.node
- The current node. Must not be null.nsUri
- The namespace URI of which the prefix is to be found,
e.g. SdkConstants.ANDROID_URI
@NonNull public static java.lang.String lookupNamespacePrefix(@NonNull org.w3c.dom.Node node, @NonNull java.lang.String nsUri, boolean create)
node
- The current node. Must not be null.nsUri
- The namespace URI of which the prefix is to be found, e.g.
SdkConstants.ANDROID_URI
create
- whether the namespace declaration should be created, if
necessarypublic static java.lang.String lookupNamespacePrefix(@Nullable org.w3c.dom.Node node, @Nullable java.lang.String nsUri, @Nullable java.lang.String defaultPrefix, boolean create)
node
- The current node. Must not be null.nsUri
- The namespace URI of which the prefix is to be found, e.g.
SdkConstants.ANDROID_URI
defaultPrefix
- The default prefix (root) to use if the namespace is
not found. If null, do not create a new namespace if this URI
is not defined for the document.create
- whether the namespace declaration should be created, if
necessary@NonNull public static java.lang.String toXmlAttributeValue(@NonNull java.lang.String attrValue)
attrValue
- the value to be escaped@NonNull public static java.lang.String fromXmlAttributeValue(@NonNull java.lang.String escapedAttrValue)
escapedAttrValue
- the escaped value@NonNull public static java.lang.String toXmlTextValue(@NonNull java.lang.String textValue)
textValue
- the text value to be escapedpublic static void appendXmlAttributeValue(@NonNull java.lang.StringBuilder sb, @NonNull java.lang.String attrValue)
StringBuilder
and escapes it as required for a
DOM attribute node.sb
- the string builderattrValue
- the attribute value to be appended and escapedpublic static void appendXmlTextValue(@NonNull java.lang.StringBuilder sb, @NonNull java.lang.String textValue)
StringBuilder
and escapes it as required for a
DOM text node.sb
- the string buildertextValue
- the text value to be appended and escapedpublic static void appendXmlTextValue(@NonNull java.lang.StringBuilder sb, @NonNull java.lang.String textValue, int start, int end)
StringBuilder
and escapes it as required for a
DOM text node.sb
- the string builderstart
- the starting offset in the text stringend
- the ending offset in the text stringtextValue
- the text value to be appended and escapedpublic static boolean hasElementChildren(@NonNull org.w3c.dom.Node node)
node
- the node to test for element childrenpublic static java.io.Reader getUtfReader(@NonNull java.io.File file) throws java.io.IOException
The reader does not need to be closed by the caller (because the file is read in full in one shot and the resulting array is then wrapped in a byte array input stream, which does not need to be closed.)
java.io.IOException
@NonNull public static org.w3c.dom.Document parseDocument(@NonNull java.lang.String xml, boolean namespaceAware) throws javax.xml.parsers.ParserConfigurationException, java.io.IOException, org.xml.sax.SAXException
xml
- the XML content to be parsed (must be well formed)namespaceAware
- whether the parser is namespace awarejavax.xml.parsers.ParserConfigurationException
java.io.IOException
org.xml.sax.SAXException
@NonNull public static org.w3c.dom.Document parseDocument(@NonNull java.io.Reader xml, boolean namespaceAware) throws javax.xml.parsers.ParserConfigurationException, java.io.IOException, org.xml.sax.SAXException
Reader
as a DOM document, using the JDK parser. The parser does not
validate, and is optionally namespace aware.xml
- a reader for the XML content to be parsed (must be well formed)namespaceAware
- whether the parser is namespace awarejavax.xml.parsers.ParserConfigurationException
java.io.IOException
org.xml.sax.SAXException
@NonNull public static org.w3c.dom.Document parseUtfXmlFile(@NonNull java.io.File file, boolean namespaceAware) throws javax.xml.parsers.ParserConfigurationException, java.io.IOException, org.xml.sax.SAXException
file
- the UTF encoded file to parsenamespaceAware
- whether the parser is namespace awarejavax.xml.parsers.ParserConfigurationException
java.io.IOException
org.xml.sax.SAXException
@NonNull public static java.lang.String stripBom(@NonNull java.lang.String xml)
@Nullable public static org.w3c.dom.Document parseDocumentSilently(@NonNull java.lang.String xml, boolean namespaceAware)
xml
- the XML content to be parsed (must be well formed)namespaceAware
- whether the parser is namespace awarepublic static java.lang.String toXml(@NonNull org.w3c.dom.Node node)
XmlPrettyPrinter.prettyPrint(node)
in
sdk-common
.public static java.lang.String toXml(@NonNull org.w3c.dom.Node node, @Nullable java.util.Map<SourcePosition,SourceFilePosition> blame)
public static void attachSourceFile(org.w3c.dom.Node node, SourceFile sourceFile)
public static SourceFilePosition getSourceFilePosition(org.w3c.dom.Node node)
public static java.lang.String formatFloatAttribute(double value)
value
- the value to be formatted@Nullable public static java.lang.String getRootTagName(@NonNull java.io.File xmlFile)