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.siplite.parser;
026:
027: import java.util.Hashtable;
028: import gov.nist.siplite.header.*;
029: import gov.nist.core.*;
030:
031: /**
032: * A factory class that does a name lookup on a registered parser and
033: * returns a header parser for the given name.
034: *
035: * @version JAIN-SIP-1.1
036: *
037: *
038: * <a href="{@docRoot}/uncopyright.html">This code is in the public domain.</a>
039: *
040: */
041: public class ParserFactory {
042: /** Current parser table. */
043: private static Hashtable parserTable;
044:
045: /** Parameters to pass to this parser when instantiated. */
046: private static Class[] constructorArgs;
047:
048: static {
049: parserTable = new Hashtable();
050: constructorArgs = new Class[1];
051: constructorArgs[0] = new String().getClass();
052:
053: parserTable.put("t", new ToParser().getClass());
054: parserTable.put(ToHeader.NAME.toLowerCase(), new ToParser()
055: .getClass());
056:
057: parserTable.put(FromHeader.NAME.toLowerCase(), new FromParser()
058: .getClass());
059: parserTable.put("f", new FromParser().getClass());
060:
061: parserTable.put(CSeqHeader.NAME.toLowerCase(), new CSeqParser()
062: .getClass());
063:
064: parserTable.put(ViaHeader.NAME.toLowerCase(), new ViaParser()
065: .getClass());
066: parserTable.put("v", new ViaParser().getClass());
067:
068: parserTable.put(ContactHeader.NAME.toLowerCase(),
069: new ContactParser().getClass());
070: parserTable.put("m", new ContactParser().getClass());
071:
072: parserTable.put(ContentTypeHeader.NAME.toLowerCase(),
073: new ContentTypeParser().getClass());
074: parserTable.put("c", new ContentTypeParser().getClass());
075:
076: parserTable.put(ContentLengthHeader.NAME.toLowerCase(),
077: new ContentLengthParser().getClass());
078: parserTable.put("l", new ContentLengthParser().getClass());
079:
080: parserTable.put(AuthorizationHeader.NAME.toLowerCase(),
081: new AuthorizationParser().getClass());
082:
083: parserTable.put(WWWAuthenticateHeader.NAME.toLowerCase(),
084: new WWWAuthenticateParser().getClass());
085:
086: parserTable.put(CallIdHeader.NAME.toLowerCase(),
087: new CallIDParser().getClass());
088: parserTable.put("i", new CallIDParser().getClass());
089:
090: parserTable.put(RouteHeader.NAME.toLowerCase(),
091: new RouteParser().getClass());
092:
093: parserTable.put(RecordRouteHeader.NAME.toLowerCase(),
094: new RecordRouteParser().getClass());
095:
096: parserTable.put(DateHeader.NAME.toLowerCase(), new DateParser()
097: .getClass());
098:
099: parserTable.put(ProxyAuthorizationHeader.NAME.toLowerCase(),
100: new ProxyAuthorizationParser().getClass());
101:
102: parserTable.put(ProxyAuthenticateHeader.NAME.toLowerCase(),
103: new ProxyAuthenticateParser().getClass());
104:
105: parserTable.put(MaxForwardsHeader.NAME.toLowerCase(),
106: new MaxForwardsParser().getClass());
107:
108: parserTable.put(ExpiresHeader.NAME.toLowerCase(),
109: new ExpiresParser().getClass());
110:
111: parserTable.put(EventHeader.NAME.toLowerCase(),
112: new EventParser().getClass());
113: parserTable.put("o", new EventParser().getClass());
114:
115: parserTable.put(SubscriptionStateHeader.NAME.toLowerCase(),
116: new SubscriptionStateParser().getClass());
117:
118: parserTable.put("a", new AcceptContactParser().getClass());
119: parserTable.put(AcceptContactHeader.NAME.toLowerCase(),
120: new AcceptContactParser().getClass());
121: }
122:
123: /**
124: * Creates a parser for a header. This is the parser factory.
125: * @param line the text to be parsed
126: * @return the parsed data
127: * @exception ParseException if a parsing error occurs
128: */
129: public static HeaderParser createParser(String line)
130: throws ParseException {
131: String headerName = Lexer.getHeaderName(line);
132: String headerValue = Lexer.getHeaderValue(line);
133:
134: if (headerName == null || headerValue == null) {
135: throw new ParseException(
136: "The header name or value is null", 0);
137: }
138:
139: headerName = NameMap.expandHeaderName(headerName);
140: Class parserClass = (Class) parserTable.get(headerName
141: .toLowerCase());
142:
143: if (parserClass != null) {
144: Exception ex = null;
145: try {
146: HeaderParser retval = (HeaderParser) parserClass
147: .newInstance();
148: retval.setHeaderToParse(line);
149: return retval;
150: } catch (InstantiationException ie) {
151: ex = ie;
152: } catch (IllegalAccessException iae) {
153: ex = iae;
154: }
155: if (ex != null) {
156: // print system message and exit
157: InternalErrorHandler.handleException(ex);
158: }
159: return null;
160: } else {
161: if (Header.isParameterLess(headerName)
162: || headerName.equalsIgnoreCase(Header.RSEQ)
163: || headerName.equalsIgnoreCase(Header.RACK)) {
164: // The header can't have any parameters
165: HeaderParser retval = new ParameterLessParser(line);
166: return retval;
167: } else {
168: // Create a generic header parser
169: HeaderParser retval = new ExtensionParser(line);
170: return retval;
171: }
172: }
173: }
174: }
|