1. /*
  2. * @(#)SAXParserFactory.java 1.28 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.xml.parsers;
  8. import org.xml.sax.Parser;
  9. import org.xml.sax.SAXException;
  10. import org.xml.sax.SAXNotRecognizedException;
  11. import org.xml.sax.SAXNotSupportedException;
  12. /**
  13. * Defines a factory API that enables applications to configure and
  14. * obtain a SAX based parser to parse XML documents.<p>
  15. * An implementation of the <code>SAXParserFactory</code> class is
  16. * <em>NOT</em> guaranteed to be thread safe. It is up to the user application
  17. * to make sure about the use of the <code>SAXParserFactory</code> from
  18. * more than one thread. Alternatively the application can have one instance
  19. * of the <code>SAXParserFactory</code> per thread.
  20. * An application can use the same instance of the factory to obtain one or
  21. * more instances of the <code>SAXParser</code> provided the instance
  22. * of the factory isn't being used in more than one thread at a time.
  23. * <p>
  24. *
  25. * The static <code>newInstance</code> method returns a new concrete
  26. * implementation of this class.
  27. *
  28. * @since JAXP 1.0
  29. * @version 1.0
  30. */
  31. public abstract class SAXParserFactory {
  32. private boolean validating = false;
  33. private boolean namespaceAware= false;
  34. protected SAXParserFactory () {
  35. }
  36. /**
  37. * Obtain a new instance of a <code>SAXParserFactory</code>. This
  38. * static method creates a new factory instance
  39. * This method uses the following ordered lookup procedure to determine
  40. * the <code>SAXParserFactory</code> implementation class to
  41. * load:
  42. * <ul>
  43. * <li>
  44. * Use the <code>javax.xml.parsers.SAXParserFactory</code> system
  45. * property.
  46. * </li>
  47. * <li>
  48. * Use the properties file "lib/jaxp.properties" in the JRE directory.
  49. * This configuration file is in standard <code>java.util.Properties
  50. * </code> format and contains the fully qualified name of the
  51. * implementation class with the key being the system property defined
  52. * above.
  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.SAXParserFactory</code>
  59. * in jars available to the runtime.
  60. * </li>
  61. * <li>
  62. * Platform default <code>SAXParserFactory</code> instance.
  63. * </li>
  64. * </ul>
  65. *
  66. * Once an application has obtained a reference to a
  67. * <code>SAXParserFactory</code> it can use the factory to
  68. * configure and obtain parser instances.
  69. *
  70. * @return A new instance of a SAXParserFactory.
  71. *
  72. * @exception FactoryConfigurationError if the implementation is
  73. * not available or cannot be instantiated.
  74. */
  75. public static SAXParserFactory newInstance()
  76. throws FactoryConfigurationError
  77. {
  78. try {
  79. return (SAXParserFactory) FactoryFinder.find(
  80. /* The default property name according to the JAXP spec */
  81. "javax.xml.parsers.SAXParserFactory",
  82. /* The fallback implementation class name */
  83. "org.apache.crimson.jaxp.SAXParserFactoryImpl");
  84. } catch (FactoryFinder.ConfigurationError e) {
  85. throw new FactoryConfigurationError(e.getException(),
  86. e.getMessage());
  87. }
  88. }
  89. /**
  90. * Creates a new instance of a SAXParser using the currently
  91. * configured factory parameters.
  92. *
  93. * @return A new instance of a SAXParser.
  94. *
  95. * @exception ParserConfigurationException if a parser cannot
  96. * be created which satisfies the requested configuration.
  97. */
  98. public abstract SAXParser newSAXParser()
  99. throws ParserConfigurationException, SAXException;
  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 by this code will
  106. * provide support for XML namespaces; false otherwise.
  107. */
  108. public void setNamespaceAware(boolean awareness)
  109. {
  110. this.namespaceAware = awareness;
  111. }
  112. /**
  113. * Specifies that the parser produced by this code will
  114. * validate documents as they are parsed. By default the value of this is
  115. * set to <code>false</code>.
  116. *
  117. * @param validating true if the parser produced by this code will
  118. * validate documents as they are parsed; false otherwise.
  119. */
  120. public void setValidating(boolean validating)
  121. {
  122. this.validating = validating;
  123. }
  124. /**
  125. * Indicates whether or not the factory is configured to produce
  126. * parsers which are namespace aware.
  127. *
  128. * @return true if the factory is configured to produce
  129. * parsers which are namespace aware; false otherwise.
  130. */
  131. public boolean isNamespaceAware() {
  132. return namespaceAware;
  133. }
  134. /**
  135. * Indicates whether or not the factory is configured to produce
  136. * parsers which validate the XML content during parse.
  137. *
  138. * @return true if the factory is configured to produce parsers which validate
  139. * the XML content during parse; false otherwise.
  140. */
  141. public boolean isValidating() {
  142. return validating;
  143. }
  144. /**
  145. *
  146. * Sets the particular feature in the underlying implementation of
  147. * org.xml.sax.XMLReader.
  148. * A list of the core features and properties can be found at
  149. * <a href="http://www.megginson.com/SAX/Java/features.html"> http://www.megginson.com/SAX/Java/features.html </a>
  150. *
  151. * @param name The name of the feature to be set.
  152. * @param value The value of the feature to be set.
  153. * @exception SAXNotRecognizedException When the underlying XMLReader does
  154. * not recognize the property name.
  155. *
  156. * @exception SAXNotSupportedException When the underlying XMLReader
  157. * recognizes the property name but doesn't support the
  158. * property.
  159. *
  160. * @see org.xml.sax.XMLReader#setFeature
  161. */
  162. public abstract void setFeature(String name, boolean value)
  163. throws ParserConfigurationException, SAXNotRecognizedException,
  164. SAXNotSupportedException;
  165. /**
  166. *
  167. * Returns the particular property requested for in the underlying
  168. * implementation of org.xml.sax.XMLReader.
  169. *
  170. * @param name The name of the property to be retrieved.
  171. * @return Value of the requested property.
  172. *
  173. * @exception SAXNotRecognizedException When the underlying XMLReader does
  174. * not recognize the property name.
  175. *
  176. * @exception SAXNotSupportedException When the underlying XMLReader
  177. * recognizes the property name but doesn't support the
  178. * property.
  179. *
  180. * @see org.xml.sax.XMLReader#getProperty
  181. */
  182. public abstract boolean getFeature(String name)
  183. throws ParserConfigurationException, SAXNotRecognizedException,
  184. SAXNotSupportedException;
  185. }