Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.xml/share/classes/javax/xml/xpath/XPathFactory.java
40948 views
1
/*
2
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package javax.xml.xpath;
27
28
import com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl;
29
import jdk.xml.internal.SecuritySupport;
30
31
/**
32
* <p>An {@code XPathFactory} instance can be used to create
33
* {@link javax.xml.xpath.XPath} objects.</p>
34
*
35
*<p>See {@link #newInstance(String uri)} for lookup mechanism.</p>
36
*
37
* <p>The {@link XPathFactory} class is not thread-safe. In other words,
38
* it is the application's responsibility to ensure that at most
39
* one thread is using a {@link XPathFactory} object at any
40
* given moment. Implementations are encouraged to mark methods
41
* as <code>synchronized</code> to protect themselves from broken clients.
42
*
43
* <p>{@link XPathFactory} is not re-entrant. While one of the
44
* <code>newInstance</code> methods is being invoked, applications
45
* may not attempt to recursively invoke a <code>newInstance</code> method,
46
* even from the same thread.
47
*
48
* @author Norman Walsh
49
* @author Jeff Suttor
50
*
51
* @since 1.5
52
*/
53
public abstract class XPathFactory {
54
55
56
/**
57
* <p>The default property name according to the JAXP spec.</p>
58
*/
59
public static final String DEFAULT_PROPERTY_NAME = "javax.xml.xpath.XPathFactory";
60
61
/**
62
* <p>Default Object Model URI.</p>
63
*/
64
public static final String DEFAULT_OBJECT_MODEL_URI = "http://java.sun.com/jaxp/xpath/dom";
65
66
/**
67
* <p>Protected constructor as {@link #newInstance()} or {@link #newInstance(String uri)}
68
* or {@link #newInstance(String uri, String factoryClassName, ClassLoader classLoader)}
69
* should be used to create a new instance of an {@code XPathFactory}.</p>
70
*/
71
protected XPathFactory() {
72
}
73
74
/**
75
* Creates a new instance of the {@code XPathFactory} builtin
76
* system-default implementation.
77
*
78
* @implSpec The {@code XPathFactory} builtin
79
* system-default implementation is only required to support the
80
* {@link #DEFAULT_OBJECT_MODEL_URI default object model}, the
81
* {@linkplain org.w3c.dom W3C DOM}, but may support additional
82
* object models.
83
*
84
* @return A new instance of the {@code XPathFactory} builtin
85
* system-default implementation.
86
*
87
* @since 9
88
*/
89
public static XPathFactory newDefaultInstance() {
90
return new XPathFactoryImpl();
91
}
92
93
/**
94
* <p>Get a new {@code XPathFactory} instance using the default object model,
95
* {@link #DEFAULT_OBJECT_MODEL_URI},
96
* the W3C DOM.</p>
97
*
98
* <p>This method is functionally equivalent to:</p>
99
* <pre>
100
* newInstance(DEFAULT_OBJECT_MODEL_URI)
101
* </pre>
102
*
103
* <p>Since the implementation for the W3C DOM is always available, this method will never fail.</p>
104
*
105
* @return Instance of an {@code XPathFactory}.
106
*
107
* @throws RuntimeException When there is a failure in creating an
108
* {@code XPathFactory} for the default object model.
109
*/
110
public static XPathFactory newInstance() {
111
112
try {
113
return newInstance(DEFAULT_OBJECT_MODEL_URI);
114
} catch (XPathFactoryConfigurationException e) {
115
throw new RuntimeException(
116
"XPathFactory#newInstance() failed to create an XPathFactory for the default object model: "
117
+ DEFAULT_OBJECT_MODEL_URI
118
+ " with the XPathFactoryConfigurationException: "
119
+ e.getMessage(), e
120
);
121
}
122
}
123
124
/**
125
* Obtains a new {@code XPathFactory} instance using the specified object model.
126
* This method uses the
127
* <a href="../../../module-summary.html#LookupMechanism">JAXP Lookup Mechanism</a>
128
* to determine and load the {@code XPathFactory} implementation that supports
129
* the specified object model.
130
*
131
* <p>Tip for Trouble-shooting:
132
* <p>See {@link java.util.Properties#load(java.io.InputStream)} for exactly how a property file is parsed.
133
* In particular, colons ':' need to be escaped in a property file, so make sure the URIs are properly escaped in it.
134
* For example:
135
* <pre>
136
* http\://java.sun.com/jaxp/xpath/dom=org.acme.DomXPathFactory
137
* </pre>
138
*
139
* @param uri Identifies the underlying object model.
140
* The specification only defines the URI {@link #DEFAULT_OBJECT_MODEL_URI},
141
* <code>http://java.sun.com/jaxp/xpath/dom</code> for the W3C DOM,
142
* the org.w3c.dom package, and implementations are free to introduce other URIs for other object models.
143
*
144
* @return Instance of an {@code XPathFactory}.
145
*
146
* @throws XPathFactoryConfigurationException If the specified object model
147
* is unavailable, or if there is a configuration error.
148
* @throws NullPointerException If <code>uri</code> is <code>null</code>.
149
* @throws IllegalArgumentException If <code>uri</code> is <code>null</code>
150
* or <code>uri.length() == 0</code>.
151
*/
152
public static XPathFactory newInstance(final String uri)
153
throws XPathFactoryConfigurationException {
154
155
if (uri == null) {
156
throw new NullPointerException(
157
"XPathFactory#newInstance(String uri) cannot be called with uri == null");
158
}
159
160
if (uri.length() == 0) {
161
throw new IllegalArgumentException(
162
"XPathFactory#newInstance(String uri) cannot be called with uri == \"\"");
163
}
164
165
ClassLoader classLoader = SecuritySupport.getContextClassLoader();
166
167
if (classLoader == null) {
168
//use the current class loader
169
classLoader = XPathFactory.class.getClassLoader();
170
}
171
172
XPathFactory xpathFactory = new XPathFactoryFinder(classLoader).newFactory(uri);
173
174
if (xpathFactory == null) {
175
throw new XPathFactoryConfigurationException(
176
"No XPathFactory implementation found for the object model: "
177
+ uri);
178
}
179
180
return xpathFactory;
181
}
182
183
/**
184
* <p>Obtain a new instance of a {@code XPathFactory} from a factory class name. {@code XPathFactory}
185
* is returned if specified factory class supports the specified object model.
186
* This function is useful when there are multiple providers in the classpath.
187
* It gives more control to the application as it can specify which provider
188
* should be loaded.</p>
189
*
190
*
191
* <h4>Tip for Trouble-shooting</h4>
192
* <p>Setting the <code>jaxp.debug</code> system property will cause
193
* this method to print a lot of debug messages
194
* to <code>System.err</code> about what it is doing and where it is looking at.</p>
195
*
196
* <p> If you have problems try:</p>
197
* <pre>
198
* java -Djaxp.debug=1 YourProgram ....
199
* </pre>
200
*
201
* @param uri Identifies the underlying object model. The specification only defines the URI
202
* {@link #DEFAULT_OBJECT_MODEL_URI},<code>http://java.sun.com/jaxp/xpath/dom</code>
203
* for the W3C DOM, the org.w3c.dom package, and implementations are free to introduce
204
* other URIs for other object models.
205
*
206
* @param factoryClassName fully qualified factory class name that provides implementation of <code>javax.xml.xpath.XPathFactory</code>.
207
*
208
* @param classLoader <code>ClassLoader</code> used to load the factory class. If <code>null</code>
209
* current <code>Thread</code>'s context classLoader is used to load the factory class.
210
*
211
*
212
* @return New instance of a {@code XPathFactory}
213
*
214
* @throws XPathFactoryConfigurationException
215
* if <code>factoryClassName</code> is <code>null</code>, or
216
* the factory class cannot be loaded, instantiated
217
* or the factory class does not support the object model specified
218
* in the <code>uri</code> parameter.
219
*
220
* @throws NullPointerException If <code>uri</code> is <code>null</code>.
221
* @throws IllegalArgumentException If <code>uri</code> is <code>null</code>
222
* or <code>uri.length() == 0</code>.
223
*
224
* @see #newInstance()
225
* @see #newInstance(String uri)
226
*
227
* @since 1.6
228
*/
229
public static XPathFactory newInstance(String uri, String factoryClassName, ClassLoader classLoader)
230
throws XPathFactoryConfigurationException{
231
ClassLoader cl = classLoader;
232
233
if (uri == null) {
234
throw new NullPointerException(
235
"XPathFactory#newInstance(String uri) cannot be called with uri == null");
236
}
237
238
if (uri.length() == 0) {
239
throw new IllegalArgumentException(
240
"XPathFactory#newInstance(String uri) cannot be called with uri == \"\"");
241
}
242
243
if (cl == null) {
244
cl = SecuritySupport.getContextClassLoader();
245
}
246
247
XPathFactory f = new XPathFactoryFinder(cl).createInstance(factoryClassName);
248
249
if (f == null) {
250
throw new XPathFactoryConfigurationException(
251
"No XPathFactory implementation found for the object model: "
252
+ uri);
253
}
254
//if this factory supports the given schemalanguage return this factory else thrown exception
255
if (f.isObjectModelSupported(uri)) {
256
return f;
257
} else {
258
throw new XPathFactoryConfigurationException("Factory "
259
+ factoryClassName + " doesn't support given " + uri
260
+ " object model");
261
}
262
263
}
264
265
/**
266
* <p>Is specified object model supported by this {@code XPathFactory}?</p>
267
*
268
* @param objectModel Specifies the object model which the returned {@code XPathFactory} will understand.
269
*
270
* @return <code>true</code> if {@code XPathFactory} supports <code>objectModel</code>, else <code>false</code>.
271
*
272
* @throws NullPointerException If <code>objectModel</code> is <code>null</code>.
273
* @throws IllegalArgumentException If <code>objectModel.length() == 0</code>.
274
*/
275
public abstract boolean isObjectModelSupported(String objectModel);
276
277
/**
278
* <p>Set a feature for this {@code XPathFactory} and
279
* <code>XPath</code>s created by this factory.</p>
280
*
281
* <p>
282
* Feature names are fully qualified {@link java.net.URI}s.
283
* Implementations may define their own features.
284
* An {@link XPathFactoryConfigurationException} is thrown if this
285
* {@code XPathFactory} or the <code>XPath</code>s
286
* it creates cannot support the feature.
287
* It is possible for an {@code XPathFactory} to expose a feature value
288
* but be unable to change its state.
289
* </p>
290
*
291
* <p>
292
* All implementations are required to support the {@link javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} feature.
293
* When the feature is <code>true</code>, any reference to an external function is an error.
294
* Under these conditions, the implementation must not call the {@link XPathFunctionResolver}
295
* and must throw an {@link XPathFunctionException}.
296
* </p>
297
*
298
* @param name Feature name.
299
* @param value Is feature state <code>true</code> or <code>false</code>.
300
*
301
* @throws XPathFactoryConfigurationException if this {@code XPathFactory} or the <code>XPath</code>s
302
* it creates cannot support this feature.
303
* @throws NullPointerException if <code>name</code> is <code>null</code>.
304
*/
305
public abstract void setFeature(String name, boolean value)
306
throws XPathFactoryConfigurationException;
307
308
/**
309
* <p>Get the state of the named feature.</p>
310
*
311
* <p>
312
* Feature names are fully qualified {@link java.net.URI}s.
313
* Implementations may define their own features.
314
* An {@link XPathFactoryConfigurationException} is thrown if this
315
* {@code XPathFactory} or the <code>XPath</code>s
316
* it creates cannot support the feature.
317
* It is possible for an {@code XPathFactory} to expose a feature value
318
* but be unable to change its state.
319
* </p>
320
*
321
* @param name Feature name.
322
*
323
* @return State of the named feature.
324
*
325
* @throws XPathFactoryConfigurationException if this
326
* {@code XPathFactory} or the <code>XPath</code>s
327
* it creates cannot support this feature.
328
* @throws NullPointerException if <code>name</code> is <code>null</code>.
329
*/
330
public abstract boolean getFeature(String name)
331
throws XPathFactoryConfigurationException;
332
333
/**
334
* <p>Establish a default variable resolver.</p>
335
*
336
* <p>Any <code>XPath</code> objects constructed from this factory will use
337
* the specified resolver by default.</p>
338
*
339
* <p>A <code>NullPointerException</code> is thrown if <code>resolver</code>
340
* is <code>null</code>.</p>
341
*
342
* @param resolver Variable resolver.
343
*
344
* @throws NullPointerException If <code>resolver</code> is
345
* <code>null</code>.
346
*/
347
public abstract void setXPathVariableResolver(XPathVariableResolver resolver);
348
349
/**
350
* <p>Establish a default function resolver.</p>
351
*
352
* <p>Any <code>XPath</code> objects constructed from this factory will
353
* use the specified resolver by default.</p>
354
*
355
* <p>A <code>NullPointerException</code> is thrown if
356
* <code>resolver</code> is <code>null</code>.</p>
357
*
358
* @param resolver XPath function resolver.
359
*
360
* @throws NullPointerException If <code>resolver</code> is
361
* <code>null</code>.
362
*/
363
public abstract void setXPathFunctionResolver(XPathFunctionResolver resolver);
364
365
/**
366
* <p>Return a new <code>XPath</code> using the underlying object
367
* model determined when the {@code XPathFactory} was instantiated.</p>
368
*
369
* @return New instance of an <code>XPath</code>.
370
*/
371
public abstract XPath newXPath();
372
}
373
374