refactored namespace support for xpath-router and xpath-selector

This commit is contained in:
Jonas Partner
2008-10-01 17:21:32 +00:00
parent 13100735e0
commit 7843007930
10 changed files with 74 additions and 189 deletions

View File

@@ -1,93 +0,0 @@
/*
* 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

@@ -36,7 +36,7 @@ import org.w3c.dom.NodeList;
*
*/
public class XPathExpressionParser extends AbstractSingleBeanDefinitionParser {
@Override
protected boolean shouldGenerateId() {
return false;
@@ -47,15 +47,17 @@ public class XPathExpressionParser extends AbstractSingleBeanDefinitionParser {
return true;
}
@SuppressWarnings("unchecked")
@Override
protected Class getBeanClass(Element element) {
return XPathExpressionFactory.class;
}
@SuppressWarnings("unchecked")
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
String strXpathExpression = element.getAttribute("xpath-expression");
String strXpathExpression = element.getAttribute("expression");
String strXpathExpressionPrefix = element.getAttribute("ns-prefix");
String strXpathExpressionNamespace = element.getAttribute("ns-uri");
String nameSpaceMapRef = element.getAttribute("namespace-map");
@@ -105,6 +107,7 @@ public class XPathExpressionParser extends AbstractSingleBeanDefinitionParser {
@SuppressWarnings("unchecked")
protected Map parseNamespaceMapElement(Element element, ParserContext parserContext, BeanDefinition parentDefinition) {
BeanDefinitionParserDelegate beanParser = new BeanDefinitionParserDelegate(parserContext.getReaderContext());
beanParser.initDefaults(element.getOwnerDocument().getDocumentElement());

View File

@@ -16,24 +16,25 @@
package org.springframework.integration.xml.config;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
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.Assert;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
/**
* @author Jonas Partner
*/
public class XPathRouterParser extends AbstractSingleBeanDefinitionParser {
private XPathExpressionBeanDefintionBuilder xpathBuilder = new XPathExpressionBeanDefintionBuilder();
private XPathExpressionParser xpathParser = new XPathExpressionParser();
@Override
protected boolean shouldGenerateId() {
return false;
@@ -48,18 +49,15 @@ public class XPathRouterParser extends AbstractSingleBeanDefinitionParser {
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");
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))) {
NodeList xPathExpressionNodes = element.getElementsByTagNameNS(element.getNamespaceURI(), "xpath-expression");
Assert.isTrue(xPathExpressionNodes.getLength() < 2, "Only one xpath-expression child can be specified");
boolean xPathExpressionChildPresent = xPathExpressionNodes.getLength() == 1;
boolean xPathReferencePresent = StringUtils.hasText(xPathExpressionRef);
if ((xPathExpressionChildPresent && xPathReferencePresent)
|| (!xPathExpressionChildPresent && !xPathReferencePresent)) {
throw new ConfigurationException("Exactly one of 'xpath-expression' or 'xpath-expression-ref' is required.");
}
@@ -69,16 +67,15 @@ public class XPathRouterParser extends AbstractSingleBeanDefinitionParser {
else {
builder.getBeanDefinition().setBeanClass(XPathSingleChannelNameResolver.class);
}
if (StringUtils.hasText(xPathExpression)) {
AbstractBeanDefinition xPathExpressionBeanDefinition = xpathBuilder.handleXpathExpression(element, parserContext);
String xpathExpressionBeanName = BeanDefinitionReaderUtils.registerWithGeneratedName(
xPathExpressionBeanDefinition, parserContext.getRegistry());
builder.addConstructorArgReference(xpathExpressionBeanName);
}
else {
if (xPathExpressionChildPresent) {
BeanDefinition beanDefinition = xpathParser.parse((Element) xPathExpressionNodes.item(0), parserContext);
builder.addConstructorArgValue(beanDefinition);
} else {
builder.addConstructorArgReference(xPathExpressionRef);
}
}
}

View File

@@ -15,9 +15,8 @@
*/
package org.springframework.integration.xml.config;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
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;
@@ -26,15 +25,16 @@ import org.springframework.integration.xml.selector.StringValueTestXPathMessageS
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
/**
*
* @author Jonas Partner
*
*
*/
public class XPathSelectorParser extends AbstractSingleBeanDefinitionParser {
private XPathExpressionBeanDefintionBuilder xpathBuilder = new XPathExpressionBeanDefintionBuilder();
private XPathExpressionParser xpathParser = new XPathExpressionParser();
@Override
protected boolean shouldGenerateId() {
@@ -50,40 +50,40 @@ public class XPathSelectorParser extends AbstractSingleBeanDefinitionParser {
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))) {
NodeList xPathExpressionNodes = element.getElementsByTagNameNS(element.getNamespaceURI(), "xpath-expression");
Assert.isTrue(xPathExpressionNodes.getLength() < 2, "Only one xpath-expression child can be specified");
boolean xPathExpressionChildPresent = xPathExpressionNodes.getLength() == 1;
boolean xPathReferencePresent = StringUtils.hasText(xPathExpressionRef);
if ((xPathExpressionChildPresent && xPathReferencePresent)
|| (!xPathExpressionChildPresent && !xPathReferencePresent)) {
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");
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");
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 {
throw new ConfigurationException("Unrecognised value: " + evaluationType
+ " for evaluation-result-type only boolean or string supported");
}
if (xPathExpressionChildPresent) {
BeanDefinition beanDefinition = xpathParser.parse((Element) xPathExpressionNodes.item(0), parserContext);
builder.addConstructorArgValue(beanDefinition);
}
else {
builder.addConstructorArgReference(xPathExpressionRef);

View File

@@ -83,6 +83,7 @@
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="xpath-router">
<xsd:complexType>
<xsd:annotation>
@@ -91,23 +92,16 @@
</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:element ref="beans:map" minOccurs="0" maxOccurs="1"/>
<xsd:element ref="xpath-expression" maxOccurs="1" minOccurs="0" />
</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>
@@ -116,16 +110,10 @@
</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:element ref="beans:map" minOccurs="0" maxOccurs="1"/>
<xsd:element ref="xpath-expression" maxOccurs="1" minOccurs="0" />
</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"
<xsd:attribute name="xpath-expression-ref" type="xsd:string"
use="optional" />
<xsd:attribute name="evaluation-result-type" use="required">
<xsd:simpleType>
@@ -135,8 +123,6 @@
</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" />
@@ -154,7 +140,7 @@
<xsd:element ref="beans:map" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:string" use="optional" />
<xsd:attribute name="xpath-expression" type="xsd:string"
<xsd:attribute name="expression" type="xsd:string"
use="optional" />
<xsd:attribute name="ns-prefix" type="xsd:string"
use="optional" />