Example usage for javax.xml.parsers DocumentBuilder setEntityResolver

List of usage examples for javax.xml.parsers DocumentBuilder setEntityResolver

Introduction

In this page you can find the example usage for javax.xml.parsers DocumentBuilder setEntityResolver.

Prototype


public abstract void setEntityResolver(EntityResolver er);

Source Link

Document

Specify the EntityResolver to be used to resolve entities present in the XML document to be parsed.

Usage

From source file:Utils.java

/**
 * Read XML as DOM.//from w  ww . j  a  v a2 s.  c  o  m
 */
public static Document readXml(InputStream is) throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    dbf.setValidating(false);
    dbf.setIgnoringComments(false);
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setNamespaceAware(true);
    // dbf.setCoalescing(true);
    // dbf.setExpandEntityReferences(true);

    DocumentBuilder db = null;
    db = dbf.newDocumentBuilder();
    db.setEntityResolver(new NullResolver());

    // db.setErrorHandler( new MyErrorHandler());

    return db.parse(is);
}

From source file:Utils.java

public static Document readXml(StreamSource is) throws SAXException, IOException, ParserConfigurationException {

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    dbf.setValidating(false);/*from  w w  w  .j  av  a  2s  . c om*/
    dbf.setIgnoringComments(false);
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setNamespaceAware(true);
    // dbf.setCoalescing(true);
    // dbf.setExpandEntityReferences(true);

    DocumentBuilder db = null;
    db = dbf.newDocumentBuilder();
    db.setEntityResolver(new NullResolver());

    // db.setErrorHandler( new MyErrorHandler());
    InputSource is2 = new InputSource();
    is2.setSystemId(is.getSystemId());
    is2.setByteStream(is.getInputStream());
    is2.setCharacterStream(is.getReader());

    return db.parse(is2);
}

From source file:Main.java

private static DocumentBuilder getBuilder() throws ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);/* ww  w  . j a  v a2s.  c o  m*/
    factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
    factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

    DocumentBuilder builder = factory.newDocumentBuilder();
    // prevent DTD entities from being resolved.
    builder.setEntityResolver(new EntityResolver() {
        @Override
        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
            return new InputSource(new StringReader(""));
        }
    });

    return builder;
}

From source file:Main.java

public static Document read(String filename) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(false);//  w w w.j a  va  2  s. com
    DocumentBuilder builder = null;
    Document document = null;
    try {
        builder = factory.newDocumentBuilder();
        builder.setEntityResolver(new EntityResolver() {
            public InputSource resolveEntity(String publicId, String systemId)
                    throws SAXException, IOException {
                return new InputSource(new StringReader(""));
            }
        });
        document = builder.parse(new File(filename));
        document.getDocumentElement().normalize();
    } catch (Exception e) {
    }
    return document;
}

From source file:Main.java

/**
 * Parses a document from the given string
 * //from w ww .java 2s . c o m
 * @param template The string to parse
 * @return The parsed {@link Document}
 */
public static Document getTemplateDocument(String template) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = null;
    dbf.setValidating(false);
    try {
        db = dbf.newDocumentBuilder();
        db.setEntityResolver(new EntityResolver() {
            public InputSource resolveEntity(String publicID, String systemID) throws SAXException {
                return new InputSource(new StringReader(""));
            }
        });
        Document doc = db.parse(new ByteArrayInputStream(template.getBytes("utf8")));
        return doc;
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

private static void validateDTD0(String xmlFile, final String dtdPath)
        throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setValidating(true);
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    if (dtdPath != null && !dtdPath.isEmpty()) {
        documentBuilder.setEntityResolver(new EntityResolver() {

            public InputSource resolveEntity(String publicId, String systemId)
                    throws SAXException, IOException {

                return new InputSource(new FileInputStream(dtdPath));
            }//from  ww  w  . j  a v a2 s. c o  m
        });
    }
    documentBuilder.setErrorHandler(new ErrorHandler() {

        public void warning(SAXParseException exception) throws SAXException {

        }

        public void fatalError(SAXParseException exception) throws SAXException {

            throw exception;
        }

        public void error(SAXParseException exception) throws SAXException {

            throw exception;
        }
    });

    documentBuilder.parse(new FileInputStream(xmlFile));

}

From source file:com.mycila.xmltool.XMLDocumentBuilderFactory.java

public static DocumentBuilder newDocumentBuilder(boolean ignoreNamespaces) {
    try {//from ww w.ja va 2  s.c  o  m
        javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory
                .newInstance();
        factory.setNamespaceAware(!ignoreNamespaces);
        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setErrorHandler(new XMLErrorHandler(true));
        builder.setEntityResolver(CachedEntityResolver.instance);
        return builder;
    } catch (ParserConfigurationException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}

From source file:info.magnolia.cms.util.ConfigUtil.java

/**
 * Uses a map to find dtds in the resources.
 * @param xml//from w  w  w .  j  a v a  2 s .  com
 * @param dtds
 * @return
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 */
public static Document string2DOM(String xml, final Map<String, String> dtds)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(true);
    DocumentBuilder builder;
    builder = dbf.newDocumentBuilder();
    builder.setEntityResolver(new MapDTDEntityResolver(dtds));
    return builder.parse(IOUtils.toInputStream(xml));
}

From source file:DomUtil.java

/**
 * Read XML as DOM.//from  w  w w  .j a  va2s . c o m
 */
public static Document readXml(InputStream is) throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    dbf.setValidating(false);
    dbf.setIgnoringComments(false);
    dbf.setIgnoringElementContentWhitespace(true);
    // dbf.setCoalescing(true);
    // dbf.setExpandEntityReferences(true);

    DocumentBuilder db = null;
    db = dbf.newDocumentBuilder();
    db.setEntityResolver(new NullResolver());

    // db.setErrorHandler( new MyErrorHandler());

    Document doc = db.parse(is);
    return doc;
}

From source file:com.sun.portal.portletcontainer.admin.PortletRegistryHelper.java

public static DocumentBuilder getDocumentBuilder() throws PortletRegistryException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = null;
    try {/*from   w  w w . jav a2s .  co m*/
        docBuilder = dbf.newDocumentBuilder();
        docBuilder.setEntityResolver(new NoOpEntityResolver());
    } catch (ParserConfigurationException pce) {
        throw new PortletRegistryException(pce);
    }
    return docBuilder;
}