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); }
/** * Create a router uses an XPath expression with one namespace. For example, * expression='/ns1:one/@type' prefix='ns1' namespace='www.example.org' * * @param expression the XPath expression as a String * @param prefix namespace prefix * @param namespace namespace uri */ public XPathRouter(String expression, String prefix, String namespace) { Assert.hasText(expression, "expression must not be empty"); Map<String, String> namespaces = new HashMap<String, String>(); namespaces.put(prefix, namespace); this.xPathExpression = XPathExpressionFactory.createXPathExpression(expression, namespaces); }
/** * Construct an instance based on the provided xpath expression, namespaces and * {@link TransformerFactory}. * @param expression the xpath expression for splitting. * @param namespaces the XML namespace for parsing. * @param transformerFactory the {@link TransformerFactory} * for parsing and building documents. * @since 4.3.19 */ public XPathMessageSplitter(String expression, Map<String, String> namespaces, TransformerFactory transformerFactory) { this(XPathExpressionFactory.createXPathExpression(expression, namespaces), transformerFactory); XPath xpath = XPathFactory.newInstance().newXPath(); SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext(); namespaceContext.setBindings(namespaces); xpath.setNamespaceContext(namespaceContext); try { this.jaxpExpression = xpath.compile(expression); } catch (XPathExpressionException e) { throw new org.springframework.xml.xpath.XPathParseException( "Could not compile [" + expression + "] to a XPathExpression: " + e.getMessage(), e); } }
XPathExpression expression = XPathExpressionFactory.createXPathExpression(xpath); Node node = CONVERTER.convertToNode(object);
@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(expected = MessagingException.class) public void nonNodePayload() throws Exception { XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type"); XPathRouter router = new XPathRouter(expression); router.getChannelKeys(new GenericMessage<String>("test")); }
@Override public void afterPropertiesSet() throws Exception { schemaLocationXPathExpression = XPathExpressionFactory.createXPathExpression(schemaLocationExpression, expressionNamespaces); }
@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); }
@Override public void afterPropertiesSet() throws Exception { schemaLocationXPathExpression = XPathExpressionFactory.createXPathExpression(schemaLocationExpression, expressionNamespaces); }