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