diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/IntegrationXmlNamespaceHandler.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/IntegrationXmlNamespaceHandler.java index f68cae12e5..db4be6038d 100644 --- a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/IntegrationXmlNamespaceHandler.java +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/IntegrationXmlNamespaceHandler.java @@ -30,6 +30,7 @@ public class IntegrationXmlNamespaceHandler extends NamespaceHandlerSupport { registerBeanDefinitionParser("xpath-router", new XPathRouterParser()); registerBeanDefinitionParser("xpath-selector", new XPathSelectorParser()); registerBeanDefinitionParser("xpath-expression", new XPathExpressionParser()); + registerBeanDefinitionParser("xpath-splitter", new XPathMessageSplitterParser()); } } diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XPathMessageSplitterParser.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XPathMessageSplitterParser.java new file mode 100644 index 0000000000..8a56a52aa2 --- /dev/null +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XPathMessageSplitterParser.java @@ -0,0 +1,76 @@ +/* + * Copyright 2002-2008 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.config.BeanDefinition; +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; +import org.springframework.beans.factory.xml.ParserContext; +import org.springframework.integration.xml.splitter.XPathMessageSplitter; +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 XPathMessageSplitterParser extends AbstractSingleBeanDefinitionParser { + + @Override + protected Class getBeanClass(Element element) { + return XPathMessageSplitter.class; + } + + private XPathExpressionParser xpathParser = new XPathExpressionParser(); + + @Override + protected boolean shouldGenerateId() { + return false; + } + + @Override + protected boolean shouldGenerateIdAsFallback() { + return true; + } + + @Override + protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { + + boolean multiChannel = Boolean.parseBoolean(element.getAttribute("multi-channel")); + String xPathExpressionRef = element.getAttribute("xpath-expression-ref"); + + 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); + + boolean exactlyOneSpecified = !(xPathExpressionChildPresent && xPathReferencePresent) && (!xPathExpressionChildPresent ^ !xPathReferencePresent); + + Assert.isTrue( exactlyOneSpecified,"Exactly one of 'xpath-expression' or 'xpath-expression-ref' is required."); + + if (xPathExpressionChildPresent) { + BeanDefinition beanDefinition = xpathParser.parse((Element) xPathExpressionNodes.item(0), parserContext); + builder.addConstructorArgValue(beanDefinition); + } else { + builder.addConstructorArgReference(xPathExpressionRef); + } + } + + + +} diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/spring-integration-xml-1.0.xsd b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/spring-integration-xml-1.0.xsd index c30bb9ede2..27fbdb0d40 100644 --- a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/spring-integration-xml-1.0.xsd +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/spring-integration-xml-1.0.xsd @@ -140,6 +140,26 @@ + + + + + Defines an XPath splitter. + + + + + + + + + + + + diff --git a/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/XPathMessageSplitterParserTests.java b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/XPathMessageSplitterParserTests.java new file mode 100644 index 0000000000..8e2a401379 --- /dev/null +++ b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/XPathMessageSplitterParserTests.java @@ -0,0 +1,51 @@ +/* + * Copyright 2002-2008 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.integration.channel.QueueChannel; +import org.springframework.integration.message.GenericMessage; +import org.springframework.integration.xml.splitter.XPathMessageSplitter; +import org.springframework.integration.xml.util.XmlTestUtil; +import org.springframework.test.context.ContextConfiguration; +import org.w3c.dom.Document; + +/** + * @author Jonas Partner + */ +@ContextConfiguration +public class XPathMessageSplitterParserTests { + + + @Test + public void testSimpleStringExpression() throws Exception { + Document doc = XmlTestUtil.getDocumentForString("BobJohn"); + GenericMessage docMessage = new GenericMessage(doc); + + TestXmlApplicationContext ctx = TestXmlApplicationContextHelper.getTestAppContext(""); + XPathMessageSplitter splitter = (XPathMessageSplitter) ctx.getBean("splitter"); + + QueueChannel queueChannel = new QueueChannel(10); + splitter.setOutputChannel(queueChannel); + splitter.onMessage(docMessage); + assertEquals("Wrong number of split messages ", 2, queueChannel.getMesssageCount()); + + } + +} diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/channel/QueueChannel.java b/org.springframework.integration/src/main/java/org/springframework/integration/channel/QueueChannel.java index d649863f64..3cda730c17 100644 --- a/org.springframework.integration/src/main/java/org/springframework/integration/channel/QueueChannel.java +++ b/org.springframework.integration/src/main/java/org/springframework/integration/channel/QueueChannel.java @@ -122,4 +122,12 @@ public class QueueChannel extends AbstractPollableChannel { return purgedMessages; } + public int getMesssageCount(){ + return this.queue.size(); + } + + public int getRemainingCapacity(){ + return this.queue.remainingCapacity(); + } + }