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/DocumentBuilderFactory.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.DocumentBuilderFactoryImpl;
29
import javax.xml.validation.Schema;
30
31
/**
32
* Defines a factory API that enables applications to obtain a
33
* parser that produces DOM object trees from XML documents.
34
*
35
* @author Jeff Suttor
36
* @author Neeraj Bajaj
37
*
38
* @since 1.4
39
*/
40
41
public abstract class DocumentBuilderFactory {
42
private static final String DEFAULT_IMPL =
43
"com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl";
44
private boolean validating = false;
45
private boolean namespaceAware = false;
46
private boolean whitespace = false;
47
private boolean expandEntityRef = true;
48
private boolean ignoreComments = false;
49
private boolean coalescing = false;
50
51
/**
52
* Protected constructor to prevent instantiation.
53
* Use {@link #newInstance()}.
54
*/
55
protected DocumentBuilderFactory () {
56
}
57
58
/**
59
* Creates a new NamespaceAware instance of the {@code DocumentBuilderFactory}
60
* builtin system-default implementation. Parsers produced by the factory
61
* instance provides support for XML namespaces by default.
62
*
63
* @implSpec
64
* In addition to creating a factory instance using the same process as
65
* {@link #newDefaultInstance()}, this method must set NamespaceAware to true.
66
*
67
* @return a new instance of the {@code DocumentBuilderFactory} builtin
68
* system-default implementation.
69
*
70
* @since 13
71
*/
72
public static DocumentBuilderFactory newDefaultNSInstance() {
73
return makeNSAware(new DocumentBuilderFactoryImpl());
74
}
75
76
/**
77
* Creates a new NamespaceAware instance of a {@code DocumentBuilderFactory}.
78
* Parsers produced by the factory instance provides support for XML namespaces
79
* by default.
80
*
81
* @implSpec
82
* In addition to creating a factory instance using the same process as
83
* {@link #newInstance()}, this method must set NamespaceAware to true.
84
*
85
* @return a new instance of a {@code DocumentBuilderFactory}
86
*
87
* @throws FactoryConfigurationError in case of {@linkplain
88
* java.util.ServiceConfigurationError service configuration error}
89
* or if the implementation is not available or cannot be instantiated.
90
*
91
* @since 13
92
*/
93
public static DocumentBuilderFactory newNSInstance() {
94
return makeNSAware(FactoryFinder.find(DocumentBuilderFactory.class, DEFAULT_IMPL));
95
}
96
97
/**
98
* Creates a new NamespaceAware instance of a {@code DocumentBuilderFactory}
99
* from the class name. Parsers produced by the factory instance provides
100
* support for XML namespaces by default.
101
*
102
* @implSpec
103
* In addition to creating a factory instance using the same process as
104
* {@link #newInstance(java.lang.String, java.lang.ClassLoader)}, this method
105
* must set NamespaceAware to true.
106
*
107
* @param factoryClassName a fully qualified factory class name that provides
108
* implementation of
109
* {@code javax.xml.parsers.DocumentBuilderFactory}.
110
*
111
* @param classLoader the {@code ClassLoader} used to load the factory class.
112
* If it is {@code null}, the current {@code Thread}'s
113
* context classLoader is used to load the factory class.
114
*
115
* @return a new instance of a {@code DocumentBuilderFactory}
116
*
117
* @throws FactoryConfigurationError if {@code factoryClassName} is {@code null}, or
118
* the factory class cannot be loaded, instantiated.
119
*
120
* @since 13
121
*/
122
public static DocumentBuilderFactory newNSInstance(String factoryClassName,
123
ClassLoader classLoader) {
124
return makeNSAware(FactoryFinder.newInstance(
125
DocumentBuilderFactory.class, factoryClassName, classLoader, false));
126
}
127
128
/**
129
* Creates a new instance of the {@code DocumentBuilderFactory} builtin
130
* system-default implementation.
131
*
132
* @return A new instance of the {@code DocumentBuilderFactory} builtin
133
* system-default implementation.
134
*
135
* @since 9
136
*/
137
public static DocumentBuilderFactory newDefaultInstance() {
138
return new DocumentBuilderFactoryImpl();
139
}
140
141
/**
142
* Obtains a new instance of a {@code DocumentBuilderFactory}.
143
* This method uses the
144
* <a href="../../../module-summary.html#LookupMechanism">JAXP Lookup Mechanism</a>
145
* to determine the {@code DocumentBuilderFactory} implementation class to load.
146
*
147
* <p>
148
* Once an application has obtained a reference to a
149
* {@code DocumentBuilderFactory}, it can use the factory to
150
* configure and obtain parser instances.
151
*
152
*
153
* <h4>Tip for Trouble-shooting</h4>
154
* <p>
155
* Setting the {@code jaxp.debug} system property will cause
156
* this method to print a lot of debug messages
157
* to {@code System.err} about what it is doing and where it is looking at.
158
*
159
* <p>
160
* If you have problems loading {@link DocumentBuilder}s, try:
161
* <pre>
162
* java -Djaxp.debug=1 YourProgram ....
163
* </pre>
164
*
165
* @return New instance of a {@code DocumentBuilderFactory}
166
*
167
* @throws FactoryConfigurationError in case of {@linkplain
168
* java.util.ServiceConfigurationError service configuration error} or if
169
* the implementation is not available or cannot be instantiated.
170
*/
171
public static DocumentBuilderFactory newInstance() {
172
return FactoryFinder.find(
173
/* The default property name according to the JAXP spec */
174
DocumentBuilderFactory.class, // "javax.xml.parsers.DocumentBuilderFactory"
175
/* The fallback implementation class name */
176
DEFAULT_IMPL);
177
}
178
179
/**
180
* Obtain a new instance of a {@code DocumentBuilderFactory} from class name.
181
* This function is useful when there are multiple providers in the classpath.
182
* It gives more control to the application as it can specify which provider
183
* should be loaded.
184
*
185
* <p>Once an application has obtained a reference to a {@code DocumentBuilderFactory}
186
* it can use the factory to configure and obtain parser instances.
187
*
188
*
189
* <h4>Tip for Trouble-shooting</h4>
190
* <p>Setting the {@code jaxp.debug} system property will cause
191
* this method to print a lot of debug messages
192
* to {@code System.err} about what it is doing and where it is looking at.
193
*
194
* <p> If you have problems try:
195
* <pre>
196
* java -Djaxp.debug=1 YourProgram ....
197
* </pre>
198
*
199
* @param factoryClassName fully qualified factory class name that provides
200
* implementation of {@code javax.xml.parsers.DocumentBuilderFactory}.
201
*
202
* @param classLoader {@code ClassLoader} used to load the factory class. If {@code null}
203
* current {@code Thread}'s context classLoader is used to load the factory class.
204
*
205
* @return New instance of a {@code DocumentBuilderFactory}
206
*
207
* @throws FactoryConfigurationError if {@code factoryClassName} is {@code null}, or
208
* the factory class cannot be loaded, instantiated.
209
*
210
* @see #newInstance()
211
*
212
* @since 1.6
213
*/
214
public static DocumentBuilderFactory newInstance(String factoryClassName, ClassLoader classLoader){
215
//do not fallback if given classloader can't find the class, throw exception
216
return FactoryFinder.newInstance(DocumentBuilderFactory.class,
217
factoryClassName, classLoader, false);
218
}
219
220
private static DocumentBuilderFactory makeNSAware(DocumentBuilderFactory dbf) {
221
dbf.setNamespaceAware(true);
222
return dbf;
223
}
224
225
/**
226
* Creates a new instance of a {@link javax.xml.parsers.DocumentBuilder}
227
* using the currently configured parameters.
228
*
229
* @return A new instance of a DocumentBuilder.
230
*
231
* @throws ParserConfigurationException if a DocumentBuilder
232
* cannot be created which satisfies the configuration requested.
233
*/
234
235
public abstract DocumentBuilder newDocumentBuilder()
236
throws ParserConfigurationException;
237
238
239
/**
240
* Specifies that the parser produced by this code will
241
* provide support for XML namespaces. By default the value of this is set
242
* to {@code false}
243
*
244
* @param awareness true if the parser produced will provide support
245
* for XML namespaces; false otherwise.
246
*/
247
248
public void setNamespaceAware(boolean awareness) {
249
this.namespaceAware = awareness;
250
}
251
252
/**
253
* Specifies that the parser produced by this code will
254
* validate documents as they are parsed. By default the value of this
255
* is set to {@code false}.
256
*
257
* <p>
258
* Note that "the validation" here means
259
* <a href="http://www.w3.org/TR/REC-xml#proc-types">a validating
260
* parser</a> as defined in the XML recommendation.
261
* In other words, it essentially just controls the DTD validation.
262
* (except the legacy two properties defined in JAXP 1.2.)
263
*
264
* <p>
265
* To use modern schema languages such as W3C XML Schema or
266
* RELAX NG instead of DTD, you can configure your parser to be
267
* a non-validating parser by leaving the {@link #setValidating(boolean)}
268
* method {@code false}, then use the {@link #setSchema(Schema)}
269
* method to associate a schema to a parser.
270
*
271
* @param validating true if the parser produced will validate documents
272
* as they are parsed; false otherwise.
273
*/
274
275
public void setValidating(boolean validating) {
276
this.validating = validating;
277
}
278
279
/**
280
* Specifies that the parsers created by this factory must eliminate
281
* whitespace in element content (sometimes known loosely as
282
* 'ignorable whitespace') when parsing XML documents (see XML Rec
283
* 2.10). Note that only whitespace which is directly contained within
284
* element content that has an element only content model (see XML
285
* Rec 3.2.1) will be eliminated. Due to reliance on the content model
286
* this setting requires the parser to be in validating mode. By default
287
* the value of this is set to {@code false}.
288
*
289
* @param whitespace true if the parser created must eliminate whitespace
290
* in the element content when parsing XML documents;
291
* false otherwise.
292
*/
293
294
public void setIgnoringElementContentWhitespace(boolean whitespace) {
295
this.whitespace = whitespace;
296
}
297
298
/**
299
* Specifies that the parser produced by this code will
300
* expand entity reference nodes. By default the value of this is set to
301
* {@code true}
302
*
303
* @param expandEntityRef true if the parser produced will expand entity
304
* reference nodes; false otherwise.
305
*/
306
307
public void setExpandEntityReferences(boolean expandEntityRef) {
308
this.expandEntityRef = expandEntityRef;
309
}
310
311
/**
312
* Specifies that the parser produced by this code will
313
* ignore comments. By default the value of this is set to {@code false}.
314
*
315
* @param ignoreComments {@code boolean} value to ignore comments during processing
316
*/
317
318
public void setIgnoringComments(boolean ignoreComments) {
319
this.ignoreComments = ignoreComments;
320
}
321
322
/**
323
* Specifies that the parser produced by this code will
324
* convert CDATA nodes to Text nodes and append it to the
325
* adjacent (if any) text node. By default the value of this is set to
326
* {@code false}
327
*
328
* @param coalescing true if the parser produced will convert CDATA nodes
329
* to Text nodes and append it to the adjacent (if any)
330
* text node; false otherwise.
331
*/
332
333
public void setCoalescing(boolean coalescing) {
334
this.coalescing = coalescing;
335
}
336
337
/**
338
* Indicates whether or not the factory is configured to produce
339
* parsers which are namespace aware.
340
*
341
* @return true if the factory is configured to produce parsers which
342
* are namespace aware; false otherwise.
343
*/
344
345
public boolean isNamespaceAware() {
346
return namespaceAware;
347
}
348
349
/**
350
* Indicates whether or not the factory is configured to produce
351
* parsers which validate the XML content during parse.
352
*
353
* @return true if the factory is configured to produce parsers
354
* which validate the XML content during parse; false otherwise.
355
*/
356
357
public boolean isValidating() {
358
return validating;
359
}
360
361
/**
362
* Indicates whether or not the factory is configured to produce
363
* parsers which ignore ignorable whitespace in element content.
364
*
365
* @return true if the factory is configured to produce parsers
366
* which ignore ignorable whitespace in element content;
367
* false otherwise.
368
*/
369
370
public boolean isIgnoringElementContentWhitespace() {
371
return whitespace;
372
}
373
374
/**
375
* Indicates whether or not the factory is configured to produce
376
* parsers which expand entity reference nodes.
377
*
378
* @return true if the factory is configured to produce parsers
379
* which expand entity reference nodes; false otherwise.
380
*/
381
382
public boolean isExpandEntityReferences() {
383
return expandEntityRef;
384
}
385
386
/**
387
* Indicates whether or not the factory is configured to produce
388
* parsers which ignores comments.
389
*
390
* @return true if the factory is configured to produce parsers
391
* which ignores comments; false otherwise.
392
*/
393
394
public boolean isIgnoringComments() {
395
return ignoreComments;
396
}
397
398
/**
399
* Indicates whether or not the factory is configured to produce
400
* parsers which converts CDATA nodes to Text nodes and appends it to
401
* the adjacent (if any) Text node.
402
*
403
* @return true if the factory is configured to produce parsers
404
* which converts CDATA nodes to Text nodes and appends it to
405
* the adjacent (if any) Text node; false otherwise.
406
*/
407
408
public boolean isCoalescing() {
409
return coalescing;
410
}
411
412
/**
413
* Allows the user to set specific attributes on the underlying
414
* implementation.
415
* <p>
416
* All implementations that implement JAXP 1.5 or newer are required to
417
* support the {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_DTD} and
418
* {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_SCHEMA} properties.
419
*
420
* <ul>
421
* <li>
422
* Setting the {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_DTD} property
423
* restricts the access to external DTDs, external Entity References to the
424
* protocols specified by the property.
425
* If access is denied during parsing due to the restriction of this property,
426
* {@link org.xml.sax.SAXException} will be thrown by the parse methods defined by
427
* {@link javax.xml.parsers.DocumentBuilder}.
428
* </li>
429
* <li>
430
* Setting the {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_SCHEMA} property
431
* restricts the access to external Schema set by the schemaLocation attribute to
432
* the protocols specified by the property. If access is denied during parsing
433
* due to the restriction of this property, {@link org.xml.sax.SAXException}
434
* will be thrown by the parse methods defined by
435
* {@link javax.xml.parsers.DocumentBuilder}.
436
* </li>
437
* </ul>
438
*
439
* @param name The name of the attribute.
440
* @param value The value of the attribute.
441
*
442
* @throws IllegalArgumentException thrown if the underlying
443
* implementation doesn't recognize the attribute.
444
*/
445
public abstract void setAttribute(String name, Object value)
446
throws IllegalArgumentException;
447
448
/**
449
* Allows the user to retrieve specific attributes on the underlying
450
* implementation.
451
*
452
* @param name The name of the attribute.
453
*
454
* @return value The value of the attribute.
455
*
456
* @throws IllegalArgumentException thrown if the underlying
457
* implementation doesn't recognize the attribute.
458
*/
459
public abstract Object getAttribute(String name)
460
throws IllegalArgumentException;
461
462
/**
463
* Set a feature for this {@code DocumentBuilderFactory}
464
* and {@code DocumentBuilder}s created by this factory.
465
*
466
* <p>
467
* Feature names are fully qualified {@link java.net.URI}s.
468
* Implementations may define their own features.
469
* A {@link ParserConfigurationException} is thrown if this {@code DocumentBuilderFactory} or the
470
* {@code DocumentBuilder}s it creates cannot support the feature.
471
* It is possible for a {@code DocumentBuilderFactory} to expose a feature value but be unable to change its state.
472
*
473
*
474
* <p>
475
* All implementations are required to support the {@link javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} feature.
476
* When the feature is:
477
* <ul>
478
* <li>
479
* {@code true}: the implementation will limit XML processing to conform to implementation limits.
480
* Examples include entity expansion limits and XML Schema constructs that would consume large amounts of resources.
481
* If XML processing is limited for security reasons, it will be reported via a call to the registered
482
* {@link org.xml.sax.ErrorHandler#fatalError(SAXParseException exception)}.
483
* See {@link DocumentBuilder#setErrorHandler(org.xml.sax.ErrorHandler errorHandler)}.
484
* </li>
485
* <li>
486
* {@code false}: the implementation will processing XML according to the XML specifications without
487
* regard to possible implementation limits.
488
* </li>
489
* </ul>
490
*
491
* @param name Feature name.
492
* @param value Is feature state {@code true} or {@code false}.
493
*
494
* @throws ParserConfigurationException if this {@code DocumentBuilderFactory} or the {@code DocumentBuilder}s
495
* it creates cannot support this feature.
496
* @throws NullPointerException If the {@code name} parameter is null.
497
* @since 1.5
498
*/
499
public abstract void setFeature(String name, boolean value)
500
throws ParserConfigurationException;
501
502
/**
503
* Get the state of the named feature.
504
*
505
* <p>
506
* Feature names are fully qualified {@link java.net.URI}s.
507
* Implementations may define their own features.
508
* An {@link ParserConfigurationException} is thrown if this {@code DocumentBuilderFactory} or the
509
* {@code DocumentBuilder}s it creates cannot support the feature.
510
* It is possible for an {@code DocumentBuilderFactory} to expose a feature value but be unable to change its state.
511
*
512
* @param name Feature name.
513
*
514
* @return State of the named feature.
515
*
516
* @throws ParserConfigurationException if this {@code DocumentBuilderFactory}
517
* or the {@code DocumentBuilder}s it creates cannot support this feature.
518
* @since 1.5
519
*/
520
public abstract boolean getFeature(String name)
521
throws ParserConfigurationException;
522
523
524
/**
525
* Gets the {@link Schema} object specified through
526
* the {@link #setSchema(Schema schema)} method.
527
*
528
* @return
529
* the {@link Schema} object that was last set through
530
* the {@link #setSchema(Schema)} method, or null
531
* if the method was not invoked since a {@link DocumentBuilderFactory}
532
* is created.
533
*
534
* @throws UnsupportedOperationException When implementation does not
535
* override this method.
536
*
537
* @since 1.5
538
*/
539
public Schema getSchema() {
540
throw new UnsupportedOperationException(
541
"This parser does not support specification \""
542
+ this.getClass().getPackage().getSpecificationTitle()
543
+ "\" version \""
544
+ this.getClass().getPackage().getSpecificationVersion()
545
+ "\""
546
);
547
548
}
549
550
/**
551
* Set the {@link Schema} to be used by parsers created
552
* from this factory.
553
*
554
* <p>
555
* When a {@link Schema} is non-null, a parser will use a validator
556
* created from it to validate documents before it passes information
557
* down to the application.
558
*
559
* <p>When errors are found by the validator, the parser is responsible
560
* to report them to the user-specified {@link org.xml.sax.ErrorHandler}
561
* (or if the error handler is not set, ignore them or throw them), just
562
* like any other errors found by the parser itself.
563
* In other words, if the user-specified {@link org.xml.sax.ErrorHandler}
564
* is set, it must receive those errors, and if not, they must be
565
* treated according to the implementation specific
566
* default error handling rules.
567
*
568
* <p>
569
* A validator may modify the outcome of a parse (for example by
570
* adding default values that were missing in documents), and a parser
571
* is responsible to make sure that the application will receive
572
* modified DOM trees.
573
*
574
* <p>
575
* Initially, null is set as the {@link Schema}.
576
*
577
* <p>
578
* This processing will take effect even if
579
* the {@link #isValidating()} method returns {@code false}.
580
*
581
* <p>It is an error to use
582
* the {@code http://java.sun.com/xml/jaxp/properties/schemaSource}
583
* property and/or the {@code http://java.sun.com/xml/jaxp/properties/schemaLanguage}
584
* property in conjunction with a {@link Schema} object.
585
* Such configuration will cause a {@link ParserConfigurationException}
586
* exception when the {@link #newDocumentBuilder()} is invoked.
587
*
588
*
589
* <h4>Note for implementors</h4>
590
*
591
* <p>
592
* A parser must be able to work with any {@link Schema}
593
* implementation. However, parsers and schemas are allowed
594
* to use implementation-specific custom mechanisms
595
* as long as they yield the result described in the specification.
596
*
597
*
598
* @param schema {@code Schema} to use or {@code null}
599
* to remove a schema.
600
*
601
* @throws UnsupportedOperationException When implementation does not
602
* override this method.
603
*
604
* @since 1.5
605
*/
606
public void setSchema(Schema schema) {
607
throw new UnsupportedOperationException(
608
"This parser does not support specification \""
609
+ this.getClass().getPackage().getSpecificationTitle()
610
+ "\" version \""
611
+ this.getClass().getPackage().getSpecificationVersion()
612
+ "\""
613
);
614
}
615
616
617
618
/**
619
* Set state of XInclude processing.
620
*
621
* <p>If XInclude markup is found in the document instance, should it be
622
* processed as specified in <a href="http://www.w3.org/TR/xinclude/">
623
* XML Inclusions (XInclude) Version 1.0</a>.
624
*
625
* <p>XInclude processing defaults to {@code false}.
626
*
627
* @param state Set XInclude processing to {@code true} or
628
* {@code false}
629
*
630
* @throws UnsupportedOperationException When implementation does not
631
* override this method.
632
*
633
* @since 1.5
634
*/
635
public void setXIncludeAware(final boolean state) {
636
if (state) {
637
throw new UnsupportedOperationException(" setXIncludeAware " +
638
"is not supported on this JAXP" +
639
" implementation or earlier: " + this.getClass());
640
}
641
}
642
643
/**
644
* Get state of XInclude processing.
645
*
646
* @return current state of XInclude processing
647
*
648
* @throws UnsupportedOperationException When implementation does not
649
* override this method.
650
*
651
* @since 1.5
652
*/
653
public boolean isXIncludeAware() {
654
throw new UnsupportedOperationException(
655
"This parser does not support specification \""
656
+ this.getClass().getPackage().getSpecificationTitle()
657
+ "\" version \""
658
+ this.getClass().getPackage().getSpecificationVersion()
659
+ "\""
660
);
661
}
662
}
663
664