Namespace support for XPathRouter INT-300
This commit is contained in:
@@ -29,6 +29,7 @@ public class IntegrationXmlNamespaceHandler extends NamespaceHandlerSupport {
|
||||
registerBeanDefinitionParser("marshalling-transformer", new XmlMarshallingTransformerParser());
|
||||
registerBeanDefinitionParser("unmarshalling-transformer", new XmlUnmarshallingTransformerParser());
|
||||
registerBeanDefinitionParser("xslt-transformer", new XsltPayloadTransformerParser());
|
||||
registerBeanDefinitionParser("xpath-router", new XPathRouterParser());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* 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.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.integration.ConfigurationException;
|
||||
import org.springframework.integration.router.MultiChannelRouter;
|
||||
import org.springframework.integration.router.SingleChannelRouter;
|
||||
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 {
|
||||
|
||||
@Override
|
||||
protected boolean shouldGenerateId() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldGenerateIdAsFallback() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
|
||||
|
||||
boolean multiChannel = Boolean.parseBoolean(element.getAttribute("multi-channel"));
|
||||
boolean resolutionRequired = Boolean.parseBoolean(element.getAttribute("resolution-required"));
|
||||
String xPathExpression = element.getAttribute("xpath-expression");
|
||||
String xPathExpressionRef = element.getAttribute("xpath-expression-ref");
|
||||
if ((StringUtils.hasText(xPathExpression) && StringUtils.hasText(xPathExpressionRef))
|
||||
|| (!StringUtils.hasText(xPathExpression) && !StringUtils.hasText(xPathExpressionRef))) {
|
||||
throw new ConfigurationException("Exactl one of xpath-expression or xpath-expression-ref is required");
|
||||
}
|
||||
|
||||
BeanDefinitionBuilder resolverDefinitionBuilder = null;
|
||||
if (multiChannel) {
|
||||
builder.getBeanDefinition().setBeanClass(MultiChannelRouter.class);
|
||||
resolverDefinitionBuilder = BeanDefinitionBuilder
|
||||
.genericBeanDefinition(XPathMultiChannelNameResolver.class);
|
||||
}
|
||||
else {
|
||||
builder.getBeanDefinition().setBeanClass(SingleChannelRouter.class);
|
||||
resolverDefinitionBuilder = BeanDefinitionBuilder
|
||||
.genericBeanDefinition(XPathSingleChannelNameResolver.class);
|
||||
}
|
||||
|
||||
if (StringUtils.hasText(xPathExpression)) {
|
||||
XPathExpression expression = XPathExpressionFactory.createXPathExpression(xPathExpression);
|
||||
resolverDefinitionBuilder.addConstructorArgValue(expression);
|
||||
}
|
||||
else {
|
||||
resolverDefinitionBuilder.addConstructorArgReference(xPathExpressionRef);
|
||||
}
|
||||
|
||||
builder.getBeanDefinition().getPropertyValues().addPropertyValue("resolutionRequired", resolutionRequired);
|
||||
builder.getBeanDefinition().getPropertyValues().addPropertyValue("channelNameResolver",
|
||||
resolverDefinitionBuilder.getBeanDefinition());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -38,7 +38,7 @@
|
||||
</xsd:attribute>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
|
||||
<xsd:element name="unmarshalling-transformer">
|
||||
<xsd:complexType>
|
||||
<xsd:annotation>
|
||||
@@ -51,22 +51,46 @@
|
||||
use="required" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
|
||||
<xsd:element name="xslt-transformer">
|
||||
|
||||
|
||||
<xsd:element name="xslt-transformer">
|
||||
<xsd:complexType>
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Defines a XML unmarshalling transformer.
|
||||
Defines a XML XSLT transformer.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:attribute name="id" type="xsd:string" use="required" />
|
||||
<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:complexType>
|
||||
</xsd:element>
|
||||
|
||||
|
||||
|
||||
<xsd:element name="xpath-router">
|
||||
<xsd:complexType>
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Defines a 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:attribute name="resolution-required" type="xsd:boolean"
|
||||
default="true" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
|
||||
</xsd:schema>
|
||||
@@ -20,6 +20,11 @@ import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessagingException;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jonas Partner
|
||||
*
|
||||
*/
|
||||
public class AbstractXPathChannelNameResolver {
|
||||
|
||||
protected Node extractNode(Message<?> message) {
|
||||
|
||||
@@ -26,36 +26,40 @@ import org.springframework.xml.xpath.XPathExpression;
|
||||
import org.w3c.dom.DOMException;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jonas Partner
|
||||
*
|
||||
*/
|
||||
public class XPathMultiChannelNameResolver extends AbstractXPathChannelNameResolver implements MultiChannelNameResolver {
|
||||
|
||||
private final XPathExpression xPathExpression;
|
||||
|
||||
|
||||
private NodeMapper nodeMapper = new TextContentNodeMapper();
|
||||
|
||||
public XPathMultiChannelNameResolver(XPathExpression xPathExpression){
|
||||
|
||||
public XPathMultiChannelNameResolver(XPathExpression xPathExpression) {
|
||||
Assert.notNull("XPAthExpression must be provided");
|
||||
this.xPathExpression = xPathExpression;
|
||||
}
|
||||
|
||||
public void setNodeMapper(NodeMapper nodeMapper){
|
||||
Assert.notNull(nodeMapper,"NodeMapper can not be null");
|
||||
|
||||
public void setNodeMapper(NodeMapper nodeMapper) {
|
||||
Assert.notNull(nodeMapper, "NodeMapper can not be null");
|
||||
this.nodeMapper = nodeMapper;
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public String[] resolve(Message<?> message) {
|
||||
Node node =extractNode(message);
|
||||
Node node = extractNode(message);
|
||||
List channelNamesList = xPathExpression.evaluate(node, nodeMapper);
|
||||
return (String[])channelNamesList.toArray(new String[channelNamesList.size()]);
|
||||
return (String[]) channelNamesList.toArray(new String[channelNamesList.size()]);
|
||||
}
|
||||
|
||||
private static class TextContentNodeMapper implements NodeMapper{
|
||||
|
||||
private static class TextContentNodeMapper implements NodeMapper {
|
||||
|
||||
public Object mapNode(Node node, int nodeNum) throws DOMException {
|
||||
return node.getTextContent();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -17,27 +17,29 @@
|
||||
package org.springframework.integration.xml.router;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessagingException;
|
||||
import org.springframework.integration.router.ChannelNameResolver;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.xml.xpath.XPathExpression;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jonas Partner
|
||||
*
|
||||
*/
|
||||
public class XPathSingleChannelNameResolver extends AbstractXPathChannelNameResolver implements ChannelNameResolver {
|
||||
|
||||
private final XPathExpression xPathExpression;
|
||||
|
||||
|
||||
public XPathSingleChannelNameResolver(XPathExpression xPathExpression){
|
||||
Assert.notNull("XPAthExpression must be provided");
|
||||
|
||||
public XPathSingleChannelNameResolver(XPathExpression xPathExpression) {
|
||||
Assert.notNull("XPathExpression must be provided");
|
||||
this.xPathExpression = xPathExpression;
|
||||
}
|
||||
|
||||
|
||||
public String resolve(Message<?> message) {
|
||||
Node node = extractNode(message);
|
||||
System.out.println(xPathExpression.evaluateAsString(node));
|
||||
return xPathExpression.evaluateAsString(node);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user