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

@@ -216,28 +216,115 @@
 e.camel.
-StringSource
+Exchange;%0Aimport org.apache.camel.StringSource;%0Aimport org.apache.camel.converter.jaxp.XmlConverter
 ;%0Aimport
@@ -825,16 +825,83 @@
 rter %7B%0A%0A
+    private final XmlConverter xmlConverter = new XmlConverter();%0A%0A
     @Con
@@ -910,39 +910,32 @@
 rter%0A    public 
-static 
 GPathResult from
@@ -1095,39 +1095,32 @@
 rter%0A    public 
-static 
 GPathResult from
@@ -1298,15 +1298,8 @@
 lic 
-static 
 GPat
@@ -1321,24 +1321,43 @@
 e(Node input
+, Exchange exchange
 ) throws IOE
@@ -1445,236 +1445,63 @@
     
-StringWriter writer = new StringWriter();%0A        Transformer t = TransformerFactory.newInstance().newTransformer();%0A        t.transform(new DOMSource
+return fromString(xmlConverter.toString
 (input
-)
 , 
-new StreamResult(writer));%0A        return fromString(writer.toString(
+exchange
 ));%0A

Rewrite converting Node to GPathResult