Skip to content

Commit dd4309d

Browse files
committed
Backported SWS-264 to 1.0 branch
1 parent 5b09b9d commit dd4309d

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

xml/src/main/java/org/springframework/xml/JaxpVersion.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,27 @@ public abstract class JaxpVersion {
8080
public static int getJaxpVersion() {
8181
return jaxpVersion;
8282
}
83-
}
83+
84+
/**
85+
* Convenience method to determine if the current JAXP version is at least 1.3 (packaged with JDK 1.5).
86+
*
87+
* @return <code>true</code> if the current JAXP version is at least JAXP 1.3
88+
* @see #getJaxpVersion()
89+
* @see #JAXP_13
90+
*/
91+
public static boolean isAtLeastJaxp13() {
92+
return getJaxpVersion() >= JAXP_13;
93+
}
94+
95+
/**
96+
* Convenience method to determine if the current JAXP version is at least 1.4 (packaged with JDK 1.6).
97+
*
98+
* @return <code>true</code> if the current JAXP version is at least JAXP 1.4
99+
* @see #getJaxpVersion()
100+
* @see #JAXP_14
101+
*/
102+
public static boolean isAtLeastJaxp14() {
103+
return getJaxpVersion() >= JAXP_14;
104+
}
105+
106+
}

xml/src/main/java/org/springframework/xml/xpath/XPathExpressionFactory.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.apache.commons.logging.Log;
2323
import org.apache.commons.logging.LogFactory;
24+
2425
import org.springframework.util.Assert;
2526
import org.springframework.util.ClassUtils;
2627
import 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

Comments
 (0)