diff --git a/spring-integration-xml/src/main/java/org/springframework/integration/xml/config/IntegrationXmlNamespaceHandler.java b/spring-integration-xml/src/main/java/org/springframework/integration/xml/config/IntegrationXmlNamespaceHandler.java index 2ce1d0b910..977aa159cd 100644 --- a/spring-integration-xml/src/main/java/org/springframework/integration/xml/config/IntegrationXmlNamespaceHandler.java +++ b/spring-integration-xml/src/main/java/org/springframework/integration/xml/config/IntegrationXmlNamespaceHandler.java @@ -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()); diff --git a/spring-integration-xml/src/main/java/org/springframework/integration/xml/config/XPathTransformerParser.java b/spring-integration-xml/src/main/java/org/springframework/integration/xml/config/XPathTransformerParser.java new file mode 100644 index 0000000000..629c265c60 --- /dev/null +++ b/spring-integration-xml/src/main/java/org/springframework/integration/xml/config/XPathTransformerParser.java @@ -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"); + } + +} diff --git a/spring-integration-xml/src/main/resources/org/springframework/integration/xml/config/spring-integration-xml-2.0.xsd b/spring-integration-xml/src/main/resources/org/springframework/integration/xml/config/spring-integration-xml-2.0.xsd index 869b6fd82e..10afde02a7 100644 --- a/spring-integration-xml/src/main/resources/org/springframework/integration/xml/config/spring-integration-xml-2.0.xsd +++ b/spring-integration-xml/src/main/resources/org/springframework/integration/xml/config/spring-integration-xml-2.0.xsd @@ -157,6 +157,43 @@ + + + + + Defines an XPath transformer. + + + + + + + + The XPath expression to be evaluated against the input Message's payload. + + + + + + + The result type expected from the XPath evaluation. This will be the payload type of the output Message. + + + + + + + + + + + + + + + + + diff --git a/spring-integration-xml/src/test/java/org/springframework/integration/xml/config/XPathTransformerParserTests-context.xml b/spring-integration-xml/src/test/java/org/springframework/integration/xml/config/XPathTransformerParserTests-context.xml new file mode 100644 index 0000000000..0b7b7caa91 --- /dev/null +++ b/spring-integration-xml/src/test/java/org/springframework/integration/xml/config/XPathTransformerParserTests-context.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + diff --git a/spring-integration-xml/src/test/java/org/springframework/integration/xml/config/XPathTransformerParserTests.java b/spring-integration-xml/src/test/java/org/springframework/integration/xml/config/XPathTransformerParserTests.java new file mode 100644 index 0000000000..f2c95fc047 --- /dev/null +++ b/spring-integration-xml/src/test/java/org/springframework/integration/xml/config/XPathTransformerParserTests.java @@ -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("").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 nodeList = (List) payload; + assertEquals(3, nodeList.size()); + } + +}