Search in sources :

Example 36 with EntityResolver

use of org.xml.sax.EntityResolver in project commons-gdx by gemserk.

the class DocumentParser method parse.

/**
 * Returns an XML Document from an InputStream using a DocumentBuilder with the specified validation and namespaceaware flags.
 *
 * @param schema
 *            The Schema to be used to parse the Document, null if no Schema wanted.
 * @param is
 *            The InputStream to be processed.
 * @param validating
 *            If the document should be validated or not
 * @param namespaceaware
 *            If the document should be processed with name space awareness or not.
 */
public Document parse(Schema schema, InputStream is, boolean validating, boolean namespaceaware) {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        if (schema != null)
            factory.setSchema(schema);
        factory.setValidating(validating);
        factory.setNamespaceAware(namespaceaware);
        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setErrorHandler(new DefaultErrorHandler());
        builder.setEntityResolver(new EntityResolver() {

            public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
                return new InputSource(new ByteArrayInputStream(new byte[0]));
            }
        });
        return builder.parse(is);
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    } catch (SAXException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) EntityResolver(org.xml.sax.EntityResolver) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 37 with EntityResolver

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

the class ParserTest method testCustomEntityResolver.

public void testCustomEntityResolver() throws ParserConfigurationException, SAXException, IOException {
    try {
        DocumentBuilderFactory dbf = new DocumentBuilderFactory() {

            DocumentBuilderFactory def = DocumentBuilderFactory.newInstance();

            @Override
            public void setFeature(String name, boolean value) throws ParserConfigurationException {
                def.setFeature(name, value);
            }

            @Override
            public void setAttribute(String name, Object value) throws IllegalArgumentException {
                def.setAttribute(name, value);
            }

            @Override
            public DocumentBuilder newDocumentBuilder() throws ParserConfigurationException {
                DocumentBuilder db = def.newDocumentBuilder();
                db.setEntityResolver(new EntityResolver() {

                    @Override
                    public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
                        if ("foo:test".equals(systemId)) {
                            return new InputSource(new ByteArrayInputStream("foo&bar".getBytes("UTF-8")));
                        } else {
                            return null;
                        }
                    }
                });
                return db;
            }

            @Override
            public boolean getFeature(String name) throws ParserConfigurationException {
                return def.getFeature(name);
            }

            @Override
            public Object getAttribute(String name) throws IllegalArgumentException {
                return def.getAttribute(name);
            }
        };
        DomUtil.setBuilderFactory(dbf);
        String testBody = "<?xml version='1.0'?>\n<!DOCTYPE foo [" + " <!ENTITY test SYSTEM \"foo:test\">" + "]>\n<foo>&test;</foo>";
        InputStream is = new ByteArrayInputStream(testBody.getBytes("UTF-8"));
        Document d = DomUtil.parseDocument(is);
        Element root = d.getDocumentElement();
        String text = DomUtil.getText(root);
        assertEquals("custom entity resolver apparently not called", "foo&bar", text);
    } finally {
        DomUtil.setBuilderFactory(null);
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) EntityResolver(org.xml.sax.EntityResolver) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 38 with EntityResolver

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

the class XPathUtil method makeDocumentBuilder.

/**
 * Create a DocumentBuilder using the makeDocumentFactory func.
 *
 * @param validate should the parser validate documents?
 * @param whitespace should the parser eliminate whitespace in element content?
 * @param namespace should the parser be namespace aware?
 * @param downloadDTDs if true, parser should attempt to resolve external entities
 * @return document builder
 * @throws ParserConfigurationException if {@link DocumentBuilder} can not be created for the wanted configuration
 */
public static DocumentBuilder makeDocumentBuilder(boolean validate, boolean whitespace, boolean namespace, boolean downloadDTDs) throws ParserConfigurationException {
    DocumentBuilder builder = makeDocumentBuilderFactory(validate, whitespace, namespace).newDocumentBuilder();
    builder.setErrorHandler(new MyErrorHandler(validate, false));
    if (!downloadDTDs) {
        EntityResolver er = (publicId, systemId) -> new InputSource(new ByteArrayInputStream(new byte[0]));
        builder.setEntityResolver(er);
    }
    return builder;
}
Also used : Transformer(javax.xml.transform.Transformer) XMLInputFactory(javax.xml.stream.XMLInputFactory) StreamResult(javax.xml.transform.stream.StreamResult) LoggerFactory(org.slf4j.LoggerFactory) StringUtils(org.apache.commons.lang3.StringUtils) XMLStreamConstants(javax.xml.stream.XMLStreamConstants) ErrorHandler(org.xml.sax.ErrorHandler) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLStreamReader(javax.xml.stream.XMLStreamReader) Document(org.w3c.dom.Document) XMLStreamException(javax.xml.stream.XMLStreamException) XdmNode(net.sf.saxon.s9api.XdmNode) EntityResolver(org.xml.sax.EntityResolver) PrintWriter(java.io.PrintWriter) XPathAPI(org.apache.xpath.XPathAPI) LoadingCache(com.github.benmanes.caffeine.cache.LoadingCache) Processor(net.sf.saxon.s9api.Processor) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) SAXException(org.xml.sax.SAXException) XObject(org.apache.xpath.objects.XObject) Tidy(org.w3c.tidy.Tidy) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) XdmValue(net.sf.saxon.s9api.XdmValue) DOMSource(javax.xml.transform.dom.DOMSource) TransformerException(javax.xml.transform.TransformerException) Source(javax.xml.transform.Source) ArrayList(java.util.ArrayList) XdmItem(net.sf.saxon.s9api.XdmItem) Node(org.w3c.dom.Node) SaxonApiException(net.sf.saxon.s9api.SaxonApiException) XMLConstants(javax.xml.XMLConstants) OutputStream(java.io.OutputStream) InputSource(org.xml.sax.InputSource) Caffeine(com.github.benmanes.caffeine.cache.Caffeine) Logger(org.slf4j.Logger) NodeList(org.w3c.dom.NodeList) XPathSelector(net.sf.saxon.s9api.XPathSelector) StringWriter(java.io.StringWriter) XPathExecutable(net.sf.saxon.s9api.XPathExecutable) IOException(java.io.IOException) OutputKeys(javax.xml.transform.OutputKeys) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) FactoryConfigurationError(javax.xml.stream.FactoryConfigurationError) SAXSource(javax.xml.transform.sax.SAXSource) SAXParseException(org.xml.sax.SAXParseException) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) AssertionResult(org.apache.jmeter.assertions.AssertionResult) TransformerFactory(javax.xml.transform.TransformerFactory) PrefixResolver(org.apache.xml.utils.PrefixResolver) InputStream(java.io.InputStream) InputSource(org.xml.sax.InputSource) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) EntityResolver(org.xml.sax.EntityResolver)

Example 39 with EntityResolver

use of org.xml.sax.EntityResolver in project j2objc by google.

the class ParserAdapterTest method testGetSetEntityResolver.

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

Example 40 with EntityResolver

use of org.xml.sax.EntityResolver in project j2objc by google.

the class XMLReaderAdapterTest method testSetEntityResolver.

public void testSetEntityResolver() {
    EntityResolver resolver = new MockResolver();
    // Ordinary case
    adapter.setEntityResolver(resolver);
    assertEquals(resolver, reader.getEntityResolver());
    // null case
    adapter.setEntityResolver(null);
    assertEquals(null, reader.getEntityResolver());
}
Also used : EntityResolver(org.xml.sax.EntityResolver) MockResolver(org.apache.harmony.tests.org.xml.sax.support.MockResolver)

Aggregations

EntityResolver (org.xml.sax.EntityResolver)85 InputSource (org.xml.sax.InputSource)41 SAXException (org.xml.sax.SAXException)37 IOException (java.io.IOException)32 DefaultHandler (org.xml.sax.helpers.DefaultHandler)25 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)23 DocumentBuilder (javax.xml.parsers.DocumentBuilder)22 Test (org.junit.jupiter.api.Test)22 Document (org.w3c.dom.Document)18 StringReader (java.io.StringReader)15 Field (java.lang.reflect.Field)14 SAXParseException (org.xml.sax.SAXParseException)14 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)11 NodeList (org.w3c.dom.NodeList)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 InputStream (java.io.InputStream)10 ArrayList (java.util.ArrayList)9 Node (org.w3c.dom.Node)9 ErrorHandler (org.xml.sax.ErrorHandler)9 Element (org.w3c.dom.Element)8