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 new file mode 100644 index 0000000000000..561fc162a60b0 --- /dev/null +++ b/components/camel-groovy/src/main/java/org/apache/camel/groovy/converter/GPathResultConverter.java @@ -0,0 +1,35 @@ +package org.apache.camel.groovy.converter; + +import groovy.util.XmlSlurper; +import groovy.util.slurpersupport.GPathResult; +import org.apache.camel.Converter; +import org.apache.camel.Exchange; +import org.apache.camel.StringSource; +import org.apache.camel.converter.jaxp.XmlConverter; +import org.w3c.dom.Node; +import org.xml.sax.SAXException; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; +import java.io.IOException; + +@Converter +public class GPathResultConverter { + + private final XmlConverter xmlConverter = new XmlConverter(); + + @Converter + public GPathResult fromString(String input) throws ParserConfigurationException, SAXException, IOException { + return new XmlSlurper().parseText(input); + } + + @Converter + public GPathResult fromStringSource(StringSource input) throws IOException, SAXException, ParserConfigurationException { + return fromString(input.getText()); + } + + @Converter + public GPathResult fromNode(Node input, Exchange exchange) throws IOException, SAXException, ParserConfigurationException, TransformerException { + return fromString(xmlConverter.toString(input, exchange)); + } +} diff --git a/components/camel-groovy/src/main/resources/META-INF/services/org/apache/camel/TypeConverter b/components/camel-groovy/src/main/resources/META-INF/services/org/apache/camel/TypeConverter index 62a0595dd9143..8a0e315228895 100644 --- a/components/camel-groovy/src/main/resources/META-INF/services/org/apache/camel/TypeConverter +++ b/components/camel-groovy/src/main/resources/META-INF/services/org/apache/camel/TypeConverter @@ -14,4 +14,5 @@ # See the License for the specific language governing permissions and # limitations under the License. # -org.apache.camel.groovy.converter.TypeConverter \ No newline at end of file +org.apache.camel.groovy.converter.TypeConverter +org.apache.camel.groovy.converter.GPathResultConverter \ No newline at end of file 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 new file mode 100644 index 0000000000000..517cfc001a9af --- /dev/null +++ b/components/camel-groovy/src/test/groovy/org/apache/camel/groovy/converter/GPathResultConverterTest.groovy @@ -0,0 +1,54 @@ +package org.apache.camel.groovy.converter + +import groovy.util.slurpersupport.GPathResult +import org.apache.camel.CamelContext +import org.apache.camel.Exchange +import org.apache.camel.StringSource +import org.apache.camel.impl.DefaultCamelContext +import org.apache.camel.impl.DefaultExchange +import org.junit.Test +import org.w3c.dom.Node +import org.xml.sax.InputSource + +import javax.xml.parsers.DocumentBuilderFactory + +import static org.junit.Assert.assertEquals +import static org.junit.Assert.assertNotNull + +public class GPathResultConverterTest { + String xml = "This is test" + CamelContext context = new DefaultCamelContext() + + @Test + void "should convert string to GPathResult"() { + Exchange exchange = new DefaultExchange(context) + exchange.in.setBody(xml, String) + GPathResult result = exchange.in.getBody(GPathResult) + checkGPathResult(result) + } + + @Test + void "should convert string source to GPathResult"() { + StringSource input = new StringSource(xml) + Exchange exchange = new DefaultExchange(context) + exchange.in.setBody(input, StringSource) + GPathResult result = exchange.in.getBody(GPathResult) + checkGPathResult(result) + } + + @Test + void "should convert node to GPathResult"() { + 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) + checkGPathResult(result) + } + + private void checkGPathResult(GPathResult gPathResult) { + assertNotNull(gPathResult) + assertEquals(gPathResult.name(), "test") + assertEquals(gPathResult.elem1.text(), "This is test") + } +}