Search in sources :

Example 21 with EntityResolver

use of org.xml.sax.EntityResolver in project robovm by robovm.

the class XMLFilterImplTest method testGetSetEntityResolver.

public void testGetSetEntityResolver() {
    EntityResolver resolver = new MockResolver();
    parent.setEntityResolver(resolver);
    assertEquals(resolver, parent.getEntityResolver());
    parent.setEntityResolver(null);
    assertEquals(null, parent.getEntityResolver());
}
Also used : EntityResolver(org.xml.sax.EntityResolver) MockResolver(tests.api.org.xml.sax.support.MockResolver)

Example 22 with EntityResolver

use of org.xml.sax.EntityResolver in project cobar by alibaba.

the class ConfigUtil method getDocument.

public static Document getDocument(final InputStream dtd, InputStream xml) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);
    factory.setNamespaceAware(false);
    DocumentBuilder builder = factory.newDocumentBuilder();
    builder.setEntityResolver(new EntityResolver() {

        @Override
        public InputSource resolveEntity(String publicId, String systemId) {
            return new InputSource(dtd);
        }
    });
    builder.setErrorHandler(new ErrorHandler() {

        @Override
        public void warning(SAXParseException e) {
        }

        @Override
        public void error(SAXParseException e) throws SAXException {
            throw e;
        }

        @Override
        public void fatalError(SAXParseException e) throws SAXException {
            throw e;
        }
    });
    return builder.parse(xml);
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) EntityResolver(org.xml.sax.EntityResolver) SAXException(org.xml.sax.SAXException)

Example 23 with EntityResolver

use of org.xml.sax.EntityResolver in project Asqatasun by Asqatasun.

the class KbCsvMojo method getUrls.

private List<String> getUrls(String url) throws JDOMException, JaxenException, IOException {
    SAXBuilder builder = new SAXBuilder();
    EntityResolver resolver = new XhtmlEntityResolver();
    builder.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
    builder.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    builder.setEntityResolver(resolver);
    Document document = builder.build(new URL(url));
    XPath xpath = new JDOMXPath("//*[@id='resultat']//*[@href]/@href");
    List<Attribute> results = xpath.selectNodes(document);
    List<String> urls = new ArrayList<String>();
    for (Attribute attr : results) {
        urls.add(attr.getValue());
    }
    return urls;
}
Also used : XPath(org.jaxen.XPath) JDOMXPath(org.jaxen.jdom.JDOMXPath) SAXBuilder(org.jdom.input.SAXBuilder) JDOMXPath(org.jaxen.jdom.JDOMXPath) Attribute(org.jdom.Attribute) ArrayList(java.util.ArrayList) EntityResolver(org.xml.sax.EntityResolver) Document(org.jdom.Document) URL(java.net.URL)

Example 24 with EntityResolver

use of org.xml.sax.EntityResolver in project tomee by apache.

the class SuperProperties method getDocumentBuilder.

private DocumentBuilder getDocumentBuilder() {
    if (builder == null) {
        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(true);
        try {
            builder = factory.newDocumentBuilder();
        } catch (final ParserConfigurationException e) {
            throw new Error(e);
        }
        builder.setErrorHandler(new ErrorHandler() {

            public void warning(final SAXParseException e) throws SAXException {
                throw e;
            }

            public void error(final SAXParseException e) throws SAXException {
                throw e;
            }

            public void fatalError(final SAXParseException e) throws SAXException {
                throw e;
            }
        });
        builder.setEntityResolver(new EntityResolver() {

            public InputSource resolveEntity(final String publicId, final String systemId) throws SAXException, IOException {
                if (systemId.equals(PROP_DTD_NAME)) {
                    final InputSource result = new InputSource(new StringReader(PROP_DTD));
                    result.setSystemId(PROP_DTD_NAME);
                    return result;
                }
                throw new SAXException("Invalid DOCTYPE declaration: " + systemId);
            }
        });
    }
    return builder;
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) SAXParseException(org.xml.sax.SAXParseException) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) EntityResolver(org.xml.sax.EntityResolver) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Aggregations

EntityResolver (org.xml.sax.EntityResolver)24 InputSource (org.xml.sax.InputSource)19 SAXException (org.xml.sax.SAXException)16 IOException (java.io.IOException)12 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)10 DocumentBuilder (javax.xml.parsers.DocumentBuilder)9 SAXParseException (org.xml.sax.SAXParseException)9 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)7 Document (org.w3c.dom.Document)6 ErrorHandler (org.xml.sax.ErrorHandler)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 StringReader (java.io.StringReader)4 SAXBuilder (org.jdom.input.SAXBuilder)4 NodeList (org.w3c.dom.NodeList)4 Document (org.jdom.Document)3 Element (org.w3c.dom.Element)3 MockResolver (tests.api.org.xml.sax.support.MockResolver)3 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 SAXParser (javax.xml.parsers.SAXParser)2