OPEN - issue INT-309: XPath Message Selector

http://jira.springframework.org/browse/INT-309
namespace support
This commit is contained in:
Jonas Partner
2008-10-01 12:28:27 +00:00
parent 1b4598568c
commit 8cc19d88b1
12 changed files with 543 additions and 168 deletions

View File

@@ -0,0 +1,12 @@
#Wed Oct 01 13:21:01 BST 2008
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.5
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.5

View File

@@ -30,6 +30,7 @@ public class IntegrationXmlNamespaceHandler extends NamespaceHandlerSupport {
registerBeanDefinitionParser("unmarshalling-transformer", new XmlUnmarshallingTransformerParser());
registerBeanDefinitionParser("xslt-transformer", new XsltPayloadTransformerParser());
registerBeanDefinitionParser("xpath-router", new XPathRouterParser());
registerBeanDefinitionParser("xpath-selector", new XPathSelectorParser());
}
}

View File

@@ -0,0 +1,93 @@
/*
* 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.config;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.BeanDefinitionParserDelegate;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.xml.xpath.XPathExpressionFactory;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class XPathExpressionBeanDefintionBuilder {
public AbstractBeanDefinition handleXpathExpression(Element element, ParserContext parserContext) {
String strXpathExpression = element.getAttribute("xpath-expression");
String strXpathExpressionPrefix = element.getAttribute("ns-prefix");
String strXpathExpressionNamespace = element.getAttribute("ns-uri");
String nameSpaceMapRef = element.getAttribute("namespace-map");
Assert.hasText(strXpathExpression, "xpath-expression attribute is required");
boolean prefixProvided = StringUtils.hasText(strXpathExpressionPrefix);
boolean namespaceProvided = StringUtils.hasText(strXpathExpressionNamespace);
boolean namespaceMapProvided = StringUtils.hasText(nameSpaceMapRef);
if (prefixProvided || namespaceProvided) {
Assert.isTrue(prefixProvided && namespaceProvided,
"Both xpath-prefix and xpath-namespace must be specified if one is specified");
Assert.isTrue(!namespaceMapProvided, "It is not valid to sepcify both xpath-namespace and namespace-map");
}
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(XPathExpressionFactory.class);
builder.setFactoryMethod("createXPathExpression");
builder.addConstructorArgValue(strXpathExpression);
if (prefixProvided) {
Map<String, String> namespaceMap = new HashMap<String, String>();
namespaceMap.put(strXpathExpressionPrefix, strXpathExpressionNamespace);
builder.addConstructorArgValue(namespaceMap);
}
else if (StringUtils.hasText(nameSpaceMapRef)) {
builder.addConstructorArgReference(nameSpaceMapRef);
}
else if (element.getChildNodes().getLength() > 0) {
NodeList nodeList = element.getChildNodes();
Element mapElement = null;
int elementCount = 0;
for (int i = 0; i < nodeList.getLength(); i++) {
Node currentNode = nodeList.item(i);
if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
mapElement = (Element) currentNode;
elementCount++;
}
}
Assert.isTrue(elementCount == 1, "Only one namespace map child allowed");
if (mapElement != null) {
Map namespaceMap = parseNamespaceMapElement(mapElement, parserContext, builder.getBeanDefinition());
builder.getBeanDefinition().getConstructorArgumentValues().addGenericArgumentValue(namespaceMap);
}
}
return builder.getBeanDefinition();
}
protected Map parseNamespaceMapElement(Element element, ParserContext parserContext, BeanDefinition parentDefinition) {
BeanDefinitionParserDelegate beanParser = new BeanDefinitionParserDelegate(parserContext.getReaderContext());
beanParser.initDefaults(element.getOwnerDocument().getDocumentElement());
return beanParser.parseMapElement(element, parentDefinition);
}
}

View File

@@ -16,23 +16,24 @@
package org.springframework.integration.xml.config;
import org.w3c.dom.Element;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.ConfigurationException;
import org.springframework.integration.xml.router.XPathMultiChannelNameResolver;
import org.springframework.integration.xml.router.XPathSingleChannelNameResolver;
import org.springframework.util.StringUtils;
import org.springframework.xml.xpath.XPathExpression;
import org.springframework.xml.xpath.XPathExpressionFactory;
import org.w3c.dom.Element;
/**
* @author Jonas Partner
*/
public class XPathRouterParser extends AbstractSingleBeanDefinitionParser {
private XPathExpressionBeanDefintionBuilder xpathBuilder = new XPathExpressionBeanDefintionBuilder();
@Override
protected boolean shouldGenerateId() {
return false;
@@ -45,22 +46,35 @@ public class XPathRouterParser extends AbstractSingleBeanDefinitionParser {
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
boolean multiChannel = Boolean.parseBoolean(element.getAttribute("multi-channel"));
String xPathExpression = element.getAttribute("xpath-expression");
String strXpathExpressionPrefix = element.getAttribute("xpath-prefix");
String strXpathExpressionNamespace = element.getAttribute("xpath-namespace");
String nameSpaceMapRef = element.getAttribute("namespace-map");
String xPathExpressionRef = element.getAttribute("xpath-expression-ref");
if ((StringUtils.hasText(xPathExpression) && StringUtils.hasText(xPathExpressionRef))
boolean strXpathAttSpecified = StringUtils.hasText(xPathExpression)
|| StringUtils.hasText(strXpathExpressionPrefix) || StringUtils.hasText(nameSpaceMapRef)
|| StringUtils.hasText(strXpathExpressionNamespace);
if ((strXpathAttSpecified && StringUtils.hasText(xPathExpressionRef))
|| (!StringUtils.hasText(xPathExpression) && !StringUtils.hasText(xPathExpressionRef))) {
throw new ConfigurationException("Exactly one of 'xpath-expression' or 'xpath-expression-ref' is required.");
}
if (multiChannel) {
builder.getBeanDefinition().setBeanClass(XPathMultiChannelNameResolver.class);
}
else {
builder.getBeanDefinition().setBeanClass(XPathSingleChannelNameResolver.class);
}
if (StringUtils.hasText(xPathExpression)) {
XPathExpression expression = XPathExpressionFactory.createXPathExpression(xPathExpression);
builder.addConstructorArgValue(expression);
AbstractBeanDefinition xPathExpressionBeanDefinition = xpathBuilder.handleXpathExpression(element, parserContext);
String xpathExpressionBeanName = BeanDefinitionReaderUtils.registerWithGeneratedName(
xPathExpressionBeanDefinition, parserContext.getRegistry());
builder.addConstructorArgReference(xpathExpressionBeanName);
}
else {
builder.addConstructorArgReference(xPathExpressionRef);

View File

@@ -0,0 +1,93 @@
/*
* 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.config;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.ConfigurationException;
import org.springframework.integration.xml.selector.BooleanTestXPathMessageSelector;
import org.springframework.integration.xml.selector.StringValueTestXPathMessageSelector;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
/**
*
* @author Jonas Partner
*
*/
public class XPathSelectorParser extends AbstractSingleBeanDefinitionParser {
private XPathExpressionBeanDefintionBuilder xpathBuilder = new XPathExpressionBeanDefintionBuilder();
@Override
protected boolean shouldGenerateId() {
return false;
}
@Override
protected boolean shouldGenerateIdAsFallback() {
return true;
}
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
String evaluationType = element.getAttribute("evaluation-result-type");
String xPathExpression = element.getAttribute("xpath-expression");
String strXpathExpressionPrefix = element.getAttribute("xpath-prefix");
String strXpathExpressionNamespace = element.getAttribute("xpath-namespace");
String nameSpaceMapRef = element.getAttribute("namespace-map");
String xPathExpressionRef = element.getAttribute("xpath-expression-ref");
String stringTestValue = element.getAttribute("string-test-value");
boolean strXpathAttSpecified = StringUtils.hasText(xPathExpression)
|| StringUtils.hasText(strXpathExpressionPrefix) || StringUtils.hasText(nameSpaceMapRef)
|| StringUtils.hasText(strXpathExpressionNamespace);
if ((strXpathAttSpecified && StringUtils.hasText(xPathExpressionRef))
|| (!StringUtils.hasText(xPathExpression) && !StringUtils.hasText(xPathExpressionRef))) {
throw new ConfigurationException("Exactly one of 'xpath-expression' or 'xpath-expression-ref' is required.");
}
if (evaluationType.equals("boolean")) {
builder.getBeanDefinition().setBeanClass(BooleanTestXPathMessageSelector.class);
Assert.state(!StringUtils.hasText(stringTestValue), "string-test-value should not be specified when evaluation-result-type is boolean");
}
else if (evaluationType.equals("string")){
Assert.hasText(stringTestValue, "string-test-value must be specified when evaluation-result-type is string");
builder.addConstructorArgValue(stringTestValue);
builder.getBeanDefinition().setBeanClass(StringValueTestXPathMessageSelector.class);
} else {
throw new ConfigurationException("Unrecognised value: " + evaluationType + " for evaluation-result-type only boolean or string supported");
}
if (StringUtils.hasText(xPathExpression)) {
AbstractBeanDefinition xPathExpressionBeanDefinition = xpathBuilder.handleXpathExpression(element, null);
String xpathExpressionBeanName = BeanDefinitionReaderUtils.registerWithGeneratedName(
xPathExpressionBeanDefinition, parserContext.getRegistry());
builder.addConstructorArgReference(xpathExpressionBeanName);
}
else {
builder.addConstructorArgReference(xPathExpressionRef);
}
}
}

View File

@@ -1,20 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.springframework.org/schema/integration/xml"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:tool="http://www.springframework.org/schema/tool"
targetNamespace="http://www.springframework.org/schema/integration/xml"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xsd:import namespace="http://www.springframework.org/schema/beans"/>
<xsd:import namespace="http://www.springframework.org/schema/tool"/>
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:tool="http://www.springframework.org/schema/tool"
targetNamespace="http://www.springframework.org/schema/integration/xml"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xsd:import namespace="http://www.springframework.org/schema/beans" />
<xsd:import namespace="http://www.springframework.org/schema/tool" />
<xsd:annotation>
<xsd:documentation>
Defines the configuration elements for Spring Integration's XML support.
</xsd:documentation>
</xsd:annotation>
<xsd:element name="marshalling-transformer">
<xsd:complexType>
<xsd:annotation>
@@ -24,22 +20,23 @@
</xsd:annotation>
<xsd:complexContent>
<xsd:extension base="transformerType">
<xsd:attribute name="marshaller" type="xsd:string" use="required"/>
<xsd:attribute name="marshaller" type="xsd:string"
use="required" />
<xsd:attribute name="result-type" use="optional">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="DOMResult"/>
<xsd:enumeration value="StringResult"/>
<xsd:enumeration value="DOMResult" />
<xsd:enumeration value="StringResult" />
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute name="result-factory" use="optional"/>
<xsd:attribute name="result-transformer" type="xsd:string" use="optional"/>
<xsd:attribute name="result-factory" use="optional" />
<xsd:attribute name="result-transformer" type="xsd:string"
use="optional" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="unmarshalling-transformer">
<xsd:complexType>
<xsd:annotation>
@@ -49,12 +46,12 @@
</xsd:annotation>
<xsd:complexContent>
<xsd:extension base="transformerType">
<xsd:attribute name="unmarshaller" type="xsd:string" use="required"/>
<xsd:attribute name="unmarshaller" type="xsd:string"
use="required" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="xslt-transformer">
<xsd:complexType>
<xsd:annotation>
@@ -64,24 +61,28 @@
</xsd:annotation>
<xsd:complexContent>
<xsd:extension base="transformerType">
<xsd:attribute name="xsl-resource" type="xsd:string" use="optional"/>
<xsd:attribute name="xsl-templates" type="xsd:string" use="optional"/>
<xsd:attribute name="source-factory" type="xsd:string" use="optional"/>
<xsd:attribute name="result-factory" type="xsd:string" use="optional"/>
<xsd:attribute name="xsl-resource" type="xsd:string"
use="optional" />
<xsd:attribute name="xsl-templates" type="xsd:string"
use="optional" />
<xsd:attribute name="source-factory" type="xsd:string"
use="optional" />
<xsd:attribute name="result-factory" type="xsd:string"
use="optional" />
<xsd:attribute name="result-type" use="optional">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="DOMResult"/>
<xsd:enumeration value="StringResult"/>
<xsd:enumeration value="DOMResult" />
<xsd:enumeration value="StringResult" />
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute name="result-transformer" type="xsd:string" use="optional"/>
<xsd:attribute name="result-transformer" type="xsd:string"
use="optional" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="xpath-router">
<xsd:complexType>
<xsd:annotation>
@@ -89,17 +90,64 @@
Defines an XPath router.
</xsd:documentation>
</xsd:annotation>
<xsd:attribute name="id" type="xsd:string" use="required"/>
<xsd:attribute name="xpath-expression-ref" type="xsd:string" use="optional"/>
<xsd:attribute name="xpath-expression" type="xsd:string" use="optional"/>
<xsd:attribute name="multi-channel" type="xsd:boolean" default="false"/>
<xsd:sequence>
<xsd:element ref="beans:map" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:string" use="required" />
<xsd:attribute name="xpath-expression-ref" type="xsd:string"
use="optional" />
<xsd:attribute name="xpath-expression" type="xsd:string"
use="optional" />
<xsd:attribute name="ns-prefix" type="xsd:string"
use="optional" />
<xsd:attribute name="ns-uri" type="xsd:string"
use="optional" />
<xsd:attribute name="multi-channel" type="xsd:boolean"
default="false" />
<xsd:attribute name="namespace-map" type="xsd:string"
use="optional" />
</xsd:complexType>
</xsd:element>
<xsd:element name="xpath-selector">
<xsd:complexType>
<xsd:annotation>
<xsd:documentation>
Defines an XPath selector.
</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:element ref="beans:map" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:string" use="required" />
<xsd:attribute name="xpath-expression-ref" type="xsd:string"
use="optional" />
<xsd:attribute name="xpath-expression" type="xsd:string"
use="optional" />
<xsd:attribute name="ns-prefix" type="xsd:string"
use="optional" />
<xsd:attribute name="ns-uri" type="xsd:string"
use="optional" />
<xsd:attribute name="evaluation-result-type" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="boolean" />
<xsd:enumeration value="string" />
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute name="namespace-map" type="xsd:string"
use="optional" />
<xsd:attribute name="string-test-value" type="xsd:string"
use="optional" />
</xsd:complexType>
</xsd:element>
<xsd:complexType name="transformerType">
<xsd:attribute name="id" type="xsd:string" use="optional"/>
<xsd:attribute name="input-channel" type="xsd:string" use="required"/>
<xsd:attribute name="output-channel" type="xsd:string" use="required"/>
<xsd:attribute name="id" type="xsd:string" use="optional" />
<xsd:attribute name="input-channel" type="xsd:string"
use="required" />
<xsd:attribute name="output-channel" type="xsd:string"
use="required" />
</xsd:complexType>
</xsd:schema>

View File

@@ -30,6 +30,7 @@ import org.w3c.dom.Node;
* extract a channel name. The payload is extracted as a node using the provided
* {@link XmlPayloadConverter} with {@link DefaultXmlPayloadConverter} being the
* default.
* The provided {@link XPathExpression} should evaluate to a non empty string
* @author Jonas Partner
*/
public class XPathSingleChannelNameResolver extends AbstractXPathChannelNameResolver {

View File

@@ -0,0 +1,62 @@
/*
* 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.config;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.springframework.context.support.AbstractXmlApplicationContext;
import org.springframework.core.io.AbstractResource;
import org.springframework.core.io.Resource;
public class TestXmlApplicationContext extends AbstractXmlApplicationContext {
private final Resource[] resources;
public TestXmlApplicationContext(String ... xmlStrings){
resources = new Resource[xmlStrings.length];
for (int i = 0 ; i < xmlStrings.length; i++) {
resources[i] = new TestResource(xmlStrings[i]);
}
refresh();
}
@Override
protected Resource[] getConfigResources() {
return resources;
}
private static class TestResource extends AbstractResource{
String xmlString;
TestResource(String xmlString){
this.xmlString = xmlString;
}
public String getDescription() {
return "test";
}
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(xmlString.getBytes("UTF-8"));
}
}
}

View File

@@ -0,0 +1,42 @@
/*
* 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.config;
public class TestXmlApplicationContextHelper {
public static TestXmlApplicationContext getTestAppContext(String xmlFragment) {
String xml = header + xmlFragment + footer;
TestXmlApplicationContext ctx = new TestXmlApplicationContext(xml);
return ctx;
}
private final static String header = "<?xml version='1.0' encoding='UTF-8'?>"
+ "<beans xmlns='http://www.springframework.org/schema/beans' "
+ "xmlns:si-xml='http://www.springframework.org/schema/integration/xml' "
+ "xmlns:si='http://www.springframework.org/schema/integration' "
+ "xmlns:util='http://www.springframework.org/schema/util' "
+ "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " + "xsi:schemaLocation="
+ "'http://www.springframework.org/schema/beans "
+ "http://www.springframework.org/schema/beans/spring-beans.xsd "
+ "http://www.springframework.org/schema/integration "
+ "http://www.springframework.org/schema/integration/spring-integration-1.0.xsd "
+ "http://www.springframework.org/schema/integration/xml "
+ "http://www.springframework.org/schema/integration/xml/spring-integration-xml-1.0.xsd "
+ "http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd' >";
private final static String footer = "</beans>";
}

View File

@@ -1,54 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:si-xml="http://www.springframework.org/schema/integration/xml"
xmlns:si="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/xml
http://www.springframework.org/schema/integration/xml/spring-integration-xml-1.0.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
<bean id="messageChannel" class="org.springframework.integration.channel.QueueChannel" />
<si:message-bus/>
<si:channel id="inputOne" />
<si:channel id="inputTwo" />
<si:channel id="outputOne">
<si:queue capacity="10"/>
</si:channel>
<si:channel id="outputTwo">
<si:queue capacity="10"/>
</si:channel>
<si:channel id="errorChannel">
<si:queue capacity="10"/>
</si:channel>
<si-xml:xpath-router id="routerOne"
xpath-expression-ref="xpathExpression" />
<si:router ref="routerOne" input-channel="inputOne" error-handler="errorHandler"/>
<si-xml:xpath-router id="routerTwo"
xpath-expression-ref="xpathExpressionMulti" multi-channel="true" />
<si:router ref="routerTwo" input-channel="inputTwo" error-handler="errorHandler"/>
<bean id="xpathExpression"
class="org.springframework.xml.xpath.XPathExpressionFactoryBean">
<property name="expression" value="/name" />
</bean>
<bean id="xpathExpressionMulti"
class="org.springframework.xml.xpath.XPathExpressionFactoryBean">
<property name="expression" value="//name" />
</bean>
<bean id="errorHandler" class="org.springframework.integration.channel.MessagePublishingErrorHandler">
<property name="errorChannel" ref="errorChannel"/>
</bean>
</beans>

View File

@@ -16,100 +16,96 @@
package org.springframework.integration.xml.config;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.w3c.dom.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.PollableChannel;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.xml.router.XPathSingleChannelNameResolver;
import org.springframework.integration.xml.util.XmlTestUtil;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.w3c.dom.Document;
/**
* @author Jonas Partner
*/
@ContextConfiguration
public class XPathRouterParserTests extends AbstractJUnit4SpringContextTests{
public class XPathRouterParserTests {
@Autowired @Qualifier("inputOne")
MessageChannel inputOne;
@Autowired @Qualifier("inputTwo")
MessageChannel inputTwo;
@Autowired @Qualifier("outputOne")
PollableChannel outputOne;
@Autowired @Qualifier("outputTwo")
PollableChannel outputTwo;
@Autowired @Qualifier("errorChannel")
PollableChannel errorChannel;
@SuppressWarnings("unchecked")
@Test
public void testOutputOne() throws Exception{
public void testSimpleStringExpression() throws Exception {
Document doc = XmlTestUtil.getDocumentForString("<name>outputOne</name>");
GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
inputOne.send(docMessage);
GenericMessage<Document> received = (GenericMessage<Document>) outputOne.receive(1000);
assertNotNull("Did not receive message from outputOne", received);
}
@SuppressWarnings("unchecked")
@Test
public void testOutputTwo() throws Exception{
Document doc = XmlTestUtil.getDocumentForString("<name>outputTwo</name>");
GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
inputOne.send(docMessage);
GenericMessage<Document> received = (GenericMessage<Document>) outputTwo.receive(1000);
assertNotNull("Did not receive message from two", received);
TestXmlApplicationContext ctx = TestXmlApplicationContextHelper.getTestAppContext("<si-xml:xpath-router id='router' xpath-expression='/name' />");
XPathSingleChannelNameResolver router = (XPathSingleChannelNameResolver) ctx.getBean("router");
String[] channelNames = router.resolveChannelNames(docMessage);
assertEquals("Wrong number of channel names returned", 1, channelNames.length);
assertEquals("Wrong channel name", "outputOne", channelNames[0]);
}
@SuppressWarnings("unchecked")
@Test
public void testOutputThree() throws Exception{
Document doc = XmlTestUtil.getDocumentForString("<name>outputThree</name>");
public void testNamespacedStringExpression() throws Exception {
Document doc = XmlTestUtil.getDocumentForString("<ns1:name xmlns:ns1='www.example.org'>outputOne</ns1:name>");
GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
inputOne.send(docMessage);
GenericMessage<Document> received = (GenericMessage<Document>) errorChannel.receive(1000);
assertNotNull("Did not receive message on errors", received);
}
@SuppressWarnings("unchecked")
@Test
public void testOutputOneMulti() throws Exception{
Document doc = XmlTestUtil.getDocumentForString("<name>outputOne</name>");
GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
inputTwo.send(docMessage);
GenericMessage<Document> received = (GenericMessage<Document>) outputOne.receive(1000);
assertNotNull("Did not receive message from outputOne", received);
}
@SuppressWarnings("unchecked")
@Test
public void testOutputOneAndTwoMulti() throws Exception{
Document doc = XmlTestUtil.getDocumentForString("<doc><name>outputOne</name><name>outputTwo</name></doc>");
GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
inputTwo.send(docMessage);
GenericMessage<Document> received = (GenericMessage<Document>) outputTwo.receive(1000);
assertNotNull("Did not receive message from two", received);
TestXmlApplicationContext ctx = TestXmlApplicationContextHelper.getTestAppContext("<si-xml:xpath-router id='router' xpath-expression='/ns2:name' ns-prefix='ns2' ns-uri='www.example.org' />");
XPathSingleChannelNameResolver router = (XPathSingleChannelNameResolver) ctx.getBean("router");
String[] channelNames = router.resolveChannelNames(docMessage);
assertEquals("Wrong number of channel names returned", 1, channelNames.length);
assertEquals("Wrong channel name", "outputOne", channelNames[0]);
}
@SuppressWarnings("unchecked")
@Test
public void testOutputThreeMulti() throws Exception{
Document doc = XmlTestUtil.getDocumentForString("<name>outputThree</name>");
public void testStringExpressionWithNestedNamespaceMap() throws Exception {
Document doc = XmlTestUtil
.getDocumentForString("<ns1:name xmlns:ns1='www.example.org' xmlns:ns2='www.example.org2'><ns2:type>outputOne</ns2:type></ns1:name>");
GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
inputTwo.send(docMessage);
GenericMessage<Document> received = (GenericMessage<Document>) errorChannel.receive(1000);
assertNotNull("Did not receive message on errors", received);
StringBuffer buffer = new StringBuffer(
"<si-xml:xpath-router id='router' xpath-expression='/ns1:name/ns2:type' >");
buffer
.append("<map><entry key='ns1' value='www.example.org' /> <entry key='ns2' value='www.example.org2'/></map>");
buffer.append("</si-xml:xpath-router>");
TestXmlApplicationContext ctx = TestXmlApplicationContextHelper.getTestAppContext(buffer.toString());
XPathSingleChannelNameResolver router = (XPathSingleChannelNameResolver) ctx.getBean("router");
String[] channelNames = router.resolveChannelNames(docMessage);
assertEquals("Wrong number of channel names returned", 1, channelNames.length);
assertEquals("Wrong channel name", "outputOne", channelNames[0]);
}
@Test
public void testStringExpressionWithReferenceToNamespaceMap() throws Exception {
Document doc = XmlTestUtil
.getDocumentForString("<ns1:name xmlns:ns1='www.example.org' xmlns:ns2='www.example.org2'><ns2:type>outputOne</ns2:type></ns1:name>");
GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
StringBuffer buffer = new StringBuffer(
"<si-xml:xpath-router id='router' xpath-expression='/ns1:name/ns2:type' namespace-map='nsMap'>");
buffer
.append("</si-xml:xpath-router>")
.append("<util:map id='nsMap'><entry key='ns1' value='www.example.org' /><entry key='ns2' value='www.example.org2' /></util:map>");
TestXmlApplicationContext ctx = TestXmlApplicationContextHelper.getTestAppContext(buffer.toString());
XPathSingleChannelNameResolver router = (XPathSingleChannelNameResolver) ctx.getBean("router");
String[] channelNames = router.resolveChannelNames(docMessage);
assertEquals("Wrong number of channel names returned", 1, channelNames.length);
assertEquals("Wrong channel name", "outputOne", channelNames[0]);
}
@Test(expected = Exception.class)
public void testNamespacedWithNoPrefixStringExpression() throws Exception {
TestXmlApplicationContextHelper.getTestAppContext("<si-xml:xpath-router id='router' xpath-expression='/ns2:name' xpath-namespace='www.example.org' />");
}
@Test(expected = Exception.class)
public void testPrefixWithNoNamespaceStringExpression() throws Exception {
TestXmlApplicationContextHelper.getTestAppContext("<si-xml:xpath-router id='router' xpath-expression='/ns2:name' xpath-prefix='ns2' />");
}
}

View File

@@ -0,0 +1,67 @@
/*
* 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.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;
public class XPathSelectorParserTests {
@Test
public void testSimpleStringExpressionBoolean() throws Exception {
String contextXml = "<si-xml:xpath-selector id='selector' xpath-expression='/name' evaluation-result-type='boolean' />";
MessageSelector selector =getSelector( contextXml);
assertTrue(selector.accept(new GenericMessage<Document>(XmlTestUtil.getDocumentForString("<name>outputOne</name>"))));
assertFalse(selector.accept(new GenericMessage<Document>(XmlTestUtil.getDocumentForString("<other>outputOne</other>"))));
}
@Test
public void testStringExpressionWithNamespaceBoolean() throws Exception {
String contextXml = "<si-xml:xpath-selector id='selector' xpath-expression='/ns:name' ns-prefix='ns' ns-uri='www.example.org' evaluation-result-type='boolean' />";
MessageSelector selector = getSelector(contextXml);
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 {
String contextXml = "<si-xml:xpath-selector id='selector' xpath-expression='/ns:name' ns-prefix='ns' ns-uri='www.example.org' evaluation-result-type='boolean' />";
MessageSelector selector = getSelector(contextXml);
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>"))));
}
public MessageSelector getSelector( String testcontextXml) throws Exception{
TestXmlApplicationContext ctx =
TestXmlApplicationContextHelper.getTestAppContext(testcontextXml);
return (MessageSelector) ctx.getBean("selector");
}
}