Search in sources :

Example 41 with Attributes

use of org.xml.sax.Attributes 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 42 with Attributes

use of org.xml.sax.Attributes in project spring-framework by spring-projects.

the class AbstractStaxXMLReaderTestCase method mockContentHandler.

protected final ContentHandler mockContentHandler() throws Exception {
    ContentHandler contentHandler = mock(ContentHandler.class);
    willAnswer(new CopyCharsAnswer()).given(contentHandler).characters(any(char[].class), anyInt(), anyInt());
    willAnswer(new CopyCharsAnswer()).given(contentHandler).ignorableWhitespace(any(char[].class), anyInt(), anyInt());
    willAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            invocation.getArguments()[3] = new AttributesImpl((Attributes) invocation.getArguments()[3]);
            return null;
        }
    }).given(contentHandler).startElement(anyString(), anyString(), anyString(), any(Attributes.class));
    return contentHandler;
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Attributes(org.xml.sax.Attributes) ContentHandler(org.xml.sax.ContentHandler)

Example 43 with Attributes

use of org.xml.sax.Attributes 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 44 with Attributes

use of org.xml.sax.Attributes 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 45 with Attributes

use of org.xml.sax.Attributes in project camel by apache.

the class ManualGenerator method grabBodyContent.

private String grabBodyContent() throws MalformedURLException, IOException {
    URL url = new URL(page);
    File file = new File(targetDir, ".manualCache-" + url.getFile().substring(1));
    try {
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        XMLReader parser = new Parser();
        parser.setFeature(Parser.namespacesFeature, false);
        parser.setFeature(Parser.namespacePrefixesFeature, false);
        parser.setProperty(Parser.schemaProperty, new org.ccil.cowan.tagsoup.HTMLSchema() {

            {
                //problem with nested lists that the confluence {toc} macro creates
                elementType("ul", M_LI, M_BLOCK | M_LI, 0);
            }
        });
        StringWriter w = new StringWriter();
        XMLWriter xmlWriter = new XMLWriter(w) {

            int inDiv = Integer.MAX_VALUE;

            int count;

            public void characters(char[] ch, int start, int len) throws SAXException {
                if (inDiv <= count) {
                    super.characters(ch, start, len);
                }
            }

            public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
                count++;
                if ("div".equalsIgnoreCase(qName) && "wiki-content maincontent".equalsIgnoreCase(atts.getValue("class"))) {
                    inDiv = count;
                }
                if (inDiv <= count) {
                    super.startElement(uri, localName, qName, atts);
                }
            }

            public void endElement(String uri, String localName, String qName) throws SAXException {
                if (inDiv <= count) {
                    super.endElement(uri, localName, qName);
                }
                count--;
                if (inDiv > count) {
                    inDiv = Integer.MAX_VALUE;
                }
            }
        };
        xmlWriter.setOutputProperty(XMLWriter.OMIT_XML_DECLARATION, "yes");
        xmlWriter.setOutputProperty(XMLWriter.METHOD, "html");
        parser.setContentHandler(xmlWriter);
        long date = con.getLastModified();
        parser.parse(new InputSource(new BufferedInputStream(con.getInputStream())));
        FileWriter writer = new FileWriter(file);
        writer.write(Long.toString(date));
        writer.close();
        return w.toString();
    } catch (Throwable e) {
        e.printStackTrace();
        throw new RuntimeException("Failed", e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) FileWriter(java.io.FileWriter) Attributes(org.xml.sax.Attributes) XMLWriter(org.ccil.cowan.tagsoup.XMLWriter) URL(java.net.URL) Parser(org.ccil.cowan.tagsoup.Parser) HttpURLConnection(java.net.HttpURLConnection) StringWriter(java.io.StringWriter) BufferedInputStream(java.io.BufferedInputStream) File(java.io.File) XMLReader(org.xml.sax.XMLReader)

Aggregations

Attributes (org.xml.sax.Attributes)274 DefaultHandler (org.xml.sax.helpers.DefaultHandler)70 SAXException (org.xml.sax.SAXException)66 Test (org.junit.Test)59 AttributesImpl (org.xml.sax.helpers.AttributesImpl)50 SAXParser (javax.xml.parsers.SAXParser)48 POSaveFailedException (org.adempiere.pipo.exception.POSaveFailedException)37 InputSource (org.xml.sax.InputSource)31 SAXParserFactory (javax.xml.parsers.SAXParserFactory)30 IOException (java.io.IOException)29 File (java.io.File)22 ByteArrayInputStream (java.io.ByteArrayInputStream)19 ArrayList (java.util.ArrayList)17 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)17 InputStream (java.io.InputStream)16 ContentHandler (org.xml.sax.ContentHandler)15 XMLReader (org.xml.sax.XMLReader)15 StringReader (java.io.StringReader)10 Transformer (org.apache.sling.rewriter.Transformer)10 MockBundle (org.apache.sling.commons.testing.osgi.MockBundle)9