1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47:
48:
54: public abstract class XPathFactory
55: {
56:
57:
60: public static final String DEFAULT_PROPERTY_NAME =
61: "javax.xml.xpath.XPathFactory";
62:
63:
66: public static final String DEFAULT_OBJECT_MODEL_URI =
67: XPathConstants.DOM_OBJECT_MODEL;
68:
69: protected XPathFactory()
70: {
71: }
72:
73:
76: public static final XPathFactory newInstance()
77: {
78: try
79: {
80: return newInstance(DEFAULT_OBJECT_MODEL_URI);
81: }
82: catch (XPathFactoryConfigurationException e)
83: {
84: throw new RuntimeException(e.getMessage());
85: }
86: }
87:
88:
103: public static final XPathFactory newInstance(String uri)
104: throws XPathFactoryConfigurationException
105: {
106: ClassLoader loader = Thread.currentThread().getContextClassLoader();
107: if (loader == null)
108: {
109: loader = XPathFactory.class.getClassLoader();
110: }
111: String className = null;
112: int count = 0;
113: do
114: {
115: className = getFactoryClassName(loader, count++);
116: if (className != null)
117: {
118: try
119: {
120: Class<?> t = (loader != null) ? loader.loadClass(className) :
121: Class.forName(className);
122: XPathFactory ret = (XPathFactory) t.newInstance();
123: if (ret.isObjectModelSupported(uri))
124: {
125: return ret;
126: }
127: className = null;
128: }
129: catch (ClassNotFoundException e)
130: {
131: className = null;
132: }
133: catch (Exception e)
134: {
135: throw new XPathFactoryConfigurationException(e);
136: }
137: }
138: }
139: while (className == null && count < 4);
140: String msg = "no factories with support for " + uri;
141: throw new XPathFactoryConfigurationException(msg);
142: }
143:
144: private static String getFactoryClassName(ClassLoader loader, int attempt)
145: {
146: final String propertyName = DEFAULT_PROPERTY_NAME;
147: switch (attempt)
148: {
149: case 0:
150: return System.getProperty(propertyName);
151: case 1:
152: try
153: {
154: File file = new File(System.getProperty("java.home"));
155: file = new File(file, "lib");
156: file = new File(file, "jaxp.properties");
157: InputStream in = new FileInputStream(file);
158: Properties props = new Properties();
159: props.load(in);
160: in.close();
161: return props.getProperty(propertyName);
162: }
163: catch (IOException e)
164: {
165: return null;
166: }
167: case 2:
168: try
169: {
170: String serviceKey = "/META-INF/services/" + propertyName;
171: InputStream in = (loader != null) ?
172: loader.getResourceAsStream(serviceKey) :
173: XPathFactory.class.getResourceAsStream(serviceKey);
174: if (in != null)
175: {
176: BufferedReader r =
177: new BufferedReader(new InputStreamReader(in));
178: String ret = r.readLine();
179: r.close();
180: return ret;
181: }
182: }
183: catch (IOException e)
184: {
185: }
186: return null;
187: case 3:
188: return "gnu.xml.xpath.XPathFactoryImpl";
189: default:
190: return null;
191: }
192: }
193:
194:
198: public abstract boolean isObjectModelSupported(String objectModel);
199:
200:
203: public abstract void setFeature(String name, boolean value)
204: throws XPathFactoryConfigurationException;
205:
206:
209: public abstract boolean getFeature(String name)
210: throws XPathFactoryConfigurationException;
211:
212:
215: public abstract void setXPathVariableResolver(XPathVariableResolver resolver);
216:
217:
220: public abstract void setXPathFunctionResolver(XPathFunctionResolver resolver);
221:
222:
225: public abstract XPath newXPath();
226:
227: }