Search in sources :

Example 1 with SchematronConfigException

use of org.apache.camel.component.schematron.exception.SchematronConfigException in project camel by apache.

the class SchematronEndpoint method doStart.

@Override
protected void doStart() throws Exception {
    super.doStart();
    if (transformerFactory == null) {
        createTransformerFactory();
    }
    if (rules == null) {
        try {
            // Attempt to read the schematron rules from the class path first.
            LOG.debug("Reading schematron rules from class path {}", path);
            InputStream schRules = ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext(), path);
            rules = TemplatesFactory.newInstance().getTemplates(schRules, transformerFactory);
        } catch (Exception classPathException) {
            // Attempts from the file system.
            LOG.debug("Error loading schematron rules from class path, attempting file system {}", path);
            try {
                InputStream schRules = FileUtils.openInputStream(new File(path));
                rules = TemplatesFactory.newInstance().getTemplates(schRules, transformerFactory);
            } catch (FileNotFoundException e) {
                LOG.debug("Schematron rules not found in the file system {}", path);
                // Can be more meaningful, for example, xslt compilation error.
                throw classPathException;
            }
        }
        // rules not found in class path nor in file system.
        if (rules == null) {
            LOG.error("Failed to load schematron rules {}", path);
            throw new SchematronConfigException("Failed to load schematron rules: " + path);
        }
    }
}
Also used : InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) SchematronConfigException(org.apache.camel.component.schematron.exception.SchematronConfigException) File(java.io.File) SchematronConfigException(org.apache.camel.component.schematron.exception.SchematronConfigException) FileNotFoundException(java.io.FileNotFoundException)

Example 2 with SchematronConfigException

use of org.apache.camel.component.schematron.exception.SchematronConfigException in project camel by apache.

the class TemplatesFactory method getTemplates.

/**
     * Generate the schematron template for given rule.
     *
     * @param rules the schematron rules
     * @param fac   the transformer factory.
     * @return schematron template.
     */
public Templates getTemplates(final InputStream rules, final TransformerFactory fac) {
    Node node = null;
    Source source = new StreamSource(rules);
    try {
        for (String template : PIPELINE) {
            String path = Constants.SCHEMATRON_TEMPLATES_ROOT_DIR.concat("/").concat(template);
            InputStream xsl = this.getClass().getClassLoader().getResourceAsStream(path);
            Transformer t = fac.newTransformer(new StreamSource(xsl));
            DOMResult result = new DOMResult();
            t.transform(source, result);
            source = new DOMSource(node = result.getNode());
        }
        return fac.newTemplates(new DOMSource(node));
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new SchematronConfigException(e);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) DOMResult(javax.xml.transform.dom.DOMResult) InputStream(java.io.InputStream) Node(org.w3c.dom.Node) StreamSource(javax.xml.transform.stream.StreamSource) SchematronConfigException(org.apache.camel.component.schematron.exception.SchematronConfigException) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) SchematronConfigException(org.apache.camel.component.schematron.exception.SchematronConfigException)

Aggregations

InputStream (java.io.InputStream)2 SchematronConfigException (org.apache.camel.component.schematron.exception.SchematronConfigException)2 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 Source (javax.xml.transform.Source)1 Transformer (javax.xml.transform.Transformer)1 DOMResult (javax.xml.transform.dom.DOMResult)1 DOMSource (javax.xml.transform.dom.DOMSource)1 StreamSource (javax.xml.transform.stream.StreamSource)1 Node (org.w3c.dom.Node)1