OPEN - issue INT-309: XPath Message Selector

Added XPAth MessageSelector namespace to follow
This commit is contained in:
Jonas Partner
2008-09-29 16:07:44 +00:00
parent 039679b173
commit d03bb7ae5a
6 changed files with 420 additions and 3 deletions

View File

@@ -0,0 +1,94 @@
/*
* Copyright 2002-2007 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* 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.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
* @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){
this.xPathExpresion = XPathExpressionFactory.createXPathExpression(xPathExpression);
}
/**
*
* @param xPathExpression
* @param prefix
* @param 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);
}
/**
*
* @param xPathExpression
* @param namespaces
*/
public AbstractXPathMessageSelector(String xPathExpression, Map<String,String> namespaces){
this.xPathExpresion = XPathExpressionFactory.createXPathExpression(xPathExpression,namespaces);
}
/**
*
* @param xPathExpression
*/
public AbstractXPathMessageSelector(XPathExpression xPathExpression){
this.xPathExpresion = xPathExpression;
}
protected XmlPayloadConverter getConverter() {
return converter;
}
/**
* Converter used to convert payloads prior to XPAth testing
* @param converter
*/
public void setConverter(XmlPayloadConverter converter) {
this.converter = converter;
}
protected XPathExpression getXPathExpresion() {
return xPathExpresion;
}
}

View File

@@ -0,0 +1,80 @@
/*
* Copyright 2002-2007 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* 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;
/**
* 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
*
*/
public class BooleanTestXPathMessageSelector extends AbstractXPathMessageSelector {
/**
* Create a boolean testing XPath {@link MessageSelector} supporting
* mutliple namespaces
* @param pathExpression
* @param namespaces
*/
public BooleanTestXPathMessageSelector(String pathExpression, Map<String, String> namespaces) {
super(pathExpression, namespaces);
}
/**
* Create a boolean testing XPath {@link MessageSelector} supporting a
* single namespace
* @param pathExpression
* @param prefix
* @param namespace
*/
public BooleanTestXPathMessageSelector(String pathExpression, String prefix, String namespace) {
super(pathExpression, prefix, namespace);
}
/**
* Creates a boolean testing XPath {@link MessageSelector} with no namespace
* support
* @param pathExpression
*/
public BooleanTestXPathMessageSelector(String pathExpression) {
super(pathExpression);
}
/**
* Creates a boolean testing XPath {@link MessageSelector} using the
* provided {@link XPathExpression}
* @param pathExpression
*/
public BooleanTestXPathMessageSelector(XPathExpression pathExpression) {
super(pathExpression);
}
/**
* return true if the {@link XPathExpression} evaluates to <code>true</code>
*/
public boolean accept(Message<?> message) {
Document doc = getConverter().convertToDocument(message.getPayload());
return getXPathExpresion().evaluateAsBoolean(doc);
}
}

View File

@@ -0,0 +1,108 @@
/*
* Copyright 2002-2007 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* 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;
/**
* XPath {@link MessageSelector} which tests for a provided value Supports
* payloads of type {@link Document} or {@link String}
* @author Jonas Partner
*
*/
public class StringValueTestXPathMessageSelector extends AbstractXPathMessageSelector {
private final String valueToTestFor;
private volatile boolean caseSensitive = true;
/**
* Create a selector which tests for the given value and supports multiple
* namespaces
* @param pathExpression
* @param namespaces
* @param valueToTestFor
*/
public StringValueTestXPathMessageSelector(String pathExpression, Map<String, String> namespaces,
String valueToTestFor) {
super(pathExpression, namespaces);
this.valueToTestFor = valueToTestFor;
}
/**
* Creates a single namespace Xpath selector
* @param pathExpression
* @param prefix
* @param namespace
* @param valueToTestFor
*/
public StringValueTestXPathMessageSelector(String pathExpression, String prefix, String namespace,
String valueToTestFor) {
super(pathExpression, prefix, namespace);
this.valueToTestFor = valueToTestFor;
}
/**
* Creates non namespaced testing selector
* @param pathExpression
* @param valueToTestFor
*/
public StringValueTestXPathMessageSelector(String pathExpression, String valueToTestFor) {
super(pathExpression);
this.valueToTestFor = valueToTestFor;
}
/**
* Creates selector with provided {@link XPathExpression}
* @param pathExpression
* @param valueToTestFor
*/
public StringValueTestXPathMessageSelector(XPathExpression pathExpression, String valueToTestFor) {
super(pathExpression);
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) {
if (caseSensitive) {
return valueToTestFor.equals(getXPathExpresion().evaluateAsString(
getConverter().convertToDocument(message.getPayload())));
}
else {
return valueToTestFor.equalsIgnoreCase(getXPathExpresion().evaluateAsString(
getConverter().convertToDocument(message.getPayload())));
}
}
/**
* should comparison of value returned by {@link XPathExpression} to test
* value be case sensitive
* @param caseSensitive
*/
public void setCaseSensitive(boolean caseSensitive) {
this.caseSensitive = caseSensitive;
}
}