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