XML { public static String evaluate(String xpath, Object node) { Node domNode = getNode(node); if (domNode == null) { log.severe("Unable to evaluate non DOM Node object"); return null; } if (xpath == null || xpath.trim().isEmpty()) { return serialize(node); } try { xpath = getExpression(xpath); XPath xp = XPathFactory.newInstance().newXPath(); Node result = (Node) xp.evaluate(xpath, domNode, XPathConstants.NODE); if (result != null) { if (result.getNodeType() == Node.TEXT_NODE) { return result.getNodeValue(); } else if (result.getNodeType() == Node.ATTRIBUTE_NODE) { return result.getNodeValue(); } return serialize(result); } } catch (DOMException|XPathExpressionException e) { log.log(Level.SEVERE, "Failed to evaluate xpath '" + xpath + "'", e); } return null; }  static String serialize(Object node); static boolean predicate(String xpath, Object node); static String evaluate(String xpath, Object node);  }
@Test public void testEvaluateOrderIdText() { String xml = "<soapenv:Envelope xmlns:soapenv=\"http: + "xmlns:urn=\"urn:switchyard-quickstart-demo:orders:1.0\">\n <soapenv:Header/>\n" + "<soapenv:Body>\n <urn:submitOrder>\n <order>\n " + "<orderId>1</orderId>\n <itemId>BUTTER</itemId>\n " + "<quantity>100</quantity>\n <customer>Fred</customer>\n " + "</order>\n </urn:submitOrder>\n </soapenv:Body>\n</soapenv:Envelope>"; String result = XML.evaluate("*[local-name() = 'Envelope']/*[local-name() = 'Body']" + "/*[local-name() = 'submitOrder']/order/orderId/text()", xml); assertNotNull(result); assertEquals("1", result); }
@Test public void testEvaluateOrderIdTextWildcard() { String xml = "<soapenv:Envelope xmlns:soapenv=\"http: + "xmlns:urn=\"urn:switchyard-quickstart-demo:orders:1.0\">\n <soapenv:Header/>\n" + "<soapenv:Body>\n <urn:submitOrder>\n <order>\n " + "<orderId>1</orderId>\n <itemId>BUTTER</itemId>\n " + "<quantity>100</quantity>\n <customer>Fred</customer>\n " + "</order>\n </urn:submitOrder>\n </soapenv:Body>\n</soapenv:Envelope>"; String result = XML.evaluate("*:Envelope/*:Body" + "/*:submitOrder/order/orderId/text()", xml); assertNotNull(result); assertEquals("1", result); }
@Test public void testEvaluateOrderIdNode() { String xml = "<soapenv:Envelope xmlns:soapenv=\"http: + "xmlns:urn=\"urn:switchyard-quickstart-demo:orders:1.0\">\n <soapenv:Header/>\n" + "<soapenv:Body>\n <urn:submitOrder>\n <order>\n " + "<orderId>1</orderId>\n <itemId>BUTTER</itemId>\n " + "<quantity>100</quantity>\n <customer>Fred</customer>\n " + "</order>\n </urn:submitOrder>\n </soapenv:Body>\n</soapenv:Envelope>"; String result = XML.evaluate("*[local-name() = 'Envelope']/*[local-name() = 'Body']" + "/*[local-name() = 'submitOrder']/order/orderId", xml); assertNotNull(result); assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><orderId>1</orderId>", result); }
@Test public void testEvaluateOrderIdNodeWildcard() { String xml = "<soapenv:Envelope xmlns:soapenv=\"http: + "xmlns:urn=\"urn:switchyard-quickstart-demo:orders:1.0\">\n <soapenv:Header/>\n" + "<soapenv:Body>\n <urn:submitOrder>\n <order>\n " + "<orderId>1</orderId>\n <itemId>BUTTER</itemId>\n " + "<quantity>100</quantity>\n <customer>Fred</customer>\n " + "</order>\n </urn:submitOrder>\n </soapenv:Body>\n</soapenv:Envelope>"; String result = XML.evaluate("*:Envelope/*:Body" + "/*:submitOrder/order/orderId", xml); assertNotNull(result); assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><orderId>1</orderId>", result); }
@Test public void testEvaluateMissingNode() { String xml = "<soapenv:Envelope xmlns:soapenv=\"http: + "xmlns:urn=\"urn:switchyard-quickstart-demo:orders:1.0\">\n <soapenv:Header/>\n" + "<soapenv:Body>\n <urn:submitOrder>\n <order>\n " + "<orderId>1</orderId>\n <itemId>BUTTER</itemId>\n " + "<quantity>100</quantity>\n <customer>Fred</customer>\n " + "</order>\n </urn:submitOrder>\n </soapenv:Body>\n</soapenv:Envelope>"; String result = XML.evaluate("*[local-name() = 'Envelope']/*[local-name() = 'Body']" + "/*[local-name() = 'submitOrder']/invalid", xml); assertNull(result); }
@Test public void testEvaluateMissingNodeWildcard() { String xml = "<soapenv:Envelope xmlns:soapenv=\"http: + "xmlns:urn=\"urn:switchyard-quickstart-demo:orders:1.0\">\n <soapenv:Header/>\n" + "<soapenv:Body>\n <urn:submitOrder>\n <order>\n " + "<orderId>1</orderId>\n <itemId>BUTTER</itemId>\n " + "<quantity>100</quantity>\n <customer>Fred</customer>\n " + "</order>\n </urn:submitOrder>\n </soapenv:Body>\n</soapenv:Envelope>"; String result = XML.evaluate("*:Envelope/*:Body/*:submitOrder/invalid", xml); assertNull(result); }