INT-1517, combined Single and Multi channel XPath routers, modified XPathRouterParser to inherit form the base parser for all routers, combine single/multiple xpath router tests into one

This commit is contained in:
Oleg Zhurakousky
2010-10-14 06:45:22 -04:00
parent 3a8444c949
commit 1d0b773f2b
10 changed files with 101 additions and 332 deletions

View File

@@ -16,84 +16,48 @@
package org.springframework.integration.xml.config;
import java.util.List;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.config.xml.AbstractConsumerEndpointParser;
import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
import org.springframework.integration.config.xml.AbstractChannelNameResolvingRouterParser;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
/**
* Parser for the <xpath-router/> element.
*
* @author Jonas Partner
* @author Mark Fisher
* @author Oleg Zhurakousky
*/
public class XPathRouterParser extends AbstractConsumerEndpointParser {
public class XPathRouterParser extends AbstractChannelNameResolvingRouterParser {
private XPathExpressionParser xpathParser = new XPathExpressionParser();
@Override
protected boolean shouldGenerateId() {
return false;
}
@Override
protected boolean shouldGenerateIdAsFallback() {
return true;
}
@Override
protected BeanDefinitionBuilder parseHandler(Element element, ParserContext parserContext) {
protected BeanDefinition doParseRouter(Element element,
ParserContext parserContext) {
BeanDefinitionBuilder xpathRouterBuilder = BeanDefinitionBuilder.genericBeanDefinition(
"org.springframework.integration.xml.router.XPathRouter");
NodeList xPathExpressionNodes = element.getElementsByTagNameNS(
element.getNamespaceURI(), "xpath-expression");
Assert.isTrue(xPathExpressionNodes.getLength() < 2,
"Only one xpath-expression child can be specified.");
Assert.isTrue(xPathExpressionNodes.getLength() < 2, "Only one xpath-expression child can be specified.");
String xPathExpressionRef = element.getAttribute("xpath-expression-ref");
boolean xPathExpressionChildPresent = (xPathExpressionNodes.getLength() == 1);
boolean xPathReferencePresent = StringUtils.hasText(xPathExpressionRef);
Assert.isTrue(xPathExpressionChildPresent ^ xPathReferencePresent,
"Exactly one of 'xpath-expression' or 'xpath-expression-ref' is required.");
boolean multiChannel = Boolean.parseBoolean(element.getAttribute("multi-channel"));
String classname = "org.springframework.integration.xml.router." +
((multiChannel) ? "XPathMultiChannelRouter" : "XPathSingleChannelRouter");
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(classname);
List<Element> childElements = DomUtils.getChildElementsByTagName(element, "mapping");
if (childElements != null && childElements.size() > 0) {
ManagedMap<String, String> channelMap = new ManagedMap<String, String>();
for (Element childElement : childElements) {
String key = childElement.getAttribute("value");
channelMap.put(key, childElement.getAttribute("channel"));
}
builder.addPropertyValue("channelIdentifierMap", channelMap);
}
if (xPathExpressionChildPresent) {
BeanDefinition beanDefinition = this.xpathParser.parse(
(Element) xPathExpressionNodes.item(0), parserContext);
builder.addConstructorArgValue(beanDefinition);
xpathRouterBuilder.addConstructorArgValue(beanDefinition);
}
else {
builder.addConstructorArgReference(xPathExpressionRef);
xpathRouterBuilder.addConstructorArgReference(xPathExpressionRef);
}
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "resolution-required");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "ignore-channel-name-resolution-failures");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "channel-resolver");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "default-output-channel");
return builder;
return xpathRouterBuilder.getBeanDefinition();
}
}

View File

@@ -1,95 +0,0 @@
/*
* 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.
* 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.router;
import java.util.List;
import java.util.Map;
import org.springframework.integration.Message;
import org.springframework.integration.xml.XmlPayloadConverter;
import org.springframework.util.Assert;
import org.springframework.xml.xpath.NodeMapper;
import org.springframework.xml.xpath.XPathExpression;
import org.w3c.dom.DOMException;
import org.w3c.dom.Node;
/**
* A router that evaluates the XPath expression using
* {@link XPathExpression#evaluateAsNodeList(Node)} which returns zero or more
* nodes in conjunction with an instance of {@link NodeMapper} to produce zero
* or more channel names. An instance of {@link XmlPayloadConverter} is used to
* extract the payload as a {@link Node}.
*
* @author Jonas Partner
*/
public class XPathMultiChannelRouter extends AbstractXPathRouter {
private volatile NodeMapper nodeMapper = new TextContentNodeMapper();
/**
* @see AbstractXPathRouter#AbstractXPathRouter(String, Map)
*/
public XPathMultiChannelRouter(String expression, Map<String, String> namespaces) {
super(expression, namespaces);
}
/**
* @see AbstractXPathRouter#AbstractXPathRouter(String, String, String)
*/
public XPathMultiChannelRouter(String expression, String prefix, String namespace) {
super(expression, prefix, namespace);
}
/**
* @see AbstractXPathRouter#AbstractXPathRouter(String)
*/
public XPathMultiChannelRouter(String expression) {
super(expression);
}
/**
* @see AbstractXPathRouter#AbstractXPathRouter(XPathExpression)
*/
public XPathMultiChannelRouter(XPathExpression expression) {
super(expression);
}
public void setNodeMapper(NodeMapper nodeMapper) {
Assert.notNull(nodeMapper, "NodeMapper must not be null");
this.nodeMapper = nodeMapper;
}
@SuppressWarnings("unchecked")
public List<Object> getChannelIndicatorList(Message<?> message) {
Node node = getConverter().convertToNode(message.getPayload());
return getXPathExpression().evaluate(node, this.nodeMapper);
}
private static class TextContentNodeMapper implements NodeMapper {
public Object mapNode(Node node, int nodeNum) throws DOMException {
return node.getTextContent();
}
}
}

View File

@@ -17,21 +17,29 @@
package org.springframework.integration.xml.router;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.integration.Message;
import org.springframework.integration.router.AbstractMessageRouter;
import org.springframework.integration.xml.DefaultXmlPayloadConverter;
import org.springframework.integration.xml.XmlPayloadConverter;
import org.springframework.xml.xpath.NodeMapper;
import org.springframework.xml.xpath.XPathExpression;
import org.springframework.xml.xpath.XPathExpressionFactory;
import org.w3c.dom.DOMException;
import org.w3c.dom.Node;
/**
* Abstract base class for Message Routers that use
* {@link XPathExpression} evaluation to determine channel names.
*
* @author Jonas Partner
* @author Oleg Zhurakousky
*/
public abstract class AbstractXPathRouter extends AbstractMessageRouter {
public class XPathRouter extends AbstractMessageRouter {
private volatile NodeMapper nodeMapper = new TextContentNodeMapper();
private final XPathExpression xPathExpression;
@@ -45,7 +53,7 @@ public abstract class AbstractXPathRouter extends AbstractMessageRouter {
* @param expression
* @param namespaces
*/
public AbstractXPathRouter(String expression, Map<String, String> namespaces) {
public XPathRouter(String expression, Map<String, String> namespaces) {
this.xPathExpression = XPathExpressionFactory.createXPathExpression(expression, namespaces);
}
@@ -57,7 +65,7 @@ public abstract class AbstractXPathRouter extends AbstractMessageRouter {
* @param prefix
* @param namespace
*/
public AbstractXPathRouter(String expression, String prefix, String namespace) {
public XPathRouter(String expression, String prefix, String namespace) {
Map<String, String> namespaces = new HashMap<String, String>();
namespaces.put(prefix, namespace);
this.xPathExpression = XPathExpressionFactory.createXPathExpression(expression, namespaces);
@@ -69,7 +77,7 @@ public abstract class AbstractXPathRouter extends AbstractMessageRouter {
*
* @param expression
*/
public AbstractXPathRouter(String expression) {
public XPathRouter(String expression) {
this.xPathExpression = XPathExpressionFactory.createXPathExpression(expression);
}
@@ -78,7 +86,7 @@ public abstract class AbstractXPathRouter extends AbstractMessageRouter {
*
* @param expression
*/
public AbstractXPathRouter(XPathExpression expression) {
public XPathRouter(XPathExpression expression) {
this.xPathExpression = expression;
}
@@ -103,4 +111,20 @@ public abstract class AbstractXPathRouter extends AbstractMessageRouter {
public String getComponentType(){
return "xml:xpath-router";
}
@Override
@SuppressWarnings("unchecked")
public List<Object> getChannelIndicatorList(Message<?> message) {
Node node = getConverter().convertToNode(message.getPayload());
return getXPathExpression().evaluate(node, this.nodeMapper);
}
private static class TextContentNodeMapper implements NodeMapper {
public Object mapNode(Node node, int nodeNum) throws DOMException {
return node.getTextContent();
}
}
}

View File

@@ -1,93 +0,0 @@
/*
* 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.
* 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.router;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.integration.Message;
import org.springframework.integration.MessagingException;
import org.springframework.integration.xml.DefaultXmlPayloadConverter;
import org.springframework.integration.xml.XmlPayloadConverter;
import org.springframework.xml.xpath.XPathExpression;
import org.w3c.dom.Node;
/**
* Router that evaluates the payload using {@link XPathExpression#evaluateAsString(Node)}
* to extract a channel name. The payload is extracted as a node using the
* provided {@link XmlPayloadConverter} with {@link DefaultXmlPayloadConverter}
* being the default.
*
* <p>The provided {@link XPathExpression} must evaluate to a non-empty String.
*
* @author Jonas Partner
*/
public class XPathSingleChannelRouter extends AbstractXPathRouter {
/**
* @see AbstractXPathRouter#AbstractXPathRouter(String, Map)
*/
public XPathSingleChannelRouter(String expression, Map<String, String> namespaces) {
super(expression, namespaces);
}
/**
* @see AbstractXPathRouter#AbstractXPathRouter(String, String, String)
*/
public XPathSingleChannelRouter(String expression, String prefix, String namespace) {
super(expression, prefix, namespace);
}
/**
* @see AbstractXPathRouter#AbstractXPathRouter(String)
*/
public XPathSingleChannelRouter(String expression) {
super(expression);
}
/**
* @see AbstractXPathRouter#AbstractXPathRouter(XPathExpression)
*/
public XPathSingleChannelRouter(XPathExpression expression) {
super(expression);
}
/**
* Evaluates the payload using {@link XPathExpression#evaluateAsString(Node)}
*
* @throws MessagingException if the {@link XPathExpression} evaluates to
* an empty string
*/
@Override
protected List<Object> getChannelIndicatorList(Message<?> message) {
List<Object> channels = new ArrayList<Object>();
Node node = getConverter().convertToNode(message.getPayload());
String result = getXPathExpression().evaluateAsString(node);
if (result == null || "".equals(result)) {
return null;
} else {
channels.add(result);
}
return channels;
}
}

View File

@@ -387,7 +387,6 @@
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="multi-channel" type="xsd:string" default="false"/>
<xsd:attribute name="channel-resolver" type="xsd:string" use="optional">
<xsd:annotation>
<xsd:appinfo>