Example usage for javax.xml.parsers DocumentBuilderFactory setExpandEntityReferences

List of usage examples for javax.xml.parsers DocumentBuilderFactory setExpandEntityReferences

Introduction

In this page you can find the example usage for javax.xml.parsers DocumentBuilderFactory setExpandEntityReferences.

Prototype


public void setExpandEntityReferences(boolean expandEntityRef) 

Source Link

Document

Specifies that the parser produced by this code will expand entity reference nodes.

Usage

From source file:Main.java

/**
 * Loads an XML document from an InputStream
 *//*from   w  w  w. j ava  2  s.c  om*/
public static Document loadXmlDoc(final InputStream stream) {
    Document result = null;
    try {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setExpandEntityReferences(false);
        domFactory.setIgnoringComments(true);//strips comments
        //         domFactory.setIgnoringElementContentWhitespace(true);//would be nice if it worked
        domFactory.setNamespaceAware(true);
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        result = builder.parse(stream);
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

From source file:org.springsource.ide.eclipse.commons.content.core.util.ContentUtil.java

public static DocumentBuilder createDocumentBuilder() throws CoreException {
    if (SPRING_IDE_AVAILABLE) {
        return SpringCoreUtils.getDocumentBuilder();
    } else {//w  w w .jav a2  s  . c o m
        DocumentBuilderFactory xmlFact = DocumentBuilderFactory.newInstance();
        xmlFact.setExpandEntityReferences(false);
        try {
            return xmlFact.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            throw new CoreException(null);
        }
    }
}

From source file:Main.java

public static Document parseStreamToXML(InputStream in) {
    try {/*from  w  w w .ja  va  2 s  .  co  m*/
        // Use JAXP to create a document builder
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

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

        return builderFactory.newDocumentBuilder().parse(in);
    } catch (ParserConfigurationException errParser) {
        throw new RuntimeException("Error getting XML parser", errParser);
    } catch (SAXException errSax) {
        throw new RuntimeException("Error parsing XML files", errSax);
    } catch (IOException errIO) {
        throw new RuntimeException("Error parsing XML files", errIO);
    }
}

From source file:Main.java

public static Document parseStreamToXML(Reader in) {
    try {/* w w  w.ja va2s.c om*/
        // Use JAXP to create a document builder
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

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

        return builderFactory.newDocumentBuilder().parse(new InputSource(in));
    } catch (ParserConfigurationException errParser) {
        throw new RuntimeException("Error getting XML parser", errParser);
    } catch (SAXException errSax) {
        throw new RuntimeException("Error parsing XML files", errSax);
    } catch (IOException errIO) {
        throw new RuntimeException("Error parsing XML files", errIO);
    }
}

From source file:Main.java

/**
 * Get a DocumentBuilder that is namespace aware.
 * @return a namespace-aware DocumentBuilder.
 *///ww  w  .  j av a  2s.  c o  m
public static DocumentBuilder getDocumentBuilder() throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    dbf.setValidating(false);
    dbf.setXIncludeAware(false);
    dbf.setExpandEntityReferences(true);
    dbf.setCoalescing(false);

    //dbf.setFeature("http://xml.org/sax/features/namespaces", false);
    //dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
    dbf.setFeature("http://xml.org/sax/features/validation", false);
    dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

    return dbf.newDocumentBuilder();
}

From source file:Main.java

private static DocumentBuilderFactory newDocumentBuilderFactory() {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setIgnoringComments(true);// w w  w . ja va 2s .c om
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setExpandEntityReferences(false);
    return dbf;
}

From source file:Main.java

/**
 * Creates a new {@link DocumentBuilder} object.
 *//* w  w  w. j  a  v a2s .c  o  m*/
private static DocumentBuilder newDocumentBuilder() throws ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true);
    factory.setNamespaceAware(true);
    factory.setValidating(false);
    factory.setIgnoringComments(true);
    factory.setExpandEntityReferences(false);
    factory.setCoalescing(false);

    factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
    factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
    factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);

    return factory.newDocumentBuilder();
}

From source file:Main.java

public static Document parse(String xmlStr) throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    factory.setValidating(false);/*from  w w  w .  jav  a  2  s.  c o m*/

    factory.setNamespaceAware(false);

    factory.setIgnoringComments(true);

    factory.setIgnoringElementContentWhitespace(false);

    factory.setCoalescing(false);

    factory.setExpandEntityReferences(true);

    DocumentBuilder builder = factory.newDocumentBuilder();

    ByteArrayInputStream inputStream = new ByteArrayInputStream(xmlStr.getBytes());

    return builder.parse(inputStream);

}

From source file:Main.java

/**
 * Returns a new {@code DocumentBuilderFactory} instance with XML External Entity (XXE) processing disabled.
 *
 * @return the new {@code DocumentBuilderFactory} instance with XXE processing disabled
 * @throws ParserConfigurationException if an error occurred while disabling XXE processing
 * @see DocumentBuilderFactory/*  ww  w. j  a v  a 2s.  c om*/
 */
public static DocumentBuilderFactory newXxeDisabledDocumentBuilderFactory()
        throws ParserConfigurationException {
    // Disable XXE processing, not required by default.
    // https://www.owasp.org/index.php/XML_External_Entity_%28XXE%29_Processing
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
    factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
    factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
    factory.setExpandEntityReferences(false);
    return factory;
}

From source file:Main.java

private static void setUpSecurity(DocumentBuilderFactory dbFactory) throws ParserConfigurationException {
    dbFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
    dbFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
    dbFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
    dbFactory.setXIncludeAware(false);/*from   w w w . ja  v  a  2  s.c  o m*/
    dbFactory.setExpandEntityReferences(false);
}