XML { public static boolean predicate(String xpath, Object node) { Node domNode = getNode(node); if (domNode == null) { log.severe("Unable to evaluate non DOM Node object"); return false; } if (xpath == null) { log.severe("Predicate has not xpath expression"); return false; } try { xpath = getExpression(xpath); XPath xp = XPathFactory.newInstance().newXPath(); Boolean result = (Boolean) xp.evaluate(xpath, domNode, XPathConstants.BOOLEAN); if (result != null) { return result; } } catch (XPathExpressionException e) { log.log(Level.SEVERE, "Failed to execute predicate xpath '" + xpath + "'", e); } return false; }  static String serialize(Object node); static boolean predicate(String xpath, Object node); static String evaluate(String xpath, Object node);  }
@Test public void testPredicateFoundNode() { 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>"; boolean result = XML.predicate("*[local-name() = 'Envelope']/*[local-name() = 'Body']" + "/*[local-name() = 'submitOrder']/order", xml); assertTrue(result); }
@Test public void testPredicateFoundNodeWildcard() { 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>"; boolean result = XML.predicate("*:Envelope/*:Body/*:submitOrder/order", xml); assertTrue(result); }
@Test public void testPredicateMissingNode() { 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>"; boolean result = XML.predicate("*[local-name() = 'Envelope']/*[local-name() = 'Body']" + "/*[local-name() = 'submitOrder']/invalid", xml); assertFalse(result); }
@Test public void testPredicateMissingNodeWildcard() { 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>"; boolean result = XML.predicate("*:Envelope/*:Body/*:submitOrder/invalid", xml); assertFalse(result); }