Example usage for org.dom4j.io SAXReader SAXReader

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

Introduction

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

Prototype

public SAXReader(String xmlReaderClassName) throws SAXException 

Source Link

Usage

From source file:com.doculibre.constellio.services.SolrServicesImpl.java

License:Open Source License

public static Document readXMLConfigInCloud(String collectionName, String fileName) {
    String realCollectionName;//from   www  . j a v a2 s  . c o  m
    if (SolrServicesImpl.isAliasInCloud(collectionName)) {
        realCollectionName = SolrServicesImpl.getRealCollectionInCloud(collectionName);
    } else {
        realCollectionName = collectionName;
    }
    Document schemaDocument = null;
    try {
        SolrZkClient zkClient = SolrCoreContext.getSolrZkClient();
        byte[] data = zkClient.getData(ZkController.CONFIGS_ZKNODE + "/" + realCollectionName + "/" + fileName,
                null, null, true);
        schemaDocument = new SAXReader(false).read(new ByteArrayInputStream(data));
    } catch (DocumentException e) {
        throw new RuntimeException(e);
    } catch (KeeperException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return schemaDocument;
}

From source file:com.doculibre.constellio.services.SolrServicesImpl.java

License:Open Source License

private static Document readSchema(File schemaFile) {
    Document schemaDocument;//from  w w  w  .  ja  v a 2s.  c  o m
    if (schemaFile.exists()) {
        try {
            schemaDocument = new SAXReader(false).read(schemaFile);
        } catch (DocumentException e) {
            throw new RuntimeException(e);
        }
    } else {
        schemaDocument = null;
    }
    return schemaDocument;
}

From source file:com.dtolabs.shared.resources.ResourceXMLParser.java

License:Apache License

/**
 * Parse the document, applying the configured Receiver to the parsed entities
 *
 * @throws ResourceXMLParserException parse error
 * @throws java.io.IOException io error/*from w ww.  ja  v  a2s.com*/
 */
public void parse() throws ResourceXMLParserException, IOException {
    final EntityResolver resolver = createEntityResolver();
    final SAXReader reader = new SAXReader(false);
    reader.setEntityResolver(resolver);

    try {

        final Document doc;
        if (null == this.doc) {
            final InputStream in;
            if (null != file) {
                in = new FileInputStream(file);
            } else {
                in = input;
            }
            try {
                doc = reader.read(in);
            } finally {
                if (null != file) {
                    in.close();
                }
            }
        } else {
            doc = this.doc;
        }

        final EntitySet set = new EntitySet();
        final Element root = doc.getRootElement();

        final List list = root.selectNodes(entityXpath);
        for (final Object n : list) {
            final Node node = (Node) n;
            final Entity ent = parseEnt(node, set);
            if (null != receiver) {
                if (!receiver.resourceParsed(ent)) {
                    break;
                }
            }
        }
        if (null != receiver) {
            receiver.resourcesParsed(set);
        }

    } catch (DocumentException e) {
        throw new ResourceXMLParserException(e);
    }
}

From source file:com.genericworkflownodes.knime.config.impl.GalaxyNodeConfigurationReader.java

License:Open Source License

@Override
public INodeConfiguration read(InputStream in) throws Exception {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser parser = factory.newSAXParser();

    SAXReader reader = new SAXReader(parser.getXMLReader());
    reader.setValidation(false);//from   ww  w  . j a  v a 2  s  . co m

    SimpleErrorHandler errorHandler = new SimpleErrorHandler();

    reader.setErrorHandler(errorHandler);

    doc = reader.read(in);

    if (!errorHandler.isValid()) {
        System.err.println(errorHandler.getErrorReport());
        throw new Exception("Galaxy tool xml file is not valid !");
    }

    readPorts();
    readParameters();
    readDescription();

    config.setXml(doc.asXML());

    return config;
}

From source file:com.genericworkflownodes.knime.schemas.SchemaValidator.java

License:Open Source License

/**
 * Validate the given xml stream against the stored schemata.
 * //from w w  w .  j  a  va  2 s .co m
 * @param xmlstream
 *            The stream to validate.
 * @return True if the file is valid, false otherwise.
 */
public boolean validates(InputStream xmlstream) {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

    SimpleErrorHandler errorHandler = new SimpleErrorHandler();

    try {
        factory.setSchema(schemaFactory.newSchema(getSchemaSources()));

        SAXParser parser = factory.newSAXParser();

        SAXReader reader = new SAXReader(parser.getXMLReader());
        reader.setValidation(false);

        reader.setErrorHandler(errorHandler);

        reader.read(xmlstream);
    } catch (SAXException e) {
        throw new RuntimeException(e);
    } catch (DocumentException e) {
        throw new RuntimeException(e);
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    }

    if (!errorHandler.isValid()) {
        errorReport = errorHandler.getErrorReport();
        return false;
    }

    return true;
}

From source file:com.googlecode.fascinator.common.PythonUtils.java

License:Open Source License

public PythonUtils(JsonSimpleConfig config) throws PluginException {
    this.config = config;
    // Security//from www  .  j  a  v a  2 s .c o  m
    String accessControlType = "accessmanager";
    access = PluginManager.getAccessManager(accessControlType);
    access.init(config.toString());

    // XML parsing
    namespaces = new HashMap<String, String>();
    DocumentFactory docFactory = new DocumentFactory();
    docFactory.setXPathNamespaceURIs(namespaces);
    saxReader = new SAXReader(docFactory);

    // Message Queues
    String brokerUrl = config.getString(ActiveMQConnectionFactory.DEFAULT_BROKER_BIND_URL, "messaging", "url");
    connectionFactory = new ActiveMQConnectionFactory(brokerUrl);
    try {
        connection = connectionFactory.createConnection();
        connection.start();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        // create single producer for multiple destinations
        producer = session.createProducer(null);
        producer.setDeliveryMode(DeliveryMode.PERSISTENT);

        // cache destinations
        destinations = new HashMap<String, Destination>();
    } catch (JMSException ex) {
        throw new PluginException(ex);
    }
    String access_plugin = config.getString(DEFAULT_ACCESS_PLUGIN, "accesscontrol", "type");
    if (access_plugin.indexOf(",") >= 0) {
        String[] plugin_list = access_plugin.split(",");
        current_access_plugin = plugin_list[0];
    } else {
        current_access_plugin = access_plugin;
    }
}

From source file:com.googlecode.fascinator.common.sax.SafeSAXReader.java

License:Open Source License

/** Constructor */
public SafeSAXReader() {
    reader = new SAXReader(new SafeSAXParser());
    reader.setValidation(false);//w w  w  .  j  a va  2  s.  c  om
    try {
        reader.setFeature(feature, false);
    } catch (SAXException ex) {
        // Do we care?
    }
}

From source file:com.googlecode.fascinator.redbox.sru.SRUClient.java

License:Open Source License

/**
 * <p>//from  ww w  .  ja  v  a  2 s .c om
 * Simple init for the SAX Reader.
 * </p>
 * 
 */
private void saxInit() {
    namespaces = new HashMap<>();
    DocumentFactory docFactory = new DocumentFactory();
    docFactory.setXPathNamespaceURIs(namespaces);
    saxReader = new SAXReader(docFactory);
}

From source file:com.haulmont.bali.util.Dom4j.java

License:Apache License

private static SAXReader getSaxReader() {
    String useThreadLocalCache = System.getProperty("cuba.saxParserThreadLocalCache");
    if (useThreadLocalCache == null || Boolean.parseBoolean(useThreadLocalCache)) {
        try {/*from www  .  j a v a  2  s .  c  o m*/
            return new SAXReader(getParser().getXMLReader());
        } catch (SAXException e) {
            throw new RuntimeException("Unable to create SAX reader", e);
        }
    } else {
        return new SAXReader();
    }
}

From source file:com.haulmont.yarg.structure.xml.impl.DefaultXmlReader.java

License:Apache License

@Override
public Report parseXml(String xml) throws IOException {
    try {/*from ww  w  .  ja  v  a2s. co  m*/
        SAXReader reader = null;
        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();

            SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

            factory.setSchema(schemaFactory.newSchema(new Source[] { new StreamSource(
                    Thread.currentThread().getContextClassLoader().getResourceAsStream("reporting.xsd")) }));

            SAXParser parser = factory.newSAXParser();

            reader = new SAXReader(parser.getXMLReader());
        } catch (SAXException e) {
            throw new ReportingXmlException(
                    String.format("An error occurred during loading reporting xsd. \\n[%s]", xml), e);
        } catch (ParserConfigurationException e) {
            throw new ReportingXmlException(
                    String.format("An error occurred during loading reporting xsd. \\n[%s]", xml), e);
        }

        Document document = reader.read(new StringReader(xml));
        Element rootElement = document.getRootElement();
        Map<String, ReportTemplate> templateMap = parseTemplates(rootElement);
        List<ReportParameter> reportParameters = parseInputParameters(rootElement);
        List<ReportFieldFormat> reportFieldFormats = parseValueFormats(rootElement);
        BandBuilder rootBandDefinitionBuilder = new BandBuilder().name(BandData.ROOT_BAND_NAME);
        parseQueries(rootElement.element("rootBand"), rootBandDefinitionBuilder);
        parseChildBandDefinitions(rootElement.element("rootBand"), rootBandDefinitionBuilder);
        ReportBand rootBandDefinition = rootBandDefinitionBuilder.build();
        String reportName = rootElement.attribute("name").getText();
        ReportImpl report = new ReportImpl(reportName, templateMap, rootBandDefinition, reportParameters,
                reportFieldFormats);
        return report;
    } catch (DocumentException e) {
        throw new ReportingXmlException(
                String.format("An error occurred while parsing report xml. \\n[%s]", xml), e);
    } catch (FileNotFoundException e) {
        throw new ReportingXmlException(String.format("Could not find report template. \\n[%s]", xml), e);
    } catch (ClassNotFoundException e) {
        throw new ReportingXmlException(String.format("Report parameter class not found. \\n[%s]", xml), e);
    }
}