~ubuntu-branches/ubuntu/karmic/libjaxp1.3-java/karmic

« back to all changes in this revision

Viewing changes to javax/xml/parsers/DocumentBuilderFactory.java

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2006-08-03 10:30:58 UTC
  • Revision ID: james.westby@ubuntu.com-20060803103058-7jwwiqv9g8w9094d
Tags: upstream-1.3.03
ImportĀ upstreamĀ versionĀ 1.3.03

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2003-2004 The Apache Software Foundation.
 
3
 *
 
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
 
7
 *
 
8
 *     http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
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.
 
15
 */
 
16
 
 
17
// $Id: DocumentBuilderFactory.java 226183 2005-04-08 10:39:14Z neeraj $
 
18
 
 
19
package javax.xml.parsers;
 
20
 
 
21
import javax.xml.validation.Schema;
 
22
 
 
23
/**
 
24
 * Defines a factory API that enables applications to obtain a
 
25
 * parser that produces DOM object trees from XML documents.
 
26
 *
 
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) $
 
29
 */
 
30
 
 
31
public abstract class DocumentBuilderFactory {
 
32
        
 
33
    /** The default property name according to the JAXP spec */
 
34
    private static final String DEFAULT_PROPERTY_NAME = "javax.xml.parsers.DocumentBuilderFactory";
 
35
 
 
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;
 
42
    
 
43
    private boolean canonicalState = false;
 
44
    
 
45
    protected DocumentBuilderFactory () {
 
46
    }
 
47
 
 
48
    /**
 
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
 
54
     * load:
 
55
     * <ul>
 
56
     * <li>
 
57
     * Use the <code>javax.xml.parsers.DocumentBuilderFactory</code> system
 
58
     * property.
 
59
     * </li>
 
60
     * <li>
 
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
 
65
     * above.
 
66
     * 
 
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.
 
72
     * </li>
 
73
     * <li>
 
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.
 
79
     * </li>
 
80
     * <li>
 
81
     * Platform default <code>DocumentBuilderFactory</code> instance.
 
82
     * </li>
 
83
     * </ul>
 
84
     *
 
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.
 
88
     * 
 
89
     * 
 
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>
 
94
     * 
 
95
     * <p> If you have problems loading {@link DocumentBuilder}s, try:</p>
 
96
     * <pre>
 
97
     * java -Djaxp.debug=1 YourProgram ....
 
98
     * </pre>
 
99
     * 
 
100
     * @return New instance of a <code>DocumentBuilderFactory</code>
 
101
     *
 
102
     * @exception FactoryConfigurationError if the implementation is not
 
103
     * available or cannot be instantiated.
 
104
     */
 
105
    public static DocumentBuilderFactory newInstance() {
 
106
        try {
 
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(),
 
114
                                                e.getMessage());
 
115
        }
 
116
 
 
117
    }
 
118
 
 
119
    /**
 
120
     * Creates a new instance of a {@link javax.xml.parsers.DocumentBuilder}
 
121
     * using the currently configured parameters.
 
122
     *
 
123
     * @exception ParserConfigurationException if a DocumentBuilder
 
124
     * cannot be created which satisfies the configuration requested.
 
125
     * @return A new instance of a DocumentBuilder.
 
126
     */
 
127
    
 
128
    public abstract DocumentBuilder newDocumentBuilder()
 
129
        throws ParserConfigurationException;
 
130
    
 
131
    
 
132
    /**
 
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>
 
136
     *
 
137
     * @param awareness true if the parser produced will provide support
 
138
     *                  for XML namespaces; false otherwise.
 
139
     */
 
140
    
 
141
    public void setNamespaceAware(boolean awareness) {
 
142
        this.namespaceAware = awareness;
 
143
    }
 
144
 
 
145
    /**
 
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>.
 
149
     * 
 
150
     * <p>
 
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.)
 
157
     * </p>
 
158
     * 
 
159
     * <p>
 
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.
 
165
     * </p>
 
166
     * 
 
167
     * @param validating true if the parser produced will validate documents
 
168
     *                   as they are parsed; false otherwise.
 
169
     */
 
170
    
 
171
    public void setValidating(boolean validating) {
 
172
        this.validating = validating;
 
173
    }
 
174
 
 
175
    /**
 
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>.
 
184
     *
 
185
     * @param whitespace true if the parser created must eliminate whitespace
 
186
     *                   in the element content when parsing XML documents;
 
187
     *                   false otherwise.
 
188
     */
 
189
 
 
190
    public void setIgnoringElementContentWhitespace(boolean whitespace) {
 
191
        this.whitespace = whitespace;
 
192
    }
 
193
 
 
194
    /**
 
195
     * Specifies that the parser produced by this code will
 
196
     * expand entity reference nodes. By default the value of this is set to
 
197
     * <code>true</code>
 
198
     *
 
199
     * @param expandEntityRef true if the parser produced will expand entity
 
200
     *                        reference nodes; false otherwise.
 
201
     */
 
202
    
 
203
    public void setExpandEntityReferences(boolean expandEntityRef) {
 
204
        this.expandEntityRef = expandEntityRef;
 
205
    }
 
206
 
 
207
    /**
 
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
 
210
     * </code>.</p>
 
211
     * 
 
212
     * @param ignoreComments <code>boolean</code> value to ignore comments during processing
 
213
     */
 
214
    
 
215
    public void setIgnoringComments(boolean ignoreComments) {
 
216
        this.ignoreComments = ignoreComments;
 
217
    }
 
218
 
 
219
    /**
 
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
 
223
     * <code>false</code>
 
224
     *
 
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.
 
228
     */
 
229
    
 
230
    public void setCoalescing(boolean coalescing) {
 
231
        this.coalescing = coalescing;
 
232
    }
 
233
 
 
234
    /**
 
235
     * Indicates whether or not the factory is configured to produce
 
236
     * parsers which are namespace aware.
 
237
     *
 
238
     * @return  true if the factory is configured to produce parsers which
 
239
     *          are namespace aware; false otherwise.
 
240
     */
 
241
    
 
242
    public boolean isNamespaceAware() {
 
243
        return namespaceAware;
 
244
    }
 
245
 
 
246
    /**
 
247
     * Indicates whether or not the factory is configured to produce
 
248
     * parsers which validate the XML content during parse.
 
249
     *
 
250
     * @return  true if the factory is configured to produce parsers
 
251
     *          which validate the XML content during parse; false otherwise.
 
252
     */
 
253
    
 
254
    public boolean isValidating() {
 
255
        return validating;
 
256
    }
 
257
 
 
258
    /**
 
259
     * Indicates whether or not the factory is configured to produce
 
260
     * parsers which ignore ignorable whitespace in element content.
 
261
     *
 
262
     * @return  true if the factory is configured to produce parsers
 
263
     *          which ignore ignorable whitespace in element content;
 
264
     *          false otherwise.
 
265
     */
 
266
    
 
267
    public boolean isIgnoringElementContentWhitespace() {
 
268
        return whitespace;
 
269
    }
 
270
 
 
271
    /**
 
272
     * Indicates whether or not the factory is configured to produce
 
273
     * parsers which expand entity reference nodes.
 
274
     *
 
275
     * @return  true if the factory is configured to produce parsers
 
276
     *          which expand entity reference nodes; false otherwise.
 
277
     */
 
278
    
 
279
    public boolean isExpandEntityReferences() {
 
280
        return expandEntityRef;
 
281
    }
 
282
 
 
283
    /**
 
284
     * Indicates whether or not the factory is configured to produce
 
285
     * parsers which ignores comments.
 
286
     *
 
287
     * @return  true if the factory is configured to produce parsers
 
288
     *          which ignores comments; false otherwise.
 
289
     */
 
290
    
 
291
    public boolean isIgnoringComments() {
 
292
        return ignoreComments;
 
293
    }
 
294
 
 
295
    /**
 
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.
 
299
     *
 
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.
 
303
     */
 
304
    
 
305
    public boolean isCoalescing() {
 
306
        return coalescing;
 
307
    }
 
308
 
 
309
    /**
 
310
     * Allows the user to set specific attributes on the underlying
 
311
     * implementation.
 
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.
 
316
     */
 
317
    public abstract void setAttribute(String name, Object value)
 
318
                throws IllegalArgumentException;
 
319
 
 
320
    /**
 
321
     * Allows the user to retrieve specific attributes on the underlying
 
322
     * implementation.
 
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.
 
327
     */
 
328
    public abstract Object getAttribute(String name)
 
329
                throws IllegalArgumentException;
 
330
                
 
331
        /**
 
332
         * <p>Set a feature for this <code>DocumentBuilderFactory</code> and <code>DocumentBuilder</code>s created by this factory.</p>
 
333
         * 
 
334
         * <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.
 
340
         * </p>
 
341
         * 
 
342
         * <p>
 
343
         * All implementations are required to support the {@link javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} feature.
 
344
         * When the feature is:</p>
 
345
         * <ul>
 
346
         *   <li>
 
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)}.
 
352
         *   </li>
 
353
         *   <li>
 
354
         *     <code>false</code>: the implementation will processing XML according to the XML specifications without
 
355
         *     regard to possible implementation limits.
 
356
         *   </li>
 
357
         * </ul>
 
358
         * 
 
359
         * @param name Feature name.
 
360
         * @param value Is feature state <code>true</code> or <code>false</code>.
 
361
         *  
 
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.
 
365
         */
 
366
        public abstract void setFeature(String name, boolean value)
 
367
                throws ParserConfigurationException;
 
368
 
 
369
        /**
 
370
         * <p>Get the state of the named feature.</p>
 
371
         * 
 
372
         * <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.
 
378
         * </p>
 
379
         * 
 
380
         * @param name Feature name.
 
381
         * 
 
382
         * @return State of the named feature.
 
383
         * 
 
384
         * @throws ParserConfigurationException if this <code>DocumentBuilderFactory</code>
 
385
         *   or the <code>DocumentBuilder</code>s it creates cannot support this feature.
 
386
         */
 
387
        public abstract boolean getFeature(String name)
 
388
                throws ParserConfigurationException;
 
389
                
 
390
    
 
391
    /** <p>Get current state of canonicalization.</p>
 
392
     *
 
393
     * @return current state canonicalization control
 
394
     */
 
395
    /*
 
396
    public boolean getCanonicalization() {
 
397
        return canonicalState;
 
398
    }
 
399
    */
 
400
    
 
401
    
 
402
    /**
 
403
     * Gets the {@link Schema} object specified through
 
404
     * the {@link #setSchema(Schema schema)} method.
 
405
     * 
 
406
     * 
 
407
     * @throws UnsupportedOperationException
 
408
     *      For backward compatibility, when implementations for
 
409
     *      earlier versions of JAXP is used, this exception will be
 
410
     *      thrown.
 
411
     * 
 
412
     * @return
 
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}
 
416
     *      is created.
 
417
     * 
 
418
     * @since 1.5
 
419
     */
 
420
    public Schema getSchema() {
 
421
        throw new UnsupportedOperationException(
 
422
            "This parser does not support specification \""
 
423
            + this.getClass().getPackage().getSpecificationTitle()
 
424
            + "\" version \""
 
425
            + this.getClass().getPackage().getSpecificationVersion()
 
426
            + "\""
 
427
            );
 
428
 
 
429
    }
 
430
    
 
431
    /* <p>Set canonicalization control to <code>true</code> or
 
432
     * </code>false</code>.</p>
 
433
     *
 
434
     * @param state of canonicalization
 
435
     */
 
436
    /*
 
437
    public void setCanonicalization(boolean state) {
 
438
        canonicalState = state;
 
439
    }
 
440
    */
 
441
    
 
442
    /**
 
443
     * <p>Set the {@link Schema} to be used by parsers created
 
444
     * from this factory.
 
445
     * 
 
446
     * <p>
 
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.
 
450
     * 
 
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.
 
459
     * 
 
460
     * <p>
 
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.  
 
465
     * 
 
466
     * <p>
 
467
     * Initialy, null is set as the {@link Schema}. 
 
468
     * 
 
469
     * <p>
 
470
     * This processing will take effect even if
 
471
     * the {@link #isValidating()} method returns <tt>false</tt>.
 
472
     * 
 
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>
 
479
     *
 
480
     *  
 
481
     * <h4>Note for implmentors</h4>
 
482
     * <p>
 
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.
 
487
     * 
 
488
     * @param schema <code>Schema</code> to use or <code>null</code> to remove a schema.
 
489
     * 
 
490
     * @throws UnsupportedOperationException
 
491
     *      For backward compatibility, when implementations for
 
492
     *      earlier versions of JAXP is used, this exception will be
 
493
     *      thrown.
 
494
     * 
 
495
     * @since 1.5
 
496
     */
 
497
    public void setSchema(Schema schema) {
 
498
        throw new UnsupportedOperationException(
 
499
            "This parser does not support specification \""
 
500
            + this.getClass().getPackage().getSpecificationTitle()
 
501
            + "\" version \""
 
502
            + this.getClass().getPackage().getSpecificationVersion()
 
503
            + "\""
 
504
            );
 
505
    }
 
506
    
 
507
 
 
508
    
 
509
    /**
 
510
     * <p>Set state of XInclude processing.</p>
 
511
     * 
 
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>
 
515
     * 
 
516
     * <p>XInclude processing defaults to <code>false</code>.</p>
 
517
     * 
 
518
     * @param state Set XInclude processing to <code>true</code> or
 
519
     *   <code>false</code>
 
520
     * 
 
521
     * @throws UnsupportedOperationException
 
522
     *      For backward compatibility, when implementations for
 
523
     *      earlier versions of JAXP is used, this exception will be
 
524
     *      thrown.
 
525
     * 
 
526
     * @since 1.5
 
527
     */
 
528
    public void setXIncludeAware(final boolean state) {
 
529
        throw new UnsupportedOperationException(
 
530
            "This parser does not support specification \""
 
531
            + this.getClass().getPackage().getSpecificationTitle()
 
532
            + "\" version \""
 
533
            + this.getClass().getPackage().getSpecificationVersion()
 
534
            + "\""
 
535
            );
 
536
    }
 
537
 
 
538
    /**
 
539
     * <p>Get state of XInclude processing.</p>
 
540
     * 
 
541
     * @return current state of XInclude processing
 
542
     * 
 
543
     * @throws UnsupportedOperationException
 
544
     *      For backward compatibility, when implementations for
 
545
     *      earlier versions of JAXP is used, this exception will be
 
546
     *      thrown.
 
547
     * 
 
548
     * @since 1.5
 
549
     */
 
550
    public boolean isXIncludeAware() {
 
551
        throw new UnsupportedOperationException(
 
552
            "This parser does not support specification \""
 
553
            + this.getClass().getPackage().getSpecificationTitle()
 
554
            + "\" version \""
 
555
            + this.getClass().getPackage().getSpecificationVersion()
 
556
            + "\""
 
557
            );
 
558
    }
 
559
}