1. // XMLReaderFactory.java - factory for creating a new reader.
  2. // Written by David Megginson, sax@megginson.com
  3. // NO WARRANTY! This class is in the Public Domain.
  4. // $Id: XMLReaderFactory.java,v 1.2 2001/08/01 06:43:20 tcng Exp $
  5. package org.xml.sax.helpers;
  6. import org.xml.sax.Parser;
  7. import org.xml.sax.XMLReader;
  8. import org.xml.sax.SAXException;
  9. /**
  10. * Factory for creating an XML reader.
  11. *
  12. * <blockquote>
  13. * <em>This module, both source code and documentation, is in the
  14. * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
  15. * </blockquote>
  16. *
  17. * <p>This class contains static methods for creating an XML reader
  18. * from an explicit class name, or for creating an XML reader based
  19. * on the value of the <code>org.xml.sax.driver</code> system
  20. * property:</p>
  21. *
  22. * <pre>
  23. * try {
  24. * XMLReader myReader = XMLReaderFactory.createXMLReader();
  25. * } catch (SAXException e) {
  26. * System.err.println(e.getMessage());
  27. * }
  28. * </pre>
  29. *
  30. * <p>Note that these methods will not be usable in environments where
  31. * system properties are not accessible or where the application or
  32. * applet is not permitted to load classes dynamically.</p>
  33. *
  34. * <p><strong>Note to implementors:</strong> SAX implementations in specialized
  35. * environments may replace this class with a different one optimized for the
  36. * environment, as long as its method signatures remain the same.</p>
  37. *
  38. * @since SAX 2.0
  39. * @author David Megginson,
  40. * <a href="mailto:sax@megginson.com">sax@megginson.com</a>
  41. * @version 2.0
  42. * @see org.xml.sax.XMLReader
  43. */
  44. final public class XMLReaderFactory
  45. {
  46. /**
  47. * Private constructor.
  48. *
  49. * <p>This constructor prevents the class from being instantiated.</p>
  50. */
  51. private XMLReaderFactory ()
  52. {
  53. }
  54. /**
  55. * Attempt to create an XML reader from a system property.
  56. *
  57. * <p>This method uses the value of the system property
  58. * "org.xml.sax.driver" as the full name of a Java class
  59. * and tries to instantiate that class as a SAX2
  60. * XMLReader.</p>
  61. *
  62. * <p>Note that many Java interpreters allow system properties
  63. * to be specified on the command line.</p>
  64. *
  65. * @return A new XMLReader.
  66. * @exception org.xml.sax.SAXException If the value of the
  67. * "org.xml.sax.driver" system property is null,
  68. * or if the class cannot be loaded and instantiated.
  69. * @see #createXMLReader(java.lang.String)
  70. */
  71. public static XMLReader createXMLReader ()
  72. throws SAXException
  73. {
  74. String className = System.getProperty("org.xml.sax.driver");
  75. if (className == null) {
  76. Parser parser;
  77. try {
  78. parser = ParserFactory.makeParser();
  79. } catch (Exception e) {
  80. parser = null;
  81. }
  82. if (parser == null) {
  83. throw new
  84. SAXException("System property org.xml.sax.driver not specified");
  85. } else {
  86. return new ParserAdapter(parser);
  87. }
  88. } else {
  89. return createXMLReader(className);
  90. }
  91. }
  92. /**
  93. * Attempt to create an XML reader from a class name.
  94. *
  95. * <p>Given a class name, this method attempts to load
  96. * and instantiate the class as an XML reader.</p>
  97. *
  98. * @return A new XML reader.
  99. * @exception org.xml.sax.SAXException If the class cannot be
  100. * loaded, instantiated, and cast to XMLReader.
  101. * @see #createXMLReader()
  102. */
  103. public static XMLReader createXMLReader (String className)
  104. throws SAXException
  105. {
  106. try {
  107. // Original SAX code
  108. // return (XMLReader)(Class.forName(className).newInstance());
  109. return (XMLReader) NewInstance.newInstance(className);
  110. } catch (ClassNotFoundException e1) {
  111. throw new SAXException("SAX2 driver class " + className +
  112. " not found", e1);
  113. } catch (IllegalAccessException e2) {
  114. throw new SAXException("SAX2 driver class " + className +
  115. " found but cannot be loaded", e2);
  116. } catch (InstantiationException e3) {
  117. throw new SAXException("SAX2 driver class " + className +
  118. " loaded but cannot be instantiated (no empty public constructor?)",
  119. e3);
  120. } catch (ClassCastException e4) {
  121. throw new SAXException("SAX2 driver class " + className +
  122. " does not implement XMLReader", e4);
  123. }
  124. }
  125. }
  126. // end of XMLReaderFactory.java