INT-300 XPathRouter
Name space support to follow
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -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("<doc type=\"one\" />");
|
||||
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("<doc type=\"one\"><book>bOne</book><book>bTwo</book></doc>");
|
||||
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"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -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("<doc type=\"one\" />");
|
||||
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"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user