Moved MessageSelector interface and implementations into a new 'org.springframework.integration.selector' package (instead of a sub-package under 'message'). Also added support for "Strategy" enum in MessageSelector with the following values available [ALL, ANY, AT_LEAST_HALF, MORE_THAN_HALF] (INT-308).

This commit is contained in:
Mark Fisher
2008-10-14 16:16:55 +00:00
parent 9286f1fdaa
commit 109343d14e
37 changed files with 372 additions and 142 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2008 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.
@@ -13,82 +13,77 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.xml.selector;
import java.util.HashMap;
import java.util.Map;
import org.springframework.integration.message.selector.MessageSelector;
import org.springframework.integration.selector.MessageSelector;
import org.springframework.integration.xml.DefaultXmlPayloadConverter;
import org.springframework.integration.xml.XmlPayloadConverter;
import org.springframework.xml.xpath.XPathExpression;
import org.springframework.xml.xpath.XPathExpressionFactory;
/**
* Base class for XPath {@link MessageSelector} implementations
* Base class for XPath {@link MessageSelector} implementations.
*
* @author Jonas Partner
*
*/
public abstract class AbstractXPathMessageSelector implements MessageSelector {
private final XPathExpression xPathExpresion;
private XmlPayloadConverter converter = new DefaultXmlPayloadConverter();
/**
*
* @param xPathExpression simple String expression
*/
public AbstractXPathMessageSelector(String xPathExpression){
public AbstractXPathMessageSelector(String xPathExpression) {
this.xPathExpresion = XPathExpressionFactory.createXPathExpression(xPathExpression);
}
/**
*
* @param xPathExpression
* @param prefix
* @param namespace
*/
public AbstractXPathMessageSelector(String xPathExpression, String prefix, String namespace){
public AbstractXPathMessageSelector(String xPathExpression, String prefix, String namespace) {
Map<String,String> namespaces = new HashMap<String, String>();
namespaces.put(prefix, namespace);
this.xPathExpresion = XPathExpressionFactory.createXPathExpression(xPathExpression,namespaces);
this.xPathExpresion = XPathExpressionFactory.createXPathExpression(xPathExpression, namespaces);
}
/**
*
* @param xPathExpression
* @param namespaces
*/
public AbstractXPathMessageSelector(String xPathExpression, Map<String,String> namespaces){
this.xPathExpresion = XPathExpressionFactory.createXPathExpression(xPathExpression,namespaces);
public AbstractXPathMessageSelector(String xPathExpression, Map<String,String> namespaces) {
this.xPathExpresion = XPathExpressionFactory.createXPathExpression(xPathExpression, namespaces);
}
/**
*
* @param xPathExpression
*/
public AbstractXPathMessageSelector(XPathExpression xPathExpression){
public AbstractXPathMessageSelector(XPathExpression xPathExpression) {
this.xPathExpresion = xPathExpression;
}
protected XmlPayloadConverter getConverter() {
return converter;
}
/**
* Converter used to convert payloads prior to XPAth testing
* @param converter
* Specify the converter used to convert payloads prior to XPath testing.
*/
public void setConverter(XmlPayloadConverter converter) {
this.converter = converter;
}
protected XmlPayloadConverter getConverter() {
return this.converter;
}
protected XPathExpression getXPathExpresion() {
return xPathExpresion;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2008 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.
@@ -13,69 +13,77 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.xml.selector;
import java.util.Map;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.selector.MessageSelector;
import org.springframework.xml.xpath.XPathExpression;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.springframework.integration.message.Message;
import org.springframework.integration.selector.MessageSelector;
import org.springframework.xml.xpath.XPathExpression;
/**
* Boolean XPath testing {@link MessageSelector}. Requires an XPathExpression
* which can be evaluated using {@link XPathExpression} evaluateAsBoolean.
* Supports payloads of type {@link Document} or {@link String}
* @author Jonas Partner
* which can be evaluated using {@link XPathExpression#evaluateAsBoolean(Node)}.
* Supports payloads of type {@link Document} or {@link String}.
*
* @author Jonas Partner
*/
public class BooleanTestXPathMessageSelector extends AbstractXPathMessageSelector {
/**
* Create a boolean testing XPath {@link MessageSelector} supporting
* mutliple namespaces
* @param pathExpression
* mutliple namespaces.
*
* @param expression
* @param namespaces
*/
public BooleanTestXPathMessageSelector(String pathExpression, Map<String, String> namespaces) {
super(pathExpression, namespaces);
public BooleanTestXPathMessageSelector(String expression, Map<String, String> namespaces) {
super(expression, namespaces);
}
/**
* Create a boolean testing XPath {@link MessageSelector} supporting a
* single namespace
* @param pathExpression
* single namespace.
*
* @param expression
* @param prefix
* @param namespace
*/
public BooleanTestXPathMessageSelector(String pathExpression, String prefix, String namespace) {
super(pathExpression, prefix, namespace);
public BooleanTestXPathMessageSelector(String expression, String prefix, String namespace) {
super(expression, prefix, namespace);
}
/**
* Creates a boolean testing XPath {@link MessageSelector} with no namespace
* support
* @param pathExpression
* Create a boolean testing XPath {@link MessageSelector} with no namespace
* support.
*
* @param expression
*/
public BooleanTestXPathMessageSelector(String pathExpression) {
super(pathExpression);
public BooleanTestXPathMessageSelector(String expression) {
super(expression);
}
/**
* Creates a boolean testing XPath {@link MessageSelector} using the
* provided {@link XPathExpression}
* @param pathExpression
* Create a boolean testing XPath {@link MessageSelector} using the
* provided {@link XPathExpression}.
*
* @param expression
*/
public BooleanTestXPathMessageSelector(XPathExpression pathExpression) {
super(pathExpression);
public BooleanTestXPathMessageSelector(XPathExpression expression) {
super(expression);
}
/**
* return true if the {@link XPathExpression} evaluates to <code>true</code>
* Return true if the {@link XPathExpression} evaluates to <code>true</code>
*/
public boolean accept(Message<?> message) {
Node node = getConverter().convertToNode(message.getPayload());
return getXPathExpresion().evaluateAsBoolean(node);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2008 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.
@@ -13,21 +13,23 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.xml.selector;
import java.util.Map;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.selector.MessageSelector;
import org.springframework.xml.xpath.XPathExpression;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.springframework.integration.message.Message;
import org.springframework.integration.selector.MessageSelector;
import org.springframework.xml.xpath.XPathExpression;
/**
* XPath {@link MessageSelector} which tests for a provided value Supports
* payloads of type {@link Document} or {@link String}
* @author Jonas Partner
* XPath {@link MessageSelector} that tests if a provided value supports
* payloads of type {@link Document} or {@link String}.
*
* @author Jonas Partner
*/
public class StringValueTestXPathMessageSelector extends AbstractXPathMessageSelector {
@@ -35,75 +37,79 @@ public class StringValueTestXPathMessageSelector extends AbstractXPathMessageSel
private volatile boolean caseSensitive = true;
/**
* Create a selector which tests for the given value and supports multiple
* namespaces
* @param pathExpression
* namespaces.
*
* @param expression
* @param namespaces
* @param valueToTestFor
*/
public StringValueTestXPathMessageSelector(String pathExpression, Map<String, String> namespaces,
String valueToTestFor) {
super(pathExpression, namespaces);
public StringValueTestXPathMessageSelector(String expression, Map<String, String> namespaces, String valueToTestFor) {
super(expression, namespaces);
this.valueToTestFor = valueToTestFor;
}
/**
* Creates a single namespace Xpath selector
* @param pathExpression
* Creates a single namespace Xpath selector.
*
* @param expression
* @param prefix
* @param namespace
* @param valueToTestFor
*/
public StringValueTestXPathMessageSelector(String pathExpression, String prefix, String namespace,
String valueToTestFor) {
super(pathExpression, prefix, namespace);
public StringValueTestXPathMessageSelector(String expression, String prefix, String namespace, String valueToTestFor) {
super(expression, prefix, namespace);
this.valueToTestFor = valueToTestFor;
}
/**
* Creates non namespaced testing selector
* @param pathExpression
* Creates non-namespaced testing selector.
*
* @param expression
* @param valueToTestFor
*/
public StringValueTestXPathMessageSelector(String pathExpression, String valueToTestFor) {
super(pathExpression);
public StringValueTestXPathMessageSelector(String expression, String valueToTestFor) {
super(expression);
this.valueToTestFor = valueToTestFor;
}
/**
* Creates selector with provided {@link XPathExpression}
* @param pathExpression
* Creates a selector with the provided {@link XPathExpression}.
*
* @param expression
* @param valueToTestFor
*/
public StringValueTestXPathMessageSelector(XPathExpression pathExpression, String valueToTestFor) {
super(pathExpression);
public StringValueTestXPathMessageSelector(XPathExpression expression, String valueToTestFor) {
super(expression);
this.valueToTestFor = valueToTestFor;
}
/**
* Evaluate the payload returning true if the value returned by the
* {@link XPathExpression} is equal to the valueToTestFor
*/
public boolean accept(Message<?> message) {
Node nodeToTest = getConverter().convertToNode(message.getPayload());
String xPathResult = getXPathExpresion().evaluateAsString(nodeToTest);
if (caseSensitive) {
return valueToTestFor.equals(xPathResult);
}
else {
return valueToTestFor.equalsIgnoreCase(xPathResult);
}
}
/**
* should comparison of value returned by {@link XPathExpression} to test
* value be case sensitive
* Specify whether comparison of value returned by {@link XPathExpression}
* to test value should be case sensitive. Default is 'true'.
*
* @param caseSensitive
*/
public void setCaseSensitive(boolean caseSensitive) {
this.caseSensitive = caseSensitive;
}
/**
* Evaluate the payload and return true if the value returned by the
* {@link XPathExpression} is equal to the <code>valueToTestFor</code>.
*/
public boolean accept(Message<?> message) {
Node nodeToTest = getConverter().convertToNode(message.getPayload());
String xPathResult = getXPathExpresion().evaluateAsString(nodeToTest);
if (this.caseSensitive) {
return this.valueToTestFor.equals(xPathResult);
}
else {
return this.valueToTestFor.equalsIgnoreCase(xPathResult);
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2008 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.
@@ -13,17 +13,22 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.xml.config;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.selector.MessageSelector;
import org.springframework.integration.xml.util.XmlTestUtil;
import org.w3c.dom.Document;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.selector.MessageSelector;
import org.springframework.integration.xml.util.XmlTestUtil;
/**
* @author Jonas Partner
*/
public class XPathSelectorParserTests {
@Test
@@ -43,8 +48,7 @@ public class XPathSelectorParserTests {
assertTrue(selector.accept(new GenericMessage<Document>(XmlTestUtil.getDocumentForString("<ns1:name xmlns:ns1='www.example.org'>outputOne</ns1:name>"))));
assertFalse(selector.accept(new GenericMessage<Document>(XmlTestUtil.getDocumentForString("<name>outputOne</name>"))));
}
@Test
public void testStringExpressionWithNestedMap() throws Exception {
StringBuffer contextXml = new StringBuffer("<si-xml:xpath-selector id='selector' evaluation-result-type='boolean'>");
@@ -57,7 +61,8 @@ public class XPathSelectorParserTests {
assertFalse(selector.accept(new GenericMessage<Document>(XmlTestUtil.getDocumentForString("<name>outputOne</name>"))));
}
public MessageSelector getSelector( String testcontextXml) throws Exception{
TestXmlApplicationContext ctx =
TestXmlApplicationContextHelper.getTestAppContext(testcontextXml);
@@ -65,6 +70,4 @@ public class XPathSelectorParserTests {
}
}