Search in sources :

Example 21 with ExpatReader

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

the class ExpatSaxParserTest method runDtdTest.

private TestDtdHandler runDtdTest(String s) throws Exception {
    Reader in = new StringReader(s);
    ExpatReader reader = new ExpatReader();
    TestDtdHandler handler = new TestDtdHandler();
    reader.setContentHandler(handler);
    reader.setDTDHandler(handler);
    reader.setLexicalHandler(handler);
    reader.parse(new InputSource(in));
    return handler;
}
Also used : InputSource(org.xml.sax.InputSource) ExpatReader(org.apache.harmony.xml.ExpatReader) StringReader(java.io.StringReader) ExpatReader(org.apache.harmony.xml.ExpatReader) Reader(java.io.Reader) XMLReader(org.xml.sax.XMLReader) StringReader(java.io.StringReader)

Example 22 with ExpatReader

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

the class ExpatSaxParserTest method testExternalEntity.

public void testExternalEntity() throws IOException, SAXException {
    class Handler extends DefaultHandler {

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

        StringBuilder text = new StringBuilder();

        public InputSource resolveEntity(String publicId, String systemId) throws IOException, SAXException {
            if (publicId.equals("publicA") && systemId.equals("systemA")) {
                return new InputSource(new StringReader("<a/>"));
            } else if (publicId.equals("publicB") && systemId.equals("systemB")) {
                /*
                     * Explicitly set the encoding here or else the parser will
                     * try to use the parent parser's encoding which is utf-16.
                     */
                InputSource inputSource = new InputSource(new ByteArrayInputStream("bob".getBytes("utf-8")));
                inputSource.setEncoding("utf-8");
                return inputSource;
            }
            throw new AssertionError();
        }

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

        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            elementNames.add("/" + localName);
        }

        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            text.append(ch, start, length);
        }
    }
    Reader in = new StringReader("<?xml version=\"1.0\"?>\n" + "<!DOCTYPE foo [\n" + "  <!ENTITY a PUBLIC 'publicA' 'systemA'>\n" + "  <!ENTITY b PUBLIC 'publicB' 'systemB'>\n" + "]>\n" + "<foo>\n" + "  &a;<b>&b;</b></foo>");
    ExpatReader reader = new ExpatReader();
    Handler handler = new Handler();
    reader.setContentHandler(handler);
    reader.setEntityResolver(handler);
    reader.parse(new InputSource(in));
    assertEquals(Arrays.asList("foo", "a", "/a", "b", "/b", "/foo"), handler.elementNames);
    assertEquals("bob", handler.text.toString().trim());
}
Also used : InputSource(org.xml.sax.InputSource) ExpatReader(org.apache.harmony.xml.ExpatReader) Attributes(org.xml.sax.Attributes) 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) DefaultHandler(org.xml.sax.helpers.DefaultHandler) ByteArrayInputStream(java.io.ByteArrayInputStream) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) List(java.util.List)

Example 23 with ExpatReader

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

the class ExpatSaxParserTest method parse.

/**
     * Parses xml from the given reader and fires events on the given SAX
     * handler.
     */
private 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 24 with ExpatReader

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

the class ExpatSaxParserTest method testCdata.

public void testCdata() throws Exception {
    Reader in = new StringReader("<a><![CDATA[<b></b>]]> <![CDATA[<c></c>]]></a>");
    ExpatReader reader = new ExpatReader();
    TestCdataHandler handler = new TestCdataHandler();
    reader.setContentHandler(handler);
    reader.setLexicalHandler(handler);
    reader.parse(new InputSource(in));
    assertEquals(2, handler.startCdata);
    assertEquals(2, handler.endCdata);
    assertEquals("<b></b> <c></c>", handler.buffer.toString());
}
Also used : InputSource(org.xml.sax.InputSource) ExpatReader(org.apache.harmony.xml.ExpatReader) StringReader(java.io.StringReader) ExpatReader(org.apache.harmony.xml.ExpatReader) Reader(java.io.Reader) XMLReader(org.xml.sax.XMLReader) StringReader(java.io.StringReader)

Example 25 with ExpatReader

use of org.apache.harmony.xml.ExpatReader in project XobotOS by xamarin.

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)

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