Example usage for org.dom4j.io SAXReader setFeature

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

Introduction

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

Prototype

public void setFeature(String name, boolean value) throws SAXException 

Source Link

Document

Sets a SAX feature on the underlying SAX parser.

Usage

From source file:com.panet.imeta.trans.steps.getxmldata.GetXMLData.java

License:Open Source License

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

    try {/*ww  w . j  a v a  2 s. c  om*/
        SAXReader reader = new SAXReader();
        data.stopPruning = false;

        // Validate XML against specified schema?
        if (meta.isValidating()) {
            reader.setValidation(true);
            reader.setFeature("http://apache.org/xml/features/validation/schema", true);
        }

        // 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(Messages.getString("GetXMLData.Log.StreamingMode.Activated"));
            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(Messages.getString("GetXMLData.Log.StreamingMode.Stopped"));
                        data.stopPruning = true;
                        path.getCurrent().getDocument().detach(); // trick
                        // to
                        // stop
                        // reader
                        return;
                    }

                    // process a ROW element
                    if (log.isDebug())
                        logDebug(Messages.getString("GetXMLData.Log.StreamingMode.StartProcessing"));
                    Element row = path.getCurrent();
                    try {
                        processStreaming(row.getDocument());
                    } 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(Messages.getString("GetXMLData.Log.StreamingMode.EndProcessing"));
                }
            });
        }

        if (IsInXMLField) {
            // read string to parse
            data.document = reader.read(new StringReader(StringXML));
        } else if (readurl) {
            // read url as source
            data.document = reader.read(new URL(StringXML));
        } else {
            // get encoding. By default UTF-8
            String encoding = "UTF-8";
            if (!Const.isEmpty(meta.getEncoding()))
                encoding = meta.getEncoding();
            data.document = reader.read(KettleVFS.getInputStream(file), encoding);
        }

        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:com.sap.data.db.dao.StructureUtil.java

private Document getDocument(String filePath) throws DocumentException, FileNotFoundException, SAXException {
    InputStream inputStream = null;
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    if (classLoader != null) {
        inputStream = classLoader.getResourceAsStream(filePath);
    }/*w w w . ja  v a2s .  c o m*/
    SAXReader reader = new SAXReader();
    reader.setFeature(this.NONVALIDATING_DTD, false);
    Document document = reader.read(inputStream);
    return document;
}

From source file:com.thoughtworks.cruise.utils.configfile.CruiseConfigDom.java

License:Apache License

static Document parse(String xml) throws DocumentException, SAXException, URISyntaxException {
    URL resource = schemaResource();
    SAXReader builder = new SAXReader();
    builder.setFeature("http://apache.org/xml/features/validation/schema", true);
    builder.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
            resource.toURI().toString());
    builder.setValidation(true);//from   w w w .ja v  a  2s.  com
    Document dom = null;
    try {
        dom = builder.read(new StringReader(xml));
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("XML invalid. " + xml, e);
    }
    return dom;
}

From source file:com.webslingerz.jpt.PageTemplateImpl.java

License:Open Source License

private static SAXReader createXMLReader() throws SAXException {
    SAXReader reader = new SAXReader();
    reader.setIgnoreComments(false);/*from   w ww.j  a  v a 2s.  c o  m*/
    reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    return reader;
}

From source file:cz.mzk.editor.server.fedora.utils.DocumentValidator.java

License:Open Source License

/**
 * Validation according to schema in url
 * //from   ww w.j  av  a2  s.co m
 * @param documentFile
 * @param schemaUrl
 * @return
 */
public static boolean isValid(File documentFile, String schemaUrl) {
    try {
        SAXReader reader = new SAXReader(true);
        // set the validation feature to true to report validation errors
        reader.setFeature("http://xml.org/sax/features/validation", true);
        // set the validation/schema feature to true to report validation errors against a schema
        reader.setFeature("http://apache.org/xml/features/validation/schema", true);
        // set the validation/schema-full-checking feature to true to enable full schema, grammar-constraint checking
        reader.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
        //reader.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", schemaUrl);
        reader.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation", schemaUrl);
        readDocument(reader, documentFile);
        return true;
    } catch (DocumentException ex) {
        logger.log(Level.SEVERE, ex.getMessage());
        return false;
    } catch (SAXException ex) {
        logger.log(Level.SEVERE, null, ex);
        return false;
    }
}

From source file:cz.mzk.editor.server.fedora.utils.DocumentValidator.java

License:Open Source License

/**
 * Validation according to schema in url
 * //  w  ww  .ja  v a2 s  .c  o  m
 * @param documentFile
 * @param schemaUrl
 * @return
 */
public static boolean isValid(File documentFile, File schemaFile) {
    try {
        SAXReader reader = new SAXReader(true);
        reader.setFeature("http://apache.org/xml/features/validation/schema", true);
        // set the validation feature to true to report validation errors
        reader.setFeature("http://xml.org/sax/features/validation", true);
        // set the validation/schema feature to true to report validation errors against a schema
        reader.setFeature("http://apache.org/xml/features/validation/schema", true);
        // set the validation/schema-full-checking feature to true to enable full schema, grammar-constraint checking
        reader.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
        reader.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
                schemaFile.getAbsolutePath());
        readDocument(reader, documentFile);
        return true;
    } catch (DocumentException ex) {
        logger.log(Level.SEVERE, ex.getMessage());
        return false;
    } catch (SAXException ex) {
        logger.log(Level.SEVERE, null, ex);
        return false;
    }
}

From source file:cz.mzk.editor.server.fedora.utils.DocumentValidator.java

License:Open Source License

public static boolean isValid(File file) {
    try {// w  w  w.  j  a  va 2s . c o m
        SAXReader reader = new SAXReader(true);
        reader.setFeature("http://apache.org/xml/features/validation/schema", true);
        // set the validation feature to true to report validation errors
        reader.setFeature("http://xml.org/sax/features/validation", true);
        // set the validation/schema feature to true to report validation errors against a schema
        reader.setFeature("http://apache.org/xml/features/validation/schema", true);
        // set the validation/schema-full-checking feature to true to enable full schema, grammar-constraint checking
        reader.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
        readDocument(reader, file);
        return true;
    } catch (DocumentException ex) {
        logger.log(Level.SEVERE, ex.getMessage());
        return false;
    } catch (SAXException ex) {
        logger.log(Level.SEVERE, null, ex);
        return false;
    }
}

From source file:de.ailis.wlandsuite.utils.XmlUtils.java

License:Open Source License

/**
 * Reads a document from the specified input stream. The XML reader is fully
 * configured to validate the wlandsuite namespace.
 * /*from  w  w w  . ja v a 2  s  .c o  m*/
 * @param stream
 *            The input stream
 * @return The validated document
 */

public static Document readDocument(InputStream stream) {
    SAXReader reader;

    reader = new SAXReader(true);
    try {
        reader.setFeature("http://xml.org/sax/features/validation", true);
        reader.setFeature("http://apache.org/xml/features/validation/schema", true);
        reader.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
        reader.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation",
                "http://ailis.de/wlandsuite classpath://de/ailis/wlandsuite/resource/wlandsuite.xsd");
        reader.setEntityResolver(ClasspathEntityResolver.getInstance());
        return reader.read(stream);
    } catch (SAXException e) {
        throw new XmlException("Unable to configure XML reader: " + e.toString(), e);
    } catch (DocumentException e) {
        throw new XmlException("Unable to read XML document: " + e.toString(), e);
    }
}

From source file:dom4j.Dom4JExample.java

public Document parse() throws DocumentException, SAXException {
    SAXReader reader = new SAXReader();

    reader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false);

    //reader.setFeature("http://xml.org/sax/features/external-general-entities", true);//WORKS but throws exception if DTD tag is encountered
    //reader.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); // NOT WORKING FOR entity attack but should work for DOS attack
    Document document = reader.read(this.getClass().getResourceAsStream(Utils.INTERNAL_XML_LOCATION));
    return document;
}

From source file:edu.ku.brc.helpers.XMLHelper.java

License:Open Source License

/**
 * Reads a DOM from a stream//from w w w .ja  va2 s  .co m
 * @param fileinputStream the stream to be read
 * @return the root element of the DOM
 */
public static org.dom4j.Element readFileToDOM4J(final FileInputStream fileinputStream) throws Exception {
    SAXReader saxReader = new SAXReader();

    saxReader.setValidation(false);
    saxReader.setStripWhitespaceText(true);

    saxReader.setFeature("http://xml.org/sax/features/namespaces", false);
    saxReader.getXMLReader().setFeature("http://xml.org/sax/features/namespaces", false);

    //saxReader.setFeature("http://apache.org/xml/features/validation/schema", false);
    //saxReader.setFeature("http://xml.org/sax/features/validation", false);
    //saxReader.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
    //                   (FormViewFactory.class.getResource("../form.xsd")).getPath());

    org.dom4j.Document document = saxReader.read(fileinputStream);
    return document.getRootElement();
}