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:fr.cph.stock.language.XMLRetriever.java

License:Apache License

/**
 * Parser of the input stream//from w  ww . j  a  v  a 2 s  . c  o  m
 *
 * @param inputStream the stream
 * @return a Document
 * @throws DocumentException the document exception
 */

protected final Document parse(final URL inputStream) throws DocumentException {
    final SAXReader reader = new SAXReader(true);
    reader.setEntityResolver(getEntityResolver());
    return reader.read(inputStream);
}

From source file:galign.helpers.AlignmentMapping.java

License:Apache License

/**
 * @throws org.xml.sax.SAXException//  w  ww .j a v  a  2  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 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// w w  w.  j  a  v  a2 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//  w w  w  . j  a  v  a 2  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:gov.nih.nci.caarray.upgrade.SingleConnectionHibernateHelper.java

License:BSD License

/**
 * @param connection/*from ww w.  j a  v a2 s.  c o  m*/
 */
public void initialize(Connection connection) {
    HibernateSingleConnectionProvider.setConnection(connection);

    InputStream configurationStream = FixIlluminaGenotypingCsvDesignProbeNamesMigrator.class
            .getResourceAsStream("/hibernate.cfg.xml");
    SAXReader reader = new SAXReader();
    reader.setEntityResolver(new org.hibernate.util.DTDEntityResolver());
    Document configurationDocument = null;
    try {
        configurationDocument = reader.read(configurationStream);
    } catch (DocumentException e) {
        throw new UnhandledException(e);
    }
    Node sessionFactoryNode = configurationDocument
            .selectSingleNode("/hibernate-configuration/session-factory");

    Iterator<?> iter = ((Branch) sessionFactoryNode).nodeIterator();
    while (iter.hasNext()) {
        Node currentNode = (Node) iter.next();
        if (currentNode.getNodeType() == Node.ELEMENT_NODE && !currentNode.getName().equals("mapping")) {
            iter.remove();
        }
    }

    DOMWriter domWriter = new DOMWriter();
    org.w3c.dom.Document document = null;
    try {
        document = domWriter.write(configurationDocument);
    } catch (DocumentException e) {
        throw new UnhandledException(e);
    }

    configuration = new AnnotationConfiguration();
    configuration.setProperty(Environment.CONNECTION_PROVIDER,
            "gov.nih.nci.caarray.upgrade.HibernateSingleConnectionProvider");

    configuration.configure(document);

    configuration.setProperty(Environment.TRANSACTION_STRATEGY,
            "org.hibernate.transaction.JDBCTransactionFactory");
    configuration.setProperty(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
    configuration.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "false");
    configuration.getProperties().remove(Environment.TRANSACTION_MANAGER_STRATEGY);
    configuration.setNamingStrategy(new NamingStrategy());

    sessionFactory = configuration.buildSessionFactory();
}

From source file:hk.hku.cecid.piazza.commons.util.LoggerLog4j.java

License:Open Source License

/**
 * Loads the configuration from the specified url location.
 * A DOM configuration will be triggered if the url ends with ".xml". 
 * /*w  w w  .  j  av  a 2s  .  c o m*/
 * @param url the url of the configuration source.
 * @throws Exception if the operation is unsuccessful. 
 * @see hk.hku.cecid.piazza.commons.module.PersistentComponent#loading(java.net.URL)
 */
protected void loading(URL url) throws Exception {
    if (url.getPath().toLowerCase().endsWith(".xml")) {
        Properties params = getParameters();
        if (params != null) {
            String checkConfig = params.getProperty("checkConfig");

            if (checkConfig != null) {
                if (checkConfig.equals("true")) {

                    SAXReader xmlReader = new SAXReader();
                    xmlReader.setEntityResolver(new EntityResolver() {
                        public InputSource resolveEntity(String publicId, String systemId)
                                throws SAXException, IOException {
                            return new InputSource(new ByteArrayInputStream(new byte[0]));
                        }
                    });
                    org.dom4j.Document doc = xmlReader.read(url);
                    Node node = doc.selectSingleNode("/*[local-name()='configuration']/category/priority");
                    String priority = node.valueOf("@value");
                    if (!chechPriorityString(priority)) {
                        throw new UtilitiesException(
                                "Log4j does not support the value for priority - " + priority);
                    }

                    // Node node2 = doc.selectSingleNode("/*[local-name()='configuration']/root/priority");
                    String priority2 = node.valueOf("@value");
                    if (!chechPriorityString(priority2)) {
                        throw new UtilitiesException(
                                "Log4j does not support the value for priority - " + priority2);
                    }
                }
            }
        }

        DOMConfigurator.configure(url);
    } else {
        PropertyConfigurator.configure(url);
    }
}

From source file:hudson.tasks.junit.XMLEntityResolver.java

License:Open Source License

/**
 * Install EntityResolver for resolving DTDs, which are in files created by TestNG.
 *///from   w  w w  .j ava 2s . c om
@Override
public void configure(SAXReader reader, Object context) {
    if (context instanceof SuiteResultParserConfigurationContext) {
        reader.setEntityResolver(this);
    }
}

From source file:io.vertigo.persona.plugins.security.loaders.XmlSecurityLoader.java

License:Apache License

private static Document createDocument(final URL url, final EntityResolver entityResolver) {
    Assertion.checkNotNull(url);// w w  w. j  a v a 2  s.com
    //-----
    try {
        final SAXReader builder = new SAXReader(true);
        builder.setEntityResolver(entityResolver);
        return builder.read(url.openStream());
    } catch (final Exception e) {
        throw new WrappedException("Erreur durant la lecture du fichier XML " + url, e);
    }
}

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  w  w. j  a  v a 2s .  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.eclipsecs.core.config.ConfigurationReader.java

License:Open Source License

/**
 * Reads the checkstyle configuration from the given stream an returs a list of all modules within this
 * configuration./* w  ww . ja  v  a2 s  .c om*/
 * 
 * @param in
 *            the stream the configuration is loaded from
 * @return the list of modules
 * @throws CheckstylePluginException
 *             error while reading the configuration
 */
public static List<Module> read(InputSource in) throws CheckstylePluginException {

    List<Module> rules = null;
    try {

        final SAXReader reader = new SAXReader();
        reader.setEntityResolver(new XMLUtil.InternalDtdEntityResolver(PUBLIC2INTERNAL_DTD_MAP));
        final Document document = reader.read(in);

        rules = getModules(document);
    } catch (final DocumentException ex) {
        CheckstylePluginException.rethrow(ex);
    }

    return rules != null ? rules : new ArrayList<Module>();
}