XPathExpression namespace support
This commit is contained in:
@@ -31,6 +31,8 @@ public class IntegrationXmlNamespaceHandler extends NamespaceHandlerSupport {
|
||||
registerBeanDefinitionParser("xslt-transformer", new XsltPayloadTransformerParser());
|
||||
registerBeanDefinitionParser("xpath-router", new XPathRouterParser());
|
||||
registerBeanDefinitionParser("xpath-selector", new XPathSelectorParser());
|
||||
registerBeanDefinitionParser("xpath-expression", new XPathExpressionParser());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
* 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.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
|
||||
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;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jonas Partner
|
||||
*
|
||||
*/
|
||||
public class XPathExpressionParser extends AbstractSingleBeanDefinitionParser {
|
||||
|
||||
@Override
|
||||
protected boolean shouldGenerateId() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldGenerateIdAsFallback() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
protected Class getBeanClass(Element element) {
|
||||
return XPathExpressionFactory.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
|
||||
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");
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -143,6 +143,29 @@
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
<xsd:element name="xpath-expression">
|
||||
<xsd:complexType>
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Defines an XPath expression.
|
||||
</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="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="namespace-map" 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"
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* 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.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.BeanDefinitionStoreException;
|
||||
import org.springframework.integration.xml.util.XmlTestUtil;
|
||||
import org.springframework.xml.xpath.XPathExpression;
|
||||
|
||||
public class XPathExpressionParserTests {
|
||||
|
||||
@Test
|
||||
public void testSimpleStringExpression() throws Exception {
|
||||
String xmlDoc = "<si-xml:xpath-expression id='xpathExpression' xpath-expression='/name' />";
|
||||
XPathExpression xPathExpression = getXPathExpression(xmlDoc);
|
||||
assertEquals("outputOne",xPathExpression.evaluateAsString(XmlTestUtil.getDocumentForString("<name>outputOne</name>")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNamespacedStringExpression() throws Exception {
|
||||
String xmlDoc = "<si-xml:xpath-expression id='xpathExpression' xpath-expression='/ns1:name' ns-prefix='ns1' ns-uri='www.example.org' />";
|
||||
XPathExpression xPathExpression = getXPathExpression(xmlDoc);
|
||||
assertEquals("outputOne",xPathExpression.evaluateAsString(XmlTestUtil.getDocumentForString("<ns1:name xmlns:ns1='www.example.org'>outputOne</ns1:name>")));
|
||||
assertEquals("",xPathExpression.evaluateAsString(XmlTestUtil.getDocumentForString("<name>outputOne</name>")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringExpressionWithNamespaceMapReference() throws Exception {
|
||||
StringBuffer xmlDoc = new StringBuffer("<si-xml:xpath-expression id='xpathExpression' xpath-expression='/ns1:name' namespace-map='myNamespaces' />");
|
||||
xmlDoc.append("<util:map id='myNamespaces'><entry key='ns1' value='www.example.org' /></util:map>");
|
||||
XPathExpression xPathExpression = getXPathExpression(xmlDoc.toString());
|
||||
assertEquals("outputOne",xPathExpression.evaluateAsString(XmlTestUtil.getDocumentForString("<ns1:name xmlns:ns1='www.example.org'>outputOne</ns1:name>")));
|
||||
assertEquals("",xPathExpression.evaluateAsString(XmlTestUtil.getDocumentForString("<name>outputOne</name>")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringExpressionWithNamespaceInnerBean() throws Exception {
|
||||
StringBuffer xmlDoc = new StringBuffer("<si-xml:xpath-expression id='xpathExpression' xpath-expression='/ns1:name' >");
|
||||
xmlDoc.append("<map><entry key='ns1' value='www.example.org' /></map>").append("</si-xml:xpath-expression>");
|
||||
XPathExpression xPathExpression = getXPathExpression(xmlDoc.toString());
|
||||
assertEquals("outputOne",xPathExpression.evaluateAsString(XmlTestUtil.getDocumentForString("<ns1:name xmlns:ns1='www.example.org'>outputOne</ns1:name>")));
|
||||
assertEquals("",xPathExpression.evaluateAsString(XmlTestUtil.getDocumentForString("<name>outputOne</name>")));
|
||||
}
|
||||
|
||||
|
||||
@Test(expected=BeanDefinitionStoreException.class)
|
||||
public void testNamespacePrefixButNoUri() throws Exception {
|
||||
String xmlDoc = "<si-xml:xpath-expression id='xpathExpression' xpath-expression='/ns1:name' ns-prefix='ns1' />";
|
||||
XPathExpression xPathExpression = getXPathExpression(xmlDoc);
|
||||
assertEquals("outputOne",xPathExpression.evaluateAsString(XmlTestUtil.getDocumentForString("<ns1:name xmlns:ns1='www.example.org'>outputOne</ns1:name>")));
|
||||
assertEquals("",xPathExpression.evaluateAsString(XmlTestUtil.getDocumentForString("<name>outputOne</name>")));
|
||||
|
||||
}
|
||||
|
||||
public XPathExpression getXPathExpression(String contextXml){
|
||||
TestXmlApplicationContext ctx = TestXmlApplicationContextHelper.getTestAppContext(contextXml);
|
||||
return (XPathExpression) ctx.getBean("xpathExpression");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user