1. // SAX parser factory.
  2. // No warranty; no copyright -- use this as you will.
  3. // $Id: ParserFactory.java,v 1.2 2001/08/01 06:43:19 tcng Exp $
  4. package org.xml.sax.helpers;
  5. import java.lang.ClassNotFoundException;
  6. import java.lang.IllegalAccessException;
  7. import java.lang.InstantiationException;
  8. import java.lang.SecurityException;
  9. import java.lang.ClassCastException;
  10. import org.xml.sax.Parser;
  11. /**
  12. * Java-specific class for dynamically loading SAX parsers.
  13. *
  14. * <blockquote>
  15. * <em>This module, both source code and documentation, is in the
  16. * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
  17. * </blockquote>
  18. *
  19. * <p><strong>Note:</strong> This class is designed to work with the now-deprecated
  20. * SAX1 {@link org.xml.sax.Parser Parser} class. SAX2 applications should use
  21. * {@link org.xml.sax.helpers.XMLReaderFactory XMLReaderFactory} instead.</p>
  22. *
  23. * <p>ParserFactory is not part of the platform-independent definition
  24. * of SAX; it is an additional convenience class designed
  25. * specifically for Java XML application writers. SAX applications
  26. * can use the static methods in this class to allocate a SAX parser
  27. * dynamically at run-time based either on the value of the
  28. * `org.xml.sax.parser' system property or on a string containing the class
  29. * name.</p>
  30. *
  31. * <p>Note that the application still requires an XML parser that
  32. * implements SAX1.</p>
  33. *
  34. * @deprecated This class works with the deprecated
  35. * {@link org.xml.sax.Parser Parser}
  36. * interface.
  37. * @since SAX 1.0
  38. * @author David Megginson,
  39. * <a href="mailto:sax@megginson.com">sax@megginson.com</a>
  40. * @version 2.0
  41. * @see org.xml.sax.Parser
  42. * @see java.lang.Class
  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. // Original SAX code
  114. // return (Parser)(Class.forName(className).newInstance());
  115. return (Parser) NewInstance.newInstance(className);
  116. }
  117. }
  118. // end of ParserFactory.java