Example usage for org.dom4j.io SAXReader setXMLReaderClassName

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

Introduction

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

Prototype

public void setXMLReaderClassName(String xmlReaderClassName) throws SAXException 

Source Link

Document

Sets the class name of the XMLReader to be used to parse SAX events.

Usage

From source file:galign.helpers.AlignmentMapping.java

License:Apache License

/**
 * @throws org.xml.sax.SAXException/*w  w  w .j  a  v a 2s  . 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//from  www . j  a 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 ww w.j a v  a 2  s .c o  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:org.jboss.mx.metadata.XMLMetaData.java

License:Open Source License

/**
 * Constructs the Model MBean metadata. This implementation reads the
 * document type definition from the beginning of the XML file and picks
 * a corresponding XML builder based on the schema name. In case no
 * document type is defined the latest schema builder for this JBossMX
 * release is used. <p>// w ww .  j  a  v  a 2  s  .  co m
 *
 * The SAX parser implementation is selected by default based on JAXP
 * configuration. If you want to use JAXP to select the parser, you can
 * set the system property <tt>"javax.xml.parsers.SAXParserFactory"</tt>.
 * For example, to use Xerces you might define:   <br><pre>
 *
 *    java -Djavax.xml.parsers.SAXParserFactory=org.apache.xerces.jaxp.SAXParserFactoryImpl ...
 *
 * </pre>
 *
 * In case you can't or don't want to use JAXP to configure the SAX parser
 * implementation you can override the SAX parser implementation by setting
 * an MBean descriptor field {@link XMBeanConstants#SAX_PARSER} to the
 * parser class string value.
 *
 * @return initialized MBean info
 * @throws NotCompliantMBeanException if there were errors building the
 *         MBean info from the given XML file.
 */
public MBeanInfo build() throws NotCompliantMBeanException {
    try {
        int version = NO_VERSION;

        if (versionString == null) {
            // by default, let JAXP pick the SAX parser
            SAXReader reader = new SAXReader();

            // check if user wants to override the SAX parser property
            if (properties.get(SAX_PARSER) != null) {
                try {
                    reader.setXMLReaderClassName(getStringProperty(SAX_PARSER));
                }

                catch (SAXException e) {
                    //Should log and ignore, I guess
                } // end of try-catch
            }
            // by default we validate
            reader.setValidation(true);

            // the user can override the validation by setting the VALIDATE property
            try {
                boolean validate = getBooleanProperty(XML_VALIDATION);
                reader.setValidation(validate);
            } catch (IllegalPropertyException e) {
                // FIXME: log the exception (warning)

                // fall through, use the default value
            }

            //supply it with our dtd locally.
            reader.setEntityResolver(new JBossEntityResolver());

            // get the element and start parsing...
            Document doc = reader.read(url);
            element = doc.getRootElement();
            DocumentType type = doc.getDocType();

            version = validateVersionString(type.getPublicID());
            if (version == NO_VERSION) {
                version = validateVersionString(type.getSystemID());
            } // end of if ()

        } else {
            version = validateVersionString(versionString);
        } // end of else

        if (element == null) {
            throw new IllegalStateException("No element supplied with explict version!");
        }
        // These are the known schemas for us. Pick the correct one based on
        // schema or default to the latest.docURL.endsWith(JBOSSMX_XMBEAN_DTD_1_0)
        if (version == JBOSS_XMBEAN_1_0 || version == JBOSS_XMBEAN_1_1 || version == JBOSS_XMBEAN_1_2) {
            // jboss_xmbean_1_0.dtd is the only implemented useful xmbean
            return new JBossXMBean10(mmbClassName, resourceClassName, element, properties).build();
        } else {
            throw new NotCompliantMBeanException("Unknown xmbean type " + versionString);
        } // end of else

    } catch (DocumentException e) {
        throw new JBossNotCompliantMBeanException("Error parsing the XML file, from XMLMetaData: ", e);
    }
}