Search in sources :

Example 6 with MockResolver

use of tests.api.org.xml.sax.support.MockResolver in project robovm by robovm.

the class DocumentBuilderTest method testSetEntityResolver.

public void testSetEntityResolver() {
    // Ordinary case
    InputStream source = new ByteArrayInputStream("<a>&foo;</a>".getBytes());
    InputStream entity = new ByteArrayInputStream("bar".getBytes());
    MockResolver resolver = new MockResolver();
    resolver.addEntity("foo", "foo", new InputSource(entity));
    Document d;
    try {
        db = dbf.newDocumentBuilder();
        db.setEntityResolver(resolver);
        d = db.parse(source);
    } catch (Exception e) {
        throw new RuntimeException("Unexpected exception", e);
    }
    Element root = (Element) d.getElementsByTagName("a").item(0);
    assertEquals("bar", ((Text) root.getFirstChild()).getData());
    // null case
    source = new ByteArrayInputStream("<a>&foo;</a>".getBytes());
    try {
        db = dbf.newDocumentBuilder();
        db.setEntityResolver(null);
        d = db.parse(source);
    } catch (Exception e) {
        throw new RuntimeException("Unexpected exception", e);
    }
    root = (Element) d.getElementsByTagName("a").item(0);
    assertEquals("foo", ((EntityReference) root.getFirstChild()).getNodeName());
}
Also used : InputSource(org.xml.sax.InputSource) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) MockResolver(tests.api.org.xml.sax.support.MockResolver) Document(org.w3c.dom.Document) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException)

Aggregations

MockResolver (tests.api.org.xml.sax.support.MockResolver)6 IOException (java.io.IOException)3 EntityResolver (org.xml.sax.EntityResolver)3 InputSource (org.xml.sax.InputSource)3 SAXException (org.xml.sax.SAXException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 Document (org.w3c.dom.Document)2 Element (org.w3c.dom.Element)2 SAXParseException (org.xml.sax.SAXParseException)2 ErrorHandler (org.xml.sax.ErrorHandler)1 MethodLogger (tests.api.org.xml.sax.support.MethodLogger)1 MockHandler (tests.api.org.xml.sax.support.MockHandler)1