21
21
22
22
import org .apache .commons .logging .Log ;
23
23
import org .apache .commons .logging .LogFactory ;
24
+
24
25
import org .springframework .util .Assert ;
25
26
import org .springframework .util .ClassUtils ;
26
27
import org .springframework .xml .JaxpVersion ;
@@ -42,9 +43,14 @@ public abstract class XPathExpressionFactory {
42
43
43
44
private static final String JAXEN_CLASS_NAME = "org.jaxen.XPath" ;
44
45
46
+ private static boolean jaxp13Available ;
47
+
45
48
private static boolean jaxenAvailable ;
46
49
47
50
static {
51
+ // Check whether JAXP 1.3 is available
52
+ jaxp13Available = JaxpVersion .isAtLeastJaxp13 ();
53
+
48
54
// Check whether Jaxen is available
49
55
try {
50
56
ClassUtils .forName (JAXEN_CLASS_NAME );
@@ -81,18 +87,24 @@ public static XPathExpression createXPathExpression(String expression)
81
87
public static XPathExpression createXPathExpression (String expression , Map namespaces )
82
88
throws IllegalStateException , XPathParseException {
83
89
Assert .hasLength (expression , "expression is empty" );
84
- if (JaxpVersion .getJaxpVersion () >= JaxpVersion .JAXP_13 ) {
85
- logger .trace ("Creating [javax.xml.xpath.XPathExpression]" );
86
- return Jaxp13XPathExpressionFactory .createXPathExpression (expression , namespaces );
90
+ if (jaxp13Available ) {
91
+ try {
92
+ logger .trace ("Creating [javax.xml.xpath.XPathExpression]" );
93
+ return Jaxp13XPathExpressionFactory .createXPathExpression (expression , namespaces );
94
+ }
95
+ catch (XPathException e ) {
96
+ throw e ;
97
+ }
98
+ catch (Throwable e ) {
99
+ jaxp13Available = false ;
100
+ }
87
101
}
88
- else if (jaxenAvailable ) {
102
+ if (jaxenAvailable ) {
89
103
logger .trace ("Creating [org.jaxen.XPath]" );
90
104
return JaxenXPathExpressionFactory .createXPathExpression (expression , namespaces );
91
105
}
92
- else {
93
- throw new IllegalStateException (
94
- "Could not create XPathExpression: could not locate JAXP 1.3, or Jaxen on the class path" );
95
- }
106
+ throw new IllegalStateException (
107
+ "Could not create XPathExpression: could not locate JAXP 1.3, or Jaxen on the class path" );
96
108
}
97
109
98
110
0 commit comments