1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.xml.xpath;
18
19 import java.util.Collections;
20 import java.util.Map;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 import org.springframework.util.Assert;
26 import org.springframework.util.ClassUtils;
27 import org.springframework.xml.JaxpVersion;
28
29
30
31
32
33
34
35
36
37
38
39
40 public abstract class XPathExpressionFactory {
41
42 private static final Log logger = LogFactory.getLog(XPathExpressionFactory.class);
43
44 private static final String JAXEN_CLASS_NAME = "org.jaxen.XPath";
45
46 private static boolean jaxp13Available;
47
48 private static boolean jaxenAvailable;
49
50 static {
51
52 jaxp13Available = JaxpVersion.isAtLeastJaxp13();
53
54
55 try {
56 ClassUtils.forName(JAXEN_CLASS_NAME);
57 jaxenAvailable = true;
58 }
59 catch (ClassNotFoundException ex) {
60 jaxenAvailable = false;
61 }
62 }
63
64
65
66
67
68
69
70
71
72 public static XPathExpression createXPathExpression(String expression)
73 throws IllegalStateException, XPathParseException {
74 return createXPathExpression(expression, Collections.EMPTY_MAP);
75 }
76
77
78
79
80
81
82
83
84
85
86
87 public static XPathExpression createXPathExpression(String expression, Map namespaces)
88 throws IllegalStateException, XPathParseException {
89 Assert.hasLength(expression, "expression is empty");
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 }
101 }
102 if (jaxenAvailable) {
103 logger.trace("Creating [org.jaxen.XPath]");
104 return JaxenXPathExpressionFactory.createXPathExpression(expression, namespaces);
105 }
106 throw new IllegalStateException(
107 "Could not create XPathExpression: could not locate JAXP 1.3, or Jaxen on the class path");
108 }
109
110
111 }