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:no.kantega.commons.util.XMLHelper.java

public static Document openDocument(InputStream is, EntityResolver er, String systemId) throws SystemException {
    Document doc = null;/*from   w  ww  .  ja v  a 2 s . c  om*/
    try {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = docFactory.newDocumentBuilder();
        if (er != null) {
            builder.setEntityResolver(er);
        }
        if (systemId != null) {
            doc = builder.parse(is, systemId);
        } else {
            doc = builder.parse(is);
        }
    } catch (Exception e) {
        throw new SystemException("Error opening XML document from InputStream", e);
    }

    return doc;
}

From source file:XMLUtil.java

public static Document parse(InputSource input, boolean validate, boolean namespaceAware,
        ErrorHandler errorHandler, EntityResolver entityResolver) throws IOException, SAXException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(validate);/*from   w w  w.ja v a 2s  . c o  m*/
    factory.setNamespaceAware(namespaceAware);
    DocumentBuilder builder = null;
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException ex) {
        throw new SAXException(ex);
    }

    if (errorHandler != null) {
        builder.setErrorHandler(errorHandler);
    }

    if (entityResolver != null) {
        builder.setEntityResolver(entityResolver);
    }

    assert input != null : "InputSource cannot be null";

    return builder.parse(input);
}

From source file:Main.java

/** 
 * Create from factory a DocumentBuilder and let it create a org.w3c.dom.Document.
 * This method takes InputSource. After successful finish the document tree is returned.
 *
 * @param input a parser input (for URL users use: <code>new InputSource(url.toExternalForm())</code>
 * @param validate if true validating parser is used
 * @param namespaceAware if true DOM is created by namespace aware parser
 * @param errorHandler a error handler to notify about exception or <code>null</code>
 * @param entityResolver SAX entity resolver or <code>null</code>; see class Javadoc for hints
 *
 * @throws IOException if an I/O problem during parsing occurs
 * @throws SAXException is thrown if a parser error occurs
 * @throws FactoryConfigurationError Application developers should never need to directly catch errors of this type.
 *
 * @return document representing given input, or null if a parsing error occurs
 *//*from w  w  w.  j  a  v  a 2 s  .  com*/
public static Document parse(InputSource input, boolean validate, boolean namespaceAware,
        ErrorHandler errorHandler, EntityResolver entityResolver) throws IOException, SAXException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(validate);
    factory.setNamespaceAware(namespaceAware);

    DocumentBuilder builder = null;
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException ex) {
        throw new SAXException("Cannot create parser satisfying configuration parameters", ex); //NOI18N
    }

    if (errorHandler != null) {
        builder.setErrorHandler(errorHandler);
    }

    if (entityResolver != null) {
        builder.setEntityResolver(entityResolver);
    }

    return builder.parse(input);
}

From source file:com.meterware.httpunit.HttpUnitUtils.java

/**
 * creates a parser using JAXP API./*from w w  w.  jav  a2 s .c om*/
 */
public static DocumentBuilder newParser() throws SAXException {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setEntityResolver(new HttpUnitUtils.ClasspathEntityResolver());
        return builder;
    } catch (ParserConfigurationException ex) {
        // redirect the new exception for code compatibility
        throw new SAXException(ex);
    }
}

From source file:com.gargoylesoftware.htmlunit.xml.XmlUtil.java

/**
 * Builds a document from the content of the web response.
 * A warning is logged if an exception is thrown while parsing the XML content
 * (for instance when the content is not a valid XML and can't be parsed).
 *
 * @param webResponse the response from the server
 * @throws IOException if the page could not be created
 * @return the parse result//w ww .j  a va 2s  . c o  m
 * @throws SAXException if the parsing fails
 * @throws ParserConfigurationException if a DocumentBuilder cannot be created
 */
public static Document buildDocument(final WebResponse webResponse)
        throws IOException, SAXException, ParserConfigurationException {

    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    if (webResponse == null) {
        return factory.newDocumentBuilder().newDocument();
    }

    factory.setNamespaceAware(true);
    final InputStreamReader reader = new InputStreamReader(webResponse.getContentAsStream(),
            webResponse.getContentCharset());

    // we have to do the blank input check and the parsing in one step
    final TrackBlankContentReader tracker = new TrackBlankContentReader(reader);

    final InputSource source = new InputSource(tracker);
    final DocumentBuilder builder = factory.newDocumentBuilder();
    builder.setErrorHandler(DISCARD_MESSAGES_HANDLER);
    builder.setEntityResolver(new EntityResolver() {
        @Override
        public InputSource resolveEntity(final String publicId, final String systemId)
                throws SAXException, IOException {
            return new InputSource(new StringReader(""));
        }
    });
    try {
        return builder.parse(source);
    } catch (final SAXException e) {
        if (tracker.wasBlank()) {
            return factory.newDocumentBuilder().newDocument();
        }
        throw e;
    }
}

From source file:dk.dbc.rawrepo.oai.OAIWorker.java

private static DocumentBuilder makeDocumentBuilder() {
    synchronized (DocumentBuilderFactory.class) {
        try {//from  w w  w  .j  av  a2s. c o  m
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);
            dbf.setIgnoringComments(true);
            dbf.setIgnoringElementContentWhitespace(true);
            DocumentBuilder db = dbf.newDocumentBuilder();
            db.setEntityResolver(new NullResolver());
            return db;
        } catch (ParserConfigurationException ex) {
            throw new RuntimeException(ex);
        }
    }
}

From source file:Main.java

/** Read an XML file into a DOM tree
 * // w  w  w .  j  av a  2  s .c  o  m
 * @param file the file to read
 * @return a new XML document
 * @throws SAXException if an XML parse error occurs
 * @throws IOException if a file I/O error occurs
 */
public static Document read(File file) throws SAXException, IOException {
    try {
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = builderFactory.newDocumentBuilder();

        // Workaround for missing external DTD.  When the parser looks up
        // an external reference, we provide an empty document back.  While
        // not correct, we don't expect SVG files loaded by this program
        // to depend on externally defined entities; nor do we require
        // validation.
        //
        // http://stackoverflow.com/questions/2640825/how-to-parse-a-xhtml-ignoring-the-doctype-declaration-using-dom-parser
        builder.setEntityResolver(new EntityResolver() {
            public InputSource resolveEntity(String publicId, String systemId)
                    throws SAXException, IOException {
                return new InputSource(new StringReader(""));
            }
        });

        return builder.parse(file);
    } catch (ParserConfigurationException e) {
        // This exception is never thrown, treat as fatal if it is
        throw new RuntimeException(e);
    }
}

From source file:XMLUtils.java

public static synchronized DocumentBuilder getParser() throws ParserConfigurationException {
    if (fParsersPool.isEmpty()) {
        // create a new parser
        initFactory();//from w w  w.  ja v a 2  s .c  o m
        DocumentBuilder builder = fDocumentFactory.newDocumentBuilder();
        builder.setEntityResolver(entityResolver);
        return builder;
    } else {
        return (DocumentBuilder) fParsersPool.pop();
    }
}

From source file:Main.java

/**
 * Parses an XML document into a DOM tree.
 *
 * <div class="nonnormative">/*  ww w.j av  a2s  .  co  m*/
 *
 * <p>
 * Remember that when parsing XML files you often want to set an explicit
 * entity resolver. For example, consider a file such as this:</p>
 *
 * <pre>
 * &lt;?xml version="1.0" encoding="UTF-8"?&gt;
 * &lt;!DOCTYPE root PUBLIC "-//NetBeans//DTD Foo 1.0//EN" "http://www.netbeans.org/dtds/foo-1_0.dtd"&gt;
 * &lt;root/&gt;
 * </pre>
 *
 * <p>
 * If you parse this with a null entity resolver, or you use the default
 * resolver (EntityCatalog.getDefault) but do not do anything special with
 * this DTD, you will probably find the parse blocking to make a network
 * connection <em>even when you are not validating</em>. That is because
 * DTDs can be used to define entities and other XML oddities, and are not a
 * pure constraint language like Schema or RELAX-NG.</p>
 *
 * <p>
 * There are three basic ways to avoid the network connection.</p>
 *
 * <ol>
 *
 * <li><p>
 * Register the DTD. This is generally the best thing to do. See
 * EntityCatalog's documentation for details, but for example in your layer
 * use:</p>
 *
 * <pre>
 * &lt;filesystem&gt;
 *   &lt;folder name="xml"&gt;
 *     &lt;folder name="entities"&gt;
 *       &lt;folder name="NetBeans"&gt;
 *         &lt;file name="DTD_Foo_1_0"
 *               url="resources/foo-1_0.dtd"&gt;
 *           &lt;attr name="hint.originalPublicID"
 *                 stringvalue="-//NetBeans//DTD Foo 1.0//EN"/&gt;
 *         &lt;/file&gt;
 *       &lt;/folder&gt;
 *     &lt;/folder&gt;
 *   &lt;/folder&gt;
 * &lt;/filesystem&gt;
 * </pre>
 *
 * <p>
 * Now the default system entity catalog will resolve the public ID to the
 * local copy in your module, not the network copy. Additionally, anyone who
 * mounts the "NetBeans Catalog" in the XML Entity Catalogs node in the
 * Runtime tab will be able to use your local copy of the DTD automatically,
 * for validation, code completion, etc. (The network URL should really
 * exist, though, for the benefit of other tools!)</p></li>
 *
 * <li><p>
 * You can also set an explicit entity resolver which maps that particular
 * public ID to some local copy of the DTD, if you do not want to register
 * it globally in the system for some reason. If handed other public IDs,
 * just return null to indicate that the system ID should be
 * loaded.</p></li>
 *
 * <li><p>
 * In some cases where XML parsing is very performance-sensitive, and you
 * know that you do not need validation and furthermore that the DTD defines
 * no infoset (there are no entity or character definitions, etc.), you can
 * speed up the parse. Turn off validation, but also supply a custom entity
 * resolver that does not even bother to load the DTD at all:</p>
 *
 * <pre>
 * public InputSource resolveEntity(String pubid, String sysid)
 *     throws SAXException, IOException {
 *   if (pubid.equals("-//NetBeans//DTD Foo 1.0//EN")) {
 *     return new InputSource(new ByteArrayInputStream(new byte[0]));
 *   } else {
 *     return EntityCatalog.getDefault().resolveEntity(pubid, sysid);
 *   }
 * }
 * </pre></li>
 *
 * </ol>
 *
 * </div>
 *
 * @param input          a parser input (for URL users use:
 *                       <code>new InputSource(url.toString())</code>
 * @param validate       if true validating parser is used
 * @param namespaceAware if true DOM is created by namespace aware parser
 * @param errorHandler   a error handler to notify about exception (such as
 *                       {@link #defaultErrorHandler}) or <code>null</code>
 * @param entityResolver SAX entity resolver (such as
 *                       EntityCatalog#getDefault) or <code>null</code>
 *
 * @throws IOException               if an I/O problem during parsing occurs
 * @throws SAXException              is thrown if a parser error occurs
 * @throws FactoryConfigurationError Application developers should never
 *                                   need to directly catch errors of this
 *                                   type.
 *
 * @return document representing given input
 */
public static Document parse(InputSource input, boolean validate, boolean namespaceAware,
        ErrorHandler errorHandler, EntityResolver entityResolver) throws IOException, SAXException {

    DocumentBuilder builder = null;
    DocumentBuilderFactory factory = getFactory(validate, namespaceAware);

    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException ex) {
        throw new SAXException("Cannot create parser satisfying configuration parameters", ex); //NOI18N
    }

    if (errorHandler != null) {
        builder.setErrorHandler(errorHandler);
    }

    if (entityResolver != null) {
        builder.setEntityResolver(entityResolver);
    }

    return builder.parse(input);
}

From source file:com.vmware.identity.SharedUtils.java

/**
 * Read XML as DOM.//  w w w  . j  ava  2  s.c om
 */
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);
}