Skip to content
Merged
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
@@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
org.apache.camel.groovy.converter.TypeConverter
org.apache.camel.groovy.converter.TypeConverter
org.apache.camel.groovy.converter.GPathResultConverter
Original file line number Diff line number Diff line change
@@ -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 = "<test><elem1>This is test</elem1></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")
}
}