INT-1156 initial commit for XPathTransformerParser and corresponding XSD updates

This commit is contained in:
Mark Fisher
2010-06-08 18:19:58 +00:00
parent b40d6277c4
commit 0afa84bdcb
5 changed files with 218 additions and 1 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* 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.
@@ -20,6 +20,7 @@ import org.springframework.integration.config.xml.AbstractIntegrationNamespaceHa
/**
* @author Jonas Partner
* @author Mark Fisher
*/
public class IntegrationXmlNamespaceHandler extends AbstractIntegrationNamespaceHandler {
@@ -27,6 +28,7 @@ public class IntegrationXmlNamespaceHandler extends AbstractIntegrationNamespace
registerBeanDefinitionParser("marshalling-transformer", new MarshallingTransformerParser());
registerBeanDefinitionParser("unmarshalling-transformer", new UnmarshallingTransformerParser());
registerBeanDefinitionParser("xslt-transformer", new XsltPayloadTransformerParser());
registerBeanDefinitionParser("xpath-transformer", new XPathTransformerParser());
registerBeanDefinitionParser("xpath-router", new XPathRouterParser());
registerBeanDefinitionParser("xpath-selector", new XPathSelectorParser());
registerBeanDefinitionParser("xpath-expression", new XPathExpressionParser());

View File

@@ -0,0 +1,49 @@
/*
* 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.config;
import org.w3c.dom.Element;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.config.xml.AbstractTransformerParser;
import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* Parser for the 'xpath-transformer' element.
*
* @author Mark Fisher
* @since 2.0
*/
public class XPathTransformerParser extends AbstractTransformerParser {
@Override
protected String getTransformerClassName() {
return "org.springframework.integration.xml.transformer.XPathTransformer";
}
@Override
protected void parseTransformer(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
String expression = element.getAttribute("expression");
Assert.isTrue(StringUtils.hasText(expression), "The 'expression' attribute is required.");
builder.addConstructorArgValue(expression);
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "evaluation-type");
}
}

View File

@@ -157,6 +157,43 @@
</xsd:complexType>
</xsd:element>
<xsd:element name="xpath-transformer">
<xsd:complexType>
<xsd:annotation>
<xsd:documentation>
Defines an XPath transformer.
</xsd:documentation>
</xsd:annotation>
<xsd:complexContent>
<xsd:extension base="inputOutputEndpoint">
<xsd:attribute name="expression" type="xsd:string" use="required">
<xsd:annotation>
<xsd:documentation>
The XPath expression to be evaluated against the input Message's payload.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="evaluation-type" default="STRING_RESULT">
<xsd:annotation>
<xsd:documentation>
The result type expected from the XPath evaluation. This will be the payload type of the output Message.
</xsd:documentation>
</xsd:annotation>
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="BOOLEAN_RESULT"/>
<xsd:enumeration value="NODE_RESULT"/>
<xsd:enumeration value="NODE_LIST_RESULT"/>
<xsd:enumeration value="NUMBER_RESULT"/>
<xsd:enumeration value="STRING_RESULT"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="xpath-router">
<xsd:complexType>
<xsd:annotation>

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration/xml"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:si="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/xml
http://www.springframework.org/schema/integration/xml/spring-integration-xml.xsd">
<si:channel id="output">
<si:queue/>
</si:channel>
<xpath-transformer input-channel="defaultInput" expression="/person/@name" output-channel="output"/>
<xpath-transformer input-channel="numberInput" expression="/person/@age" evaluation-type="NUMBER_RESULT" output-channel="output"/>
<xpath-transformer input-channel="booleanInput" expression="/person/@married = 'true'" evaluation-type="BOOLEAN_RESULT" output-channel="output"/>
<xpath-transformer input-channel="nodeInput" expression="/person/@age" evaluation-type="NODE_RESULT" output-channel="output"/>
<xpath-transformer input-channel="nodeListInput" expression="/person/@*" evaluation-type="NODE_LIST_RESULT" output-channel="output"/>
</beans:beans>

View File

@@ -0,0 +1,102 @@
/*
* 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.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.w3c.dom.Node;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.channel.PollableChannel;
import org.springframework.integration.core.Message;
import org.springframework.integration.core.MessageChannel;
import org.springframework.integration.message.MessageBuilder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Mark Fisher
* @since 2.0
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class XPathTransformerParserTests {
@Autowired
private MessageChannel defaultInput;
@Autowired
private MessageChannel numberInput;
@Autowired
private MessageChannel booleanInput;
@Autowired
private MessageChannel nodeInput;
@Autowired
private MessageChannel nodeListInput;
@Autowired
private PollableChannel output;
private final Message<?> message = MessageBuilder.withPayload("<person name='John Doe' age='42' married='true'/>").build();
@Test
public void stringResultByDefault() {
this.defaultInput.send(message);
assertEquals("John Doe", output.receive(0).getPayload());
}
@Test
public void numberResult() {
this.numberInput.send(message);
assertEquals(new Double(42), output.receive(0).getPayload());
}
@Test
public void booleanResult() {
this.booleanInput.send(message);
assertEquals(Boolean.TRUE, output.receive(0).getPayload());
}
@Test
public void nodeResult() {
this.nodeInput.send(message);
Object payload = output.receive(0).getPayload();
assertTrue(payload instanceof Node);
Node node = (Node) payload;
assertEquals("42", node.getTextContent());
}
@Test
@SuppressWarnings("unchecked")
public void nodeListResult() {
this.nodeListInput.send(message);
Object payload = output.receive(0).getPayload();
assertTrue(List.class.isAssignableFrom(payload.getClass()));
List<Node> nodeList = (List<Node>) payload;
assertEquals(3, nodeList.size());
}
}