77import org .w3c .dom .Node ;
88
99import javax .xml .namespace .NamespaceContext ;
10+ import javax .xml .namespace .QName ;
1011import javax .xml .xpath .XPath ;
1112import javax .xml .xpath .XPathConstants ;
1213import javax .xml .xpath .XPathExpression ;
2021 */
2122public class HasXPath extends TypeSafeMatcher <Node > {
2223
23- private final Matcher <String > valueMatcher ;
24+ private final Matcher <? > valueMatcher ;
2425 private final XPathExpression compiledXPath ;
2526 private final String xpathString ;
27+ private final QName evaluationMode ;
2628
2729 /**
2830 * @param xPathExpression XPath expression.
@@ -40,27 +42,31 @@ public HasXPath(String xPathExpression, Matcher<String> valueMatcher) {
4042 * May be null to specify that the XPath must exist but the value is irrelevant.
4143 */
4244 public HasXPath (String xPathExpression , NamespaceContext namespaceContext , Matcher <String > valueMatcher ) {
43- try {
44- XPath xPath = XPathFactory .newInstance ().newXPath ();
45- if (namespaceContext != null )
46- {
47- xPath .setNamespaceContext (namespaceContext );
48- }
49- compiledXPath = xPath .compile (xPathExpression );
50- this .xpathString = xPathExpression ;
51- this .valueMatcher = valueMatcher ;
52- } catch (XPathExpressionException e ) {
53- throw new IllegalArgumentException ("Invalid XPath : " + xPathExpression , e );
45+ this (xPathExpression , namespaceContext , valueMatcher , XPathConstants .STRING );
46+ }
47+
48+ private HasXPath (String xPathExpression , NamespaceContext namespaceContext , Matcher <?> valueMatcher , QName mode ) {
49+ try {
50+ XPath xPath = XPathFactory .newInstance ().newXPath ();
51+ if (namespaceContext != null ) {
52+ xPath .setNamespaceContext (namespaceContext );
5453 }
54+ compiledXPath = xPath .compile (xPathExpression );
55+ this .xpathString = xPathExpression ;
56+ this .valueMatcher = valueMatcher ;
57+ this .evaluationMode = mode ;
58+ } catch (XPathExpressionException e ) {
59+ throw new IllegalArgumentException ("Invalid XPath : " + xPathExpression , e );
60+ }
5561 }
5662
5763 public boolean matchesSafely (Node item ) {
5864 try {
59- String result = ( String ) compiledXPath .evaluate (item , XPathConstants . STRING );
65+ Object result = compiledXPath .evaluate (item , evaluationMode );
6066 if (result == null ) {
6167 return false ;
6268 } else if (valueMatcher == null ) {
63- return ! result . equals ( "" ) ;
69+ return true ;
6470 } else {
6571 return valueMatcher .matches (result );
6672 }
@@ -75,24 +81,24 @@ public void describeTo(Description description) {
7581 description .appendText (" " ).appendDescriptionOf (valueMatcher );
7682 }
7783 }
78-
84+
7985 @ Factory
8086 public static Matcher <Node > hasXPath (String xPath , Matcher <String > valueMatcher ) {
8187 return hasXPath (xPath , null , valueMatcher );
8288 }
8389
8490 @ Factory
8591 public static Matcher <Node > hasXPath (String xPath , NamespaceContext namespaceContext , Matcher <String > valueMatcher ) {
86- return new HasXPath (xPath , namespaceContext , valueMatcher );
92+ return new HasXPath (xPath , namespaceContext , valueMatcher , XPathConstants . STRING );
8793 }
8894
8995 @ Factory
9096 public static Matcher <Node > hasXPath (String xPath ) {
91- return hasXPath (xPath , null , null );
97+ return hasXPath (xPath , ( NamespaceContext ) null );
9298 }
9399
94100 @ Factory
95101 public static Matcher <Node > hasXPath (String xPath , NamespaceContext namespaceContext ) {
96- return hasXPath (xPath , namespaceContext , null );
102+ return new HasXPath (xPath , namespaceContext , null , XPathConstants . NODE );
97103 }
98104}
0 commit comments