Search in sources :

Example 6 with DefaultHandler

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;
}
Also used : InputSource(org.xml.sax.InputSource) ArrayList(java.util.ArrayList) Attributes(org.xml.sax.Attributes) StringReader(java.io.StringReader) XMLReader(org.xml.sax.XMLReader) DefaultHandler(org.xml.sax.helpers.DefaultHandler)

Example 7 with DefaultHandler

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));
        }
    });
}
Also used : Attributes(org.xml.sax.Attributes) DefaultHandler(org.xml.sax.helpers.DefaultHandler)

Example 8 with DefaultHandler

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());
}
Also used : DefaultHandler(org.xml.sax.helpers.DefaultHandler)

Example 9 with 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 */
    }
}
Also used : Attributes(org.xml.sax.Attributes) ContentHandler(org.xml.sax.ContentHandler) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException)

Example 10 with DefaultHandler

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");
}
Also used : DefaultHandler(org.xml.sax.helpers.DefaultHandler)

Aggregations

DefaultHandler (org.xml.sax.helpers.DefaultHandler)139 InputStream (java.io.InputStream)61 Metadata (org.apache.tika.metadata.Metadata)59 ParseContext (org.apache.tika.parser.ParseContext)52 Test (org.junit.Test)43 Attributes (org.xml.sax.Attributes)39 SAXParser (javax.xml.parsers.SAXParser)35 SAXException (org.xml.sax.SAXException)34 ByteArrayInputStream (java.io.ByteArrayInputStream)30 SAXParserFactory (javax.xml.parsers.SAXParserFactory)25 IOException (java.io.IOException)24 Parser (org.apache.tika.parser.Parser)22 InputSource (org.xml.sax.InputSource)21 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)20 TikaInputStream (org.apache.tika.io.TikaInputStream)20 ContentHandler (org.xml.sax.ContentHandler)20 File (java.io.File)17 AutoDetectParser (org.apache.tika.parser.AutoDetectParser)17 BodyContentHandler (org.apache.tika.sax.BodyContentHandler)16 FileInputStream (java.io.FileInputStream)14