2121
2222import org .apache .commons .logging .Log ;
2323import org .apache .commons .logging .LogFactory ;
24+
2425import org .springframework .util .Assert ;
2526import org .springframework .util .ClassUtils ;
2627import org .springframework .xml .JaxpVersion ;
@@ -42,9 +43,14 @@ public abstract class XPathExpressionFactory {
4243
4344 private static final String JAXEN_CLASS_NAME = "org.jaxen.XPath" ;
4445
46+ private static boolean jaxp13Available ;
47+
4548 private static boolean jaxenAvailable ;
4649
4750 static {
51+ // Check whether JAXP 1.3 is available
52+ jaxp13Available = JaxpVersion .isAtLeastJaxp13 ();
53+
4854 // Check whether Jaxen is available
4955 try {
5056 ClassUtils .forName (JAXEN_CLASS_NAME );
@@ -81,18 +87,24 @@ public static XPathExpression createXPathExpression(String expression)
8187 public static XPathExpression createXPathExpression (String expression , Map namespaces )
8288 throws IllegalStateException , XPathParseException {
8389 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+ }
87101 }
88- else if (jaxenAvailable ) {
102+ if (jaxenAvailable ) {
89103 logger .trace ("Creating [org.jaxen.XPath]" );
90104 return JaxenXPathExpressionFactory .createXPathExpression (expression , namespaces );
91105 }
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" );
96108 }
97109
98110
0 commit comments