Search in sources :

Example 16 with ExpatReader

use of org.apache.harmony.xml.ExpatReader in project android_frameworks_base by crdroidandroid.

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)

Example 17 with ExpatReader

use of org.apache.harmony.xml.ExpatReader in project android_frameworks_base by crdroidandroid.

the class Xml method parse.

/**
     * Parses xml from the given input stream and fires events on the given SAX
     * handler.
     */
public static void parse(InputStream in, Encoding encoding, ContentHandler contentHandler) throws IOException, SAXException {
    XMLReader reader = new ExpatReader();
    reader.setContentHandler(contentHandler);
    InputSource source = new InputSource(in);
    source.setEncoding(encoding.expatName);
    reader.parse(source);
}
Also used : InputSource(org.xml.sax.InputSource) ExpatReader(org.apache.harmony.xml.ExpatReader) XMLReader(org.xml.sax.XMLReader)

Example 18 with ExpatReader

use of org.apache.harmony.xml.ExpatReader in project android_frameworks_base by DirtyUnicorns.

the class Xml method parse.

/**
     * Parses xml from the given input stream and fires events on the given SAX
     * handler.
     */
public static void parse(InputStream in, Encoding encoding, ContentHandler contentHandler) throws IOException, SAXException {
    XMLReader reader = new ExpatReader();
    reader.setContentHandler(contentHandler);
    InputSource source = new InputSource(in);
    source.setEncoding(encoding.expatName);
    reader.parse(source);
}
Also used : InputSource(org.xml.sax.InputSource) ExpatReader(org.apache.harmony.xml.ExpatReader) XMLReader(org.xml.sax.XMLReader)

Example 19 with ExpatReader

use of org.apache.harmony.xml.ExpatReader in project android_frameworks_base by ParanoidAndroid.

the class Xml method parse.

/**
     * Parses xml from the given reader and fires events on the given SAX
     * handler.
     */
public static void parse(Reader in, ContentHandler contentHandler) throws IOException, SAXException {
    XMLReader reader = new ExpatReader();
    reader.setContentHandler(contentHandler);
    reader.parse(new InputSource(in));
}
Also used : InputSource(org.xml.sax.InputSource) ExpatReader(org.apache.harmony.xml.ExpatReader) XMLReader(org.xml.sax.XMLReader)

Example 20 with ExpatReader

use of org.apache.harmony.xml.ExpatReader in project robovm by robovm.

the class ExpatSaxParserTest method testExternalEntityDownload.

public void testExternalEntityDownload() throws IOException, SAXException {
    final MockWebServer server = new MockWebServer();
    try {
        // RoboVM note: Modified to call server.shutdown() after test finishes.
        server.enqueue(new MockResponse().setBody("<bar></bar>"));
        server.play();
        class Handler extends DefaultHandler {

            final List<String> elementNames = new ArrayList<String>();

            @Override
            public InputSource resolveEntity(String publicId, String systemId) throws IOException {
                // The parser should have resolved the systemId.
                assertEquals(server.getUrl("/systemBar").toString(), systemId);
                return new InputSource(systemId);
            }

            @Override
            public void startElement(String uri, String localName, String qName, Attributes attributes) {
                elementNames.add(localName);
            }

            @Override
            public void endElement(String uri, String localName, String qName) {
                elementNames.add("/" + localName);
            }
        }
        // 'systemBar', the external entity, is relative to 'systemFoo':
        Reader in = new StringReader("<?xml version=\"1.0\"?>\n" + "<!DOCTYPE foo [\n" + "  <!ENTITY bar SYSTEM 'systemBar'>\n" + "]>\n" + "<foo>&bar;</foo>");
        ExpatReader reader = new ExpatReader();
        Handler handler = new Handler();
        reader.setContentHandler(handler);
        reader.setEntityResolver(handler);
        InputSource source = new InputSource(in);
        source.setSystemId(server.getUrl("/systemFoo").toString());
        reader.parse(source);
        assertEquals(Arrays.asList("foo", "bar", "/bar", "/foo"), handler.elementNames);
    } finally {
        server.shutdown();
    }
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) InputSource(org.xml.sax.InputSource) ExpatReader(org.apache.harmony.xml.ExpatReader) MockWebServer(com.google.mockwebserver.MockWebServer) Attributes(org.xml.sax.Attributes) StringReader(java.io.StringReader) ExpatReader(org.apache.harmony.xml.ExpatReader) Reader(java.io.Reader) XMLReader(org.xml.sax.XMLReader) StringReader(java.io.StringReader) DefaultHandler(org.xml.sax.helpers.DefaultHandler) ContentHandler(org.xml.sax.ContentHandler) ArrayList(java.util.ArrayList) List(java.util.List) DefaultHandler(org.xml.sax.helpers.DefaultHandler)

Aggregations

ExpatReader (org.apache.harmony.xml.ExpatReader)29 InputSource (org.xml.sax.InputSource)29 XMLReader (org.xml.sax.XMLReader)29 StringReader (java.io.StringReader)13 IOException (java.io.IOException)9 Reader (java.io.Reader)5 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Attributes (org.xml.sax.Attributes)2 ContentHandler (org.xml.sax.ContentHandler)2 DefaultHandler (org.xml.sax.helpers.DefaultHandler)2 MockResponse (com.google.mockwebserver.MockResponse)1 MockWebServer (com.google.mockwebserver.MockWebServer)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1