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