7
7
import org .w3c .dom .Node ;
8
8
9
9
import javax .xml .namespace .NamespaceContext ;
10
+ import javax .xml .namespace .QName ;
10
11
import javax .xml .xpath .XPath ;
11
12
import javax .xml .xpath .XPathConstants ;
12
13
import javax .xml .xpath .XPathExpression ;
20
21
*/
21
22
public class HasXPath extends TypeSafeMatcher <Node > {
22
23
23
- private final Matcher <String > valueMatcher ;
24
+ private final Matcher <? > valueMatcher ;
24
25
private final XPathExpression compiledXPath ;
25
26
private final String xpathString ;
27
+ private final QName evaluationMode ;
26
28
27
29
/**
28
30
* @param xPathExpression XPath expression.
@@ -40,27 +42,31 @@ public HasXPath(String xPathExpression, Matcher<String> valueMatcher) {
40
42
* May be null to specify that the XPath must exist but the value is irrelevant.
41
43
*/
42
44
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 );
54
53
}
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
+ }
55
61
}
56
62
57
63
public boolean matchesSafely (Node item ) {
58
64
try {
59
- String result = ( String ) compiledXPath .evaluate (item , XPathConstants . STRING );
65
+ Object result = compiledXPath .evaluate (item , evaluationMode );
60
66
if (result == null ) {
61
67
return false ;
62
68
} else if (valueMatcher == null ) {
63
- return ! result . equals ( "" ) ;
69
+ return true ;
64
70
} else {
65
71
return valueMatcher .matches (result );
66
72
}
@@ -75,24 +81,24 @@ public void describeTo(Description description) {
75
81
description .appendText (" " ).appendDescriptionOf (valueMatcher );
76
82
}
77
83
}
78
-
84
+
79
85
@ Factory
80
86
public static Matcher <Node > hasXPath (String xPath , Matcher <String > valueMatcher ) {
81
87
return hasXPath (xPath , null , valueMatcher );
82
88
}
83
89
84
90
@ Factory
85
91
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 );
87
93
}
88
94
89
95
@ Factory
90
96
public static Matcher <Node > hasXPath (String xPath ) {
91
- return hasXPath (xPath , null , null );
97
+ return hasXPath (xPath , ( NamespaceContext ) null );
92
98
}
93
99
94
100
@ Factory
95
101
public static Matcher <Node > hasXPath (String xPath , NamespaceContext namespaceContext ) {
96
- return hasXPath (xPath , namespaceContext , null );
102
+ return new HasXPath (xPath , namespaceContext , null , XPathConstants . NODE );
97
103
}
98
104
}
0 commit comments