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;
}
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());
}
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));
}
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());
}
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));
}
Aggregations