You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2018/12/03 14:46:15 UTC

[camel] branch sandbox/camel-3.x updated (d0f6a19 -> 9be0d0f)

This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a change to branch sandbox/camel-3.x
in repository https://gitbox.apache.org/repos/asf/camel.git.


    from d0f6a19  Regen
     new 6858f96  Schematron component supports class `javax.xml.transform.Source`
     new 9be0d0f  Fix backport of Schematron fix to 3.x

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../component/schematron/SchematronProducer.java   | 27 ++++++++++----
 .../schematron/processor/SchematronProcessor.java  | 12 +++++--
 .../schematron/SchematronProducerTest.java         | 41 ++++++++++++++++++++++
 3 files changed, 72 insertions(+), 8 deletions(-)


[camel] 02/02: Fix backport of Schematron fix to 3.x

Posted by ac...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a commit to branch sandbox/camel-3.x
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 9be0d0f5c99e2794eda0ae2189e4e3e6b7c7b132
Author: Andrea Cosentino <an...@gmail.com>
AuthorDate: Mon Dec 3 15:39:02 2018 +0100

    Fix backport of Schematron fix to 3.x
---
 .../java/org/apache/camel/component/schematron/SchematronProducer.java  | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/components/camel-schematron/src/main/java/org/apache/camel/component/schematron/SchematronProducer.java b/components/camel-schematron/src/main/java/org/apache/camel/component/schematron/SchematronProducer.java
index 70d73df..0b0e977 100644
--- a/components/camel-schematron/src/main/java/org/apache/camel/component/schematron/SchematronProducer.java
+++ b/components/camel-schematron/src/main/java/org/apache/camel/component/schematron/SchematronProducer.java
@@ -52,7 +52,7 @@ public class SchematronProducer extends DefaultProducer {
      * @throws Exception
      */
     public void process(Exchange exchange) throws Exception {
-        final SchematronProcessor schematronProcessor = SchematronProcessorFactory.newScehamtronEngine(endpoint.getRules());
+        final SchematronProcessor schematronProcessor = SchematronProcessorFactory.newSchematronEngine(endpoint.getRules());
         final Object payload = exchange.getIn().getBody();
         final String report;
 


[camel] 01/02: Schematron component supports class `javax.xml.transform.Source`

Posted by ac...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a commit to branch sandbox/camel-3.x
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 6858f9681a0f19c9e697773ea76bb3d561539e0c
Author: Vilmos Nagy <vi...@outlook.com>
AuthorDate: Wed Nov 14 10:51:09 2018 +0100

    Schematron component supports class `javax.xml.transform.Source`
    
    Conflicts:
    	components/camel-schematron/src/main/java/org/apache/camel/component/schematron/SchematronProducer.java
    	components/camel-schematron/src/test/java/org/apache/camel/component/schematron/SchematronProducerTest.java
---
 .../component/schematron/SchematronProducer.java   | 27 ++++++++++----
 .../schematron/processor/SchematronProcessor.java  | 12 +++++--
 .../schematron/SchematronProducerTest.java         | 41 ++++++++++++++++++++++
 3 files changed, 72 insertions(+), 8 deletions(-)

diff --git a/components/camel-schematron/src/main/java/org/apache/camel/component/schematron/SchematronProducer.java b/components/camel-schematron/src/main/java/org/apache/camel/component/schematron/SchematronProducer.java
index 9ade643..70d73df 100644
--- a/components/camel-schematron/src/main/java/org/apache/camel/component/schematron/SchematronProducer.java
+++ b/components/camel-schematron/src/main/java/org/apache/camel/component/schematron/SchematronProducer.java
@@ -16,17 +16,19 @@
  */
 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.SchematronProcessor;
 import org.apache.camel.component.schematron.processor.SchematronProcessorFactory;
 import org.apache.camel.support.DefaultProducer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import javax.xml.transform.Source;
+import java.util.HashMap;
+import java.util.Map;
+
 /**
  * The Schematron producer.
  */
@@ -50,9 +52,22 @@ public class SchematronProducer extends DefaultProducer {
      * @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.newSchematronEngine(endpoint.getRules()).validate(payload);
+        final SchematronProcessor schematronProcessor = SchematronProcessorFactory.newScehamtronEngine(endpoint.getRules());
+        final Object payload = exchange.getIn().getBody();
+        final String report;
+
+        if (payload instanceof Source) {
+            logger.debug("Applying schematron validation on payload: {}", payload);
+            report = schematronProcessor.validate((Source) payload);
+        } else if (payload instanceof String) {
+            logger.debug("Applying schematron validation on payload: {}", payload);
+            report = schematronProcessor.validate((String) payload);
+        } else {
+            String stringPayload = exchange.getIn().getBody(String.class);
+            logger.debug("Applying schematron validation on payload: {}", stringPayload);
+            report = schematronProcessor.validate(stringPayload);
+        }
+
         logger.debug("Schematron validation report \n {}", report);
         String status = getValidationStatus(report);
         logger.info("Schematron validation status : {}", status);
diff --git a/components/camel-schematron/src/main/java/org/apache/camel/component/schematron/processor/SchematronProcessor.java b/components/camel-schematron/src/main/java/org/apache/camel/component/schematron/processor/SchematronProcessor.java
index 574e26c..af1e7b6 100644
--- a/components/camel-schematron/src/main/java/org/apache/camel/component/schematron/processor/SchematronProcessor.java
+++ b/components/camel-schematron/src/main/java/org/apache/camel/component/schematron/processor/SchematronProcessor.java
@@ -52,7 +52,6 @@ public class SchematronProcessor {
      * @param templates
      */
     public SchematronProcessor(XMLReader reader, Templates templates) {
-
         this.reader = reader;
         this.templates = templates;
     }
@@ -64,9 +63,18 @@ public class SchematronProcessor {
      * @return
      */
     public String validate(final String xml) {
+        final Source source = new SAXSource(reader, new InputSource(IOUtils.toInputStream(xml)));
+        return validate(source);
+    }
 
+    /**
+     * Validates the given XML for given Rules.
+     *
+     * @param source
+     * @return
+     */
+    public String validate(Source source) {
         try {
-            final Source source = new SAXSource(reader, new InputSource(IOUtils.toInputStream(xml)));
             final StringWriter writer = new StringWriter();
             templates.newTransformer().transform(source, new StreamResult(writer));
             return writer.toString();
diff --git a/components/camel-schematron/src/test/java/org/apache/camel/component/schematron/SchematronProducerTest.java b/components/camel-schematron/src/test/java/org/apache/camel/component/schematron/SchematronProducerTest.java
index ef923ba..322fd38 100644
--- a/components/camel-schematron/src/test/java/org/apache/camel/component/schematron/SchematronProducerTest.java
+++ b/components/camel-schematron/src/test/java/org/apache/camel/component/schematron/SchematronProducerTest.java
@@ -16,8 +16,12 @@
  */
 package org.apache.camel.component.schematron;
 
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
 import javax.xml.transform.Templates;
 import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.sax.SAXSource;
 
 import net.sf.saxon.TransformerFactoryImpl;
 import org.apache.camel.Exchange;
@@ -27,8 +31,12 @@ import org.apache.camel.component.schematron.processor.ClassPathURIResolver;
 import org.apache.camel.component.schematron.processor.TemplatesFactory;
 import org.apache.camel.support.DefaultExchange;
 import org.apache.camel.test.junit4.CamelTestSupport;
+import org.apache.commons.io.IOUtils;
 import org.junit.BeforeClass;
 import org.junit.Test;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.XMLReader;
 
 /**
  * Schematron Producer Unit Test.
@@ -74,4 +82,37 @@ public class SchematronProducerTest extends CamelTestSupport {
 
     }
 
+    @Test
+    public void testProcessValidXMLAsSource() throws Exception {
+        Exchange exc = new DefaultExchange(context, ExchangePattern.InOut);
+        exc.getIn().setBody(new SAXSource(getXMLReader(), new InputSource(ClassLoader.getSystemResourceAsStream("xml/article-1.xml"))));
+
+        // process xml payload
+        producer.process(exc);
+
+        // assert
+        assertTrue(exc.getOut().getHeader(Constants.VALIDATION_STATUS).equals(Constants.SUCCESS));
+    }
+
+    @Test
+    public void testProcessInValidXMLAsSource() throws Exception {
+        Exchange exc = new DefaultExchange(context, ExchangePattern.InOut);
+        exc.getIn().setBody(new SAXSource(getXMLReader(), new InputSource(ClassLoader.getSystemResourceAsStream("xml/article-2.xml"))));
+
+        // process xml payload
+        producer.process(exc);
+
+        // assert
+        assertTrue(exc.getOut().getHeader(Constants.VALIDATION_STATUS).equals(Constants.FAILED));
+
+    }
+
+    private static XMLReader getXMLReader() throws ParserConfigurationException, SAXException {
+        final SAXParserFactory fac = SAXParserFactory.newInstance();
+        fac.setValidating(false);
+        final SAXParser parser = fac.newSAXParser();
+        XMLReader reader = parser.getXMLReader();
+        return reader;
+    }
+
 }