2
* Copyright 2003-2004 The Apache Software Foundation.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
// $Id: DocumentBuilderFactory.java 226183 2005-04-08 10:39:14Z neeraj $
19
package javax.xml.parsers;
21
import javax.xml.validation.Schema;
24
* Defines a factory API that enables applications to obtain a
25
* parser that produces DOM object trees from XML documents.
27
* @author <a href="Jeff.Suttor@Sun.com">Jeff Suttor</a>
28
* @version $Revision: 226183 $, $Date: 2005-04-08 06:39:14 -0400 (Fri, 08 Apr 2005) $
31
public abstract class DocumentBuilderFactory {
33
/** The default property name according to the JAXP spec */
34
private static final String DEFAULT_PROPERTY_NAME = "javax.xml.parsers.DocumentBuilderFactory";
36
private boolean validating = false;
37
private boolean namespaceAware = false;
38
private boolean whitespace = false;
39
private boolean expandEntityRef = true;
40
private boolean ignoreComments = false;
41
private boolean coalescing = false;
43
private boolean canonicalState = false;
45
protected DocumentBuilderFactory () {
49
* Obtain a new instance of a
50
* <code>DocumentBuilderFactory</code>. This static method creates
51
* a new factory instance.
52
* This method uses the following ordered lookup procedure to determine
53
* the <code>DocumentBuilderFactory</code> implementation class to
57
* Use the <code>javax.xml.parsers.DocumentBuilderFactory</code> system
61
* Use the properties file "lib/jaxp.properties" in the JRE directory.
62
* This configuration file is in standard <code>java.util.Properties
63
* </code> format and contains the fully qualified name of the
64
* implementation class with the key being the system property defined
67
* The jaxp.properties file is read only once by the JAXP implementation
68
* and it's values are then cached for future use. If the file does not exist
69
* when the first attempt is made to read from it, no further attempts are
70
* made to check for its existence. It is not possible to change the value
71
* of any property in jaxp.properties after it has been read for the first time.
74
* Use the Services API (as detailed in the JAR specification), if
75
* available, to determine the classname. The Services API will look
76
* for a classname in the file
77
* <code>META-INF/services/javax.xml.parsers.DocumentBuilderFactory</code>
78
* in jars available to the runtime.
81
* Platform default <code>DocumentBuilderFactory</code> instance.
85
* Once an application has obtained a reference to a
86
* <code>DocumentBuilderFactory</code> it can use the factory to
87
* configure and obtain parser instances.
90
* <h2>Tip for Trouble-shooting</h2>
91
* <p>Setting the <code>jaxp.debug</code> system property will cause
92
* this method to print a lot of debug messages
93
* to <tt>System.err</tt> about what it is doing and where it is looking at.</p>
95
* <p> If you have problems loading {@link DocumentBuilder}s, try:</p>
97
* java -Djaxp.debug=1 YourProgram ....
100
* @return New instance of a <code>DocumentBuilderFactory</code>
102
* @exception FactoryConfigurationError if the implementation is not
103
* available or cannot be instantiated.
105
public static DocumentBuilderFactory newInstance() {
107
return (DocumentBuilderFactory) FactoryFinder.find(
108
/* The default property name according to the JAXP spec */
109
"javax.xml.parsers.DocumentBuilderFactory",
110
/* The fallback implementation class name */
111
"org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
112
} catch (FactoryFinder.ConfigurationError e) {
113
throw new FactoryConfigurationError(e.getException(),
120
* Creates a new instance of a {@link javax.xml.parsers.DocumentBuilder}
121
* using the currently configured parameters.
123
* @exception ParserConfigurationException if a DocumentBuilder
124
* cannot be created which satisfies the configuration requested.
125
* @return A new instance of a DocumentBuilder.
128
public abstract DocumentBuilder newDocumentBuilder()
129
throws ParserConfigurationException;
133
* Specifies that the parser produced by this code will
134
* provide support for XML namespaces. By default the value of this is set
135
* to <code>false</code>
137
* @param awareness true if the parser produced will provide support
138
* for XML namespaces; false otherwise.
141
public void setNamespaceAware(boolean awareness) {
142
this.namespaceAware = awareness;
146
* Specifies that the parser produced by this code will
147
* validate documents as they are parsed. By default the value of this
148
* is set to <code>false</code>.
151
* Note that "the validation" here means
152
* <a href="http://www.w3.org/TR/REC-xml#proc-types">a validating
153
* parser</a> as defined in the XML recommendation.
154
* In other words, it essentially just controls the DTD validation.
155
* (except the legacy two properties defined in JAXP 1.2.
156
* See <a href="#validationCompatibility">here</a> for more details.)
160
* To use modern schema languages such as W3C XML Schema or
161
* RELAX NG instead of DTD, you can configure your parser to be
162
* a non-validating parser by leaving the {@link #setValidating(boolean)}
163
* method <tt>false</tt>, then use the {@link #setSchema(Schema)}
164
* method to associate a schema to a parser.
167
* @param validating true if the parser produced will validate documents
168
* as they are parsed; false otherwise.
171
public void setValidating(boolean validating) {
172
this.validating = validating;
176
* Specifies that the parsers created by this factory must eliminate
177
* whitespace in element content (sometimes known loosely as
178
* 'ignorable whitespace') when parsing XML documents (see XML Rec
179
* 2.10). Note that only whitespace which is directly contained within
180
* element content that has an element only content model (see XML
181
* Rec 3.2.1) will be eliminated. Due to reliance on the content model
182
* this setting requires the parser to be in validating mode. By default
183
* the value of this is set to <code>false</code>.
185
* @param whitespace true if the parser created must eliminate whitespace
186
* in the element content when parsing XML documents;
190
public void setIgnoringElementContentWhitespace(boolean whitespace) {
191
this.whitespace = whitespace;
195
* Specifies that the parser produced by this code will
196
* expand entity reference nodes. By default the value of this is set to
199
* @param expandEntityRef true if the parser produced will expand entity
200
* reference nodes; false otherwise.
203
public void setExpandEntityReferences(boolean expandEntityRef) {
204
this.expandEntityRef = expandEntityRef;
208
* <p>Specifies that the parser produced by this code will
209
* ignore comments. By default the value of this is set to <code>false
212
* @param ignoreComments <code>boolean</code> value to ignore comments during processing
215
public void setIgnoringComments(boolean ignoreComments) {
216
this.ignoreComments = ignoreComments;
220
* Specifies that the parser produced by this code will
221
* convert CDATA nodes to Text nodes and append it to the
222
* adjacent (if any) text node. By default the value of this is set to
225
* @param coalescing true if the parser produced will convert CDATA nodes
226
* to Text nodes and append it to the adjacent (if any)
227
* text node; false otherwise.
230
public void setCoalescing(boolean coalescing) {
231
this.coalescing = coalescing;
235
* Indicates whether or not the factory is configured to produce
236
* parsers which are namespace aware.
238
* @return true if the factory is configured to produce parsers which
239
* are namespace aware; false otherwise.
242
public boolean isNamespaceAware() {
243
return namespaceAware;
247
* Indicates whether or not the factory is configured to produce
248
* parsers which validate the XML content during parse.
250
* @return true if the factory is configured to produce parsers
251
* which validate the XML content during parse; false otherwise.
254
public boolean isValidating() {
259
* Indicates whether or not the factory is configured to produce
260
* parsers which ignore ignorable whitespace in element content.
262
* @return true if the factory is configured to produce parsers
263
* which ignore ignorable whitespace in element content;
267
public boolean isIgnoringElementContentWhitespace() {
272
* Indicates whether or not the factory is configured to produce
273
* parsers which expand entity reference nodes.
275
* @return true if the factory is configured to produce parsers
276
* which expand entity reference nodes; false otherwise.
279
public boolean isExpandEntityReferences() {
280
return expandEntityRef;
284
* Indicates whether or not the factory is configured to produce
285
* parsers which ignores comments.
287
* @return true if the factory is configured to produce parsers
288
* which ignores comments; false otherwise.
291
public boolean isIgnoringComments() {
292
return ignoreComments;
296
* Indicates whether or not the factory is configured to produce
297
* parsers which converts CDATA nodes to Text nodes and appends it to
298
* the adjacent (if any) Text node.
300
* @return true if the factory is configured to produce parsers
301
* which converts CDATA nodes to Text nodes and appends it to
302
* the adjacent (if any) Text node; false otherwise.
305
public boolean isCoalescing() {
310
* Allows the user to set specific attributes on the underlying
312
* @param name The name of the attribute.
313
* @param value The value of the attribute.
314
* @exception IllegalArgumentException thrown if the underlying
315
* implementation doesn't recognize the attribute.
317
public abstract void setAttribute(String name, Object value)
318
throws IllegalArgumentException;
321
* Allows the user to retrieve specific attributes on the underlying
323
* @param name The name of the attribute.
324
* @return value The value of the attribute.
325
* @exception IllegalArgumentException thrown if the underlying
326
* implementation doesn't recognize the attribute.
328
public abstract Object getAttribute(String name)
329
throws IllegalArgumentException;
332
* <p>Set a feature for this <code>DocumentBuilderFactory</code> and <code>DocumentBuilder</code>s created by this factory.</p>
335
* Feature names are fully qualified {@link java.net.URI}s.
336
* Implementations may define their own features.
337
* An {@link ParserConfigurationException} is thrown if this <code>DocumentBuilderFactory</code> or the
338
* <code>DocumentBuilder</code>s it creates cannot support the feature.
339
* It is possible for an <code>DocumentBuilderFactory</code> to expose a feature value but be unable to change its state.
343
* All implementations are required to support the {@link javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} feature.
344
* When the feature is:</p>
347
* <code>true</code>: the implementation will limit XML processing to conform to implementation limits.
348
* Examples include enity expansion limits and XML Schema constructs that would consume large amounts of resources.
349
* If XML processing is limited for security reasons, it will be reported via a call to the registered
350
* {@link org.xml.sax.ErrorHandler#fatalError(SAXParseException exception)}.
351
* See {@link DocumentBuilder#setErrorHandler(org.xml.sax.ErrorHandler errorHandler)}.
354
* <code>false</code>: the implementation will processing XML according to the XML specifications without
355
* regard to possible implementation limits.
359
* @param name Feature name.
360
* @param value Is feature state <code>true</code> or <code>false</code>.
362
* @throws ParserConfigurationException if this <code>DocumentBuilderFactory</code> or the <code>DocumentBuilder</code>s
363
* it creates cannot support this feature.
364
* @throws NullPointerException If the <code>name</code> parameter is null.
366
public abstract void setFeature(String name, boolean value)
367
throws ParserConfigurationException;
370
* <p>Get the state of the named feature.</p>
373
* Feature names are fully qualified {@link java.net.URI}s.
374
* Implementations may define their own features.
375
* An {@link ParserConfigurationException} is thrown if this <code>DocumentBuilderFactory</code> or the
376
* <code>DocumentBuilder</code>s it creates cannot support the feature.
377
* It is possible for an <code>DocumentBuilderFactory</code> to expose a feature value but be unable to change its state.
380
* @param name Feature name.
382
* @return State of the named feature.
384
* @throws ParserConfigurationException if this <code>DocumentBuilderFactory</code>
385
* or the <code>DocumentBuilder</code>s it creates cannot support this feature.
387
public abstract boolean getFeature(String name)
388
throws ParserConfigurationException;
391
/** <p>Get current state of canonicalization.</p>
393
* @return current state canonicalization control
396
public boolean getCanonicalization() {
397
return canonicalState;
403
* Gets the {@link Schema} object specified through
404
* the {@link #setSchema(Schema schema)} method.
407
* @throws UnsupportedOperationException
408
* For backward compatibility, when implementations for
409
* earlier versions of JAXP is used, this exception will be
413
* the {@link Schema} object that was last set through
414
* the {@link #setSchema(Schema)} method, or null
415
* if the method was not invoked since a {@link SAXParserFactory}
420
public Schema getSchema() {
421
throw new UnsupportedOperationException(
422
"This parser does not support specification \""
423
+ this.getClass().getPackage().getSpecificationTitle()
425
+ this.getClass().getPackage().getSpecificationVersion()
431
/* <p>Set canonicalization control to <code>true</code> or
432
* </code>false</code>.</p>
434
* @param state of canonicalization
437
public void setCanonicalization(boolean state) {
438
canonicalState = state;
443
* <p>Set the {@link Schema} to be used by parsers created
447
* When a {@link Schema} is non-null, a parser will use a validator
448
* created from it to validate documents before it passes information
449
* down to the application.
451
* <p>When errors are found by the validator, the parser is responsible
452
* to report them to the user-specified {@link org.w3c.dom.DOMErrorHandler}
453
* (or if the error handler is not set, ignore them or throw them), just
454
* like any other errors found by the parser itself.
455
* In other words, if the user-specified {@link org.w3c.dom.DOMErrorHandler}
456
* is set, it must receive those errors, and if not, they must be
457
* treated according to the implementation specific
458
* default error handling rules.
461
* A validator may modify the outcome of a parse (for example by
462
* adding default values that were missing in documents), and a parser
463
* is responsible to make sure that the application will receive
464
* modified DOM trees.
467
* Initialy, null is set as the {@link Schema}.
470
* This processing will take effect even if
471
* the {@link #isValidating()} method returns <tt>false</tt>.
473
* <p>It is an error to use
474
* the <code>http://java.sun.com/xml/jaxp/properties/schemaSource</code>
475
* property and/or the <code>http://java.sun.com/xml/jaxp/properties/schemaLanguage</code>
476
* property in conjunction with a {@link Schema} object.
477
* Such configuration will cause a {@link ParserConfigurationException}
478
* exception when the {@link #newDocumentBuilder()} is invoked.</p>
481
* <h4>Note for implmentors</h4>
483
* A parser must be able to work with any {@link Schema}
484
* implementation. However, parsers and schemas are allowed
485
* to use implementation-specific custom mechanisms
486
* as long as they yield the result described in the specification.
488
* @param schema <code>Schema</code> to use or <code>null</code> to remove a schema.
490
* @throws UnsupportedOperationException
491
* For backward compatibility, when implementations for
492
* earlier versions of JAXP is used, this exception will be
497
public void setSchema(Schema schema) {
498
throw new UnsupportedOperationException(
499
"This parser does not support specification \""
500
+ this.getClass().getPackage().getSpecificationTitle()
502
+ this.getClass().getPackage().getSpecificationVersion()
510
* <p>Set state of XInclude processing.</p>
512
* <p>If XInclude markup is found in the document instance, should it be
513
* processed as specified in <a href="http://www.w3.org/TR/xinclude/">
514
* XML Inclusions (XInclude) Version 1.0</a>.</p>
516
* <p>XInclude processing defaults to <code>false</code>.</p>
518
* @param state Set XInclude processing to <code>true</code> or
521
* @throws UnsupportedOperationException
522
* For backward compatibility, when implementations for
523
* earlier versions of JAXP is used, this exception will be
528
public void setXIncludeAware(final boolean state) {
529
throw new UnsupportedOperationException(
530
"This parser does not support specification \""
531
+ this.getClass().getPackage().getSpecificationTitle()
533
+ this.getClass().getPackage().getSpecificationVersion()
539
* <p>Get state of XInclude processing.</p>
541
* @return current state of XInclude processing
543
* @throws UnsupportedOperationException
544
* For backward compatibility, when implementations for
545
* earlier versions of JAXP is used, this exception will be
550
public boolean isXIncludeAware() {
551
throw new UnsupportedOperationException(
552
"This parser does not support specification \""
553
+ this.getClass().getPackage().getSpecificationTitle()
555
+ this.getClass().getPackage().getSpecificationVersion()