Package com.android.utils
Class PositionXmlParser
java.lang.Object
com.android.utils.PositionXmlParser
public class PositionXmlParser
extends java.lang.Object
A simple DOM XML parser which can retrieve exact beginning and end offsets
(and line and column numbers) for element nodes as well as attribute nodes.
-
Method Summary
Modifier and Type Method Description static org.w3c.dom.NodefindNodeAtLineAndCol(org.w3c.dom.Document document, int line, int column)Finds the leaf node at the given offset.static org.w3c.dom.NodefindNodeAtOffset(org.w3c.dom.Document document, int offset)Finds the leaf node at the given offset.static SourcePositiongetPosition(org.w3c.dom.Node node)Returns the position for the given node.static SourcePositiongetPosition(org.w3c.dom.Node node, int start, int end)Returns the position for the given node.static java.lang.StringgetXmlString(byte[] data)Returns the String corresponding to the given byte array of XML data (with unknown encoding).static java.lang.StringgetXmlString(byte[] data, java.lang.String defaultCharset)Returns the String corresponding to the given byte array of XML data (with unknown encoding).static org.w3c.dom.Documentparse(byte[] data)static org.w3c.dom.Documentparse(byte[] data, boolean namespaceAware)Parses the XML content from the given byte array.static org.w3c.dom.Documentparse(byte[] data, boolean namespaceAware, java.util.List<java.lang.String> parseErrors)Parses the XML content from the given byte array.static org.w3c.dom.Documentparse(java.io.InputStream input)static org.w3c.dom.Documentparse(java.io.InputStream input, boolean namespaceAware)Parses the XML content from the given input stream.static org.w3c.dom.Documentparse(java.io.InputStream input, boolean namespaceAware, java.util.List<java.lang.String> parseErrors)Parses the XML content from the given input stream.static org.w3c.dom.Documentparse(java.lang.String xml)static org.w3c.dom.Documentparse(java.lang.String xml, boolean namespaceAware)Parses the given XML content.
-
Method Details
-
parse
public static org.w3c.dom.Document parse(java.io.InputStream input, boolean namespaceAware) throws javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXException, java.io.IOExceptionParses the XML content from the given input stream.- Parameters:
input- the input stream containing the XML to be parsednamespaceAware- whether the parser should be namespace aware- Returns:
- the corresponding document
- Throws:
javax.xml.parsers.ParserConfigurationException- if a SAX parser is not availableorg.xml.sax.SAXException- if the document contains a parsing errorjava.io.IOException- if something is seriously wrong. This should not happen since the input source is known to be constructed from a string
-
parse
public static org.w3c.dom.Document parse(java.io.InputStream input, boolean namespaceAware, java.util.List<java.lang.String> parseErrors) throws javax.xml.parsers.ParserConfigurationException, java.io.IOExceptionParses the XML content from the given input stream.If a non-recoverable parser error is encountered, parsing stops, an error message is added to the
parseErrorslist, and the returned document contains the elements up to the one where the error was encountered.- Parameters:
input- the input stream containing the XML to be parsednamespaceAware- whether the parser should be namespace awareparseErrors- parsing errors, if any, are appended to this list- Returns:
- the corresponding document
- Throws:
javax.xml.parsers.ParserConfigurationException- if a SAX parser is not availablejava.io.IOException- if something is seriously wrong. This should not happen since the input source is known to be constructed from a string
-
parse
public static org.w3c.dom.Document parse(java.io.InputStream input) throws java.io.IOException, org.xml.sax.SAXException, javax.xml.parsers.ParserConfigurationException- Throws:
java.io.IOExceptionorg.xml.sax.SAXExceptionjavax.xml.parsers.ParserConfigurationException- See Also:
parse(InputStream, boolean)
-
parse
public static org.w3c.dom.Document parse(byte[] data) throws javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXException, java.io.IOException- Throws:
javax.xml.parsers.ParserConfigurationExceptionorg.xml.sax.SAXExceptionjava.io.IOException- See Also:
parse(byte[], boolean)
-
parse
public static org.w3c.dom.Document parse(java.lang.String xml) throws javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXException, java.io.IOException- Throws:
javax.xml.parsers.ParserConfigurationExceptionorg.xml.sax.SAXExceptionjava.io.IOException- See Also:
parse(String, boolean)
-
parse
public static org.w3c.dom.Document parse(byte[] data, boolean namespaceAware) throws javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXException, java.io.IOExceptionParses the XML content from the given byte array.- Parameters:
data- the raw XML data (with unknown encoding)namespaceAware- whether the parser should be namespace aware- Returns:
- the corresponding document
- Throws:
javax.xml.parsers.ParserConfigurationException- if a SAX parser is not availableorg.xml.sax.SAXException- if the document contains a parsing errorjava.io.IOException- if something is seriously wrong. This should not happen since the input source is known to be constructed from a string.
-
parse
public static org.w3c.dom.Document parse(byte[] data, boolean namespaceAware, java.util.List<java.lang.String> parseErrors) throws javax.xml.parsers.ParserConfigurationException, java.io.IOExceptionParses the XML content from the given byte array.If a non-recoverable parser error is encountered, parsing stops, an error message is added to the
parseErrorslist, and the returned document contains the elements up to the one where the error was encountered.- Parameters:
data- the raw XML data (with unknown encoding)namespaceAware- whether the parser should be namespace awareparseErrors- parsing errors, if any, are appended to this list- Returns:
- the corresponding document
- Throws:
javax.xml.parsers.ParserConfigurationException- if a SAX parser is not availablejava.io.IOException- if something is seriously wrong. This should not happen since the input source is known to be constructed from a string.
-
parse
public static org.w3c.dom.Document parse(java.lang.String xml, boolean namespaceAware) throws javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXException, java.io.IOExceptionParses the given XML content.- Parameters:
xml- the XML string to be parsed. This must be in the correct encoding alreadynamespaceAware- whether the parser should be namespace aware- Returns:
- the corresponding document
- Throws:
javax.xml.parsers.ParserConfigurationException- if a SAX parser is not availableorg.xml.sax.SAXException- if the document contains a parsing errorjava.io.IOException- if something is seriously wrong. This should not happen since the input source is known to be constructed from a string
-
getXmlString
public static java.lang.String getXmlString(byte[] data)Returns the String corresponding to the given byte array of XML data (with unknown encoding). This method attempts to guess the encoding based on the XML prologue.- Parameters:
data- the XML data to be decoded into a string- Returns:
- a string corresponding to the XML data
-
getXmlString
public static java.lang.String getXmlString(byte[] data, java.lang.String defaultCharset)Returns the String corresponding to the given byte array of XML data (with unknown encoding). This method attempts to guess the encoding based on the XML prologue.- Parameters:
data- the XML data to be decoded into a stringdefaultCharset- the default charset to use if not specified by an encoding prologue attribute or a byte order mark- Returns:
- a string corresponding to the XML data
-
getPosition
Returns the position for the given node. This is the start position. The end position can be obtained viaPositionXmlParser.Position.getEnd().- Parameters:
node- the node to look up position for- Returns:
- the position, or null if the node type is not supported for position info
-
getPosition
Returns the position for the given node. This is the start position. The end position can be obtained viaPositionXmlParser.Position.getEnd(). A specific range within the node can be specified with thestartandendparameters.- Parameters:
node- the node to look up position forstart- the relative offset within the node range to use as the starting position, inclusive, or -1 to not limit the rangeend- the relative offset within the node range to use as the ending position, or -1 to not limit the range- Returns:
- the position, or null if the node type is not supported for position info
-
findNodeAtOffset
public static org.w3c.dom.Node findNodeAtOffset(org.w3c.dom.Document document, int offset)Finds the leaf node at the given offset.- Parameters:
document- root nodeoffset- offset to look for- Returns:
- the leaf node at that offset, if any
-
findNodeAtLineAndCol
public static org.w3c.dom.Node findNodeAtLineAndCol(org.w3c.dom.Document document, int line, int column)Finds the leaf node at the given offset.- Parameters:
document- root nodeline- the linecolumn- the column, or -1- Returns:
- the leaf node at that offset, if any
-