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);
}
}
}
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);
}
}
Aggregations