diff --git a/org.springframework.integration.xml/.classpath b/org.springframework.integration.xml/.classpath index 2b219848e9..5a938f6364 100644 --- a/org.springframework.integration.xml/.classpath +++ b/org.springframework.integration.xml/.classpath @@ -12,5 +12,6 @@ + diff --git a/org.springframework.integration.xml/ivy.xml b/org.springframework.integration.xml/ivy.xml index 643bd48267..1cc7f0f701 100644 --- a/org.springframework.integration.xml/ivy.xml +++ b/org.springframework.integration.xml/ivy.xml @@ -20,7 +20,8 @@ - + + diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/router/AbstractXPathChannelNameResolver.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/router/AbstractXPathChannelNameResolver.java new file mode 100644 index 0000000000..94fd986c96 --- /dev/null +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/router/AbstractXPathChannelNameResolver.java @@ -0,0 +1,31 @@ +/* + * 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.router; + +import org.springframework.integration.message.Message; +import org.springframework.integration.message.MessagingException; +import org.w3c.dom.Node; + +public class AbstractXPathChannelNameResolver { + + protected Node extractNode(Message message) { + if (!Node.class.isAssignableFrom(message.getPayload().getClass())) { + throw new MessagingException(message, "Payload does not implement org.w3c.dom.Node so can not be evaluated"); + } + return (Node) message.getPayload(); + } +} diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/router/XPathMultiChannelNameResolver.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/router/XPathMultiChannelNameResolver.java new file mode 100644 index 0000000000..3f89de077e --- /dev/null +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/router/XPathMultiChannelNameResolver.java @@ -0,0 +1,61 @@ +/* + * 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.router; + +import java.util.List; + +import org.springframework.integration.message.Message; +import org.springframework.integration.router.MultiChannelNameResolver; +import org.springframework.util.Assert; +import org.springframework.xml.xpath.NodeMapper; +import org.springframework.xml.xpath.XPathExpression; +import org.w3c.dom.DOMException; +import org.w3c.dom.Node; + +public class XPathMultiChannelNameResolver extends AbstractXPathChannelNameResolver implements MultiChannelNameResolver { + + private final XPathExpression xPathExpression; + + private NodeMapper nodeMapper = new TextContentNodeMapper(); + + public XPathMultiChannelNameResolver(XPathExpression xPathExpression){ + Assert.notNull("XPAthExpression must be provided"); + this.xPathExpression = xPathExpression; + } + + public void setNodeMapper(NodeMapper nodeMapper){ + Assert.notNull(nodeMapper,"NodeMapper can not be null"); + this.nodeMapper = nodeMapper; + } + + @SuppressWarnings("unchecked") + public String[] resolve(Message message) { + Node node =extractNode(message); + List channelNamesList = xPathExpression.evaluate(node, nodeMapper); + return (String[])channelNamesList.toArray(new String[channelNamesList.size()]); + } + + private static class TextContentNodeMapper implements NodeMapper{ + + public Object mapNode(Node node, int nodeNum) throws DOMException { + return node.getTextContent(); + } + + } + + +} diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/router/XPathSingleChannelNameResolver.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/router/XPathSingleChannelNameResolver.java new file mode 100644 index 0000000000..58eefb6f0c --- /dev/null +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/router/XPathSingleChannelNameResolver.java @@ -0,0 +1,43 @@ +/* + * 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.router; + +import org.springframework.integration.message.Message; +import org.springframework.integration.message.MessagingException; +import org.springframework.integration.router.ChannelNameResolver; +import org.springframework.util.Assert; +import org.springframework.xml.xpath.XPathExpression; +import org.w3c.dom.Node; + +public class XPathSingleChannelNameResolver extends AbstractXPathChannelNameResolver implements ChannelNameResolver { + + private final XPathExpression xPathExpression; + + + public XPathSingleChannelNameResolver(XPathExpression xPathExpression){ + Assert.notNull("XPAthExpression must be provided"); + this.xPathExpression = xPathExpression; + } + + + public String resolve(Message message) { + Node node = extractNode(message); + return xPathExpression.evaluateAsString(node); + } + + +} diff --git a/org.springframework.integration.xml/src/test/java/org/springframework/integration/router/XPathMultiChannelNameResolverTests.java b/org.springframework.integration.xml/src/test/java/org/springframework/integration/router/XPathMultiChannelNameResolverTests.java new file mode 100644 index 0000000000..1a59697442 --- /dev/null +++ b/org.springframework.integration.xml/src/test/java/org/springframework/integration/router/XPathMultiChannelNameResolverTests.java @@ -0,0 +1,65 @@ +/* + * 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.router; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.springframework.integration.message.GenericMessage; +import org.springframework.integration.message.MessagingException; +import org.springframework.integration.message.StringMessage; +import org.springframework.integration.xml.router.XPathMultiChannelNameResolver; +import org.springframework.integration.xml.util.XmlTestUtil; +import org.springframework.xml.xpath.XPathExpression; +import org.springframework.xml.xpath.XPathExpressionFactory; +import org.w3c.dom.Document; + +public class XPathMultiChannelNameResolverTests { + + @Test + public void testSimpleSingleeAttribute() throws Exception { + Document doc = XmlTestUtil.getDocumentForString(""); + XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type"); + XPathMultiChannelNameResolver resolver = new XPathMultiChannelNameResolver(expression); + String[] channelNames = resolver.resolve(new GenericMessage(doc)); + assertEquals("Wrong number of channels returend",1,channelNames.length); + assertEquals("Wrong channel name","one",channelNames[0]); + } + + @Test + public void testMultipleNodeValues() throws Exception { + Document doc = XmlTestUtil.getDocumentForString("bOnebTwo"); + XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/book"); + XPathMultiChannelNameResolver resolver = new XPathMultiChannelNameResolver(expression); + String[] channelNames = resolver.resolve(new GenericMessage(doc)); + assertEquals("Wrong number of channels returend",2,channelNames.length); + assertEquals("Wrong channel name","bOne",channelNames[0]); + assertEquals("Wrong channel name","bTwo",channelNames[1]); + } + + + @Test(expected=MessagingException.class) + public void testNonNodePayload() throws Exception { + XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type"); + XPathMultiChannelNameResolver resolver = new XPathMultiChannelNameResolver(expression); + resolver.resolve(new StringMessage("test")); + } + + + + +} diff --git a/org.springframework.integration.xml/src/test/java/org/springframework/integration/router/XPathSingleChannelNameResolverTests.java b/org.springframework.integration.xml/src/test/java/org/springframework/integration/router/XPathSingleChannelNameResolverTests.java new file mode 100644 index 0000000000..fffd7b9d23 --- /dev/null +++ b/org.springframework.integration.xml/src/test/java/org/springframework/integration/router/XPathSingleChannelNameResolverTests.java @@ -0,0 +1,53 @@ +/* + * 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.router; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.springframework.integration.message.GenericMessage; +import org.springframework.integration.message.MessagingException; +import org.springframework.integration.message.StringMessage; +import org.springframework.integration.xml.router.XPathSingleChannelNameResolver; +import org.springframework.integration.xml.util.XmlTestUtil; +import org.springframework.xml.xpath.XPathExpression; +import org.springframework.xml.xpath.XPathExpressionFactory; +import org.w3c.dom.Document; + +public class XPathSingleChannelNameResolverTests { + + @Test + public void testSimpleDocType() throws Exception { + Document doc = XmlTestUtil.getDocumentForString(""); + XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type"); + XPathSingleChannelNameResolver resolver = new XPathSingleChannelNameResolver(expression); + String channelName = resolver.resolve(new GenericMessage(doc)); + assertEquals("Wrong channel name","one",channelName); + } + + + @Test(expected=MessagingException.class) + public void testNonNodePayload() throws Exception { + XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type"); + XPathSingleChannelNameResolver resolver = new XPathSingleChannelNameResolver(expression); + resolver.resolve(new StringMessage("test")); + } + + + + +} diff --git a/org.springframework.integration.xml/template.mf b/org.springframework.integration.xml/template.mf index 0b78d8f73a..7a093e3c2e 100644 --- a/org.springframework.integration.xml/template.mf +++ b/org.springframework.integration.xml/template.mf @@ -8,7 +8,7 @@ Import-Template: org.springframework.core.*;version="[2.5.4.A, 3.0.0)", org.springframework.oxm;version="[1.5.1.A, 2.0.0)";resolution:=optional, org.springframework.util;version="[2.5.4.A, 3.0.0)", - org.springframework.xml.transform;version="[1.5.1.A, 2.0.0)" + org.springframework.xml.*;version="[1.5.1.A, 2.0.0)" Unversioned-Imports: org.w3c.dom, javax.xml.*,