use of org.xml.sax.helpers.DefaultHandler in project robovm by robovm.
the class NamespacedAttributesLookupTest method getStartElements.
public List<String> getStartElements(String xml, final boolean namespace, boolean namespacePrefixes) throws Exception {
final List<String> result = new ArrayList<String>();
XMLReader reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
reader.setFeature(SAX_PROPERTY_NS, namespace);
reader.setFeature(SAX_PROPERTY_NS_PREFIXES, namespacePrefixes);
reader.setContentHandler(new DefaultHandler() {
@Override
public final void startElement(String uri, String localName, String qName, Attributes attributes) {
StringBuilder serialized = new StringBuilder();
/*
* Only supply the uri+localName or qname depending on whether namespaces are
* enabled. It's an optional parameter and the RI only supplies one or the other.
*/
if (namespace) {
serialized.append(uri).append(",");
serialized.append(localName);
} else {
serialized.append(qName);
}
for (int i = 0; i < attributes.getLength(); i++) {
serialized.append("\n ");
if (namespace) {
serialized.append(attributes.getURI(i)).append(",");
serialized.append(attributes.getLocalName(i));
} else {
serialized.append(attributes.getQName(i));
}
}
serialized.append("\n http://bar+c=").append(attributes.getValue("http://bar", "c")).append(",").append("\n bar:c=").append(attributes.getValue("bar:c")).append("\n");
result.add(serialized.toString());
}
});
reader.parse(new InputSource(new StringReader(xml)));
return result;
}
use of org.xml.sax.helpers.DefaultHandler in project robovm by robovm.
the class SaxTest method testYesPrefixesYesNamespaces.
/**
* Android's Expat-based SAX parser fails this test because Expat doesn't
* supply us with our much desired {@code xmlns="http://..."} attributes.
*/
public void testYesPrefixesYesNamespaces() throws Exception {
parse(true, true, "<foo bar=\"baz\"/>", new DefaultHandler() {
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) {
assertEquals("", uri);
assertEquals("foo", localName);
assertEquals("foo", qName);
assertEquals(1, attributes.getLength());
assertEquals("", attributes.getURI(0));
assertEquals("bar", attributes.getLocalName(0));
assertEquals("bar", attributes.getQName(0));
}
});
parse(true, true, "<a:foo a:bar=\"baz\" xmlns:a=\"http://quux\"/>", new DefaultHandler() {
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) {
assertEquals("http://quux", uri);
assertEquals("foo", localName);
assertEquals("a:foo", qName);
assertEquals(2, attributes.getLength());
assertEquals("http://quux", attributes.getURI(0));
assertEquals("bar", attributes.getLocalName(0));
assertEquals("a:bar", attributes.getQName(0));
assertEquals("", attributes.getURI(1));
assertEquals("", attributes.getLocalName(1));
assertEquals("xmlns:a", attributes.getQName(1));
}
});
}
use of org.xml.sax.helpers.DefaultHandler in project robovm by robovm.
the class ExpatSaxParserTest method testGlobalReferenceTableOverflow.
public void testGlobalReferenceTableOverflow() throws Exception {
// We used to use a JNI global reference per interned string.
// Framework apps have a limit of 2000 JNI global references per VM.
StringBuilder xml = new StringBuilder();
xml.append("<root>");
for (int i = 0; i < 4000; ++i) {
xml.append("<tag" + i + ">");
xml.append("</tag" + i + ">");
}
xml.append("</root>");
parse(xml.toString(), new DefaultHandler());
}
use of org.xml.sax.helpers.DefaultHandler in project robovm by robovm.
the class ExpatSaxParserTest method testExceptions.
public void testExceptions() {
// From startElement().
ContentHandler contentHandler = new DefaultHandler() {
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
throw new SAXException();
}
};
try {
parse(SNIPPET, contentHandler);
fail();
} catch (SAXException checked) {
/* expected */
}
// From endElement().
contentHandler = new DefaultHandler() {
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
throw new SAXException();
}
};
try {
parse(SNIPPET, contentHandler);
fail();
} catch (SAXException checked) {
/* expected */
}
// From characters().
contentHandler = new DefaultHandler() {
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
throw new SAXException();
}
};
try {
parse(SNIPPET, contentHandler);
fail();
} catch (SAXException checked) {
/* expected */
}
}
use of org.xml.sax.helpers.DefaultHandler in project platform_frameworks_base by android.
the class ExpatPerformanceTest method runSax.
private void runSax() throws IOException, SAXException {
long start = System.currentTimeMillis();
Xml.parse(newInputStream(), Xml.Encoding.UTF_8, new DefaultHandler());
long elapsed = System.currentTimeMillis() - start;
Log.i(TAG, "expat SAX: " + elapsed + "ms");
}
Aggregations