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