/** * 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); }
/** * 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); }
/** * 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); }