Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected void doStart() throws Exception {

if (fopFactory == null) {
fopFactory = FopFactory.newInstance();
}
}

if (userConfigURL != null) {
InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext().getClassResolver(), userConfigURL);
Expand Down
29 changes: 29 additions & 0 deletions components/camel-schematron/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Schematron Component
====================

Schematron is an XML-based language for validating XML instance documents. Schematron is used to make assertions about data in an XML document. Schematron is used to express operational and business rules.
Schematron is an ISO standard.
Use Schematron to verify data interdependencies (co-constraints), check data cardinality, and perform algorithmic checks. A co-constraint is a dependency between data within an XML document or across XML documents. Cardinality refers to the presence or absence of data. An algorithmic check determines data validity by performing an algorithm on the data.

Schematron engine
======================

Schemaron engine is an XSLT based implementation. XLST source code can be found here: https://code.google.com/p/schematron/


Schematron Tutorials
======================

http://www.schematron.com/spec.html

http://www.xml.com/pub/a/2003/11/12/schematron.html

http://www.data2type.de/en/xml-xslt-xslfo/schematron/

http://www.mulberrytech.com/papers/schematron-Philly.pdf

http://www.ldodds.com/papers/schematron_xsltuk.html#c35e2592b6b7

To build this project use

mvn clean install
64 changes: 64 additions & 0 deletions components/camel-schematron/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<parent>
<groupId>org.apache.camel</groupId>
<artifactId>components</artifactId>
<version>2.14-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.camel</groupId>
<artifactId>camel-schematron</artifactId>
<packaging>bundle</packaging>
<name>Camel :: SCHEMATRON</name>
<properties>
<camel.osgi.export.pkg>org.apache.camel.component.schematron.*</camel.osgi.export.pkg>
<camel.osgi.export.service>org.apache.camel.spi.ComponentResolver;component=schematron</camel.osgi.export.service>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
</dependency>
<dependency>
<groupId>net.sf.saxon</groupId>
<artifactId>Saxon-HE</artifactId>
<version>${saxon-version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io-version}</version>
</dependency>

<!-- testing -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>xmlunit</groupId>
<artifactId>xmlunit</artifactId>
<version>${xmlunit-version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.component.schematron;

import java.util.Map;
import org.apache.camel.Endpoint;
import org.apache.camel.impl.DefaultComponent;

/**
* Represents the component that manages {@link SchematronEndpoint}.
*/
public class SchematronComponent extends DefaultComponent {


/**
* Creates the Schematron Endpoint.
*
* @param uri
* @param remaining
* @param parameters
* @return
* @throws Exception
*/
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
Endpoint endpoint = new SchematronEndpoint(uri, remaining, this);
setProperties(endpoint, parameters);
return endpoint;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.component.schematron;

import java.io.File;
import java.io.InputStream;

import javax.xml.transform.Templates;
import org.apache.camel.Consumer;
import org.apache.camel.Processor;
import org.apache.camel.Producer;
import org.apache.camel.component.schematron.processor.TemplatesFactory;
import org.apache.camel.component.schematron.exception.SchematronConfigException;
import org.apache.camel.impl.DefaultEndpoint;
import org.apache.camel.util.ResourceHelper;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;




/**
* Schematron Endpoint.
*/
public class SchematronEndpoint extends DefaultEndpoint {

private Logger logger = LoggerFactory.getLogger(SchematronEndpoint.class);
private String remaining;
private boolean abort;
private Templates rules;


public SchematronEndpoint() {
}

public SchematronEndpoint(String uri, String remaining, SchematronComponent component) {
super(uri, component);
this.remaining = remaining;
}

public SchematronEndpoint(String endpointUri) {
super(endpointUri);
}

public Producer createProducer() throws Exception {
return new SchematronProducer(this);
}

public Consumer createConsumer(Processor processor) throws Exception {
throw new UnsupportedOperationException("Consumer is not implemented for this component");
}

public boolean isSingleton() {
return true;
}

public void setAbort(boolean abort) {
this.abort = abort;
}

public boolean isAbort() {
return abort;
}

public Templates getRules() {
return rules;
}

public void setRules(Templates rules) {
this.rules = rules;
}

@Override
protected void doStart() throws Exception {
super.doStart();

if (rules == null) {
try {
// Attempt to read the schematron rules from the class path first.
logger.info("Reading schematron rules from class path {}", remaining);
InputStream schRules = ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext().getClassResolver(), remaining);
rules = TemplatesFactory.newInstance().newTemplates(schRules);
} catch (Exception e) {
// Attempts from the file system.
logger.info("Schamatron rules not found in class path, attempting file system {}", remaining);
InputStream schRules = FileUtils.openInputStream(new File(remaining));
rules = TemplatesFactory.newInstance().newTemplates(schRules);
}

// rules not found in class path nor in file system.
if (rules == null) {
logger.error("Schematron rules not found {}", remaining);
throw new SchematronConfigException("Failed to load rules: " + remaining);
}
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.component.schematron;

import java.util.HashMap;
import java.util.Map;

import org.apache.camel.Exchange;
import org.apache.camel.component.schematron.constant.Constants;
import org.apache.camel.component.schematron.exception.SchematronValidationException;
import org.apache.camel.component.schematron.processor.SchematronProcessorFactory;
import org.apache.camel.impl.DefaultProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The Schematron producer.
*/
public class SchematronProducer extends DefaultProducer {
private Logger logger = LoggerFactory.getLogger(SchematronProducer.class);
private SchematronEndpoint endpoint;


/**
* @param endpoint the schematron endpoint.
*/
public SchematronProducer(final SchematronEndpoint endpoint) {
super(endpoint);
this.endpoint = endpoint;
}

/**
* Processes the payload. Validates the XML using the SchematronEngine
*
* @param exchange
* @throws Exception
*/
public void process(Exchange exchange) throws Exception {

String payload = exchange.getIn().getBody(String.class);
logger.debug("Applying schematron validation on payload: {}", payload);
String report = SchematronProcessorFactory.newScehamtronEngine(endpoint.getRules()).validate(payload);
logger.debug("Schematron validation report \n {}", report);
String status = getValidationStatus(report);
logger.info("Schematron validation status : {}", status);
setValidationReport(exchange, report, status);
}

/**
* Sets validation report and status
*
* @param exchange
* @param report
* @param status
*/
private void setValidationReport(Exchange exchange, String report, String status) {
// if exchange pattern is In and Out set details on the Out message.
Map<String, Object> headers = new HashMap<String, Object>();
headers.put(Constants.VALIDATION_STATUS, status);
headers.put(Constants.VALIDATION_REPORT, report);
exchange.getOut().setHeader(Constants.VALIDATION_REPORT, report);
if (exchange.getPattern().isOutCapable()) {
exchange.getOut().setHeaders(exchange.getIn().getHeaders());
exchange.getOut().getHeaders().putAll(headers);
} else {
exchange.getIn().getHeaders().putAll(headers);
}
}

/**
* Get validation status, SUCCESS or FAILURE
*
* @param report
* @return
*/
private String getValidationStatus(final String report) {

String status = report.contains(Constants.FAILED_ASSERT) ? Constants.FAILED : Constants.SUCCESS;
if (this.endpoint.isAbort() && Constants.FAILED.equals(status)) {
throw new SchematronValidationException("Schematron validation failure \n" + report);
}
return status;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.component.schematron.constant;

/**
* Utility class defining all constants needed for the schematron component.
* <p/>
*/
public final class Constants {

public static final String VALIDATION_STATUS = "CamelSchematronValidationStatus";
public static final String VALIDATION_REPORT = "CamelSchematronValidationReport";
public static final String HTTP_PURL_OCLC_ORG_DSDL_SVRL = "http://purl.oclc.org/dsdl/svrl";
public static final String FAILED_ASSERT = "failed-assert";
public static final String FAILED = "FAILED";
public static final String SUCCESS = "SUCCESS";
public static final String SCHEMATRON_TEMPLATES_ROOT_DIR = "iso-schematron-xslt2";

private Constants() {
throw new IllegalStateException("Utility class should not be instantiated");
}

}
Loading