Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.xml/share/classes/javax/xml/parsers/SAXParserFactory.java
40948 views
1
/*
2
* Copyright (c) 2000, 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.parsers;
27
28
import com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl;
29
import javax.xml.validation.Schema;
30
import org.xml.sax.SAXException;
31
import org.xml.sax.SAXNotRecognizedException;
32
import org.xml.sax.SAXNotSupportedException;
33
34
/**
35
* Defines a factory API that enables applications to configure and
36
* obtain a SAX based parser to parse XML documents.
37
*
38
* @author Jeff Suttor
39
* @author Neeraj Bajaj
40
*
41
* @since 1.4
42
*/
43
public abstract class SAXParserFactory {
44
private static final String DEFAULT_IMPL =
45
"com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl";
46
47
/**
48
* Should Parsers be validating?
49
*/
50
private boolean validating = false;
51
52
/**
53
* Should Parsers be namespace aware?
54
*/
55
private boolean namespaceAware = false;
56
57
/**
58
* Protected constructor to force use of {@link #newInstance()}.
59
*/
60
protected SAXParserFactory () {
61
62
}
63
64
/**
65
* Creates a new NamespaceAware instance of the {@code SAXParserFactory}
66
* builtin system-default implementation. Parsers produced by the factory
67
* instance provides support for XML namespaces by default.
68
*
69
* @implSpec
70
* In addition to creating a factory instance using the same process as
71
* {@link #newDefaultInstance()}, this method must set NamespaceAware to true.
72
*
73
* @return a new instance of the {@code SAXParserFactory} builtin
74
* system-default implementation.
75
*
76
* @since 13
77
*/
78
public static SAXParserFactory newDefaultNSInstance() {
79
return makeNSAware(new SAXParserFactoryImpl());
80
}
81
82
/**
83
* Creates a new NamespaceAware instance of a {@code SAXParserFactory}.
84
* Parsers produced by the factory instance provides support for XML
85
* namespaces by default.
86
*
87
* @implSpec
88
* In addition to creating a factory instance using the same process as
89
* {@link #newInstance()}, this method must set NamespaceAware to true.
90
*
91
* @return a new instance of the {@code SAXParserFactory}
92
*
93
* @throws FactoryConfigurationError in case of {@linkplain
94
* java.util.ServiceConfigurationError service configuration error}
95
* or if the implementation is not available or cannot be instantiated.
96
*
97
* @since 13
98
*/
99
public static SAXParserFactory newNSInstance() {
100
return makeNSAware(FactoryFinder.find(SAXParserFactory.class, DEFAULT_IMPL));
101
}
102
103
/**
104
* Creates a new NamespaceAware instance of a {@code SAXParserFactory} from
105
* the class name. Parsers produced by the factory instance provides
106
* support for XML namespaces by default.
107
*
108
* @implSpec
109
* In addition to creating a factory instance using the same process as
110
* {@link #newInstance(java.lang.String, java.lang.ClassLoader)}, this method
111
* must set NamespaceAware to true.
112
*
113
* @param factoryClassName a fully qualified factory class name that provides
114
* implementation of
115
* {@code javax.xml.parsers.SAXParserFactory}.
116
*
117
* @param classLoader the {@code ClassLoader} used to load the factory class.
118
* If it is {@code null}, the current {@code Thread}'s
119
* context classLoader is used to load the factory class.
120
*
121
* @return a new instance of the {@code SAXParserFactory}
122
*
123
* @throws FactoryConfigurationError if {@code factoryClassName} is {@code null}, or
124
* the factory class cannot be loaded, instantiated.
125
*
126
* @since 13
127
*/
128
public static SAXParserFactory newNSInstance(String factoryClassName,
129
ClassLoader classLoader) {
130
return makeNSAware(FactoryFinder.newInstance(
131
SAXParserFactory.class, factoryClassName, classLoader, false));
132
}
133
134
/**
135
* Creates a new instance of the {@code SAXParserFactory} builtin
136
* system-default implementation.
137
*
138
* @return A new instance of the {@code SAXParserFactory} builtin
139
* system-default implementation.
140
*
141
* @since 9
142
*/
143
public static SAXParserFactory newDefaultInstance() {
144
return new SAXParserFactoryImpl();
145
}
146
147
/**
148
* Obtains a new instance of a {@code SAXParserFactory}.
149
* This method uses the
150
* <a href="../../../module-summary.html#LookupMechanism">JAXP Lookup Mechanism</a>
151
* to determine the {@code SAXParserFactory} implementation class to load.
152
*
153
* <p>
154
* Once an application has obtained a reference to a
155
* {@code SAXParserFactory}, it can use the factory to
156
* configure and obtain parser instances.
157
*
158
*
159
*
160
* <h4>Tip for Trouble-shooting</h4>
161
* <p>
162
* Setting the {@code jaxp.debug} system property will cause
163
* this method to print a lot of debug messages
164
* to {@code System.err} about what it is doing and where it is looking at.
165
*
166
* <p>
167
* If you have problems loading {@link SAXParser}s, try:
168
* <pre>
169
* java -Djaxp.debug=1 YourProgram ....
170
* </pre>
171
*
172
*
173
* @return A new instance of a SAXParserFactory.
174
*
175
* @throws FactoryConfigurationError in case of {@linkplain
176
* java.util.ServiceConfigurationError service configuration error} or if
177
* the implementation is not available or cannot be instantiated.
178
*/
179
180
public static SAXParserFactory newInstance() {
181
return FactoryFinder.find(
182
/* The default property name according to the JAXP spec */
183
SAXParserFactory.class,
184
/* The fallback implementation class name */
185
DEFAULT_IMPL);
186
}
187
188
/**
189
* Obtain a new instance of a {@code SAXParserFactory} from class name.
190
* This function is useful when there are multiple providers in the classpath.
191
* It gives more control to the application as it can specify which provider
192
* should be loaded.
193
*
194
* <p>Once an application has obtained a reference to a {@code SAXParserFactory}
195
* it can use the factory to configure and obtain parser instances.
196
*
197
*
198
* <h4>Tip for Trouble-shooting</h4>
199
* <p>Setting the {@code jaxp.debug} system property will cause
200
* this method to print a lot of debug messages
201
* to {@code System.err} about what it is doing and where it is looking at.
202
*
203
* <p>
204
* If you have problems, try:
205
* <pre>
206
* java -Djaxp.debug=1 YourProgram ....
207
* </pre>
208
*
209
* @param factoryClassName fully qualified factory class name that provides implementation of {@code javax.xml.parsers.SAXParserFactory}.
210
*
211
* @param classLoader {@code ClassLoader} used to load the factory class. If {@code null}
212
* current {@code Thread}'s context classLoader is used to load the factory class.
213
*
214
* @return New instance of a {@code SAXParserFactory}
215
*
216
* @throws FactoryConfigurationError if {@code factoryClassName} is {@code null}, or
217
* the factory class cannot be loaded, instantiated.
218
*
219
* @see #newInstance()
220
*
221
* @since 1.6
222
*/
223
public static SAXParserFactory newInstance(String factoryClassName, ClassLoader classLoader){
224
//do not fallback if given classloader can't find the class, throw exception
225
return FactoryFinder.newInstance(SAXParserFactory.class,
226
factoryClassName, classLoader, false);
227
}
228
229
private static SAXParserFactory makeNSAware(SAXParserFactory spf) {
230
spf.setNamespaceAware(true);
231
return spf;
232
}
233
234
/**
235
* Creates a new instance of a SAXParser using the currently
236
* configured factory parameters.
237
*
238
* @return A new instance of a SAXParser.
239
*
240
* @throws ParserConfigurationException if a parser cannot
241
* be created which satisfies the requested configuration.
242
* @throws SAXException for SAX errors.
243
*/
244
245
public abstract SAXParser newSAXParser()
246
throws ParserConfigurationException, SAXException;
247
248
249
/**
250
* Specifies that the parser produced by this code will
251
* provide support for XML namespaces. By default the value of this is set
252
* to {@code false}.
253
*
254
* @param awareness true if the parser produced by this code will
255
* provide support for XML namespaces; false otherwise.
256
*/
257
258
public void setNamespaceAware(boolean awareness) {
259
this.namespaceAware = awareness;
260
}
261
262
/**
263
* Specifies that the parser produced by this code will
264
* validate documents as they are parsed. By default the value of this is
265
* set to {@code false}.
266
*
267
* <p>
268
* Note that "the validation" here means
269
* <a href="http://www.w3.org/TR/REC-xml#proc-types">a validating
270
* parser</a> as defined in the XML recommendation.
271
* In other words, it essentially just controls the DTD validation.
272
* (except the legacy two properties defined in JAXP 1.2.)
273
*
274
* <p>
275
* To use modern schema languages such as W3C XML Schema or
276
* RELAX NG instead of DTD, you can configure your parser to be
277
* a non-validating parser by leaving the {@link #setValidating(boolean)}
278
* method {@code false}, then use the {@link #setSchema(Schema)}
279
* method to associate a schema to a parser.
280
*
281
* @param validating true if the parser produced by this code will
282
* validate documents as they are parsed; false otherwise.
283
*/
284
285
public void setValidating(boolean validating) {
286
this.validating = validating;
287
}
288
289
/**
290
* Indicates whether or not the factory is configured to produce
291
* parsers which are namespace aware.
292
*
293
* @return true if the factory is configured to produce
294
* parsers which are namespace aware; false otherwise.
295
*/
296
297
public boolean isNamespaceAware() {
298
return namespaceAware;
299
}
300
301
/**
302
* Indicates whether or not the factory is configured to produce
303
* parsers which validate the XML content during parse.
304
*
305
* @return true if the factory is configured to produce parsers which validate
306
* the XML content during parse; false otherwise.
307
*/
308
309
public boolean isValidating() {
310
return validating;
311
}
312
313
/**
314
* Sets the particular feature in the underlying implementation of
315
* org.xml.sax.XMLReader.
316
* A list of the core features and properties can be found at
317
* <a href="http://www.saxproject.org/">http://www.saxproject.org/</a>
318
*
319
* <p>All implementations are required to support the {@link javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} feature.
320
* When the feature is
321
* <ul>
322
* <li>
323
* {@code true}: the implementation will limit XML processing to conform to implementation limits.
324
* Examples include entity expansion limits and XML Schema constructs that would consume large amounts of resources.
325
* If XML processing is limited for security reasons, it will be reported via a call to the registered
326
* {@link org.xml.sax.ErrorHandler#fatalError(SAXParseException exception)}.
327
* See {@link SAXParser} {@code parse} methods for handler specification.
328
* </li>
329
* <li>
330
* When the feature is {@code false}, the implementation will processing XML according to the XML specifications without
331
* regard to possible implementation limits.
332
* </li>
333
* </ul>
334
*
335
* @param name The name of the feature to be set.
336
* @param value The value of the feature to be set.
337
*
338
* @throws ParserConfigurationException if a parser cannot
339
* be created which satisfies the requested configuration.
340
* @throws SAXNotRecognizedException When the underlying XMLReader does
341
* not recognize the property name.
342
* @throws SAXNotSupportedException When the underlying XMLReader
343
* recognizes the property name but doesn't support the
344
* property.
345
* @throws NullPointerException If the {@code name} parameter is null.
346
*
347
* @see org.xml.sax.XMLReader#setFeature
348
*/
349
public abstract void setFeature(String name, boolean value)
350
throws ParserConfigurationException, SAXNotRecognizedException,
351
SAXNotSupportedException;
352
353
/**
354
*
355
* Returns the particular property requested for in the underlying
356
* implementation of org.xml.sax.XMLReader.
357
*
358
* @param name The name of the property to be retrieved.
359
*
360
* @return Value of the requested property.
361
*
362
* @throws ParserConfigurationException if a parser cannot be created which satisfies the requested configuration.
363
* @throws SAXNotRecognizedException When the underlying XMLReader does not recognize the property name.
364
* @throws SAXNotSupportedException When the underlying XMLReader recognizes the property name but doesn't support the property.
365
*
366
* @see org.xml.sax.XMLReader#getProperty
367
*/
368
public abstract boolean getFeature(String name)
369
throws ParserConfigurationException, SAXNotRecognizedException,
370
SAXNotSupportedException;
371
372
373
/**
374
* Gets the {@link Schema} object specified through
375
* the {@link #setSchema(Schema schema)} method.
376
*
377
*
378
* @throws UnsupportedOperationException When implementation does not
379
* override this method
380
*
381
* @return
382
* the {@link Schema} object that was last set through
383
* the {@link #setSchema(Schema)} method, or null
384
* if the method was not invoked since a {@link SAXParserFactory}
385
* is created.
386
*
387
* @since 1.5
388
*/
389
public Schema getSchema() {
390
throw new UnsupportedOperationException(
391
"This parser does not support specification \""
392
+ this.getClass().getPackage().getSpecificationTitle()
393
+ "\" version \""
394
+ this.getClass().getPackage().getSpecificationVersion()
395
+ "\""
396
);
397
}
398
399
/**
400
* Set the {@link Schema} to be used by parsers created
401
* from this factory.
402
*
403
* <p>When a {@link Schema} is non-null, a parser will use a validator
404
* created from it to validate documents before it passes information
405
* down to the application.
406
*
407
* <p>When warnings/errors/fatal errors are found by the validator, the parser must
408
* handle them as if those errors were found by the parser itself.
409
* In other words, if the user-specified {@link org.xml.sax.ErrorHandler}
410
* is set, it must receive those errors, and if not, they must be
411
* treated according to the implementation specific
412
* default error handling rules.
413
*
414
* <p>A validator may modify the SAX event stream (for example by
415
* adding default values that were missing in documents), and a parser
416
* is responsible to make sure that the application will receive
417
* those modified event stream.
418
*
419
* <p>Initially, {@code null} is set as the {@link Schema}.
420
*
421
* <p>This processing will take effect even if
422
* the {@link #isValidating()} method returns {@code false}.
423
*
424
* <p>It is an error to use
425
* the {@code http://java.sun.com/xml/jaxp/properties/schemaSource}
426
* property and/or the {@code http://java.sun.com/xml/jaxp/properties/schemaLanguage}
427
* property in conjunction with a non-null {@link Schema} object.
428
* Such configuration will cause a {@link SAXException}
429
* exception when those properties are set on a {@link SAXParser}.
430
*
431
* <h4>Note for implementors</h4>
432
* <p>
433
* A parser must be able to work with any {@link Schema}
434
* implementation. However, parsers and schemas are allowed
435
* to use implementation-specific custom mechanisms
436
* as long as they yield the result described in the specification.
437
*
438
* @param schema {@code Schema} to use, {@code null} to remove a schema.
439
*
440
* @throws UnsupportedOperationException When implementation does not
441
* override this method
442
*
443
* @since 1.5
444
*/
445
public void setSchema(Schema schema) {
446
throw new UnsupportedOperationException(
447
"This parser does not support specification \""
448
+ this.getClass().getPackage().getSpecificationTitle()
449
+ "\" version \""
450
+ this.getClass().getPackage().getSpecificationVersion()
451
+ "\""
452
);
453
}
454
455
/**
456
* Set state of XInclude processing.
457
*
458
* <p>If XInclude markup is found in the document instance, should it be
459
* processed as specified in <a href="http://www.w3.org/TR/xinclude/">
460
* XML Inclusions (XInclude) Version 1.0</a>.
461
*
462
* <p>XInclude processing defaults to {@code false}.
463
*
464
* @param state Set XInclude processing to {@code true} or
465
* {@code false}
466
*
467
* @throws UnsupportedOperationException When implementation does not
468
* override this method
469
*
470
* @since 1.5
471
*/
472
public void setXIncludeAware(final boolean state) {
473
if (state) {
474
throw new UnsupportedOperationException(" setXIncludeAware " +
475
"is not supported on this JAXP" +
476
" implementation or earlier: " + this.getClass());
477
}
478
}
479
480
/**
481
* Get state of XInclude processing.
482
*
483
* @return current state of XInclude processing
484
*
485
* @throws UnsupportedOperationException When implementation does not
486
* override this method
487
*
488
* @since 1.5
489
*/
490
public boolean isXIncludeAware() {
491
throw new UnsupportedOperationException(
492
"This parser does not support specification \""
493
+ this.getClass().getPackage().getSpecificationTitle()
494
+ "\" version \""
495
+ this.getClass().getPackage().getSpecificationVersion()
496
+ "\""
497
);
498
}
499
}
500
501