KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > xml > parsers > DocumentBuilderFactory


1 // $Id: DocumentBuilderFactory.java,v 1.39.16.1 2004/07/17 00:22:03 jsuttor Exp $
2

3 /*
4  * @(#)DocumentBuilderFactory.java 1.39 04/07/27
5  *
6  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
7  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
8  */

9
10 package javax.xml.parsers;
11
12 import javax.xml.validation.Schema JavaDoc;
13
14 /**
15  * Defines a factory API that enables applications to obtain a
16  * parser that produces DOM object trees from XML documents.
17  *
18  * @author <a HREF="Jeff.Suttor@Sun.com">Jeff Suttor</a>
19  * @version $Revision: 1.39.16.1 $, $Date: 2004/07/17 00:22:03 $
20  */

21
22 public abstract class DocumentBuilderFactory {
23         
24     /** The default property name according to the JAXP spec */
25     private static final String JavaDoc DEFAULT_PROPERTY_NAME = "javax.xml.parsers.DocumentBuilderFactory";
26
27     private boolean validating = false;
28     private boolean namespaceAware = false;
29     private boolean whitespace = false;
30     private boolean expandEntityRef = true;
31     private boolean ignoreComments = false;
32     private boolean coalescing = false;
33     
34     private boolean canonicalState = false;
35     
36     protected DocumentBuilderFactory () {
37     }
38
39     /**
40      * Obtain a new instance of a
41      * <code>DocumentBuilderFactory</code>. This static method creates
42      * a new factory instance.
43      * This method uses the following ordered lookup procedure to determine
44      * the <code>DocumentBuilderFactory</code> implementation class to
45      * load:
46      * <ul>
47      * <li>
48      * Use the <code>javax.xml.parsers.DocumentBuilderFactory</code> system
49      * property.
50      * </li>
51      * <li>
52      * Use the properties file "lib/jaxp.properties" in the JRE directory.
53      * This configuration file is in standard <code>java.util.Properties
54      * </code> format and contains the fully qualified name of the
55      * implementation class with the key being the system property defined
56      * above.
57      *
58      * The jaxp.properties file is read only once by the JAXP implementation
59      * and it's values are then cached for future use. If the file does not exist
60      * when the first attempt is made to read from it, no further attempts are
61      * made to check for its existence. It is not possible to change the value
62      * of any property in jaxp.properties after it has been read for the first time.
63      * </li>
64      * <li>
65      * Use the Services API (as detailed in the JAR specification), if
66      * available, to determine the classname. The Services API will look
67      * for a classname in the file
68      * <code>META-INF/services/javax.xml.parsers.DocumentBuilderFactory</code>
69      * in jars available to the runtime.
70      * </li>
71      * <li>
72      * Platform default <code>DocumentBuilderFactory</code> instance.
73      * </li>
74      * </ul>
75      *
76      * Once an application has obtained a reference to a
77      * <code>DocumentBuilderFactory</code> it can use the factory to
78      * configure and obtain parser instances.
79      *
80      *
81      * <h2>Tip for Trouble-shooting</h2>
82      * <p>Setting the <code>jaxp.debug</code> system property will cause
83      * this method to print a lot of debug messages
84      * to <tt>System.err</tt> about what it is doing and where it is looking at.</p>
85      *
86      * <p> If you have problems loading {@link DocumentBuilder}s, try:</p>
87      * <pre>
88      * java -Djaxp.debug=1 YourProgram ....
89      * </pre>
90      *
91      * @return New instance of a <code>DocumentBuilderFactory</code>
92      *
93      * @exception FactoryConfigurationError if the implementation is not
94      * available or cannot be instantiated.
95      */

96     public static DocumentBuilderFactory JavaDoc newInstance() {
97         try {
98             return (DocumentBuilderFactory JavaDoc) FactoryFinder.find(
99                 /* The default property name according to the JAXP spec */
100                 "javax.xml.parsers.DocumentBuilderFactory",
101                 /* The fallback implementation class name */
102                 "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
103         } catch (FactoryFinder.ConfigurationError JavaDoc e) {
104             throw new FactoryConfigurationError JavaDoc(e.getException(),
105                                                 e.getMessage());
106         }
107
108     }
109
110     /**
111      * Creates a new instance of a {@link javax.xml.parsers.DocumentBuilder}
112      * using the currently configured parameters.
113      *
114      * @exception ParserConfigurationException if a DocumentBuilder
115      * cannot be created which satisfies the configuration requested.
116      * @return A new instance of a DocumentBuilder.
117      */

118     
119     public abstract DocumentBuilder JavaDoc newDocumentBuilder()
120         throws ParserConfigurationException JavaDoc;
121     
122     
123     /**
124      * Specifies that the parser produced by this code will
125      * provide support for XML namespaces. By default the value of this is set
126      * to <code>false</code>
127      *
128      * @param awareness true if the parser produced will provide support
129      * for XML namespaces; false otherwise.
130      */

131     
132     public void setNamespaceAware(boolean awareness) {
133         this.namespaceAware = awareness;
134     }
135
136     /**
137      * Specifies that the parser produced by this code will
138      * validate documents as they are parsed. By default the value of this
139      * is set to <code>false</code>.
140      *
141      * <p>
142      * Note that "the validation" here means
143      * <a HREF="http://www.w3.org/TR/REC-xml#proc-types">a validating
144      * parser</a> as defined in the XML recommendation.
145      * In other words, it essentially just controls the DTD validation.
146      * (except the legacy two properties defined in JAXP 1.2.
147      * See <a HREF="#validationCompatibility">here</a> for more details.)
148      * </p>
149      *
150      * <p>
151      * To use modern schema languages such as W3C XML Schema or
152      * RELAX NG instead of DTD, you can configure your parser to be
153      * a non-validating parser by leaving the {@link #setValidating(boolean)}
154      * method <tt>false</tt>, then use the {@link #setSchema(Schema)}
155      * method to associate a schema to a parser.
156      * </p>
157      *
158      * @param validating true if the parser produced will validate documents
159      * as they are parsed; false otherwise.
160      */

161     
162     public void setValidating(boolean validating) {
163         this.validating = validating;
164     }
165
166     /**
167      * Specifies that the parsers created by this factory must eliminate
168      * whitespace in element content (sometimes known loosely as
169      * 'ignorable whitespace') when parsing XML documents (see XML Rec
170      * 2.10). Note that only whitespace which is directly contained within
171      * element content that has an element only content model (see XML
172      * Rec 3.2.1) will be eliminated. Due to reliance on the content model
173      * this setting requires the parser to be in validating mode. By default
174      * the value of this is set to <code>false</code>.
175      *
176      * @param whitespace true if the parser created must eliminate whitespace
177      * in the element content when parsing XML documents;
178      * false otherwise.
179      */

180
181     public void setIgnoringElementContentWhitespace(boolean whitespace) {
182         this.whitespace = whitespace;
183     }
184
185     /**
186      * Specifies that the parser produced by this code will
187      * expand entity reference nodes. By default the value of this is set to
188      * <code>true</code>
189      *
190      * @param expandEntityRef true if the parser produced will expand entity
191      * reference nodes; false otherwise.
192      */

193     
194     public void setExpandEntityReferences(boolean expandEntityRef) {
195         this.expandEntityRef = expandEntityRef;
196     }
197
198     /**
199      * <p>Specifies that the parser produced by this code will
200      * ignore comments. By default the value of this is set to <code>false
201      * </code>.</p>
202      *
203      * @param ignoreComments <code>boolean</code> value to ignore comments during processing
204      */

205     
206     public void setIgnoringComments(boolean ignoreComments) {
207         this.ignoreComments = ignoreComments;
208     }
209
210     /**
211      * Specifies that the parser produced by this code will
212      * convert CDATA nodes to Text nodes and append it to the
213      * adjacent (if any) text node. By default the value of this is set to
214      * <code>false</code>
215      *
216      * @param coalescing true if the parser produced will convert CDATA nodes
217      * to Text nodes and append it to the adjacent (if any)
218      * text node; false otherwise.
219      */

220     
221     public void setCoalescing(boolean coalescing) {
222         this.coalescing = coalescing;
223     }
224
225     /**
226      * Indicates whether or not the factory is configured to produce
227      * parsers which are namespace aware.
228      *
229      * @return true if the factory is configured to produce parsers which
230      * are namespace aware; false otherwise.
231      */

232     
233     public boolean isNamespaceAware() {
234         return namespaceAware;
235     }
236
237     /**
238      * Indicates whether or not the factory is configured to produce
239      * parsers which validate the XML content during parse.
240      *
241      * @return true if the factory is configured to produce parsers
242      * which validate the XML content during parse; false otherwise.
243      */

244     
245     public boolean isValidating() {
246         return validating;
247     }
248
249     /**
250      * Indicates whether or not the factory is configured to produce
251      * parsers which ignore ignorable whitespace in element content.
252      *
253      * @return true if the factory is configured to produce parsers
254      * which ignore ignorable whitespace in element content;
255      * false otherwise.
256      */

257     
258     public boolean isIgnoringElementContentWhitespace() {
259         return whitespace;
260     }
261
262     /**
263      * Indicates whether or not the factory is configured to produce
264      * parsers which expand entity reference nodes.
265      *
266      * @return true if the factory is configured to produce parsers
267      * which expand entity reference nodes; false otherwise.
268      */

269     
270     public boolean isExpandEntityReferences() {
271         return expandEntityRef;
272     }
273
274     /**
275      * Indicates whether or not the factory is configured to produce
276      * parsers which ignores comments.
277      *
278      * @return true if the factory is configured to produce parsers
279      * which ignores comments; false otherwise.
280      */

281     
282     public boolean isIgnoringComments() {
283         return ignoreComments;
284     }
285
286     /**
287      * Indicates whether or not the factory is configured to produce
288      * parsers which converts CDATA nodes to Text nodes and appends it to
289      * the adjacent (if any) Text node.
290      *
291      * @return true if the factory is configured to produce parsers
292      * which converts CDATA nodes to Text nodes and appends it to
293      * the adjacent (if any) Text node; false otherwise.
294      */

295     
296     public boolean isCoalescing() {
297         return coalescing;
298     }
299
300     /**
301      * Allows the user to set specific attributes on the underlying
302      * implementation.
303      * @param name The name of the attribute.
304      * @param value The value of the attribute.
305      * @exception IllegalArgumentException thrown if the underlying
306      * implementation doesn't recognize the attribute.
307      */

308     public abstract void setAttribute(String JavaDoc name, Object JavaDoc value)
309                 throws IllegalArgumentException JavaDoc;
310
311     /**
312      * Allows the user to retrieve specific attributes on the underlying
313      * implementation.
314      * @param name The name of the attribute.
315      * @return value The value of the attribute.
316      * @exception IllegalArgumentException thrown if the underlying
317      * implementation doesn't recognize the attribute.
318      */

319     public abstract Object JavaDoc getAttribute(String JavaDoc name)
320                 throws IllegalArgumentException JavaDoc;
321                 
322     /**
323      * <p>Set a feature for this <code>DocumentBuilderFactory</code> and <code>DocumentBuilder</code>s created by this factory.</p>
324      *
325      * <p>
326      * Feature names are fully qualified {@link java.net.URI}s.
327      * Implementations may define their own features.
328      * An {@link ParserConfigurationException} is thrown if this <code>DocumentBuilderFactory</code> or the
329      * <code>DocumentBuilder</code>s it creates cannot support the feature.
330      * It is possible for an <code>DocumentBuilderFactory</code> to expose a feature value but be unable to change its state.
331      * </p>
332      *
333      * <p>
334      * All implementations are required to support the {@link javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} feature.
335      * When the feature is:</p>
336      * <ul>
337      * <li>
338      * <code>true</code>: the implementation will limit XML processing to conform to implementation limits.
339      * Examples include enity expansion limits and XML Schema constructs that would consume large amounts of resources.
340      * If XML processing is limited for security reasons, it will be reported via a call to the registered
341      * {@link org.xml.sax.ErrorHandler#fatalError(SAXParseException exception)}.
342      * See {@link DocumentBuilder#setErrorHandler(org.xml.sax.ErrorHandler errorHandler)}.
343      * </li>
344      * <li>
345      * <code>false</code>: the implementation will processing XML according to the XML specifications without
346      * regard to possible implementation limits.
347      * </li>
348      * </ul>
349      *
350      * @param name Feature name.
351      * @param value Is feature state <code>true</code> or <code>false</code>.
352      *
353      * @throws ParserConfigurationException if this <code>DocumentBuilderFactory</code> or the <code>DocumentBuilder</code>s
354      * it creates cannot support this feature.
355      * @throws NullPointerException If the <code>name</code> parameter is null.
356      */

357     public abstract void setFeature(String JavaDoc name, boolean value)
358         throws ParserConfigurationException JavaDoc;
359
360     /**
361      * <p>Get the state of the named feature.</p>
362      *
363      * <p>
364      * Feature names are fully qualified {@link java.net.URI}s.
365      * Implementations may define their own features.
366      * An {@link ParserConfigurationException} is thrown if this <code>DocumentBuilderFactory</code> or the
367      * <code>DocumentBuilder</code>s it creates cannot support the feature.
368      * It is possible for an <code>DocumentBuilderFactory</code> to expose a feature value but be unable to change its state.
369      * </p>
370      *
371      * @param name Feature name.
372      *
373      * @return State of the named feature.
374      *
375      * @throws ParserConfigurationException if this <code>DocumentBuilderFactory</code>
376      * or the <code>DocumentBuilder</code>s it creates cannot support this feature.
377      */

378     public abstract boolean getFeature(String JavaDoc name)
379         throws ParserConfigurationException JavaDoc;
380                 
381     
382     /** <p>Get current state of canonicalization.</p>
383      *
384      * @return current state canonicalization control
385      */

386     /*
387     public boolean getCanonicalization() {
388         return canonicalState;
389     }
390     */

391     
392     
393     /**
394      * Gets the {@link Schema} object specified through
395      * the {@link #setSchema(Schema schema)} method.
396      *
397      *
398      * @throws UnsupportedOperationException
399      * For backward compatibility, when implementations for
400      * earlier versions of JAXP is used, this exception will be
401      * thrown.
402      *
403      * @return
404      * the {@link Schema} object that was last set through
405      * the {@link #setSchema(Schema)} method, or null
406      * if the method was not invoked since a {@link SAXParserFactory}
407      * is created.
408      *
409      * @since 1.5
410      */

411     public Schema JavaDoc getSchema() {
412         throw new UnsupportedOperationException JavaDoc(
413             "This parser does not support specification \""
414             + this.getClass().getPackage().getSpecificationTitle()
415             + "\" version \""
416             + this.getClass().getPackage().getSpecificationVersion()
417             + "\""
418             );
419
420     }
421     
422     /* <p>Set canonicalization control to <code>true</code> or
423      * </code>false</code>.</p>
424      *
425      * @param state of canonicalization
426      */

427     /*
428     public void setCanonicalization(boolean state) {
429         canonicalState = state;
430     }
431     */

432     
433     /**
434      * <p>Set the {@link Schema} to be used by parsers created
435      * from this factory.
436      *
437      * <p>
438      * When a {@link Schema} is non-null, a parser will use a validator
439      * created from it to validate documents before it passes information
440      * down to the application.
441      *
442      * <p>When errors are found by the validator, the parser is responsible
443      * to report them to the user-specified {@link org.w3c.dom.DOMErrorHandler}
444      * (or if the error handler is not set, ignore them or throw them), just
445      * like any other errors found by the parser itself.
446      * In other words, if the user-specified {@link org.w3c.dom.DOMErrorHandler}
447      * is set, it must receive those errors, and if not, they must be
448      * treated according to the implementation specific
449      * default error handling rules.
450      *
451      * <p>
452      * A validator may modify the outcome of a parse (for example by
453      * adding default values that were missing in documents), and a parser
454      * is responsible to make sure that the application will receive
455      * modified DOM trees.
456      *
457      * <p>
458      * Initialy, null is set as the {@link Schema}.
459      *
460      * <p>
461      * This processing will take effect even if
462      * the {@link #isValidating()} method returns <tt>false</tt>.
463      *
464      * <p>It is an error to use
465      * the <code>http://java.sun.com/xml/jaxp/properties/schemaSource</code>
466      * property and/or the <code>http://java.sun.com/xml/jaxp/properties/schemaLanguage</code>
467      * property in conjunction with a {@link Schema} object.
468      * Such configuration will cause a {@link ParserConfigurationException}
469      * exception when the {@link #newDocumentBuilder()} is invoked.</p>
470      *
471      *
472      * <h4>Note for implmentors</h4>
473      * <p>
474      * A parser must be able to work with any {@link Schema}
475      * implementation. However, parsers and schemas are allowed
476      * to use implementation-specific custom mechanisms
477      * as long as they yield the result described in the specification.
478      *
479      * @param schema <code>Schema</code> to use or <code>null</code> to remove a schema.
480      *
481      * @throws UnsupportedOperationException
482      * For backward compatibility, when implementations for
483      * earlier versions of JAXP is used, this exception will be
484      * thrown.
485      *
486      * @since 1.5
487      */

488     public void setSchema(Schema JavaDoc schema) {
489         throw new UnsupportedOperationException JavaDoc(
490             "This parser does not support specification \""
491             + this.getClass().getPackage().getSpecificationTitle()
492             + "\" version \""
493             + this.getClass().getPackage().getSpecificationVersion()
494             + "\""
495             );
496     }
497     
498
499     
500     /**
501      * <p>Set state of XInclude processing.</p>
502      *
503      * <p>If XInclude markup is found in the document instance, should it be
504      * processed as specified in <a HREF="http://www.w3.org/TR/xinclude/">
505      * XML Inclusions (XInclude) Version 1.0</a>.</p>
506      *
507      * <p>XInclude processing defaults to <code>false</code>.</p>
508      *
509      * @param state Set XInclude processing to <code>true</code> or
510      * <code>false</code>
511      *
512      * @throws UnsupportedOperationException
513      * For backward compatibility, when implementations for
514      * earlier versions of JAXP is used, this exception will be
515      * thrown.
516      *
517      * @since 1.5
518      */

519     public void setXIncludeAware(final boolean state) {
520         throw new UnsupportedOperationException JavaDoc(
521             "This parser does not support specification \""
522             + this.getClass().getPackage().getSpecificationTitle()
523             + "\" version \""
524             + this.getClass().getPackage().getSpecificationVersion()
525             + "\""
526             );
527     }
528
529     /**
530      * <p>Get state of XInclude processing.</p>
531      *
532      * @return current state of XInclude processing
533      *
534      * @throws UnsupportedOperationException
535      * For backward compatibility, when implementations for
536      * earlier versions of JAXP is used, this exception will be
537      * thrown.
538      *
539      * @since 1.5
540      */

541     public boolean isXIncludeAware() {
542         throw new UnsupportedOperationException JavaDoc(
543             "This parser does not support specification \""
544             + this.getClass().getPackage().getSpecificationTitle()
545             + "\" version \""
546             + this.getClass().getPackage().getSpecificationVersion()
547             + "\""
548             );
549     }
550 }
551
Popular Tags