MatchXPathExpression implements MatchPattern<Document> { public QName getXpathConstraint() { if (XPathExpression.matches("/+\\*\\[[^\\]]+\\]$")) { return XPathConstants.NODESET; } else if (XPathExpression .matches("^(count|sum|min|max|avg|number|string-length|round|floor|ceiling)\\(.+\\)$")) { return XPathConstants.NUMBER; } else if (XPathExpression .matches("(.+[!=><]{1,2}\\s?['A-z0-9]+$|^(not|nilled|boolean)\\(.+\\)$|=\\s?[A-z0-9]$)")) { return XPathConstants.BOOLEAN; } else if (XPathExpression.matches("(.+ /(text|name|local-name)\\(\\)$|^(string|concat)\\(.+\\)$)")) { return XPathConstants.STRING; } return XPathConstants.NODESET; } MatchXPathExpression(final String XPathExpression); QName getXpathConstraint(); @Override void negate(); @Override void onlyMatching(); @Override boolean isOnlyMatching(); @Override void onlyMatchingNode(); @Override boolean isMatchingNode(); @Override boolean isNegate(); @Override void doNotPrintSource(); @Override boolean isPrintSource(); String getXPathExpression(); @Override boolean hasMatch(Document document); @Override String getMatch(); @Override boolean writeMatches(String source, Document document, Writer writer);  }
@Test public void ConstraintNodes() throws XPathExpressionException { QName expect = XPathConstants.NODESET; MatchXPathExpression xpath = new MatchXPathExpression( " assertEquals(expect, xpath.getXpathConstraint()); MatchXPathExpression xpath2 = new MatchXPathExpression( "/session[contains(comments, 'strong')]"); assertEquals(expect, xpath6.getXpathConstraint()); MatchXPathExpression xpath7 = new MatchXPathExpression( " assertEquals(expect, xpath7.getXpathConstraint()); MatchXPathExpression xpath8 = new MatchXPathExpression( " assertEquals(expect, xpath8.getXpathConstraint()); MatchXPathExpression xpath9 = new MatchXPathExpression( " assertEquals(expect, xpath9.getXpathConstraint()); MatchXPathExpression xpath5 = new MatchXPathExpression( " assertEquals(expect, xpath5.getXpathConstraint()); MatchXPathExpression xpath4 = new MatchXPathExpression( " assertEquals(expect, xpath4.getXpathConstraint()); }
@Test public void ConstraintCountBoolean() throws XPathExpressionException { QName expect = XPathConstants.BOOLEAN; MatchXPathExpression xpath = new MatchXPathExpression( "count( assertEquals(expect, xpath.getXpathConstraint()); MatchXPathExpression xpath2 = new MatchXPathExpression( "count( assertEquals(expect, xpath2.getXpathConstraint()); MatchXPathExpression xpath3 = new MatchXPathExpression( " assertEquals(expect, xpath3.getXpathConstraint()); }
@Test public void ConstraintNumber() throws XPathExpressionException { QName expect = XPathConstants.NUMBER; MatchXPathExpression xpath = new MatchXPathExpression( "string-length( assertEquals(expect, xpath.getXpathConstraint()); MatchXPathExpression xpath2 = new MatchXPathExpression( "count( assertEquals(expect, xpath2.getXpathConstraint()); MatchXPathExpression xpath3 = new MatchXPathExpression( "count(*/distance[@units='miles'])"); assertEquals(expect, xpath3.getXpathConstraint()); MatchXPathExpression xpath4 = new MatchXPathExpression( "sum(cart/item/@price)"); assertEquals(expect, xpath4.getXpathConstraint()); MatchXPathExpression xpath5 = new MatchXPathExpression( "round(sum(cart/item/@price))"); assertEquals(expect, xpath5.getXpathConstraint()); }
@Test public void ConstraintString() throws XPathExpressionException { QName expect = XPathConstants.STRING; MatchXPathExpression xpath = new MatchXPathExpression( "concat('CLASS', ' = ', assertEquals(expect, xpath.getXpathConstraint()); }