Search in sources :

Example 11 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 12 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 = new EntityResolver() {

            @Override
            public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
                return new InputSource(new ByteArrayInputStream(new byte[] {}));
            }
        };
        builder.setEntityResolver(er);
    }
    return builder;
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) EntityResolver(org.xml.sax.EntityResolver)

Example 13 with EntityResolver

use of org.xml.sax.EntityResolver in project maven-plugins by apache.

the class XmlAppendingTransformer method processResource.

public void processResource(String resource, InputStream is, List<Relocator> relocators) throws IOException {
    Document r;
    try {
        SAXBuilder builder = new SAXBuilder(false);
        builder.setExpandEntities(false);
        if (ignoreDtd) {
            builder.setEntityResolver(new EntityResolver() {

                public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
                    return new InputSource(new StringReader(""));
                }
            });
        }
        r = builder.build(is);
    } catch (JDOMException e) {
        throw new RuntimeException("Error processing resource " + resource + ": " + e.getMessage(), e);
    }
    if (doc == null) {
        doc = r;
    } else {
        Element root = r.getRootElement();
        for (@SuppressWarnings("unchecked") Iterator<Attribute> itr = root.getAttributes().iterator(); itr.hasNext(); ) {
            Attribute a = itr.next();
            itr.remove();
            Element mergedEl = doc.getRootElement();
            Attribute mergedAtt = mergedEl.getAttribute(a.getName(), a.getNamespace());
            if (mergedAtt == null) {
                mergedEl.setAttribute(a);
            }
        }
        for (@SuppressWarnings("unchecked") Iterator<Content> itr = root.getChildren().iterator(); itr.hasNext(); ) {
            Content n = itr.next();
            itr.remove();
            doc.getRootElement().addContent(n);
        }
    }
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) InputSource(org.xml.sax.InputSource) Attribute(org.jdom.Attribute) Element(org.jdom.Element) EntityResolver(org.xml.sax.EntityResolver) IOException(java.io.IOException) Document(org.jdom.Document) JDOMException(org.jdom.JDOMException) SAXException(org.xml.sax.SAXException) Content(org.jdom.Content) StringReader(java.io.StringReader)

Example 14 with EntityResolver

use of org.xml.sax.EntityResolver in project intellij-community by JetBrains.

the class JDOMUtil method getSaxBuilder.

private static SAXBuilder getSaxBuilder() {
    SoftReference<SAXBuilder> reference = ourSaxBuilder.get();
    SAXBuilder saxBuilder = com.intellij.reference.SoftReference.dereference(reference);
    if (saxBuilder == null) {
        saxBuilder = new SAXBuilder() {

            @Override
            protected void configureParser(XMLReader parser, SAXHandler contentHandler) throws JDOMException {
                super.configureParser(parser, contentHandler);
                try {
                    parser.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
                } catch (Exception ignore) {
                }
            }
        };
        saxBuilder.setEntityResolver(new EntityResolver() {

            @Override
            @NotNull
            public InputSource resolveEntity(String publicId, String systemId) {
                return new InputSource(new CharArrayReader(ArrayUtil.EMPTY_CHAR_ARRAY));
            }
        });
        ourSaxBuilder.set(new SoftReference<SAXBuilder>(saxBuilder));
    }
    return saxBuilder;
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) InputSource(org.xml.sax.InputSource) SAXHandler(org.jdom.input.SAXHandler) EntityResolver(org.xml.sax.EntityResolver) NotNull(org.jetbrains.annotations.NotNull) XMLReader(org.xml.sax.XMLReader)

Example 15 with EntityResolver

use of org.xml.sax.EntityResolver in project intellij-community by JetBrains.

the class ModelGen method loadXml.

public static Element loadXml(File configXml) throws Exception {
    SAXBuilder saxBuilder = new SAXBuilder();
    saxBuilder.setEntityResolver(new EntityResolver() {

        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
            return new InputSource(new CharArrayReader(new char[0]));
        }
    });
    final Document document = saxBuilder.build(configXml);
    return document.getRootElement();
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) InputSource(org.xml.sax.InputSource) XMLInputSource(org.apache.xerces.xni.parser.XMLInputSource) CharArrayReader(java.io.CharArrayReader) XMLEntityResolver(org.apache.xerces.xni.parser.XMLEntityResolver) EntityResolver(org.xml.sax.EntityResolver) IOException(java.io.IOException) Document(org.jdom.Document) 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