Package com.android.utils
Class XmlUtils
- java.lang.Object
-
- com.android.utils.XmlUtils
-
public class XmlUtils extends java.lang.ObjectXML Utilities.For Kotlin usage, many of these are exposed as more convenient extension methods in
DomExtensions
-
-
Field Summary
Fields Modifier and Type Field Description static charNS_SEPARATORSeparator for xml namespace and localnamestatic java.lang.StringSAX_PARSER_FACTORYstatic java.lang.StringXML_COMMENT_BEGINstatic java.lang.StringXML_COMMENT_ENDstatic java.lang.StringXML_PROLOG
-
Constructor Summary
Constructors Constructor Description XmlUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidappendXmlAttributeValue(java.lang.StringBuilder sb, java.lang.String attrValue)Appends text to the givenStringBuilderand escapes it as required for a DOM attribute node.static voidappendXmlAttributeValue(java.lang.StringBuilder sb, java.lang.String attrValue, int start, int end)Appends text to the givenStringBuilderand escapes it as required for a DOM attribute node.static voidappendXmlTextValue(java.lang.StringBuilder sb, java.lang.String textValue)Appends text to the givenStringBuilderand escapes it as required for a DOM text node.static voidappendXmlTextValue(java.lang.StringBuilder sb, java.lang.String textValue, int start, int end)Appends text to the givenStringBuilderand escapes it as required for a DOM text node.static voidattachSourceFile(org.w3c.dom.Node node, SourceFile sourceFile)static javax.xml.parsers.SAXParserFactoryconfigureSaxFactory(javax.xml.parsers.SAXParserFactory factory, boolean namespaceAware, boolean checkDtd)static org.w3c.dom.DocumentcreateDocument(boolean namespaceAware)Creates and returns a new empty document.static javax.xml.parsers.SAXParsercreateSaxParser(javax.xml.parsers.SAXParserFactory factory)static javax.xml.parsers.SAXParsercreateSaxParser(javax.xml.parsers.SAXParserFactory factory, boolean allowDocTypeDeclarations)static java.lang.StringformatFloatValue(double value)Formats the number and removes trailing zeros after the decimal dot and also the dot itself if there were non-zero digits after it.static java.lang.StringfromXmlAttributeValue(java.lang.String escapedAttrValue)Converts the given XML-attribute-safe value to a java stringstatic javax.xml.parsers.SAXParserFactorygetConfiguredSaxFactory(boolean namespaceAware, boolean checkDtd)static org.w3c.dom.ElementgetFirstSubTag(org.w3c.dom.Node parent)Returns the first child element of the given nodestatic org.w3c.dom.ElementgetFirstSubTagByName(org.w3c.dom.Node parent, java.lang.String name)Returns the next sibling element from the given node that matches the given namestatic org.w3c.dom.ElementgetNextTag(org.w3c.dom.Node node)Returns the next sibling element from the given nodestatic org.w3c.dom.ElementgetNextTagByName(org.w3c.dom.Node node, java.lang.String name)Returns the next sibling element from the given nodestatic org.w3c.dom.CommentgetPreviousComment(org.w3c.dom.Node element)Returns the comment preceding the given element with no other elements in between, or null if the element is not preceded by a comment.static java.lang.StringgetPreviousCommentText(org.w3c.dom.Node element)Returns the text of the comment preceding the given element with no other elements in between, or null if the element is not preceded by a comment or if the comment is empty or consists of only whitespace characters.static org.w3c.dom.ElementgetPreviousTag(org.w3c.dom.Node node)Returns the previous sibling element from the given nodestatic org.w3c.dom.ElementgetPreviousTagByName(org.w3c.dom.Node node, java.lang.String name)static java.lang.StringgetRootTagName(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 java.lang.StringgetRootTagName(java.lang.String xmlText)Returns the name of the root element tag stored in the given file, or null if it can't be determined.static SourceFilePositiongetSourceFilePosition(org.w3c.dom.Node node)static intgetSubTagCount(org.w3c.dom.Node parent)Returns the number of children sub tags of the given node.static java.lang.Iterable<org.w3c.dom.Element>getSubTags(org.w3c.dom.Node parent)Returns an iterator for the children elements of the given node.static java.util.List<org.w3c.dom.Element>getSubTagsAsList(org.w3c.dom.Node parent)Returns the children elements of the given nodestatic java.lang.Iterable<org.w3c.dom.Element>getSubTagsByName(org.w3c.dom.Node parent, java.lang.String tagName)Returns an iterator for the children elements of the given node matching the given tag name.static java.io.ReadergetUtfReader(java.io.File file)Returns a character reader for the given file, which must be a UTF encoded file.static booleanhasElementChildren(org.w3c.dom.Node node)Returns true if the given node has one or more element childrenstatic booleanisProtoXml(byte[] bytes)Checks if the given array of bytes is likely to represent XML in a proto format.static booleanisProtoXml(java.io.InputStream stream)Checks if the given input stream is likely to represent XML in a proto format.static java.lang.StringlookupNamespacePrefix(org.w3c.dom.Node node, java.lang.String nsUri)Returns the namespace prefix matching the requested namespace URI.static java.lang.StringlookupNamespacePrefix(org.w3c.dom.Node node, java.lang.String nsUri, boolean create)Returns the namespace prefix matching the requested namespace URI.static java.lang.StringlookupNamespacePrefix(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.DocumentparseDocument(java.io.Reader xml, boolean namespaceAware)Parses the givenReaderas a DOM document, using the JDK parser.static org.w3c.dom.DocumentparseDocument(java.lang.String xml, boolean namespaceAware)Parses the given XML string as a DOM document, using the JDK parser.static org.w3c.dom.DocumentparseDocumentSilently(java.lang.String xml, boolean namespaceAware)Parses the given XML string as a DOM document, using the JDK parser.static org.w3c.dom.DocumentparseUtfXmlFile(java.io.File file, boolean namespaceAware)Parses the given UTF file as a DOM document, using the JDK parser.static java.lang.StringstripBom(java.lang.String xml)Strips out a leading UTF byte order mark, if presentstatic java.lang.StringtoXml(org.w3c.dom.Node node)Dump an XML tree to string.static java.lang.StringtoXml(org.w3c.dom.Node node, java.util.Map<SourcePosition,SourceFilePosition> blame)static java.lang.StringtoXmlAttributeValue(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.StringtoXmlTextValue(java.lang.String textValue)Converts the given attribute value to an XML-text-safe value, meaning that less than and ampersand characters are escaped.
-
-
-
Field Detail
-
XML_COMMENT_BEGIN
public static final java.lang.String XML_COMMENT_BEGIN
- See Also:
- Constant Field Values
-
XML_COMMENT_END
public static final java.lang.String XML_COMMENT_END
- See Also:
- Constant Field Values
-
XML_PROLOG
public static final java.lang.String XML_PROLOG
- See Also:
- Constant Field Values
-
SAX_PARSER_FACTORY
public static final java.lang.String SAX_PARSER_FACTORY
- See Also:
- Constant Field Values
-
NS_SEPARATOR
public static final char NS_SEPARATOR
Separator for xml namespace and localname- See Also:
- Constant Field Values
-
-
Method Detail
-
lookupNamespacePrefix
@NonNull public static java.lang.String lookupNamespacePrefix(@NonNull org.w3c.dom.Node node, @NonNull java.lang.String nsUri)Returns the namespace prefix matching the requested namespace URI. If no such declaration is found, returns the default "android" prefix for the Android URI, and "app" for other URI's. By default the app namespace will be created. If this is not desirable, calllookupNamespacePrefix(Node, String, boolean)instead.- Parameters:
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- Returns:
- The first prefix declared or the default "android" prefix (or "app" for non-Android URIs)
-
lookupNamespacePrefix
@NonNull public static java.lang.String lookupNamespacePrefix(@NonNull org.w3c.dom.Node node, @NonNull java.lang.String nsUri, boolean create)Returns the namespace prefix matching the requested namespace URI. If no such declaration is found, returns the default "android" prefix for the Android URI, and "app" for other URI's.- Parameters:
node- The current node. Must not be null.nsUri- The namespace URI of which the prefix is to be found, e.g.SdkConstants.ANDROID_URIcreate- whether the namespace declaration should be created, if necessary- Returns:
- The first prefix declared or the default "android" prefix (or "app" for non-Android URIs)
-
lookupNamespacePrefix
public static java.lang.String lookupNamespacePrefix(@Nullable org.w3c.dom.Node node, @Nullable java.lang.String nsUri, @Nullable java.lang.String defaultPrefix, boolean create)Returns the namespace prefix matching the requested namespace URI. If no such declaration is found, returns the default "android" prefix.- Parameters:
node- The current node. Must not be null.nsUri- The namespace URI of which the prefix is to be found, e.g.SdkConstants.ANDROID_URIdefaultPrefix- 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- Returns:
- The first prefix declared or the provided prefix (possibly with a number appended to avoid conflicts with existing prefixes.
-
toXmlAttributeValue
@NonNull public static java.lang.String toXmlAttributeValue(@NonNull 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.- Parameters:
attrValue- the value to be escaped- Returns:
- the escaped value
-
fromXmlAttributeValue
@NonNull public static java.lang.String fromXmlAttributeValue(@NonNull java.lang.String escapedAttrValue)Converts the given XML-attribute-safe value to a java string- Parameters:
escapedAttrValue- the escaped value- Returns:
- the unescaped value
-
toXmlTextValue
@NonNull public static java.lang.String toXmlTextValue(@NonNull java.lang.String textValue)Converts the given attribute value to an XML-text-safe value, meaning that less than and ampersand characters are escaped.- Parameters:
textValue- the text value to be escaped- Returns:
- the escaped value
-
appendXmlAttributeValue
public static void appendXmlAttributeValue(@NonNull java.lang.StringBuilder sb, @NonNull java.lang.String attrValue)Appends text to the givenStringBuilderand escapes it as required for a DOM attribute node.- Parameters:
sb- the string builderattrValue- the attribute value to be appended and escaped
-
appendXmlAttributeValue
public static void appendXmlAttributeValue(@NonNull java.lang.StringBuilder sb, @NonNull java.lang.String attrValue, int start, int end)Appends text to the givenStringBuilderand escapes it as required for a DOM attribute node.- Parameters:
sb- the string builderattrValue- the attribute value to be appended and escapedstart- the starting offset in the text stringend- the ending offset in the text string
-
appendXmlTextValue
public static void appendXmlTextValue(@NonNull java.lang.StringBuilder sb, @NonNull java.lang.String textValue)Appends text to the givenStringBuilderand escapes it as required for a DOM text node.- Parameters:
sb- the string buildertextValue- the text value to be appended and escaped
-
appendXmlTextValue
public static void appendXmlTextValue(@NonNull java.lang.StringBuilder sb, @NonNull java.lang.String textValue, int start, int end)Appends text to the givenStringBuilderand escapes it as required for a DOM text node.- Parameters:
sb- the string buildertextValue- the text value to be appended and escapedstart- the starting offset in the text stringend- the ending offset in the text string
-
hasElementChildren
public static boolean hasElementChildren(@NonNull org.w3c.dom.Node node)Returns true if the given node has one or more element children- Parameters:
node- the node to test for element children- Returns:
- true if the node has one or more element children
-
getUtfReader
@NonNull public static java.io.Reader getUtfReader(@NonNull java.io.File file) throws java.io.IOExceptionReturns a character reader for the given file, which must be a UTF encoded file.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.)
- Throws:
java.io.IOException
-
parseDocument
@NonNull public static org.w3c.dom.Document parseDocument(@NonNull java.lang.String xml, boolean namespaceAware) throws java.io.IOException, org.xml.sax.SAXExceptionParses the given XML string as a DOM document, using the JDK parser. The parser does not validate, and is optionally namespace aware.- Parameters:
xml- the XML content to be parsed (must be well formed)namespaceAware- whether the parser is namespace aware- Returns:
- the DOM document
- Throws:
java.io.IOExceptionorg.xml.sax.SAXException
-
parseDocument
@NonNull public static org.w3c.dom.Document parseDocument(@NonNull java.io.Reader xml, boolean namespaceAware) throws java.io.IOException, org.xml.sax.SAXExceptionParses the givenReaderas a DOM document, using the JDK parser. The parser does not validate, and is optionally namespace aware.- Parameters:
xml- a reader for the XML content to be parsed (must be well formed)namespaceAware- whether the parser is namespace aware- Returns:
- the DOM document
- Throws:
java.io.IOExceptionorg.xml.sax.SAXException
-
parseUtfXmlFile
@NonNull public static org.w3c.dom.Document parseUtfXmlFile(@NonNull java.io.File file, boolean namespaceAware) throws java.io.IOException, org.xml.sax.SAXExceptionParses the given UTF file as a DOM document, using the JDK parser. The parser does not validate, and is optionally namespace aware.- Parameters:
file- the UTF encoded file to parsenamespaceAware- whether the parser is namespace aware- Returns:
- the DOM document
- Throws:
java.io.IOExceptionorg.xml.sax.SAXException
-
createDocument
@NonNull public static org.w3c.dom.Document createDocument(boolean namespaceAware)
Creates and returns a new empty document.
-
stripBom
@NonNull public static java.lang.String stripBom(@NonNull java.lang.String xml)Strips out a leading UTF byte order mark, if present
-
parseDocumentSilently
@Nullable public static org.w3c.dom.Document parseDocumentSilently(@NonNull java.lang.String xml, boolean namespaceAware)Parses the given XML string as a DOM document, using the JDK parser. The parser does not validate, and is optionally namespace aware. Any parsing errors are silently ignored.- Parameters:
xml- the XML content to be parsed (must be well formed)namespaceAware- whether the parser is namespace aware- Returns:
- the DOM document, or null
-
configureSaxFactory
public static javax.xml.parsers.SAXParserFactory configureSaxFactory(@NonNull javax.xml.parsers.SAXParserFactory factory, boolean namespaceAware, boolean checkDtd)
-
getConfiguredSaxFactory
public static javax.xml.parsers.SAXParserFactory getConfiguredSaxFactory(boolean namespaceAware, boolean checkDtd)
-
createSaxParser
@NonNull public static javax.xml.parsers.SAXParser createSaxParser(@NonNull javax.xml.parsers.SAXParserFactory factory) throws javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXException- Throws:
javax.xml.parsers.ParserConfigurationExceptionorg.xml.sax.SAXException
-
createSaxParser
@NonNull public static javax.xml.parsers.SAXParser createSaxParser(@NonNull javax.xml.parsers.SAXParserFactory factory, boolean allowDocTypeDeclarations) throws javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXException- Throws:
javax.xml.parsers.ParserConfigurationExceptionorg.xml.sax.SAXException
-
toXml
public static java.lang.String toXml(@NonNull org.w3c.dom.Node node)Dump an XML tree to string. This does not perform any pretty printing. To perform pretty printing, useXmlPrettyPrinter.prettyPrint(node)insdk-common.
-
toXml
public static java.lang.String toXml(@NonNull org.w3c.dom.Node node, @Nullable java.util.Map<SourcePosition,SourceFilePosition> blame)
-
attachSourceFile
public static void attachSourceFile(@NonNull org.w3c.dom.Node node, @NonNull SourceFile sourceFile)
-
getSourceFilePosition
@NonNull public static SourceFilePosition getSourceFilePosition(@NonNull org.w3c.dom.Node node)
-
formatFloatValue
@NonNull public static java.lang.String formatFloatValue(double value)
Formats the number and removes trailing zeros after the decimal dot and also the dot itself if there were non-zero digits after it.- Parameters:
value- the value to be formatted- Returns:
- the corresponding XML string for the value
-
getRootTagName
@Nullable public static java.lang.String getRootTagName(@NonNull 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.
-
getRootTagName
@Nullable public static java.lang.String getRootTagName(@NonNull java.lang.String xmlText)Returns the name of the root element tag stored in the given file, or null if it can't be determined.
-
getSubTagsAsList
@NonNull public static java.util.List<org.w3c.dom.Element> getSubTagsAsList(@NonNull org.w3c.dom.Node parent)Returns the children elements of the given node- Parameters:
parent- the parent node- Returns:
- a list of element children, never null
-
getSubTags
@NonNull public static java.lang.Iterable<org.w3c.dom.Element> getSubTags(@Nullable org.w3c.dom.Node parent)Returns an iterator for the children elements of the given node. If you want to access the children as a list, usegetSubTagsAsList(Node)instead.NOTE: The iterator() call can only be called once!
-
getSubTagsByName
@NonNull public static java.lang.Iterable<org.w3c.dom.Element> getSubTagsByName(@Nullable org.w3c.dom.Node parent, @NonNull java.lang.String tagName)Returns an iterator for the children elements of the given node matching the given tag name.If you want to access the children as a list, use
getSubTagsAsList(Node)instead.NOTE: The iterator() call can only be called once!
-
getFirstSubTag
@Nullable public static org.w3c.dom.Element getFirstSubTag(@Nullable org.w3c.dom.Node parent)Returns the first child element of the given node
-
getNextTag
@Nullable public static org.w3c.dom.Element getNextTag(@Nullable org.w3c.dom.Node node)Returns the next sibling element from the given node
-
getPreviousTag
@Nullable public static org.w3c.dom.Element getPreviousTag(@Nullable org.w3c.dom.Node node)Returns the previous sibling element from the given node
-
getFirstSubTagByName
@Nullable public static org.w3c.dom.Element getFirstSubTagByName(@Nullable org.w3c.dom.Node parent, @NonNull java.lang.String name)Returns the next sibling element from the given node that matches the given name
-
getNextTagByName
@Nullable public static org.w3c.dom.Element getNextTagByName(@Nullable org.w3c.dom.Node node, @NonNull java.lang.String name)Returns the next sibling element from the given node
-
getPreviousTagByName
@Nullable public static org.w3c.dom.Element getPreviousTagByName(@Nullable org.w3c.dom.Node node, @NonNull java.lang.String name)
-
getPreviousComment
@Nullable public static org.w3c.dom.Comment getPreviousComment(@NonNull org.w3c.dom.Node element)Returns the comment preceding the given element with no other elements in between, or null if the element is not preceded by a comment.
-
getPreviousCommentText
@Nullable public static java.lang.String getPreviousCommentText(@NonNull org.w3c.dom.Node element)Returns the text of the comment preceding the given element with no other elements in between, or null if the element is not preceded by a comment or if the comment is empty or consists of only whitespace characters.
-
getSubTagCount
public static int getSubTagCount(@Nullable org.w3c.dom.Node parent)Returns the number of children sub tags of the given node.- Parameters:
parent- the parent node- Returns:
- the count of element children
-
isProtoXml
public static boolean isProtoXml(@NonNull byte[] bytes)Checks if the given array of bytes is likely to represent XML in a proto format.- Parameters:
bytes- the candidate XML contents to check- Returns:
- true if the bytes are likely to represent proto XML
-
isProtoXml
public static boolean isProtoXml(@NonNull java.io.InputStream stream)Checks if the given input stream is likely to represent XML in a proto format.- Parameters:
stream- the candidate XML stream to check- Returns:
- true if the stream is likely to represent proto XML
-
-