public Document checkStockLevel(Document doc) { String isbn = isbnSelectingXPath.evaluateAsString(doc); boolean inStock = false; // we only carry stock of one book currently if ("0321200683".equals(isbn)) { inStock = true; } doc.getDocumentElement().setAttribute("in-stock", String.valueOf(inStock)); return doc; }
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; }
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; }
/** * @param xPathExpression XPath expression as a String */ public AbstractXPathMessageSelector(String xPathExpression) { this.xPathExpresion = XPathExpressionFactory.createXPathExpression(xPathExpression); }
@Override protected List<Object> getChannelKeys(Message<?> message) { Node node = this.converter.convertToNode(message.getPayload()); if (this.evaluateAsString) { return Collections.singletonList((Object) this.xPathExpression.evaluateAsString(node)); } else { return this.xPathExpression.evaluate(node, this.nodeMapper); } }
private Object splitNode(Node node) throws ParserConfigurationException { if (this.iterator) { try { NodeList nodeList = (NodeList) this.jaxpExpression.evaluate(node, XPathConstants.NODESET); return new NodeListIterator(nodeList); } catch (XPathExpressionException e) { throw new XPathException("Could not evaluate XPath expression:" + e.getMessage(), e); } } else { List<Node> nodeList = this.xpathExpression.evaluateAsNodeList(node); if (this.createDocuments) { return convertNodesToDocuments(nodeList); } return nodeList; } }
public Object evaluateXPath(XPathExpression expression, Node node) { return expression.evaluateAsNode(node); } },
public Object evaluateXPath(XPathExpression expression, Node node) { return expression.evaluateAsNodeList(node); } };
public Object evaluateXPath(XPathExpression expression, Node node) { return expression.evaluateAsBoolean(node); } },
public Object evaluateXPath(XPathExpression expression, Node node) { return expression.evaluateAsNumber(node); } },
/** * 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); }
public Object evaluateXPath(XPathExpression expression, Node node) { return expression.evaluateAsString(node); } },
/** * Return true if the {@link XPathExpression} evaluates to <code>true</code> */ @Override public boolean accept(Message<?> message) { Node node = getConverter().convertToNode(message.getPayload()); return getXPathExpresion().evaluateAsBoolean(node); }
/** * @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); }
/** * 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 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(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); }