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

HeaderParser

public class HeaderParser extends Parser
Generic header parser class. The parsers for various headers extend this class. To create a parser for a new header, extend this class and change the createParser class.
version
JAIN-SIP-1.1 This code is in the public domain.

Fields Summary
Constructors Summary
protected HeaderParser()
Default constructor.

 
protected HeaderParser(String header)
Creates new HeaderParser.

param
header String to parse.

        this.lexer = new Lexer("command_keywordLexer", header);
    
protected HeaderParser(Lexer lexer)
Constructor with initial lexer engine.

param
lexer initial lexer engine

        this.lexer = lexer;
        this.lexer.selectLexer("command_keywordLexer");
    
Methods Summary
protected java.util.Calendardate()
Parses and return a date field.

return
a date structure with the parsed value.

        String errorMsg = "bad date field";
        try {
            Calendar retval =
                    Calendar.getInstance(TimeZone.getTimeZone("GMT"));
            String s1 = lexer.number();
            int day = Integer.parseInt(s1);
            if (day <= 0 || day >= 31)
                throw createParseException("Bad day ");
            retval.set(Calendar.DAY_OF_MONTH, day);
            lexer.match(' ");
            String month = lexer.ttoken().toLowerCase();
            if (month.equals("jan")) {
                retval.set(Calendar.MONTH, Calendar.JANUARY);
            } else if (month.equals("feb")) {
                retval.set(Calendar.MONTH, Calendar.FEBRUARY);
            } else if (month.equals("mar")) {
                retval.set(Calendar.MONTH, Calendar.MARCH);
            } else if (month.equals("apr")) {
                retval.set(Calendar.MONTH, Calendar.APRIL);
            } else if (month.equals("may")) {
                retval.set(Calendar.MONTH, Calendar.MAY);
            } else if (month.equals("jun")) {
                retval.set(Calendar.MONTH, Calendar.JUNE);
            } else if (month.equals("jul")) {
                retval.set(Calendar.MONTH, Calendar.JULY);
            } else if (month.equals("aug")) {
                retval.set(Calendar.MONTH, Calendar.AUGUST);
            } else if (month.equals("sep")) {
                retval.set(Calendar.MONTH, Calendar.SEPTEMBER);
            } else if (month.equals("oct")) {
                retval.set(Calendar.MONTH, Calendar.OCTOBER);
            } else if (month.equals("nov")) {
                retval.set(Calendar.MONTH, Calendar.NOVEMBER);
            } else if (month.equals("dec")) {
                retval.set(Calendar.MONTH, Calendar.DECEMBER);
            }
            lexer.match(' ");
            String s2 = lexer.number();
            int yr = Integer.parseInt(s2);
            retval.set(Calendar.YEAR, yr);
            return retval;

        } catch (ParseException ex) {
            throw createParseException(errorMsg);
        } catch (NumberFormatException nfe) {
            throw createParseException(errorMsg);
        }
    
protected voidheaderName(int tok)
Parses the header name until the colon and chew WS after that.

param
tok the separator token

        this.lexer.match(tok);
        this.lexer.SPorHT();
        this.lexer.match(':");
        this.lexer.SPorHT();
    
public Headerparse()
Parses the SIP header from the buffer and return a parsed structure.

return
the parsed header value
throws
ParseException if there was an error parsing.

        String name = lexer.getNextToken(':");
        lexer.consume(1);
        String body = lexer.getLine().trim();
        // we dont set any fields because the header is
        // ok
        ExtensionHeader retval = new ExtensionHeader(name, body, body);
        return retval;

    
public voidsetHeaderToParse(java.lang.String header)
Sets the header to be parsed.

param
header the string to be processed.

        if (this.lexer == null)
            this.lexer = new Lexer("command_keywordLexer", header);
        else throw createParseException("header already set");
    
protected voidtime(java.util.Calendar calendar)
Sets the time field. This has the format hour:minute:second.

param
calendar input data for setting the time
exception
ParseException if a parsing error occurs

        String errorMsg = "error processing time ";
        try {
            String s = lexer.number();
            int hour = Integer.parseInt(s);
            calendar.set(Calendar.HOUR_OF_DAY, hour);
            lexer.match(':");
            s = lexer.number();
            int min = Integer.parseInt(s);
            calendar.set(Calendar.MINUTE, min);
            lexer.match(':");
            s = lexer.number();
            int sec = Integer.parseInt(s);
            calendar.set(Calendar.SECOND, sec);
        } catch (ParseException ex) {
            throw createParseException(errorMsg);
        } catch (NumberFormatException nfe) {
            throw createParseException(errorMsg);
        }
    
protected intwkday()
Parses the weekday field.

return
an integer with the calendar content for wkday.

        dbg_enter("wkday");
        try {
            String tok = lexer.ttoken();
            String id = tok.toLowerCase();

            if (Utils.equalsIgnoreCase(SIPConstants.TOKEN_DAY_MON, id))
                return Calendar.MONDAY;
            else if (Utils.equalsIgnoreCase
                    (SIPConstants.TOKEN_DAY_TUE, id)) return Calendar.TUESDAY;
            else if (Utils.equalsIgnoreCase(SIPConstants.TOKEN_DAY_WED, id))
                return Calendar.WEDNESDAY;
            else if (Utils.equalsIgnoreCase(SIPConstants.TOKEN_DAY_THU, id))
                return Calendar.THURSDAY;
            else if (Utils.equalsIgnoreCase
                    (SIPConstants.TOKEN_DAY_FRI, id))
                return Calendar.FRIDAY;
            else if (Utils.equalsIgnoreCase
                    (SIPConstants.TOKEN_DAY_SAT, id))
                return Calendar.SATURDAY;
            else if (Utils.equalsIgnoreCase
                    (SIPConstants.TOKEN_DAY_SUN, id)) return Calendar.SUNDAY;
            else
                throw createParseException("bad wkday");
        } finally {
            dbg_leave("wkday");
        }