1. // SAX parser factory.
  2. // http://www.saxproject.org
  3. // No warranty; no copyright -- use this as you will.
  4. // $Id: ParserFactory.java,v 1.1.24.1 2004/05/01 08:34:46 jsuttor Exp $
  5. package org.xml.sax.helpers;
  6. import java.lang.ClassNotFoundException;
  7. import java.lang.IllegalAccessException;
  8. import java.lang.InstantiationException;
  9. import java.lang.SecurityException;
  10. import java.lang.ClassCastException;
  11. import org.xml.sax.Parser;
  12. /**
  13. * Java-specific class for dynamically loading SAX parsers.
  14. *
  15. * <blockquote>
  16. * <em>This module, both source code and documentation, is in the
  17. * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
  18. * See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
  19. * for further information.
  20. * </blockquote>
  21. *
  22. * <p><strong>Note:</strong> This class is designed to work with the now-deprecated
  23. * SAX1 {@link org.xml.sax.Parser Parser} class. SAX2 applications should use
  24. * {@link org.xml.sax.helpers.XMLReaderFactory XMLReaderFactory} instead.</p>
  25. *
  26. * <p>ParserFactory is not part of the platform-independent definition
  27. * of SAX; it is an additional convenience class designed
  28. * specifically for Java XML application writers. SAX applications
  29. * can use the static methods in this class to allocate a SAX parser
  30. * dynamically at run-time based either on the value of the
  31. * `org.xml.sax.parser' system property or on a string containing the class
  32. * name.</p>
  33. *
  34. * <p>Note that the application still requires an XML parser that
  35. * implements SAX1.</p>
  36. *
  37. * @deprecated This class works with the deprecated
  38. * {@link org.xml.sax.Parser Parser}
  39. * interface.
  40. * @since SAX 1.0
  41. * @author David Megginson
  42. * @version 2.0.1 (sax2r2)
  43. */
  44. public class ParserFactory {
  45. /**
  46. * Private null constructor.
  47. */
  48. private ParserFactory ()
  49. {
  50. }
  51. /**
  52. * Create a new SAX parser using the `org.xml.sax.parser' system property.
  53. *
  54. * <p>The named class must exist and must implement the
  55. * {@link org.xml.sax.Parser Parser} interface.</p>
  56. *
  57. * @exception java.lang.NullPointerException There is no value
  58. * for the `org.xml.sax.parser' system property.
  59. * @exception java.lang.ClassNotFoundException The SAX parser
  60. * class was not found (check your CLASSPATH).
  61. * @exception IllegalAccessException The SAX parser class was
  62. * found, but you do not have permission to load
  63. * it.
  64. * @exception InstantiationException The SAX parser class was
  65. * found but could not be instantiated.
  66. * @exception java.lang.ClassCastException The SAX parser class
  67. * was found and instantiated, but does not implement
  68. * org.xml.sax.Parser.
  69. * @see #makeParser(java.lang.String)
  70. * @see org.xml.sax.Parser
  71. */
  72. public static Parser makeParser ()
  73. throws ClassNotFoundException,
  74. IllegalAccessException,
  75. InstantiationException,
  76. NullPointerException,
  77. ClassCastException
  78. {
  79. String className = System.getProperty("org.xml.sax.parser");
  80. if (className == null) {
  81. throw new NullPointerException("No value for sax.parser property");
  82. } else {
  83. return makeParser(className);
  84. }
  85. }
  86. /**
  87. * Create a new SAX parser object using the class name provided.
  88. *
  89. * <p>The named class must exist and must implement the
  90. * {@link org.xml.sax.Parser Parser} interface.</p>
  91. *
  92. * @param className A string containing the name of the
  93. * SAX parser class.
  94. * @exception java.lang.ClassNotFoundException The SAX parser
  95. * class was not found (check your CLASSPATH).
  96. * @exception IllegalAccessException The SAX parser class was
  97. * found, but you do not have permission to load
  98. * it.
  99. * @exception InstantiationException The SAX parser class was
  100. * found but could not be instantiated.
  101. * @exception java.lang.ClassCastException The SAX parser class
  102. * was found and instantiated, but does not implement
  103. * org.xml.sax.Parser.
  104. * @see #makeParser()
  105. * @see org.xml.sax.Parser
  106. */
  107. public static Parser makeParser (String className)
  108. throws ClassNotFoundException,
  109. IllegalAccessException,
  110. InstantiationException,
  111. ClassCastException
  112. {
  113. return (Parser) NewInstance.newInstance (
  114. NewInstance.getClassLoader (), className);
  115. }
  116. }