You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2014/09/05 08:42:26 UTC

[2/6] git commit: Implement GPathConverter methods

Implement GPathConverter methods


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/c9e26a52
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/c9e26a52
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/c9e26a52

Branch: refs/heads/master
Commit: c9e26a52222a1e01b324ade199e731d7a96d9811
Parents: bc7a50c
Author: Dominik Adam Przybysz <al...@gmail.com>
Authored: Sun Aug 17 22:33:59 2014 +0200
Committer: Dominik Adam Przybysz <al...@gmail.com>
Committed: Sun Aug 17 22:33:59 2014 +0200

----------------------------------------------------------------------
 .../groovy/converter/GPathResultConverter.java  | 33 ++++++++++++++++++++
 .../converter/GPathResultConverterTest.groovy   |  9 ++++--
 2 files changed, 40 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/c9e26a52/components/camel-groovy/src/main/java/org/apache/camel/groovy/converter/GPathResultConverter.java
----------------------------------------------------------------------
diff --git a/components/camel-groovy/src/main/java/org/apache/camel/groovy/converter/GPathResultConverter.java b/components/camel-groovy/src/main/java/org/apache/camel/groovy/converter/GPathResultConverter.java
index 12802e3..e5008a8 100644
--- a/components/camel-groovy/src/main/java/org/apache/camel/groovy/converter/GPathResultConverter.java
+++ b/components/camel-groovy/src/main/java/org/apache/camel/groovy/converter/GPathResultConverter.java
@@ -1,8 +1,41 @@
 package org.apache.camel.groovy.converter;
 
+import groovy.util.XmlSlurper;
+import groovy.util.slurpersupport.GPathResult;
+import groovy.xml.StreamingMarkupBuilder;
 import org.apache.camel.Converter;
+import org.apache.camel.StringSource;
+import org.w3c.dom.Node;
+import org.xml.sax.SAXException;
+
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+import java.io.IOException;
+import java.io.StringWriter;
 
 @Converter
 public class GPathResultConverter {
 
+    @Converter
+    public static GPathResult fromString(String input) throws ParserConfigurationException, SAXException, IOException {
+        return new XmlSlurper().parseText(input);
+    }
+
+    @Converter
+    public static GPathResult fromStringSource(StringSource input) throws IOException, SAXException, ParserConfigurationException {
+        return fromString(input.getText());
+    }
+
+    @Converter
+    public static GPathResult fromNode(Node input) throws IOException, SAXException, ParserConfigurationException, TransformerException {
+        StringWriter writer = new StringWriter();
+        Transformer t = TransformerFactory.newInstance().newTransformer();
+        t.transform(new DOMSource(input), new StreamResult(writer));
+        return fromString(writer.toString());
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/c9e26a52/components/camel-groovy/src/test/groovy/org/apache/camel/groovy/converter/GPathResultConverterTest.groovy
----------------------------------------------------------------------
diff --git a/components/camel-groovy/src/test/groovy/org/apache/camel/groovy/converter/GPathResultConverterTest.groovy b/components/camel-groovy/src/test/groovy/org/apache/camel/groovy/converter/GPathResultConverterTest.groovy
index 22daa5e..c17feb8 100644
--- a/components/camel-groovy/src/test/groovy/org/apache/camel/groovy/converter/GPathResultConverterTest.groovy
+++ b/components/camel-groovy/src/test/groovy/org/apache/camel/groovy/converter/GPathResultConverterTest.groovy
@@ -8,8 +8,11 @@ import org.apache.camel.impl.DefaultCamelContext
 import org.apache.camel.impl.DefaultExchange
 import org.junit.Test
 import org.w3c.dom.Node
+import javax.xml.parsers.DocumentBuilderFactory
+import org.xml.sax.InputSource
 
 import static org.junit.Assert.assertEquals
+import static org.junit.Assert.assertNotNull
 
 public class GPathResultConverterTest {
     String xml = "<test><elem1>This is test</elem1></test>"
@@ -34,7 +37,8 @@ public class GPathResultConverterTest {
 
     @Test
     void "should convert node to GPathResult"() {
-        Node node = new XmlParser().parseText(xml)
+        Node node = DocumentBuilderFactory.newInstance().newDocumentBuilder()
+					.parse(new InputSource(new StringReader(xml)))
         Exchange exchange = new DefaultExchange(context)
         exchange.in.setBody(node, Node)
         GPathResult result = exchange.in.getBody(GPathResult)
@@ -42,7 +46,8 @@ public class GPathResultConverterTest {
     }
 
     private void checkGPathResult(GPathResult gPathResult) {
+		    assertNotNull(gPathResult)
         assertEquals(gPathResult.name(), "test")
         assertEquals(gPathResult.elem1.text(), "This is test")
     }
-}
\ No newline at end of file
+}