/** * Returns the position for the given node. This is the start position. The * end position can be obtained via {@link Position#getEnd()}. * * @param node the node to look up position for * @return the position, or null if the node type is not supported for * position info */ @NonNull public static SourcePosition getPosition(@NonNull Node node) { return getPosition(node, -1, -1); }
/** * @see PositionXmlParser#parse(InputStream, boolean) */ @NonNull public static Document parse(@NonNull InputStream input) throws IOException, SAXException, ParserConfigurationException { return parse(input, true); }
@Nullable private static Node findNodeAtOffset(@NonNull Node node, int offset) { Position p = getPositionHelper(node, -1, -1); if (p != null) { if (offset < p.getOffset()) { for (int i = 0, n = children.getLength(); i < n; i++) { Node item = children.item(i); Node match = findNodeAtOffset(item, offset); if (match != null) { return match; for (int i = 0, n = attributes.getLength(); i < n; i++) { Node item = attributes.item(i); Node match = findNodeAtOffset(item, offset); if (match != null) { return match;
/** * Parses the XML content from the given byte array * * @param data the raw XML data (with unknown encoding) * @param checkDtd whether or not download the DTD and validate it * @return the corresponding document * @throws ParserConfigurationException if a SAX parser is not available * @throws SAXException if the document contains a parsing error * @throws IOException if something is seriously wrong. This should not * happen since the input source is known to be constructed from * a string. */ @NonNull public static Document parse(@NonNull byte[] data, boolean checkDtd) throws ParserConfigurationException, SAXException, IOException { String xml = getXmlString(data); xml = XmlUtils.stripBom(xml); return parse(xml, new InputSource(new StringReader(xml)), true, checkDtd); }
/** * Finds the leaf node at the given offset * * @param document root node * @param offset offset to look for * @return the leaf node at that offset, if any */ @Nullable public static Node findNodeAtOffset(@NonNull Document document, int offset) { Element root = document.getDocumentElement(); if (root != null) { return findNodeAtOffset(root, offset); } return null; }
/** * 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. * @param data the XML data to be decoded into a string * @return a string corresponding to the XML data */ @NonNull public static String getXmlString(@NonNull byte[] data) { return getXmlString(data, UTF_8); }
@Nullable private static Node findNodeAtLineAndCol(@NonNull Node node, int line, int column) { Position p = getPositionHelper(node, -1, -1); if (p != null) { if (line < p.getLine() || line == p.getLine() && column != -1 for (int i = 0, n = children.getLength(); i < n; i++) { Node item = children.item(i); Node match = findNodeAtLineAndCol(item, line, column); if (match != null) { return match; for (int i = 0, n = attributes.getLength(); i < n; i++) { Node item = attributes.item(i); Node match = findNodeAtLineAndCol(item, line, column); if (match != null) { return match;
/** * Finds the leaf node at the given offset * * @param document root node * @param line the line * @param column the column, or -1 * @return the leaf node at that offset, if any */ @Nullable public static Node findNodeAtLineAndCol(@NonNull Document document, int line, int column) { Element root = document.getDocumentElement(); if (root != null) { return findNodeAtLineAndCol(root, line, column); } return null; }
/** * Returns the position for the given node. This is the start position. The * end position can be obtained via {@link Position#getEnd()}. A specific * range within the node can be specified with the {@code start} and * {@code end} parameters. * * @param node the node to look up position for * @param start the relative offset within the node range to use as the * starting position, inclusive, or -1 to not limit the range * @param end the relative offset within the node range to use as the ending * position, or -1 to not limit the range * @return the position, or null if the node type is not supported for * position info */ @NonNull public static SourcePosition getPosition(@NonNull Node node, int start, int end) { Position p = getPositionHelper(node, start, end); return p == null ? SourcePosition.UNKNOWN : p.toSourcePosition(); }
@Nullable @Override public Node findNodeAt(@NonNull XmlContext context, int offset) { return PositionXmlParser.findNodeAtOffset(context.document, offset); }
/** * Returns the encoded String for the given file. This is usually the * same as {@code Files.toString(file, Charsets.UTF8}, but if there's a UTF byte order mark * (for UTF8, UTF_16 or UTF_16LE), use that instead. * * @param client the client to use for I/O operations * @param file the file to read from * @param createString If true, create a {@link String} instead of a general {@link * CharSequence} * @return the string * @throws IOException if the file cannot be read properly */ @NonNull public static CharSequence getEncodedString( @NonNull LintClient client, @NonNull File file, boolean createString) throws IOException { byte[] bytes = client.readBytes(file); if (endsWith(file.getName(), DOT_XML)) { return PositionXmlParser.getXmlString(bytes); } return getEncodedString(bytes, createString); }
public static SourceFilePosition getSourceFilePosition(Node node) { SourceFile sourceFile = (SourceFile) node.getUserData(SOURCE_FILE_USER_DATA_KEY); if (sourceFile == null) { sourceFile = SourceFile.UNKNOWN; } return new SourceFilePosition(sourceFile, PositionXmlParser.getPosition(node)); }
/** * @see PositionXmlParser#parse(byte[], boolean) */ @NonNull public static Document parse(@NonNull byte[] data) throws ParserConfigurationException, SAXException, IOException { return parse(data, true); }
@NonNull @Override public Location resolve() { return Location.create(file, PositionXmlParser.getPosition(node)); }
/** * Parses the XML content from the given input stream. * * @param input the input stream containing the XML to be parsed * @param checkDtd whether or not download the DTD and validate it * @return the corresponding document * @throws ParserConfigurationException if a SAX parser is not available * @throws SAXException if the document contains a parsing error * @throws IOException if something is seriously wrong. This should not * happen since the input source is known to be constructed from * a string. */ @NonNull public static Document parse(@NonNull InputStream input, boolean checkDtd) throws ParserConfigurationException, SAXException, IOException { // Read in all the data ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; while (true) { int r = input.read(buf); if (r == -1) { break; } out.write(buf, 0, r); } input.close(); return parse(out.toByteArray(), checkDtd); }
/** * Returns the position of the specified {@link org.w3c.dom.Node}. */ @NonNull static SourcePosition getNodePosition(@NonNull Node xml) { return PositionXmlParser.getPosition(xml); }
/** * Parses the given XML content. * * @param xml the XML string to be parsed. This must be in the correct * encoding already. * @return the corresponding document * @throws ParserConfigurationException if a SAX parser is not available * @throws SAXException if the document contains a parsing error * @throws IOException if something is seriously wrong. This should not * happen since the input source is known to be constructed from * a string. */ @NonNull public static Document parse(@NonNull String xml) throws ParserConfigurationException, SAXException, IOException { xml = XmlUtils.stripBom(xml); return parse(xml, new InputSource(new StringReader(xml)), true, true); }
@Override public int getNodeEndOffset(@NonNull XmlContext context, @NonNull Node node) { return PositionXmlParser.getPosition(node).getEndOffset(); }
return parse(xml, new InputSource(new StringReader(xml)), false, checkDtd);
@Override public int getNodeStartOffset(@NonNull XmlContext context, @NonNull Node node) { return PositionXmlParser.getPosition(node).getStartOffset(); }