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:org.codice.ddf.cxf.SecureCxfClientFactoryTest.java

private Element getAssertionElement() throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    dbf.setValidating(false);//from w ww  .  java 2  s .  c  o m
    dbf.setIgnoringComments(false);
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setNamespaceAware(true);

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

    return db.parse(SecureCxfClientFactoryTest.class.getResourceAsStream("/SAMLAssertion.xml"))
            .getDocumentElement();
}

From source file:org.codice.ddf.security.common.jaxrs.RestSecurityTest.java

public static Document readXml(InputStream is) throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    dbf.setValidating(false);/*ww w .  j a  va2  s .com*/
    dbf.setIgnoringComments(false);
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setNamespaceAware(true);
    // dbf.setCoalescing(true);
    // dbf.setExpandEntityReferences(true);

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

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

    return db.parse(is);
}

From source file:org.codice.ddf.security.idp.server.IdpEndpointTest.java

public static Document readXml(InputStream is) throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    dbf.setValidating(false);// w  ww . ja  v a2  s .co m
    dbf.setIgnoringComments(false);
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setNamespaceAware(true);

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

    return db.parse(is);
}

From source file:org.codice.ddf.security.servlet.expiry.SessionManagementServiceTest.java

private static Document readXml(InputStream is) throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    dbf.setValidating(false);// w w  w . j a  va2 s  .  co  m
    dbf.setIgnoringComments(false);
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setNamespaceAware(true);

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

    return db.parse(is);
}

From source file:org.compass.core.config.builder.AbstractXmlConfigurationBuilder.java

protected DocumentBuilder createDocumentBuilder(DocumentBuilderFactory factory)
        throws ParserConfigurationException {
    DocumentBuilder docBuilder = factory.newDocumentBuilder();
    docBuilder.setErrorHandler(doGetErrorHandler());
    docBuilder.setEntityResolver(doGetEntityResolver());
    return docBuilder;
}

From source file:org.eshark.xmlprog.cache.ClassCache.java

public void loadFromXML(File aXMLFile) throws XMLProgFormatException {
    try {/*from  w ww .j a  va 2s. c om*/
        DocumentBuilder lDocumentBuilder = getDocumentBuilder();
        lDocumentBuilder.setEntityResolver(new Resolver());
        // For XML
        Document lXMLDocument = null;
        if (aXMLFile == null)
            lXMLDocument = lDocumentBuilder.parse(new File(FILE_PATH + "\\xmls", FILE_NAME));
        else
            lXMLDocument = lDocumentBuilder.parse(aXMLFile);

        // normalize the document
        lXMLDocument.getDocumentElement().normalize();

        // Read the XML and create the cache
        Element daoElement = (Element) lXMLDocument.getChildNodes().item(1);
        String xmlVersion = daoElement.getAttribute("version");
        if (xmlVersion.compareTo(EXTERNAL_XML_VERSION) > 0)
            throw new XMLProgFormatException("Exported Class Cache file format version " + xmlVersion
                    + " is not supported. This XMLProg Configuration installation can read" + " versions "
                    + EXTERNAL_XML_VERSION + " or older. You" + " may need to check the configuration.");
        importClasses(daoElement);
    } catch (ParserConfigurationException PCE) {
        PCE.printStackTrace();
    } catch (SAXException SXE) {
        SXE.printStackTrace();
    } catch (IOException IOE) {
        IOE.printStackTrace();
    }
}

From source file:org.exoplatform.services.jcr.impl.core.query.lucene.SearchIndex.java

/**
 * Returns the document element of the indexing configuration or
 * <code>null</code> if there is no indexing configuration.
 * //from   ww w. jav a2 s .  com
 * @return the indexing configuration or <code>null</code> if there is none.
 */
protected Element getIndexingConfigurationDOM() {
    if (indexingConfiguration == null) {
        if (indexingConfigPath != null) {

            // File config = PrivilegedFileHelper.file(indexingConfigPath);
            SecurityHelper.doPrivilegedAction(new PrivilegedAction<Object>() {
                public Object run() {
                    InputStream is = SearchIndex.class.getResourceAsStream(indexingConfigPath);
                    if (is == null) {
                        try {
                            is = cfm.getInputStream(indexingConfigPath);
                        } catch (Exception e1) {
                            log.warn("Unable to load configuration " + indexingConfigPath);
                        }
                    }

                    try {
                        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                        DocumentBuilder builder = factory.newDocumentBuilder();
                        builder.setEntityResolver(new IndexingConfigurationEntityResolver());
                        indexingConfiguration = builder.parse(is).getDocumentElement();
                    } catch (ParserConfigurationException e) {
                        log.warn("Unable to create XML parser", e);
                    } catch (IOException e) {
                        log.warn("Exception parsing " + indexingConfigPath, e);
                    } catch (SAXException e) {
                        log.warn("Exception parsing " + indexingConfigPath, e);
                    }
                    return null;
                }
            });
        }
    }
    return indexingConfiguration;
}

From source file:org.geoserver.test.GeoServerAbstractTestSupport.java

/**
 * Parses a stream into a dom./*w ww.ja v a2s  . c  o m*/
 * @param input
 * @param skipDTD If true, will skip loading and validating against the associated DTD
 */
protected Document dom(InputStream input, boolean skipDTD)
        throws ParserConfigurationException, SAXException, IOException {
    if (skipDTD) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        factory.setValidating(false);

        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setEntityResolver(new EmptyResolver());
        Document dom = builder.parse(input);

        return dom;
    } else {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        return builder.parse(input);
    }
}

From source file:org.geoserver.test.GeoServerSystemTestSupport.java

protected Document dom(InputStream stream, boolean skipDTD, String encoding)
        throws ParserConfigurationException, SAXException, IOException {

    InputSource input = new InputSource(stream);
    if (encoding != null) {
        input.setEncoding(encoding);/*from   w w w  .  j  a  va  2s  .  co m*/
    }

    if (skipDTD) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        factory.setNamespaceAware(true);
        factory.setValidating(false);

        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setEntityResolver(new EmptyResolver());
        Document dom = builder.parse(input);

        return dom;
    } else {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        return builder.parse(input);
    }
}

From source file:org.geoserver.wms.WMSTestSupport.java

/**
 * Utility method to run the transformation on tr with the provided request and returns the
 * result as a DOM/*from   ww w.  j  av  a  2  s. c om*/
 * 
 * @param req
 *            , the Object to run the xml transformation against with {@code tr}, usually an
 *            instance of a {@link Request} subclass
 * @param tr
 *            , the transformer to run the transformation with and produce the result as a DOM
 * @param namespaceAware
 *            whether to use a namespace aware parser for the response or not
 */
public static Document transform(Object req, TransformerBase tr, boolean namespaceAware) throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    tr.transform(req, out);

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(namespaceAware);

    DocumentBuilder db = dbf.newDocumentBuilder();

    /**
     * Resolves everything to an empty xml document, useful for skipping errors due to missing
     * dtds and the like
     * 
     * @author Andrea Aime - TOPP
     */
    class EmptyResolver implements org.xml.sax.EntityResolver {
        public InputSource resolveEntity(String publicId, String systemId)
                throws org.xml.sax.SAXException, IOException {
            StringReader reader = new StringReader("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            InputSource source = new InputSource(reader);
            source.setPublicId(publicId);
            source.setSystemId(systemId);

            return source;
        }
    }
    db.setEntityResolver(new EmptyResolver());

    // System.out.println(out.toString());

    Document doc = db.parse(new ByteArrayInputStream(out.toByteArray()));
    return doc;
}