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

@@ -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 {