formatting and changed 'skipSettingNullResults' to simply 'skipNullResults'
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -20,6 +20,8 @@ import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
import org.springframework.integration.transformer.Transformer;
|
||||
@@ -28,81 +30,79 @@ import org.springframework.integration.xml.XmlPayloadConverter;
|
||||
import org.springframework.integration.xml.xpath.XPathEvaluationType;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.xml.xpath.XPathExpression;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
|
||||
/**
|
||||
* Transformer implementation which evaluates XPath expressions against the message payload and inserts the
|
||||
* result of the evaluation into the messsage header
|
||||
*
|
||||
* Transformer implementation that evaluates XPath expressions against the
|
||||
* message payload and inserts the result of the evaluation into a messsage
|
||||
* header. The header names will match the keys in the map of expressions.
|
||||
*
|
||||
* @author Jonas Partner
|
||||
* @since 2.0
|
||||
*/
|
||||
public class XPathHeaderEnricher implements Transformer {
|
||||
|
||||
private final Map<String, XPathExpression> expressionMap;
|
||||
private final Map<String, XPathExpression> expressionMap;
|
||||
|
||||
private Map<String, XPathEvaluationType> evaluationTypes;
|
||||
private Map<String, XPathEvaluationType> evaluationTypes;
|
||||
|
||||
private XPathEvaluationType defaultEvaluationType = XPathEvaluationType.STRING_RESULT;
|
||||
private XPathEvaluationType defaultEvaluationType = XPathEvaluationType.STRING_RESULT;
|
||||
|
||||
private volatile boolean skipSettingNullResults = true;
|
||||
private volatile boolean skipNullResults = true;
|
||||
|
||||
private volatile XmlPayloadConverter converter = new DefaultXmlPayloadConverter();
|
||||
private volatile XmlPayloadConverter converter = new DefaultXmlPayloadConverter();
|
||||
|
||||
|
||||
/**
|
||||
* Create an instance of XPathHeaderEnricher using a map of the header name to the XPathExpression to evaluate
|
||||
* All XpathExpressions are currently evaluated as returning Strings
|
||||
*
|
||||
* @param expressionMap
|
||||
*/
|
||||
public XPathHeaderEnricher(Map<String, XPathExpression> expressionMap) {
|
||||
this.expressionMap = Collections.unmodifiableMap(expressionMap);
|
||||
}
|
||||
/**
|
||||
* Create an instance of XPathHeaderEnricher using a map with header names as keys
|
||||
* and XPathExpressions to evaluate as the values. All XPathExpressions are evaluated
|
||||
* as String results by default.
|
||||
*
|
||||
* @param expressionMap
|
||||
*/
|
||||
public XPathHeaderEnricher(Map<String, XPathExpression> expressionMap) {
|
||||
this.expressionMap = Collections.unmodifiableMap(expressionMap);
|
||||
}
|
||||
|
||||
public void setConverter(XmlPayloadConverter converter) {
|
||||
this.converter = converter;
|
||||
}
|
||||
|
||||
public void setSkipSettingNullResults(boolean skipSettingNullResults) {
|
||||
this.skipSettingNullResults = skipSettingNullResults;
|
||||
}
|
||||
public void setConverter(XmlPayloadConverter converter) {
|
||||
this.converter = converter;
|
||||
}
|
||||
|
||||
public void setEvaluationTypes(Map<String, XPathEvaluationType> evaluationTypes) {
|
||||
this.evaluationTypes = evaluationTypes;
|
||||
}
|
||||
public void setSkipNullResults(boolean skipNullResults) {
|
||||
this.skipNullResults = skipNullResults;
|
||||
}
|
||||
|
||||
public void setDefaultEvaluationType(XPathEvaluationType defaultEvaluationType) {
|
||||
this.defaultEvaluationType = defaultEvaluationType;
|
||||
}
|
||||
public void setEvaluationTypes(Map<String, XPathEvaluationType> evaluationTypes) {
|
||||
this.evaluationTypes = evaluationTypes;
|
||||
}
|
||||
|
||||
public Message<?> transform(Message<?> message) {
|
||||
MessageBuilder<?> builder = MessageBuilder.fromMessage(message);
|
||||
Node node = this.converter.convertToNode(message.getPayload());
|
||||
public void setDefaultEvaluationType(XPathEvaluationType defaultEvaluationType) {
|
||||
this.defaultEvaluationType = defaultEvaluationType;
|
||||
}
|
||||
|
||||
Set<String> keys = this.expressionMap.keySet();
|
||||
for (String key : keys) {
|
||||
XPathExpression expression = this.expressionMap.get(key);
|
||||
XPathEvaluationType evalType = this.defaultEvaluationType;
|
||||
public final Message<?> transform(Message<?> message) {
|
||||
MessageBuilder<?> builder = MessageBuilder.fromMessage(message);
|
||||
Node node = this.converter.convertToNode(message.getPayload());
|
||||
Set<String> keys = this.expressionMap.keySet();
|
||||
for (String key : keys) {
|
||||
XPathExpression expression = this.expressionMap.get(key);
|
||||
XPathEvaluationType evalType = this.defaultEvaluationType;
|
||||
if (this.evaluationTypes != null && this.evaluationTypes.containsKey(key)) {
|
||||
evalType = this.evaluationTypes.get(key);
|
||||
}
|
||||
setHeader(node, key, expression, evalType, builder);
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
if (this.evaluationTypes != null && this.evaluationTypes.containsKey(key)) {
|
||||
evalType = this.evaluationTypes.get(key);
|
||||
}
|
||||
|
||||
setHeader(node, key, expression, evalType, builder);
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
protected void setHeader(Node node, String headerName, XPathExpression expression, XPathEvaluationType evaluationType, MessageBuilder<?> builder) {
|
||||
Object result = evaluationType.evaluateXPath(expression, node);
|
||||
|
||||
boolean nullOrEmptyString = (result == null ||
|
||||
(result instanceof String && !StringUtils.hasLength((String)result)));
|
||||
|
||||
if (!nullOrEmptyString || !this.skipSettingNullResults) {
|
||||
builder.setHeader(headerName, result);
|
||||
}
|
||||
}
|
||||
private void setHeader(Node node, String headerName, XPathExpression expression,
|
||||
XPathEvaluationType evaluationType, MessageBuilder<?> builder) {
|
||||
Object result = evaluationType.evaluateXPath(expression, node);
|
||||
boolean nullOrEmptyString = (result == null) ||
|
||||
(result instanceof String && !StringUtils.hasLength((String) result));
|
||||
if (!nullOrEmptyString || !this.skipNullResults) {
|
||||
builder.setHeader(headerName, result);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -23,6 +23,7 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.core.MessageHeaders;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
@@ -30,79 +31,63 @@ import org.springframework.integration.xml.xpath.XPathEvaluationType;
|
||||
import org.springframework.xml.xpath.XPathExpression;
|
||||
import org.springframework.xml.xpath.XPathExpressionFactory;
|
||||
|
||||
|
||||
/**
|
||||
* @author Jonas Partner
|
||||
* @since 2.0
|
||||
*/
|
||||
public class XPathHeaderEnricherTests {
|
||||
|
||||
|
||||
@Test
|
||||
public void simpleStringEvaluation() {
|
||||
Map<String, XPathExpression> expressionMap = new HashMap<String, XPathExpression>();
|
||||
expressionMap.put("one", XPathExpressionFactory.createXPathExpression("/root/elementOne"));
|
||||
expressionMap.put("two", XPathExpressionFactory.createXPathExpression("/root/elementTwo"));
|
||||
String docAsString = "<root><elementOne>1</elementOne><elementTwo>2</elementTwo></root>";
|
||||
XPathHeaderEnricher enricher = new XPathHeaderEnricher(expressionMap);
|
||||
Message<?> result = enricher.transform(MessageBuilder.withPayload(docAsString).build());
|
||||
MessageHeaders headers = result.getHeaders();
|
||||
assertEquals("Wrong value for element one expression", "1", headers.get("one"));
|
||||
assertEquals("Wrong value for element two expression", "2", headers.get("two"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleStringEvaluation(){
|
||||
Map<String, XPathExpression> expressionMap = new HashMap<String, XPathExpression>();
|
||||
expressionMap.put("one", XPathExpressionFactory.createXPathExpression("/root/elementOne"));
|
||||
expressionMap.put("two", XPathExpressionFactory.createXPathExpression("/root/elementTwo"));
|
||||
String docAsString = "<root><elementOne>1</elementOne><elementTwo>2</elementTwo></root>";
|
||||
@Test
|
||||
public void nullValuesSkippedByDefault() {
|
||||
Map<String, XPathExpression> expressionMap = new HashMap<String, XPathExpression>();
|
||||
expressionMap.put("two", XPathExpressionFactory.createXPathExpression("/root/elementTwo"));
|
||||
String docAsString = "<root><elementOne>1</elementOne></root>";
|
||||
XPathHeaderEnricher enricher = new XPathHeaderEnricher(expressionMap);
|
||||
Message<?> result = enricher.transform(MessageBuilder.withPayload(docAsString).build());
|
||||
MessageHeaders headers = result.getHeaders();
|
||||
assertNull("value set for two when result was null", headers.get("two"));
|
||||
}
|
||||
|
||||
XPathHeaderEnricher enricher = new XPathHeaderEnricher(expressionMap);
|
||||
Message<?> result = enricher.transform(MessageBuilder.withPayload(docAsString).build());
|
||||
|
||||
MessageHeaders headers = result.getHeaders();
|
||||
|
||||
assertEquals("Wrong value for element one expression", "1", headers.get("one"));
|
||||
assertEquals("Wrong value for element two expression", "2", headers.get("two"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDontSetNull(){
|
||||
Map<String, XPathExpression> expressionMap = new HashMap<String, XPathExpression>();
|
||||
expressionMap.put("two", XPathExpressionFactory.createXPathExpression("/root/elementTwo"));
|
||||
String docAsString = "<root><elementOne>1</elementOne></root>";
|
||||
|
||||
XPathHeaderEnricher enricher = new XPathHeaderEnricher(expressionMap);
|
||||
Message<?> result = enricher.transform(MessageBuilder.withPayload(docAsString).build());
|
||||
|
||||
MessageHeaders headers = result.getHeaders();
|
||||
assertNull("value set for two when result was null",headers.get("two"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetNull(){
|
||||
Map<String, XPathExpression> expressionMap = new HashMap<String, XPathExpression>();
|
||||
expressionMap.put("two", XPathExpressionFactory.createXPathExpression("/root/elementTwo"));
|
||||
String docAsString = "<root><elementOne>1</elementOne></root>";
|
||||
|
||||
XPathHeaderEnricher enricher = new XPathHeaderEnricher(expressionMap);
|
||||
enricher.setSkipSettingNullResults(false);
|
||||
Message<?> result = enricher.transform(MessageBuilder.withPayload(docAsString).build());
|
||||
|
||||
MessageHeaders headers = result.getHeaders();
|
||||
assertEquals("no value set for two when result was null and skip null was false","" ,headers.get("two"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetNumberEvaluation(){
|
||||
Map<String, XPathExpression> expressionMap = new HashMap<String, XPathExpression>();
|
||||
expressionMap.put("one", XPathExpressionFactory.createXPathExpression("/root/elementOne"));
|
||||
expressionMap.put("two", XPathExpressionFactory.createXPathExpression("/root/elementTwo"));
|
||||
|
||||
Map<String, XPathEvaluationType> evalTypeMap = new HashMap<String,XPathEvaluationType>();
|
||||
evalTypeMap.put("two", XPathEvaluationType.NUMBER_RESULT);
|
||||
String docAsString = "<root><elementOne>1</elementOne><elementTwo>2</elementTwo></root>";
|
||||
|
||||
XPathHeaderEnricher enricher = new XPathHeaderEnricher(expressionMap);
|
||||
enricher.setEvaluationTypes(evalTypeMap);
|
||||
Message<?> result = enricher.transform(MessageBuilder.withPayload(docAsString).build());
|
||||
|
||||
MessageHeaders headers = result.getHeaders();
|
||||
|
||||
assertEquals("Wrong value for element one expression", "1", headers.get("one"));
|
||||
assertEquals("Wrong value for element two expression", 2.0, headers.get("two"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void notSkippingNullValues() {
|
||||
Map<String, XPathExpression> expressionMap = new HashMap<String, XPathExpression>();
|
||||
expressionMap.put("two", XPathExpressionFactory.createXPathExpression("/root/elementTwo"));
|
||||
String docAsString = "<root><elementOne>1</elementOne></root>";
|
||||
XPathHeaderEnricher enricher = new XPathHeaderEnricher(expressionMap);
|
||||
enricher.setSkipNullResults(false);
|
||||
Message<?> result = enricher.transform(MessageBuilder.withPayload(docAsString).build());
|
||||
MessageHeaders headers = result.getHeaders();
|
||||
assertEquals("no value set for two when result was null and ignore null was false",
|
||||
"", headers.get("two"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void numberEvaluationResult() {
|
||||
Map<String, XPathExpression> expressionMap = new HashMap<String, XPathExpression>();
|
||||
expressionMap.put("one", XPathExpressionFactory.createXPathExpression("/root/elementOne"));
|
||||
expressionMap.put("two", XPathExpressionFactory.createXPathExpression("/root/elementTwo"));
|
||||
Map<String, XPathEvaluationType> evalTypeMap = new HashMap<String, XPathEvaluationType>();
|
||||
evalTypeMap.put("two", XPathEvaluationType.NUMBER_RESULT);
|
||||
String docAsString = "<root><elementOne>1</elementOne><elementTwo>2</elementTwo></root>";
|
||||
XPathHeaderEnricher enricher = new XPathHeaderEnricher(expressionMap);
|
||||
enricher.setEvaluationTypes(evalTypeMap);
|
||||
Message<?> result = enricher.transform(MessageBuilder.withPayload(docAsString).build());
|
||||
MessageHeaders headers = result.getHeaders();
|
||||
assertEquals("Wrong value for element one expression", "1", headers.get("one"));
|
||||
assertEquals("Wrong value for element two expression", 2.0, headers.get("two"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user