Search in sources :

Example 56 with InputSource

use of org.xml.sax.InputSource in project commons-gdx by gemserk.

the class DocumentParser method parse.

/**
	 * Returns an XML Document from an InputStream using a DocumentBuilder with the specified validation and namespaceaware flags.
	 * 
	 * @param schema
	 *            The Schema to be used to parse the Document, null if no Schema wanted.
	 * @param is
	 *            The InputStream to be processed.
	 * @param validating
	 *            If the document should be validated or not
	 * @param namespaceaware
	 *            If the document should be processed with name space awareness or not.
	 */
public Document parse(Schema schema, InputStream is, boolean validating, boolean namespaceaware) {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        if (schema != null)
            factory.setSchema(schema);
        factory.setValidating(validating);
        factory.setNamespaceAware(namespaceaware);
        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setErrorHandler(new DefaultErrorHandler());
        builder.setEntityResolver(new EntityResolver() {

            public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
                return new InputSource(new ByteArrayInputStream(new byte[0]));
            }
        });
        return builder.parse(is);
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    } catch (SAXException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) EntityResolver(org.xml.sax.EntityResolver) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 57 with InputSource

use of org.xml.sax.InputSource in project buck by facebook.

the class PositionXmlParser method parse.

/**
     * 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);
}
Also used : InputSource(org.xml.sax.InputSource) StringReader(java.io.StringReader) NonNull(com.android.annotations.NonNull)

Example 58 with InputSource

use of org.xml.sax.InputSource in project buck by facebook.

the class PositionXmlParser method parse.

@NonNull
private static Document parse(@NonNull String xml, @NonNull InputSource input, boolean checkBom, boolean checkDtd) throws ParserConfigurationException, SAXException, IOException {
    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        if (checkDtd) {
            factory.setFeature(NAMESPACE_FEATURE, true);
            factory.setFeature(NAMESPACE_PREFIX_FEATURE, true);
            factory.setFeature(PROVIDE_XMLNS_URIS, true);
        } else {
            factory.setFeature(LOAD_EXTERNAL_DTD, false);
        }
        SAXParser parser = factory.newSAXParser();
        DomBuilder handler = new DomBuilder(xml);
        XMLReader xmlReader = parser.getXMLReader();
        xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
        parser.parse(input, handler);
        return handler.getDocument();
    } catch (SAXException e) {
        if (checkBom && e.getMessage().contains("Content is not allowed in prolog")) {
            // Byte order mark in the string? Skip it. There are many markers
            // (see http://en.wikipedia.org/wiki/Byte_order_mark) so here we'll
            // just skip those up to the XML prolog beginning character, <
            //$NON-NLS-1$ //$NON-NLS-2$
            xml = xml.replaceFirst("^([\\W]+)<", "<");
            return parse(xml, new InputSource(new StringReader(xml)), false, checkDtd);
        }
        throw e;
    }
}
Also used : InputSource(org.xml.sax.InputSource) StringReader(java.io.StringReader) SAXParser(javax.xml.parsers.SAXParser) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException) NonNull(com.android.annotations.NonNull)

Example 59 with InputSource

use of org.xml.sax.InputSource in project buck by facebook.

the class XmlUtils method parseUtfXmlFile.

/**
     * Parses the given UTF file as a DOM document, using the JDK parser. The parser does not
     * validate, and is optionally namespace aware.
     *
     * @param file           the UTF encoded file to parse
     * @param namespaceAware whether the parser is namespace aware
     * @return the DOM document
     */
@NonNull
public static Document parseUtfXmlFile(@NonNull File file, boolean namespaceAware) throws ParserConfigurationException, IOException, SAXException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Reader reader = getUtfReader(file);
    try {
        InputSource is = new InputSource(reader);
        factory.setNamespaceAware(namespaceAware);
        factory.setValidating(false);
        DocumentBuilder builder = factory.newDocumentBuilder();
        return builder.parse(is);
    } finally {
        reader.close();
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) XMLStreamReader(javax.xml.stream.XMLStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) NonNull(com.android.annotations.NonNull)

Example 60 with InputSource

use of org.xml.sax.InputSource in project XobotOS by xamarin.

the class Xml method parse.

/**
     * Parses the given xml string and fires events on the given SAX handler.
     */
public static void parse(String xml, ContentHandler contentHandler) throws SAXException {
    try {
        XMLReader reader = new ExpatReader();
        reader.setContentHandler(contentHandler);
        reader.parse(new InputSource(new StringReader(xml)));
    } catch (IOException e) {
        throw new AssertionError(e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) ExpatReader(org.apache.harmony.xml.ExpatReader) StringReader(java.io.StringReader) IOException(java.io.IOException) XMLReader(org.xml.sax.XMLReader)

Aggregations

InputSource (org.xml.sax.InputSource)1124 StringReader (java.io.StringReader)401 IOException (java.io.IOException)304 Document (org.w3c.dom.Document)282 SAXException (org.xml.sax.SAXException)281 DocumentBuilder (javax.xml.parsers.DocumentBuilder)262 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)213 XMLReader (org.xml.sax.XMLReader)194 Test (org.junit.Test)160 InputStream (java.io.InputStream)158 NodeList (org.w3c.dom.NodeList)145 Element (org.w3c.dom.Element)144 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)143 ByteArrayInputStream (java.io.ByteArrayInputStream)103 SAXParser (javax.xml.parsers.SAXParser)103 SAXSource (javax.xml.transform.sax.SAXSource)95 SAXParserFactory (javax.xml.parsers.SAXParserFactory)90 File (java.io.File)82 Node (org.w3c.dom.Node)82 URL (java.net.URL)65