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:edu.ku.brc.helpers.XMLHelper.java

License:Open Source License

public static org.dom4j.Element readStrToDOM4J(final String data) throws Exception {
    SAXReader saxReader = new SAXReader();

    saxReader.setValidation(false);//from  ww w.  j  a v  a 2s  . c  o  m
    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(new StringReader(data));
    return document.getRootElement();
}

From source file:edu.ku.brc.specify.toycode.L18NStringResApp.java

License:Open Source License

/**
 * Reads a DOM from a stream//from   www  .  j ava 2  s . c om
 * @param fileinputStream the stream to be read
 * @return the root element of the DOM
 */
public static org.dom4j.Document 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());

    return saxReader.read(fileinputStream);
}

From source file:es.caib.seycon.ng.servei.PuntEntradaServiceImpl.java

protected String handleValidaXMLPUE(PuntEntrada puntEntrada) throws Exception {
    String contingut = puntEntrada.getXmlPUE();

    if (!"".equals(contingut)) { //$NON-NLS-1$

        try {//from w ww  .j av  a 2  s  .c  om
            // Validem el document
            // new
            // es.caib.seycon.mazinger.compiler.Compile().parse(contingut);
            org.dom4j.io.SAXReader reader = new org.dom4j.io.SAXReader(true);
            // set the validation feature to true to report validation
            // errors
            reader.setFeature("http://xml.org/sax/features/validation", true); //$NON-NLS-1$

            // set the validation/schema feature to true to report
            // validation errors
            // against a schema
            reader.setFeature("http://apache.org/xml/features/validation/schema", true); //$NON-NLS-1$
            // 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", //$NON-NLS-1$
                    true);
            // set the schema
            reader.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", //$NON-NLS-1$
                    "/es/caib/seycon/mazinger/Mazinger.xsd"); //$NON-NLS-1$
            // set the entity resolver (to load the schema with
            // getResourceAsStream)
            reader.getXMLReader().setEntityResolver(new SchemaLoader());
            reader.setEntityResolver(new SchemaLoader());

            Document doc = reader.read(new ByteArrayInputStream(contingut.getBytes("UTF-8"))); //$NON-NLS-1$

        } catch (Exception ex) {
            return ex.getMessage(); // Retornem l'excepci com error de
                                    // Validaci
        }
    }
    return ""; //$NON-NLS-1$
}

From source file:galign.helpers.AlignmentMapping.java

License:Apache License

/**
 * @throws org.xml.sax.SAXException//  w ww  .j  av  a2s.  c  om
 * @throws org.dom4j.DocumentException
 */
public void loadFromFile(String p_fileName) throws Exception {
    // Dom4j 1.1 had problems parsing XML files correctly so we
    // used Xerces. Now with 1.5 and AElfred 2 the problems may
    // have been fixed.

    // Reading from a file, need to use Xerces
    // SAXReader reader = new SAXReader();
    //reader.setXMLReaderClassName("org.apache.xerces.parsers.SAXParser");
    //reader.setEntityResolver(DtdResolver.getInstance());
    //reader.setValidation(true);

    SAXReader reader = new SAXReader();
    reader.setXMLReaderClassName("org.dom4j.io.aelfred2.SAXDriver");
    reader.setFeature("http://xml.org/sax/features/external-general-entities", false);
    reader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
    reader.setEntityResolver(new GamEntityResolver());
    reader.setValidation(false);

    InputSource src = new InputSource(new FileInputStream(p_fileName));

    init(reader, src);
}

From source file:galign.helpers.AlignmentPackage.java

License:Apache License

/**
 * Initializes an instance from a GAP file.
 *
 * @throws org.xml.sax.SAXException// www .ja  va2 s.c o  m
 * @throws org.dom4j.DocumentException
 */
public void loadFromFile(String p_fileName) throws Exception {
    // Dom4j 1.1 had problems parsing XML files correctly so we
    // used Xerces. Now with 1.5 and AElfred 2 the problems may
    // have been fixed.

    // Reading from a file, need to use Xerces
    // SAXReader reader = new SAXReader();
    //reader.setXMLReaderClassName("org.apache.xerces.parsers.SAXParser");
    //reader.setEntityResolver(DtdResolver.getInstance());
    //reader.setValidation(true);

    SAXReader reader = new SAXReader();
    reader.setXMLReaderClassName("org.dom4j.io.aelfred2.SAXDriver");
    reader.setFeature("http://xml.org/sax/features/external-general-entities", false);
    reader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
    reader.setEntityResolver(new GapEntityResolver());
    reader.setValidation(false);

    Reader ioReader = new InputStreamReader(new FileInputStream(p_fileName), "UTF-8");
    InputSource src = new InputSource(ioReader);

    init(reader, src, new java.io.File(p_fileName).getParent());
}

From source file:galign.helpers.tmx.TmxFile.java

License:Apache License

/**
 * @throws org.xml.sax.SAXException/*from w  w w .  j  a v a2 s  .co  m*/
 * @throws org.dom4j.DocumentException
 */
public void loadFromFile(String p_fileName) throws Exception {
    clearTus();

    m_fileName = p_fileName;

    // Dom4j 1.1 had problems parsing XML files correctly so we
    // used Xerces. Now with 1.5 and AElfred 2 the problems may
    // have been fixed.

    // Validation of XML files is not supported by AElfred so we
    // must use Xerces. But since some TMX files contain no DTD
    // decl we just don't validate.
    //SAXReader reader = new SAXReader();
    //reader.setXMLReaderClassName("org.apache.xerces.parsers.SAXParser");
    //reader.setEntityResolver(DtdResolver.getInstance());
    //reader.setValidation(true);

    SAXReader reader = new SAXReader();
    reader.setXMLReaderClassName("org.dom4j.io.aelfred2.SAXDriver");
    reader.setFeature("http://xml.org/sax/features/external-general-entities", false);
    reader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
    reader.setEntityResolver(new TmxEntityResolver());
    reader.setValidation(false);

    InputSource src = new InputSource(new FileInputStream(p_fileName));

    init(reader, src);
}

From source file:hudson.plugins.tolerantxmlparsing.TolerantXMLErrorHandler.java

License:Open Source License

/**
 * Attach an instance of this class as this reader's error handler.
 * This will also set the feature {@link #CONTINUE_AFTER_FATAL_ERROR_FEATURE CONTINUE_AFTER_FATAL_ERROR_FEATURE},
 * assuming the SAX parser is XERCES./*ww  w .j  av a2  s. com*/
 * @param reader
 */
public static void attach(SAXReader reader, boolean ignoreSetFeatureException) throws SAXException {
    try {
        reader.setFeature(XERCES_CONTINUE_AFTER_FATAL_ERROR_FEATURE, true);
    } catch (SAXException ex) {
        if (!ignoreSetFeatureException) {
            throw ex;
        }
        ex.printStackTrace();
    }
    reader.setErrorHandler(new TolerantXMLErrorHandler());
}

From source file:jt56.comm.code.util.WolfXmlUtil.java

License:Open Source License

public Document getPath(String filePath, String coding) {
    SAXReader saxReader = new SAXReader();

    Document document = null;//from w w w . j a v a2 s. c  o  m
    try {
        saxReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        BufferedReader read = new BufferedReader(
                new InputStreamReader(new FileInputStream(new File(filePath)), coding));
        document = saxReader.read(read);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return document;
}

From source file:mondrian.util.XmlParserFactoryProducer.java

License:Open Source License

public static SAXReader getSAXReader(final EntityResolver resolver) {
    SAXReader reader = new SAXReader();
    if (resolver != null) {
        reader.setEntityResolver(resolver);
    }/*from w  ww  . j ava 2 s .  c om*/
    try {
        reader.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        reader.setFeature("http://xml.org/sax/features/external-general-entities", false);
        reader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    } catch (SAXException e) {
        logger.error("Some parser properties are not supported.");
    }
    reader.setIncludeExternalDTDDeclarations(false);
    reader.setIncludeInternalDTDDeclarations(false);
    return reader;
}

From source file:net.sf.jguard.core.util.XMLUtils.java

License:Open Source License

/**
 * read the xml data storage file for users and associated principals.
 *
 * @param xml    file location to read//from w ww  .j a  va2s.  c  o  m
 * @param schema
 * @return xml content
 */
public static Document read(URL xml, InputSource schema) {

    SAXReader reader = new SAXReader(true);
    Document document;

    try {
        //activate schema validation
        reader.setFeature(XML_VALIDATION, true);
        reader.setFeature(APACHE_VALIDATION, true);
        reader.setProperty(JAXP_SCHEMA_LANGUAGE, JAXP_XML_SCHEMA_LANGUAGE);
        reader.setProperty(JAXP_SCHEMA_SOURCE, schema);

    } catch (SAXException ex) {
        logger.error("read(String) : " + ex.getMessage(), ex);
        throw new IllegalArgumentException(ex.getMessage(), ex);
    }

    try {
        document = reader.read(xml);
    } catch (DocumentException e1) {
        logger.error("read(String) : " + e1, e1);
        throw new IllegalArgumentException(e1.getMessage(), e1);
    }

    if (document == null) {
        logger.warn("we create a default document");
        document = DocumentHelper.createDocument();
    }
    return document;
}