1. /*
  2. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  3. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  4. */
  5. /*
  6. * @(#)DocumentBuilderFactory.java 1.26 03/01/23
  7. */
  8. package javax.xml.parsers;
  9. import java.io.InputStream;
  10. import java.io.IOException;
  11. import java.io.File;
  12. import java.io.FileInputStream;
  13. import java.util.Properties;
  14. import java.io.BufferedReader;
  15. import java.io.InputStreamReader;
  16. /**
  17. * Defines a factory API that enables applications to obtain a
  18. * parser that produces DOM object trees from XML documents.
  19. *
  20. * An implementation of the <code>DocumentBuilderFactory</code> class is
  21. * <em>NOT</em> guaranteed to be thread safe. It is up to the user application
  22. * to make sure about the use of the <code>DocumentBuilderFactory</code> from
  23. * more than one thread. Alternatively the application can have one instance
  24. * of the <code>DocumentBuilderFactory</code> per thread.
  25. * An application can use the same instance of the factory to obtain one or
  26. * more instances of the <code>DocumentBuilder</code> provided the instance
  27. * of the factory isn't being used in more than one thread at a time.
  28. *
  29. * @since JAXP 1.0
  30. * @version 1.0
  31. */
  32. public abstract class DocumentBuilderFactory {
  33. private boolean validating = false;
  34. private boolean namespaceAware = false;
  35. private boolean whitespace = false;
  36. private boolean expandEntityRef = true;
  37. private boolean ignoreComments = false;
  38. private boolean coalescing = false;
  39. protected DocumentBuilderFactory () {
  40. }
  41. /**
  42. * Obtain a new instance of a
  43. * <code>DocumentBuilderFactory</code>. This static method creates
  44. * a new factory instance.
  45. * This method uses the following ordered lookup procedure to determine
  46. * the <code>DocumentBuilderFactory</code> implementation class to
  47. * load:
  48. * <ul>
  49. * <li>
  50. * Use the <code>javax.xml.parsers.DocumentBuilderFactory</code> system
  51. * property.
  52. * </li>
  53. * <li>
  54. * Use the properties file "lib/jaxp.properties" in the JRE directory.
  55. * This configuration file is in standard <code>java.util.Properties
  56. * </code> format and contains the fully qualified name of the
  57. * implementation class with the key being the system property defined
  58. * above.
  59. * </li>
  60. * <li>
  61. * Use the Services API (as detailed in the JAR specification), if
  62. * available, to determine the classname. The Services API will look
  63. * for a classname in the file
  64. * <code>META-INF/services/javax.xml.parsers.DocumentBuilderFactory</code>
  65. * in jars available to the runtime.
  66. * </li>
  67. * <li>
  68. * Platform default <code>DocumentBuilderFactory</code> instance.
  69. * </li>
  70. * </ul>
  71. *
  72. * Once an application has obtained a reference to a
  73. * <code>DocumentBuilderFactory</code> it can use the factory to
  74. * configure and obtain parser instances.
  75. *
  76. * @exception FactoryConfigurationError if the implementation is not
  77. * available or cannot be instantiated.
  78. */
  79. public static DocumentBuilderFactory newInstance()
  80. throws FactoryConfigurationError
  81. {
  82. try {
  83. return (DocumentBuilderFactory) FactoryFinder.find(
  84. /* The default property name according to the JAXP spec */
  85. "javax.xml.parsers.DocumentBuilderFactory",
  86. /* The fallback implementation class name */
  87. "org.apache.crimson.jaxp.DocumentBuilderFactoryImpl");
  88. } catch (FactoryFinder.ConfigurationError e) {
  89. throw new FactoryConfigurationError(e.getException(),
  90. e.getMessage());
  91. }
  92. }
  93. /**
  94. * Creates a new instance of a {@link javax.xml.parsers.DocumentBuilder}
  95. * using the currently configured parameters.
  96. *
  97. * @exception ParserConfigurationException if a DocumentBuilder
  98. * cannot be created which satisfies the configuration requested.
  99. * @return A new instance of a DocumentBuilder.
  100. */
  101. public abstract DocumentBuilder newDocumentBuilder()
  102. throws ParserConfigurationException;
  103. /**
  104. * Specifies that the parser produced by this code will
  105. * provide support for XML namespaces. By default the value of this is set
  106. * to <code>false</code>
  107. *
  108. * @param awareness true if the parser produced will provide support
  109. * for XML namespaces; false otherwise.
  110. */
  111. public void setNamespaceAware(boolean awareness) {
  112. this.namespaceAware = awareness;
  113. }
  114. /**
  115. * Specifies that the parser produced by this code will
  116. * validate documents as they are parsed. By default the value of this
  117. * is set to <code>false</code>.
  118. *
  119. * @param validating true if the parser produced will validate documents
  120. * as they are parsed; false otherwise.
  121. */
  122. public void setValidating(boolean validating) {
  123. this.validating = validating;
  124. }
  125. /**
  126. * Specifies that the parsers created by this factory must eliminate
  127. * whitespace in element content (sometimes known loosely as
  128. * 'ignorable whitespace') when parsing XML documents (see XML Rec
  129. * 2.10). Note that only whitespace which is directly contained within
  130. * element content that has an element only content model (see XML
  131. * Rec 3.2.1) will be eliminated. Due to reliance on the content model
  132. * this setting requires the parser to be in validating mode. By default
  133. * the value of this is set to <code>false</code>.
  134. *
  135. * @param whitespace true if the parser created must eliminate whitespace
  136. * in the element content when parsing XML documents;
  137. * false otherwise.
  138. */
  139. public void setIgnoringElementContentWhitespace(boolean whitespace) {
  140. this.whitespace = whitespace;
  141. }
  142. /**
  143. * Specifies that the parser produced by this code will
  144. * expand entity reference nodes. By default the value of this is set to
  145. * <code>true</code>
  146. *
  147. * @param expandEntityRef true if the parser produced will expand entity
  148. * reference nodes; false otherwise.
  149. */
  150. public void setExpandEntityReferences(boolean expandEntityRef) {
  151. this.expandEntityRef = expandEntityRef;
  152. }
  153. /**
  154. * Specifies that the parser produced by this code will
  155. * ignore comments. By default the value of this is set to <code>false
  156. * </code>
  157. */
  158. public void setIgnoringComments(boolean ignoreComments) {
  159. this.ignoreComments = ignoreComments;
  160. }
  161. /**
  162. * Specifies that the parser produced by this code will
  163. * convert CDATA nodes to Text nodes and append it to the
  164. * adjacent (if any) text node. By default the value of this is set to
  165. * <code>false</code>
  166. *
  167. * @param coalescing true if the parser produced will convert CDATA nodes
  168. * to Text nodes and append it to the adjacent (if any)
  169. * text node; false otherwise.
  170. */
  171. public void setCoalescing(boolean coalescing) {
  172. this.coalescing = coalescing;
  173. }
  174. /**
  175. * Indicates whether or not the factory is configured to produce
  176. * parsers which are namespace aware.
  177. *
  178. * @return true if the factory is configured to produce parsers which
  179. * are namespace aware; false otherwise.
  180. */
  181. public boolean isNamespaceAware() {
  182. return namespaceAware;
  183. }
  184. /**
  185. * Indicates whether or not the factory is configured to produce
  186. * parsers which validate the XML content during parse.
  187. *
  188. * @return true if the factory is configured to produce parsers
  189. * which validate the XML content during parse; false otherwise.
  190. */
  191. public boolean isValidating() {
  192. return validating;
  193. }
  194. /**
  195. * Indicates whether or not the factory is configured to produce
  196. * parsers which ignore ignorable whitespace in element content.
  197. *
  198. * @return true if the factory is configured to produce parsers
  199. * which ignore ignorable whitespace in element content;
  200. * false otherwise.
  201. */
  202. public boolean isIgnoringElementContentWhitespace() {
  203. return whitespace;
  204. }
  205. /**
  206. * Indicates whether or not the factory is configured to produce
  207. * parsers which expand entity reference nodes.
  208. *
  209. * @return true if the factory is configured to produce parsers
  210. * which expand entity reference nodes; false otherwise.
  211. */
  212. public boolean isExpandEntityReferences() {
  213. return expandEntityRef;
  214. }
  215. /**
  216. * Indicates whether or not the factory is configured to produce
  217. * parsers which ignores comments.
  218. *
  219. * @return true if the factory is configured to produce parsers
  220. * which ignores comments; false otherwise.
  221. */
  222. public boolean isIgnoringComments() {
  223. return ignoreComments;
  224. }
  225. /**
  226. * Indicates whether or not the factory is configured to produce
  227. * parsers which converts CDATA nodes to Text nodes and appends it to
  228. * the adjacent (if any) Text node.
  229. *
  230. * @return true if the factory is configured to produce parsers
  231. * which converts CDATA nodes to Text nodes and appends it to
  232. * the adjacent (if any) Text node; false otherwise.
  233. */
  234. public boolean isCoalescing() {
  235. return coalescing;
  236. }
  237. /**
  238. * Allows the user to set specific attributes on the underlying
  239. * implementation.
  240. * @param name The name of the attribute.
  241. * @param value The value of the attribute.
  242. * @exception IllegalArgumentException thrown if the underlying
  243. * implementation doesn't recognize the attribute.
  244. */
  245. public abstract void setAttribute(String name, Object value)
  246. throws IllegalArgumentException;
  247. /**
  248. * Allows the user to retrieve specific attributes on the underlying
  249. * implementation.
  250. * @param name The name of the attribute.
  251. * @return value The value of the attribute.
  252. * @exception IllegalArgumentException thrown if the underlying
  253. * implementation doesn't recognize the attribute.
  254. */
  255. public abstract Object getAttribute(String name)
  256. throws IllegalArgumentException;
  257. }