Example usage for org.dom4j.io SAXReader setEntityResolver

List of usage examples for org.dom4j.io SAXReader setEntityResolver

Introduction

In this page you can find the example usage for org.dom4j.io SAXReader setEntityResolver.

Prototype

public void setEntityResolver(EntityResolver entityResolver) 

Source Link

Document

Sets the entity resolver used to resolve entities.

Usage

From source file:org.mitre.jawb.io.ATLASHelper.java

License:Open Source License

public static Document parse(URI aifURI) throws IOException {

    SAXReader reader = new SAXReader();
    // actually, this is ok, if we use the entity resolver
    reader.setEntityResolver(new ATLASResolver());
    reader.setIncludeExternalDTDDeclarations(false);

    try {/*  ww w  . java2s.  c om*/
        // URI.toURL() fails when opaque. don't expect an opaque here, but...
        if (DEBUG > 0)
            System.err.println("ATHelp.parse: aifURI=" + aifURI);
        return reader.read(new URL(aifURI.toString()));
    } catch (DocumentException x) {
        IOException ex = new IOException("Unable to parse input aif");
        ex.initCause(x);
        throw ex;
    }
}

From source file:org.mitre.jawb.io.ATLASHelper.java

License:Open Source License

public static Document parse(InputStream in) throws IOException {

    SAXReader reader = new SAXReader();
    // actually, this is ok, if we use the entity resolver
    reader.setEntityResolver(new ATLASResolver());
    reader.setIncludeExternalDTDDeclarations(false);

    try {/*from w  w  w.  j a va  2s.  c om*/
        // URI.toURL() fails when opaque. don't expect an opaque here, but...
        return reader.read(in);
    } catch (DocumentException x) {
        IOException ex = new IOException("Unable to parse input aif");
        ex.initCause(x);
        throw ex;
    }
}

From source file:org.nuxeo.ecm.platform.ui.web.rest.StaticNavigationHandler.java

License:Apache License

/**
 * Gets the root element of the document.
 *
 * @since 5.6/*  w  w w  .  j ava 2  s . c o m*/
 */
protected static Element getDocumentRoot(InputStream stream) {
    try {
        SAXReader saxReader = new SAXReader();
        saxReader.setEntityResolver(new DTDEntityResolver());
        saxReader.setMergeAdjacentText(true);
        return saxReader.read(stream).getRootElement();
    } catch (DocumentException de) {
        throw new RuntimeException(de);
    }
}

From source file:org.olat.core.util.xml.XMLParser.java

License:Apache License

/**
 * @param in//from   w  ww  .  j a  v  a2s. c  om
 * @param validateXML
 * @return parsed document
 */
public Document parse(InputStream in, boolean validateXML) {
    Document document;
    try {
        SAXReader reader = new SAXReader();
        reader.setEntityResolver(er);
        reader.setValidation(validateXML);
        document = reader.read(in, "");
    } catch (Exception e) {
        throw new OLATRuntimeException(XMLParser.class, "Exception reading XML", e);
    }
    return document;
}

From source file:org.olat.ims.qti.qpool.ItemFileResourceValidator.java

License:Apache License

private Document readDocument(InputStream in) {
    try {/*  ww  w.  java 2s. c  o  m*/
        SAXReader reader = new SAXReader();
        reader.setEntityResolver(new IMSEntityResolver());
        reader.setValidation(false);
        return reader.read(in, "");
    } catch (Exception e) {
        return null;
    }
}

From source file:org.opencms.util.ant.CmsXmlUtils.java

License:Open Source License

/**
 * Helper to unmarshal (read) xml contents from an input source into a document.<p>
 * //www.java 2  s .c o m
 * Using this method ensures that the OpenCms XML entity resolver is used.<p>
 * 
 * Important: The encoding provided will NOT be used during unmarshalling,
 * the XML parser will do this on the base of the information in the source String.
 * The encoding is used for initializing the created instance of the document,
 * which means it will be used when marshalling the document again later.<p>
 *  
 * @param source the XML input source to use
 * @param resolver the XML entity resolver to use
 * @param validate if the reader should try to validate the xml code
 * 
 * @return the unmarshalled XML document
 * 
 * @throws Exception if something goes wrong
 */
public static Document unmarshalHelper(InputSource source, EntityResolver resolver, boolean validate)
        throws Exception {

    SAXReader reader = new SAXReader();
    if (resolver != null) {
        reader.setEntityResolver(resolver);
    }
    reader.setMergeAdjacentText(true);
    reader.setStripWhitespaceText(true);
    if (!validate) {
        reader.setValidation(false);
        reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    }
    return reader.read(source);
}

From source file:org.opencms.xml.CmsXmlUtils.java

License:Open Source License

/**
 * Helper to unmarshal (read) xml contents from an input source into a document.<p>
 * /*w  w w. ja  va  2 s  . c  om*/
 * Using this method ensures that the OpenCms XML entity resolver is used.<p>
 * 
 * Important: The encoding provided will NOT be used during unmarshalling,
 * the XML parser will do this on the base of the information in the source String.
 * The encoding is used for initializing the created instance of the document,
 * which means it will be used when marshalling the document again later.<p>
 *  
 * @param source the XML input source to use
 * @param resolver the XML entity resolver to use
 * @param validate if the reader should try to validate the xml code
 * 
 * @return the unmarshalled XML document
 * 
 * @throws CmsXmlException if something goes wrong
 */
public static Document unmarshalHelper(InputSource source, EntityResolver resolver, boolean validate)
        throws CmsXmlException {

    try {
        SAXReader reader = new SAXReader();
        if (resolver != null) {
            reader.setEntityResolver(resolver);
        }
        reader.setMergeAdjacentText(true);
        reader.setStripWhitespaceText(true);
        if (!validate) {
            reader.setValidation(false);
            reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        }
        return reader.read(source);
    } catch (DocumentException e) {
        throw new CmsXmlException(Messages.get().container(Messages.ERR_UNMARSHALLING_XML_DOC_0), e);
    } catch (SAXException e) {
        throw new CmsXmlException(Messages.get().container(Messages.ERR_UNMARSHALLING_XML_DOC_0), e);
    }
}

From source file:org.openo.commsvc.protocolstack.netconf.service.svc.protocol.tool.XmlUtil.java

License:Apache License

/**
 * Get the XML SAXReader//from   www.jav a2 s. c  o m
 * 
 * @author
 * @return SAXReader XML is SAXReader
 */
public static SAXReader newSAXReader() {
    SAXReader reader = new SAXReader();
    reader.setEntityResolver(new EntityResolver() {

        /**
         * Implementation
         * <br>
         * 
         * @see org.xml.sax.EntityResolver#resolveEntity(java.lang.String, java.lang.String)
         * @author
         */
        @Override
        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
            throw new IOException("Not accept external or external dtd.");
        }
    });

    return reader;
}

From source file:org.pentaho.di.trans.steps.getxmldata.GetXMLData.java

License:Apache License

protected boolean setDocument(String StringXML, FileObject file, boolean IsInXMLField, boolean readurl)
        throws KettleException {

    this.prevRow = buildEmptyRow(); // pre-allocate previous row

    try {/*from  w w w.  j a v a 2s .  c  om*/
        SAXReader reader = XMLParserFactoryProducer.getSAXReader(null);
        data.stopPruning = false;
        // Validate XML against specified schema?
        if (meta.isValidating()) {
            reader.setValidation(true);
            reader.setFeature("http://apache.org/xml/features/validation/schema", true);
        } else {
            // Ignore DTD declarations
            reader.setEntityResolver(new IgnoreDTDEntityResolver());
        }

        // Ignore comments?
        if (meta.isIgnoreComments()) {
            reader.setIgnoreComments(true);
        }

        if (data.prunePath != null) {
            // when pruning is on: reader.read() below will wait until all is processed in the handler
            if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "GetXMLData.Log.StreamingMode.Activated"));
            }
            if (data.PathValue.equals(data.prunePath)) {
                // Edge case, but if true, there will only ever be one item in the list
                data.an = new ArrayList<>(1); // pre-allocate array and sizes
                data.an.add(null);
            }
            reader.addHandler(data.prunePath, new ElementHandler() {
                public void onStart(ElementPath path) {
                    // do nothing here...
                }

                public void onEnd(ElementPath path) {
                    if (isStopped()) {
                        // when a large file is processed and it should be stopped it is still reading the hole thing
                        // the only solution I see is to prune / detach the document and this will lead into a
                        // NPE or other errors depending on the parsing location - this will be treated in the catch part below
                        // any better idea is welcome
                        if (log.isBasic()) {
                            logBasic(BaseMessages.getString(PKG, "GetXMLData.Log.StreamingMode.Stopped"));
                        }
                        data.stopPruning = true;
                        path.getCurrent().getDocument().detach(); // trick to stop reader
                        return;
                    }

                    // process a ROW element
                    if (log.isDebug()) {
                        logDebug(BaseMessages.getString(PKG, "GetXMLData.Log.StreamingMode.StartProcessing"));
                    }
                    Element row = path.getCurrent();
                    try {
                        // Pass over the row instead of just the document. If
                        // if there's only one row, there's no need to
                        // go back to the whole document.
                        processStreaming(row);
                    } catch (Exception e) {
                        // catch the KettleException or others and forward to caller, e.g. when applyXPath() has a problem
                        throw new RuntimeException(e);
                    }
                    // prune the tree
                    row.detach();
                    if (log.isDebug()) {
                        logDebug(BaseMessages.getString(PKG, "GetXMLData.Log.StreamingMode.EndProcessing"));
                    }
                }
            });
        }

        if (IsInXMLField) {
            // read string to parse
            data.document = reader.read(new StringReader(StringXML));
        } else if (readurl && KettleVFS.startsWithScheme(StringXML)) {
            data.document = reader.read(KettleVFS.getInputStream(StringXML));
        } else if (readurl) {
            // read url as source
            HttpClient client = HttpClientManager.getInstance().createDefaultClient();
            HttpGet method = new HttpGet(StringXML);
            method.addHeader("Accept-Encoding", "gzip");
            HttpResponse response = client.execute(method);
            Header contentEncoding = response.getFirstHeader("Content-Encoding");
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                if (contentEncoding != null) {
                    String acceptEncodingValue = contentEncoding.getValue();
                    if (acceptEncodingValue.contains("gzip")) {
                        GZIPInputStream in = new GZIPInputStream(responseEntity.getContent());

                        data.document = reader.read(in);
                    }
                } else {
                    data.document = reader.read(responseEntity.getContent());
                }
            }
        } else {
            // get encoding. By default UTF-8
            String encoding = "UTF-8";
            if (!Utils.isEmpty(meta.getEncoding())) {
                encoding = meta.getEncoding();
            }
            InputStream is = KettleVFS.getInputStream(file);
            try {
                data.document = reader.read(is, encoding);
            } finally {
                BaseStep.closeQuietly(is);
            }
        }

        if (meta.isNamespaceAware()) {
            prepareNSMap(data.document.getRootElement());
        }
    } catch (Exception e) {
        if (data.stopPruning) {
            // ignore error when pruning
            return false;
        } else {
            throw new KettleException(e);
        }
    }
    return true;
}

From source file:org.pentaho.di.ui.trans.steps.getxmldata.LoopNodesImportProgressDialog.java

License:Apache License

@SuppressWarnings("unchecked")
private String[] doScan(IProgressMonitor monitor) throws Exception {
    monitor.beginTask(/*from w w w . j  a  va2s  .c om*/
            BaseMessages.getString(PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.ScanningFile", filename),
            1);

    SAXReader reader = XMLParserFactoryProducer.getSAXReader(null);
    monitor.worked(1);
    if (monitor.isCanceled()) {
        return null;
    }
    // Validate XML against specified schema?
    if (meta.isValidating()) {
        reader.setValidation(true);
        reader.setFeature("http://apache.org/xml/features/validation/schema", true);
    } else {
        // Ignore DTD
        reader.setEntityResolver(new IgnoreDTDEntityResolver());
    }
    monitor.worked(1);
    monitor.beginTask(
            BaseMessages.getString(PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.ReadingDocument"), 1);
    if (monitor.isCanceled()) {
        return null;
    }
    InputStream is = null;
    try {
        Document document = null;
        if (!Utils.isEmpty(filename)) {
            is = KettleVFS.getInputStream(filename);
            document = reader.read(is, encoding);
        } else {
            if (!Utils.isEmpty(xml)) {
                document = reader.read(new StringReader(xml));
            } else {
                document = reader.read(new URL(url));
            }
        }
        monitor.worked(1);
        monitor.beginTask(
                BaseMessages.getString(PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.DocumentOpened"), 1);
        monitor.worked(1);
        monitor.beginTask(
                BaseMessages.getString(PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.ReadingNode"), 1);

        if (monitor.isCanceled()) {
            return null;
        }
        List<Node> nodes = document.selectNodes(document.getRootElement().getName());
        monitor.worked(1);
        monitor.subTask(BaseMessages.getString(PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.FetchNodes"));

        if (monitor.isCanceled()) {
            return null;
        }
        for (Node node : nodes) {
            if (monitor.isCanceled()) {
                return null;
            }
            if (!listpath.contains(node.getPath())) {
                nr++;
                monitor.subTask(BaseMessages.getString(PKG,
                        "GetXMLDateLoopNodesImportProgressDialog.Task.FetchNodes", String.valueOf(nr)));
                monitor.subTask(BaseMessages.getString(PKG,
                        "GetXMLDateLoopNodesImportProgressDialog.Task.AddingNode", node.getPath()));
                listpath.add(node.getPath());
                addLoopXPath(node, monitor);
            }
        }
        monitor.worked(1);
    } finally {
        try {
            if (is != null) {
                is.close();
            }
        } catch (Exception e) { /* Ignore */
        }
    }
    String[] list_xpath = listpath.toArray(new String[listpath.size()]);

    monitor.setTaskName(
            BaseMessages.getString(PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.NodesReturned"));

    monitor.done();

    return list_xpath;

}