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