001: /* Copyright 2000 - 2001 Quadcap Software. All rights reserved.
002: *
003: * This software is distributed under the Quadcap Free Software License.
004: * This software may be used or modified for any purpose, personal or
005: * commercial. Open Source redistributions are permitted. Commercial
006: * redistribution of larger works derived from, or works which bundle
007: * this software requires a "Commercial Redistribution License"; see
008: * http://www.quadcap.com/purchase.
009: *
010: * Redistributions qualify as "Open Source" under one of the following terms:
011: *
012: * Redistributions are made at no charge beyond the reasonable cost of
013: * materials and delivery.
014: *
015: * Redistributions are accompanied by a copy of the Source Code or by an
016: * irrevocable offer to provide a copy of the Source Code for up to three
017: * years at the cost of materials and delivery. Such redistributions
018: * must allow further use, modification, and redistribution of the Source
019: * Code under substantially the same terms as this license.
020: *
021: * Redistributions of source code must retain the copyright notices as they
022: * appear in each source code file, these license terms, and the
023: * disclaimer/limitation of liability set forth as paragraph 6 below.
024: *
025: * Redistributions in binary form must reproduce this Copyright Notice,
026: * these license terms, and the disclaimer/limitation of liability set
027: * forth as paragraph 6 below, in the documentation and/or other materials
028: * provided with the distribution.
029: *
030: * The Software is provided on an "AS IS" basis. No warranty is
031: * provided that the Software is free of defects, or fit for a
032: * particular purpose.
033: *
034: * Limitation of Liability. Quadcap Software shall not be liable
035: * for any damages suffered by the Licensee or any third party resulting
036: * from use of the Software.
037: */
038:
039: // SAX parser factory.
040: // No warranty; no copyright -- use this as you will.
041: // $Id: ParserFactory.java,v 1.3 2001/01/06 06:11:04 stan Exp $
042: package org.xml.sax.helpers;
043:
044: import java.lang.ClassNotFoundException;
045: import java.lang.IllegalAccessException;
046: import java.lang.InstantiationException;
047: import java.lang.SecurityException;
048: import java.lang.ClassCastException;
049:
050: import org.xml.sax.Parser;
051:
052: /**
053: * Java-specific class for dynamically loading SAX parsers.
054: *
055: * <p>This class is not part of the platform-independent definition
056: * of SAX; it is an additional convenience class designed
057: * specifically for Java XML application writers. SAX applications
058: * can use the static methods in this class to allocate a SAX parser
059: * dynamically at run-time based either on the value of the
060: * `org.xml.sax.parser' system property or on a string containing the class
061: * name.</p>
062: *
063: * <p>Note that the application still requires an XML parser that
064: * implements SAX.</p>
065: *
066: * @author David Megginson (ak117@freenet.carleton.ca)
067: * @version 1.0
068: * @see org.xml.sax.Parser
069: * @see java.lang.Class
070: */
071: public class ParserFactory {
072:
073: /**
074: * Private null constructor.
075: private ParserFactor ()
076: {
077: }
078:
079:
080: /**
081: * Create a new SAX parser using the `org.xml.sax.parser' system property.
082: *
083: * <p>The named class must exist and must implement the
084: * org.xml.sax.Parser interface.</p>
085: *
086: * @exception java.lang.NullPointerException There is no value
087: * for the `org.xml.sax.parser' system property.
088: * @exception java.lang.ClassNotFoundException The SAX parser
089: * class was not found (check your CLASSPATH).
090: * @exception IllegalAccessException The SAX parser class was
091: * found, but you do not have permission to load
092: * it.
093: * @exception InstantiationException The SAX parser class was
094: * found but could not be instantiated.
095: * @exception java.lang.ClassCastException The SAX parser class
096: * was found and instantiated, but does not implement
097: * org.xml.sax.Parser.
098: * @see #makeParser(java.lang.String)
099: * @see org.xml.sax.Parser
100: */
101: public static Parser makeParser() throws ClassNotFoundException,
102: IllegalAccessException, InstantiationException,
103: NullPointerException, ClassCastException {
104: String className = System.getProperty("org.xml.sax.parser");
105: if (className == null) {
106: throw new NullPointerException(
107: "No value for sax.parser property");
108: } else {
109: return makeParser(className);
110: }
111: }
112:
113: /**
114: * Create a new SAX parser object using the class name provided.
115: *
116: * <p>The named class must exist and must implement the
117: * org.xml.sax.Parser interface.</p>
118: *
119: * @param className A string containing the name of the
120: * SAX parser class.
121: * @exception java.lang.ClassNotFoundException The SAX parser
122: * class was not found (check your CLASSPATH).
123: * @exception IllegalAccessException The SAX parser class was
124: * found, but you do not have permission to load
125: * it.
126: * @exception InstantiationException The SAX parser class was
127: * found but could not be instantiated.
128: * @exception java.lang.ClassCastException The SAX parser class
129: * was found and instantiated, but does not implement
130: * org.xml.sax.Parser.
131: * @see #makeParser()
132: * @see org.xml.sax.Parser
133: */
134: public static Parser makeParser(String className)
135: throws ClassNotFoundException, IllegalAccessException,
136: InstantiationException, ClassCastException {
137: return (Parser) (Class.forName(className).newInstance());
138: }
139:
140: }
|