public Traffic read(Class<? extends Traffic> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { Traffic traffic = new Traffic(); try { Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputMessage.getBody()); XPathExpression titlesXp = XPathExpressionFactory.createXPathExpression("/ResultSet/Result[@type='incident']/Title"); List<Node> titles = titlesXp.evaluateAsNodeList(document); XPathExpression descXp = XPathExpressionFactory.createXPathExpression("/ResultSet/Result[@type='incident']/Description"); List<Node> description = descXp.evaluateAsNodeList(document); int counter = 0; for (Node node : titles) { traffic.addIncident(((Element)node).getTextContent(), ((Element)description.get(counter++)).getTextContent()); } } catch (Exception e) { throw new HttpMessageConversionException("Failed to convert response to: " + clazz, e); } return traffic; }
public Object unmarshal(Source source) throws IOException, XmlMappingException { //this.writeXml(((DOMSource)source).getNode().getOwnerDocument()); DOMResult result = null; try { Transformer transformer = transformerFactory.newTransformer(); result = new DOMResult(); transformer.transform(source, result); } catch (Exception e) { throw new MarshallingFailureException("Failed to unmarshal SOAP Response", e); } Weather weather = new Weather(); String expression = xpathPrefix + "p:City"; String city = XPathExpressionFactory.createXPathExpression(expression, namespacePrefixes).evaluateAsString(result.getNode()); weather.setCity(city); expression = xpathPrefix + "p:State"; String state = XPathExpressionFactory.createXPathExpression(expression, namespacePrefixes).evaluateAsString(result.getNode()); weather.setState(state); expression = xpathPrefix + "p:Temperature"; String temperature = XPathExpressionFactory.createXPathExpression(expression, namespacePrefixes).evaluateAsString(result.getNode()); weather.setTemperature(temperature); expression = xpathPrefix + "p:Description"; String description = XPathExpressionFactory.createXPathExpression(expression, namespacePrefixes).evaluateAsString(result.getNode()); weather.setDescription(description); return weather; }
/** * Construct an instance based on the provided xpath expression and {@link XmlPayloadConverter}. * @param expression the xpath expression to evaluate. * @param converter the {@link XmlPayloadConverter} to use for document conversion. * @since 4.3.19 */ public XPathExpressionEvaluatingHeaderValueMessageProcessor(String expression, XmlPayloadConverter converter) { this(XPathExpressionFactory.createXPathExpression(expression), converter); }
/** * Create an {@link XPathTransformer} that will create an XPath expression from the given String * to be evaluated against converted inbound Message payloads. * * @param expression The expression. */ public XPathTransformer(String expression) { this.xpathExpression = XPathExpressionFactory.createXPathExpression(expression); }
/** * @param xPathExpression XPath expression as a String * @param namespaces Map of namespaces with prefixes as the Map keys */ public AbstractXPathMessageSelector(String xPathExpression, Map<String, String> namespaces) { this.xPathExpresion = XPathExpressionFactory.createXPathExpression(xPathExpression, namespaces); }
/** * @param xPathExpression XPath expression as a String */ public AbstractXPathMessageSelector(String xPathExpression) { this.xPathExpresion = XPathExpressionFactory.createXPathExpression(xPathExpression); }
/** * Create a router that uses an XPath expression. The expression may * contain zero or more namespace prefixes. * * @param expression the XPath expression as a String * @param namespaces map of namespaces with prefixes as the map keys */ public XPathRouter(String expression, Map<String, String> namespaces) { Assert.hasText(expression, "expression must not be empty"); this.xPathExpression = XPathExpressionFactory.createXPathExpression(expression, namespaces); }
/** * Create a router that uses an XPath expression with no namespaces. * For example '/one/@type' * * @param expression the XPath expression as a String */ public XPathRouter(String expression) { Assert.hasText(expression, "expression must not be empty"); this.xPathExpression = XPathExpressionFactory.createXPathExpression(expression); }
/** * @param xPathExpression XPath expression as a String * @param prefix namespace prefix * @param namespace namespace URI */ public AbstractXPathMessageSelector(String xPathExpression, String prefix, String namespace) { Map<String, String> namespaces = new HashMap<String, String>(); namespaces.put(prefix, namespace); this.xPathExpresion = XPathExpressionFactory.createXPathExpression(xPathExpression, namespaces); }
@Test public void testSimpleStringDoc() throws Exception { XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type"); XPathRouter router = new XPathRouter(expression); Object channelName = router.getChannelKeys(new GenericMessage<String>("<doc type='one' />")).toArray()[0]; assertEquals("Wrong channel name", "one", channelName); }
@Test public void testNodeWithXPathExpressionAsString() throws Exception { XPathExpression xpathExpression = XPathExpressionFactory.createXPathExpression("boolean(./three)"); BooleanTestXPathMessageSelector selector = new BooleanTestXPathMessageSelector(xpathExpression); Document testDocument = XmlTestUtil.getDocumentForString("<one><two><three/></two></one>"); assertTrue(selector.accept(new GenericMessage<Node>(testDocument.getElementsByTagName("two").item(0)))); assertFalse(selector.accept(new GenericMessage<Node>(testDocument.getElementsByTagName("three").item(0)))); }
@Test(expected = MessagingException.class) public void testNonNodePayload() throws Exception { XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type"); XPathRouter router = new XPathRouter(expression); router.getChannelKeys(new GenericMessage<String>("test")); }
@Test public void xpathExpressionReferenceConstructorInsteadOfString() throws Exception { XPathExpression expression = XPathExpressionFactory.createXPathExpression("/parent/child/@name"); XPathTransformer transformer = new XPathTransformer(expression); Object result = transformer.doTransform(message); assertEquals("test", result); }
@Test public void testSimpleDocType() throws Exception { Document doc = XmlTestUtil.getDocumentForString("<doc type='one' />"); XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type"); XPathRouter router = new XPathRouter(expression); Object channelName = router.getChannelKeys(new GenericMessage<Document>(doc)).toArray()[0]; assertEquals("Wrong channel name", "one", channelName); }
@Test public void testEvaluationReturnsEmptyString() throws Exception { Document doc = XmlTestUtil.getDocumentForString("<doc type='one' />"); XPathExpression expression = XPathExpressionFactory.createXPathExpression("/somethingelse/@type"); XPathRouter router = new XPathRouter(expression); List<Object> channelNames = router.getChannelKeys(new GenericMessage<Document>(doc)); assertEquals(0, channelNames.size()); }
@Test @SuppressWarnings({ "unchecked", "rawtypes" }) public void simpleSingleAttribute() throws Exception { Document doc = XmlTestUtil.getDocumentForString("<doc type=\"one\" />"); XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type"); XPathRouter router = new XPathRouter(expression); Object[] channelNames = router.getChannelKeys(new GenericMessage(doc)).toArray(); assertEquals("Wrong number of channels returned", 1, channelNames.length); assertEquals("Wrong channel name", "one", channelNames[0]); }
@Test @SuppressWarnings({ "unchecked", "rawtypes" }) public void simpleSingleAttributeAsString() throws Exception { Document doc = XmlTestUtil.getDocumentForString("<doc type=\"one\" />"); XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type"); XPathRouter router = new XPathRouter(expression); router.setEvaluateAsString(true); Object[] channelNames = router.getChannelKeys(new GenericMessage(doc)).toArray(); assertEquals("Wrong number of channels returned", 1, channelNames.length); assertEquals("Wrong channel name", "one", channelNames[0]); }
@Test @SuppressWarnings({ "unchecked", "rawtypes" }) public void simpleRootNode() throws Exception { Document doc = XmlTestUtil.getDocumentForString("<doc><foo>oleg</foo><bar>bang</bar></doc>"); XPathExpression expression = XPathExpressionFactory.createXPathExpression("name(./node())"); XPathRouter router = new XPathRouter(expression); router.setEvaluateAsString(true); Object[] channelNames = router.getChannelKeys(new GenericMessage(doc)).toArray(); assertEquals("Wrong number of channels returned", 1, channelNames.length); assertEquals("Wrong channel name", "doc", channelNames[0]); }
@Test @SuppressWarnings({ "unchecked", "rawtypes" }) public void multipleNodeValues() throws Exception { Document doc = XmlTestUtil.getDocumentForString("<doc type=\"one\"><book>bOne</book><book>bTwo</book></doc>"); XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/book"); XPathRouter router = new XPathRouter(expression); Object[] channelNames = router.getChannelKeys(new GenericMessage(doc)).toArray(); assertEquals("Wrong number of channels returned", 2, channelNames.length); assertEquals("Wrong channel name", "bOne", channelNames[0]); assertEquals("Wrong channel name", "bTwo", channelNames[1]); }
@Test public void testStringWithXPathExpressionProvided() { XPathExpression xpathExpression = XPathExpressionFactory.createXPathExpression("boolean(/one/two)"); BooleanTestXPathMessageSelector selector = new BooleanTestXPathMessageSelector(xpathExpression); assertTrue(selector.accept(new GenericMessage<String>("<one><two/></one>"))); assertFalse(selector.accept(new GenericMessage<String>("<one><three/></one>"))); }