diff options
author | Paul Duffin <paulduffin@google.com> | 2017-01-24 18:36:59 +0000 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2017-01-24 18:36:59 +0000 |
commit | 05e68aaf5378b155cc99e7c2b8b87de92011c5ce (patch) | |
tree | aa4a6a53609f47d386dfa114be0abfc01e0e5dff /hamcrest-library/src/main/java/org/hamcrest/xml | |
parent | 3752c058e57f3b560396cc9484bd353cba75cfc3 (diff) | |
parent | 1dfb5bbe24ccbd127405dc8bb8e178536d1a70b4 (diff) | |
download | hamcrest-05e68aaf5378b155cc99e7c2b8b87de92011c5ce.tar.gz |
Merge "Match upstream file structure" am: 5d9cc8c989 am: b5937e4be3 am: 11a97a2ff7
am: 1dfb5bbe24
Change-Id: Ib4f853674ae97e7d7b4e3cef05e2668ef514e045
Diffstat (limited to 'hamcrest-library/src/main/java/org/hamcrest/xml')
-rw-r--r-- | hamcrest-library/src/main/java/org/hamcrest/xml/HasXPath.java | 74 | ||||
-rw-r--r-- | hamcrest-library/src/main/java/org/hamcrest/xml/package.html | 7 |
2 files changed, 81 insertions, 0 deletions
diff --git a/hamcrest-library/src/main/java/org/hamcrest/xml/HasXPath.java b/hamcrest-library/src/main/java/org/hamcrest/xml/HasXPath.java new file mode 100644 index 0000000..742935b --- /dev/null +++ b/hamcrest-library/src/main/java/org/hamcrest/xml/HasXPath.java @@ -0,0 +1,74 @@ +package org.hamcrest.xml; + +import org.hamcrest.Description; +import org.hamcrest.Factory; +import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeMatcher; +import org.w3c.dom.Node; + +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpression; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; + +/** + * Applies a Matcher to a given XML Node in an existing XML Node tree, specified by an XPath expression. + * + * @author Joe Walnes + */ +public class HasXPath extends TypeSafeMatcher<Node> { + + private final Matcher<String> valueMatcher; + private final XPathExpression compiledXPath; + private final String xpathString; + + /** + * @param xPathExpression XPath expression. + * @param valueMatcher Matcher to use at given XPath. + * May be null to specify that the XPath must exist but the value is irrelevant. + */ + public HasXPath(String xPathExpression, Matcher<String> valueMatcher) { + try { + XPath xPath = XPathFactory.newInstance().newXPath(); + compiledXPath = xPath.compile(xPathExpression); + this.xpathString = xPathExpression; + this.valueMatcher = valueMatcher; + } catch (XPathExpressionException e) { + throw new IllegalArgumentException("Invalid XPath : " + xPathExpression, e); + } + } + + public boolean matchesSafely(Node item) { + try { + String result = (String) compiledXPath.evaluate(item, XPathConstants.STRING); + if (result == null) { + return false; + } else if (valueMatcher == null) { + return !result.equals(""); + } else { + return valueMatcher.matches(result); + } + } catch (XPathExpressionException e) { + return false; + } + } + + public void describeTo(Description description) { + description.appendText("an XML document with XPath ").appendText(xpathString); + if (valueMatcher != null) { + description.appendText(" ").appendDescriptionOf(valueMatcher); + } + } + + @Factory + public static Matcher<Node> hasXPath(String xPath, Matcher<String> valueMatcher) { + return new HasXPath(xPath, valueMatcher); + } + + @Factory + public static Matcher<Node> hasXPath(String xPath) { + return hasXPath(xPath, null); + } + +} diff --git a/hamcrest-library/src/main/java/org/hamcrest/xml/package.html b/hamcrest-library/src/main/java/org/hamcrest/xml/package.html new file mode 100644 index 0000000..d9c5f97 --- /dev/null +++ b/hamcrest-library/src/main/java/org/hamcrest/xml/package.html @@ -0,0 +1,7 @@ +<html> +<head> +</head> +<body> + <p>Matchers of XML documents.</p> +</body> +</html> |