INT-1155 now relying on the XPathEvaluationType enum from the spring-xml module instead of defining our own
This commit is contained in:
@@ -25,6 +25,7 @@ import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.transformer.AbstractTransformer;
|
||||
import org.springframework.integration.xml.DefaultXmlPayloadConverter;
|
||||
import org.springframework.integration.xml.XmlPayloadConverter;
|
||||
import org.springframework.integration.xml.xpath.XPathEvaluationType;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.xml.xpath.NodeMapper;
|
||||
import org.springframework.xml.xpath.XPathExpression;
|
||||
@@ -39,15 +40,11 @@ import org.springframework.xml.xpath.XPathExpressionFactory;
|
||||
*/
|
||||
public class XPathTransformer extends AbstractTransformer {
|
||||
|
||||
public static enum ResultType {
|
||||
BOOLEAN, NODE, NODELIST, NUMBER, OBJECT, STRING
|
||||
}
|
||||
|
||||
private final XPathExpression xpathExpression;
|
||||
|
||||
private volatile XmlPayloadConverter converter = new DefaultXmlPayloadConverter();
|
||||
|
||||
private volatile ResultType expectedResultType = ResultType.STRING;
|
||||
private volatile XPathEvaluationType evaluationType = XPathEvaluationType.STRING_RESULT;
|
||||
|
||||
private volatile NodeMapper nodeMapper;
|
||||
|
||||
@@ -68,19 +65,19 @@ public class XPathTransformer extends AbstractTransformer {
|
||||
|
||||
|
||||
/**
|
||||
* Specify the expected {@link ResultType}. The default is {@link ResultType#STRING}.
|
||||
* Specify the expected {@link XPathEvaluationType}. The default is {@link XPathEvaluationType#STRING_RESULT}.
|
||||
*/
|
||||
public void setExpectedResultType(ResultType expectedResultType) {
|
||||
this.expectedResultType = expectedResultType;
|
||||
public void setEvaluationType(XPathEvaluationType evaluationType) {
|
||||
this.evaluationType = evaluationType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a {@link NodeMapper} to use for generating the result object.
|
||||
* This will also set the expectedResultType to <code>OBJECT</code> since
|
||||
* This will also set the evaluationType to <code>null</code> since
|
||||
* the actual type determination is a responsibility of the NodeMapper.
|
||||
*/
|
||||
public void setNodeMapper(NodeMapper nodeMapper) {
|
||||
this.expectedResultType = ResultType.OBJECT;
|
||||
this.evaluationType = null;
|
||||
this.nodeMapper = nodeMapper;
|
||||
}
|
||||
|
||||
@@ -96,26 +93,12 @@ public class XPathTransformer extends AbstractTransformer {
|
||||
protected Object doTransform(Message<?> message) throws Exception {
|
||||
Node node = this.converter.convertToNode(message.getPayload());
|
||||
Object result = null;
|
||||
switch (this.expectedResultType) {
|
||||
case BOOLEAN:
|
||||
result = this.xpathExpression.evaluateAsBoolean(node);
|
||||
break;
|
||||
case NODE:
|
||||
result = this.xpathExpression.evaluateAsNode(node);
|
||||
break;
|
||||
case NODELIST:
|
||||
result = this.xpathExpression.evaluateAsNodeList(node);
|
||||
break;
|
||||
case NUMBER:
|
||||
result = this.xpathExpression.evaluateAsNumber(node);
|
||||
break;
|
||||
case STRING:
|
||||
result = this.xpathExpression.evaluateAsString(node);
|
||||
break;
|
||||
default: // works for OBJECT
|
||||
Assert.notNull(this.nodeMapper, "NodeMapper required if expectedResultType is OBJECT");
|
||||
result = this.xpathExpression.evaluateAsObject(node, this.nodeMapper);
|
||||
break;
|
||||
if (evaluationType == null) {
|
||||
Assert.notNull(this.nodeMapper, "NodeMapper required if evaluationType is null.");
|
||||
result = this.xpathExpression.evaluateAsObject(node, this.nodeMapper);
|
||||
}
|
||||
else {
|
||||
result = this.evaluationType.evaluateXPath(this.xpathExpression, node);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ import org.w3c.dom.Node;
|
||||
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
import org.springframework.integration.xml.transformer.XPathTransformer.ResultType;
|
||||
import org.springframework.integration.xml.xpath.XPathEvaluationType;
|
||||
import org.springframework.xml.xpath.NodeMapper;
|
||||
|
||||
/**
|
||||
@@ -58,7 +58,7 @@ public class XPathTransformerTests {
|
||||
@Test
|
||||
public void numberResult() throws Exception {
|
||||
XPathTransformer transformer = new XPathTransformer("/parent/child/@age");
|
||||
transformer.setExpectedResultType(ResultType.NUMBER);
|
||||
transformer.setEvaluationType(XPathEvaluationType.NUMBER_RESULT);
|
||||
Object result = transformer.doTransform(message);
|
||||
assertEquals(new Double(42), result);
|
||||
}
|
||||
@@ -66,7 +66,7 @@ public class XPathTransformerTests {
|
||||
@Test
|
||||
public void booleanResult() throws Exception {
|
||||
XPathTransformer transformer = new XPathTransformer("/parent/child/@married");
|
||||
transformer.setExpectedResultType(ResultType.BOOLEAN);
|
||||
transformer.setEvaluationType(XPathEvaluationType.BOOLEAN_RESULT);
|
||||
Object result = transformer.doTransform(message);
|
||||
assertEquals(Boolean.TRUE, result);
|
||||
}
|
||||
@@ -74,7 +74,7 @@ public class XPathTransformerTests {
|
||||
@Test
|
||||
public void nodeResult() throws Exception {
|
||||
XPathTransformer transformer = new XPathTransformer("/parent/child");
|
||||
transformer.setExpectedResultType(ResultType.NODE);
|
||||
transformer.setEvaluationType(XPathEvaluationType.NODE_RESULT);
|
||||
Object result = transformer.doTransform(message);
|
||||
assertTrue(result instanceof Node);
|
||||
Node node = (Node) result;
|
||||
@@ -88,7 +88,7 @@ public class XPathTransformerTests {
|
||||
@SuppressWarnings("unchecked")
|
||||
public void nodeListResult() throws Exception {
|
||||
XPathTransformer transformer = new XPathTransformer("/parent/child");
|
||||
transformer.setExpectedResultType(ResultType.NODELIST);
|
||||
transformer.setEvaluationType(XPathEvaluationType.NODE_LIST_RESULT);
|
||||
Message<?> message = MessageBuilder.withPayload(
|
||||
"<parent><child name='foo'/><child name='bar'/></parent>").build();
|
||||
Object result = transformer.doTransform(message);
|
||||
|
||||
Reference in New Issue
Block a user