001: /*
002: * Portions Copyright 2000-2007 Sun Microsystems, Inc. All Rights
003: * Reserved. Use is subject to license terms.
004: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License version
008: * 2 only, as published by the Free Software Foundation.
009: *
010: * This program is distributed in the hope that it will be useful, but
011: * WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * General Public License version 2 for more details (a copy is
014: * included at /legal/license.txt).
015: *
016: * You should have received a copy of the GNU General Public License
017: * version 2 along with this work; if not, write to the Free Software
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
022: * Clara, CA 95054 or visit www.sun.com if you need additional
023: * information or have any questions.
024: */
025: package gov.nist.javax.sdp.parser;
026:
027: import java.util.*;
028: import gov.nist.core.*;
029:
030: /**
031: * Factory for creating parsers for the SDP stuff.
032: *
033: * @version JAIN-SIP-1.1
034: *
035: *
036: * <a href="{@docRoot}/uncopyright.html">This code is in the public domain.</a>
037: */
038: public class ParserFactory {
039: /** Current parser table. */
040: private static Hashtable parserTable;
041: /** Parameters for instantiating the parser. */
042: private static Class[] constructorArgs;
043: /** the PArser classname. */
044: private static final String packageName = "gov.nist.javax.sdp.parser";
045:
046: /**
047: * Factory method to get the parser class.
048: * @param parserClass the name of the parser class.
049: * @return handle to parser engine
050: */
051: private static Class getParser(String parserClass) {
052: try {
053: return Class.forName(packageName + "." + parserClass);
054: } catch (ClassNotFoundException ex) {
055: System.out.println("Could not find class");
056: ex.printStackTrace();
057: System.exit(0);
058: return null; // dummy
059: }
060: }
061:
062: static {
063: constructorArgs = new Class[1];
064: constructorArgs[0] = new String().getClass();
065: parserTable = new Hashtable();
066: parserTable.put("a", getParser("AttributeFieldParser"));
067: parserTable.put("b", getParser("BandwidthFieldParser"));
068: parserTable.put("c", getParser("ConnectionFieldParser"));
069: parserTable.put("e", getParser("EmailFieldParser"));
070: parserTable.put("i", getParser("InformationFieldParser"));
071: parserTable.put("k", getParser("KeyFieldParser"));
072: parserTable.put("m", getParser("MediaFieldParser"));
073: parserTable.put("o", getParser("OriginFieldParser"));
074: parserTable.put("p", getParser("PhoneFieldParser"));
075: parserTable.put("v", getParser("ProtoVersionFieldParser"));
076: parserTable.put("r", getParser("RepeatFieldParser"));
077: parserTable.put("s", getParser("SessionNameFieldParser"));
078: parserTable.put("t", getParser("TimeFieldParser"));
079: parserTable.put("u", getParser("URIFieldParser"));
080: parserTable.put("z", getParser("ZoneFieldParser"));
081: }
082:
083: /**
084: * Factory method to create a parser engine.
085: * @param field the name of the next field to parse
086: * @return the handle to the SDP parsing engine
087: */
088: public static SDPParser createParser(String field)
089: throws ParseException {
090: String fieldName = Lexer.getFieldName(field);
091: if (fieldName == null)
092: return null;
093: Class parserClass = (Class) parserTable.get(fieldName
094: .toLowerCase());
095:
096: if (parserClass != null) {
097: Exception ex = null;
098: try {
099: SDPParser retval = (SDPParser) parserClass
100: .newInstance();
101: retval.setField(field);
102: return retval;
103: } catch (InstantiationException ie) {
104: ex = ie;
105: } catch (IllegalAccessException iae) {
106: ex = iae;
107: }
108: if (ex != null) {
109: // print system message and exit
110: InternalErrorHandler.handleException(ex);
111: return null;
112: }
113: } else
114: throw new ParseException("Could not find parser for "
115: + fieldName, 0);
116: }
117:
118: }
|