FileDocCategorySizeDatePackage
ParserFactory.javaAPI DocphoneME MR2 API (J2ME)6575Wed May 02 18:00:42 BST 2007gov.nist.siplite.parser

ParserFactory

public class ParserFactory extends Object
A factory class that does a name lookup on a registered parser and returns a header parser for the given name.
version
JAIN-SIP-1.1 This code is in the public domain.

Fields Summary
private static Hashtable
parserTable
Current parser table.
private static Class[]
constructorArgs
Parameters to pass to this parser when instantiated.
Constructors Summary
Methods Summary
public static HeaderParsercreateParser(java.lang.String line)
Creates a parser for a header. This is the parser factory.

param
line the text to be parsed
return
the parsed data
exception
ParseException if a parsing error occurs

        parserTable = new Hashtable();
        constructorArgs = new Class[1];
        constructorArgs[0] = new String().getClass();

        parserTable.put("t", new ToParser().getClass());
        parserTable.put(ToHeader.NAME.toLowerCase(), new ToParser().getClass());

        parserTable.put(FromHeader.NAME.toLowerCase(),
                new FromParser().getClass());
        parserTable.put("f", new FromParser().getClass());

        parserTable.put(CSeqHeader.NAME.toLowerCase(),
                new CSeqParser().getClass());

        parserTable.put(ViaHeader.NAME.toLowerCase(),
                new ViaParser().getClass());
        parserTable.put("v", new ViaParser().getClass());

        parserTable.put(ContactHeader.NAME.toLowerCase(),
                new ContactParser().getClass());
        parserTable.put("m", new ContactParser().getClass());

        parserTable.put(
                ContentTypeHeader.NAME.toLowerCase(),
                new ContentTypeParser().getClass());
        parserTable.put("c", new ContentTypeParser().getClass());

        parserTable.put(
                ContentLengthHeader.NAME.toLowerCase(),
                new ContentLengthParser().getClass());
        parserTable.put("l", new ContentLengthParser().getClass());

        parserTable.put(
                AuthorizationHeader.NAME.toLowerCase(),
                new AuthorizationParser().getClass());

        parserTable.put(
                WWWAuthenticateHeader.NAME.toLowerCase(),
                new WWWAuthenticateParser().getClass());

        parserTable.put(CallIdHeader.NAME.toLowerCase(),
                new CallIDParser().getClass());
        parserTable.put("i", new CallIDParser().getClass());

        parserTable.put(RouteHeader.NAME.toLowerCase(),
                new RouteParser().getClass());

        parserTable.put(
                RecordRouteHeader.NAME.toLowerCase(),
                new RecordRouteParser().getClass());

        parserTable.put(DateHeader.NAME.toLowerCase(),
                new DateParser().getClass());

        parserTable.put(
                ProxyAuthorizationHeader.NAME.toLowerCase(),
                new ProxyAuthorizationParser().getClass());

        parserTable.put(
                ProxyAuthenticateHeader.NAME.toLowerCase(),
                new ProxyAuthenticateParser().getClass());

        parserTable.put(
                MaxForwardsHeader.NAME.toLowerCase(),
                new MaxForwardsParser().getClass());

        parserTable.put(ExpiresHeader.NAME.toLowerCase(),
                new ExpiresParser().getClass());

        parserTable.put(EventHeader.NAME.toLowerCase(),
                new EventParser().getClass());
        parserTable.put("o", new EventParser().getClass());

        parserTable.put(SubscriptionStateHeader.NAME.toLowerCase(),
                new SubscriptionStateParser().getClass());

        parserTable.put("a", new AcceptContactParser().getClass());
        parserTable.put(AcceptContactHeader.NAME.toLowerCase(),
            new AcceptContactParser().getClass());
    
        String headerName = Lexer.getHeaderName(line);
        String headerValue = Lexer.getHeaderValue(line);

        if (headerName == null || headerValue == null) {
            throw new ParseException("The header name or value is null", 0);
        }

        headerName = NameMap.expandHeaderName(headerName);
        Class parserClass = (Class) parserTable.get(headerName.toLowerCase());

        if (parserClass != null) {
            Exception ex = null;
            try {
                HeaderParser retval = (HeaderParser) parserClass.newInstance();
                retval.setHeaderToParse(line);
                return retval;
            } catch (InstantiationException ie) {
                ex = ie;
            } catch (IllegalAccessException iae) {
                ex = iae;
            }
            if (ex != null) {
                // print system message and exit
                InternalErrorHandler.handleException(ex);
            }
            return null;
        } else {
            if (Header.isParameterLess(headerName) ||
                    headerName.equalsIgnoreCase(Header.RSEQ) ||
                        headerName.equalsIgnoreCase(Header.RACK)) {
                // The header can't have any parameters
                HeaderParser retval = new ParameterLessParser(line);
                return retval;
            } else {
                // Create a generic header parser
                HeaderParser retval = new ExtensionParser(line);
                return retval;
            }
        }