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 gov.nist.siplite.SIPConstants;
028: import gov.nist.siplite.header.*;
029: import gov.nist.core.*;
030: import java.util.*;
031:
032: /**
033: * Generic header parser class. The parsers for various headers extend this
034: * class. To create a parser for a new header, extend this class and change
035: * the createParser class.
036: *
037: * @version JAIN-SIP-1.1
038: *
039: *
040: * <a href="{@docRoot}/uncopyright.html">This code is in the public domain.</a>
041: *
042: */
043: public class HeaderParser extends Parser {
044:
045: /**
046: * Parses the weekday field.
047: * @return an integer with the calendar content for wkday.
048: */
049: protected int wkday() throws ParseException {
050: dbg_enter("wkday");
051: try {
052: String tok = lexer.ttoken();
053: String id = tok.toLowerCase();
054:
055: if (Utils.equalsIgnoreCase(SIPConstants.TOKEN_DAY_MON, id))
056: return Calendar.MONDAY;
057: else if (Utils.equalsIgnoreCase(SIPConstants.TOKEN_DAY_TUE,
058: id))
059: return Calendar.TUESDAY;
060: else if (Utils.equalsIgnoreCase(SIPConstants.TOKEN_DAY_WED,
061: id))
062: return Calendar.WEDNESDAY;
063: else if (Utils.equalsIgnoreCase(SIPConstants.TOKEN_DAY_THU,
064: id))
065: return Calendar.THURSDAY;
066: else if (Utils.equalsIgnoreCase(SIPConstants.TOKEN_DAY_FRI,
067: id))
068: return Calendar.FRIDAY;
069: else if (Utils.equalsIgnoreCase(SIPConstants.TOKEN_DAY_SAT,
070: id))
071: return Calendar.SATURDAY;
072: else if (Utils.equalsIgnoreCase(SIPConstants.TOKEN_DAY_SUN,
073: id))
074: return Calendar.SUNDAY;
075: else
076: throw createParseException("bad wkday");
077: } finally {
078: dbg_leave("wkday");
079: }
080:
081: }
082:
083: /**
084: * Parses and return a date field.
085: * @return a date structure with the parsed value.
086: */
087: protected Calendar date() throws ParseException {
088: String errorMsg = "bad date field";
089: try {
090: Calendar retval = Calendar.getInstance(TimeZone
091: .getTimeZone("GMT"));
092: String s1 = lexer.number();
093: int day = Integer.parseInt(s1);
094: if (day <= 0 || day >= 31)
095: throw createParseException("Bad day ");
096: retval.set(Calendar.DAY_OF_MONTH, day);
097: lexer.match(' ');
098: String month = lexer.ttoken().toLowerCase();
099: if (month.equals("jan")) {
100: retval.set(Calendar.MONTH, Calendar.JANUARY);
101: } else if (month.equals("feb")) {
102: retval.set(Calendar.MONTH, Calendar.FEBRUARY);
103: } else if (month.equals("mar")) {
104: retval.set(Calendar.MONTH, Calendar.MARCH);
105: } else if (month.equals("apr")) {
106: retval.set(Calendar.MONTH, Calendar.APRIL);
107: } else if (month.equals("may")) {
108: retval.set(Calendar.MONTH, Calendar.MAY);
109: } else if (month.equals("jun")) {
110: retval.set(Calendar.MONTH, Calendar.JUNE);
111: } else if (month.equals("jul")) {
112: retval.set(Calendar.MONTH, Calendar.JULY);
113: } else if (month.equals("aug")) {
114: retval.set(Calendar.MONTH, Calendar.AUGUST);
115: } else if (month.equals("sep")) {
116: retval.set(Calendar.MONTH, Calendar.SEPTEMBER);
117: } else if (month.equals("oct")) {
118: retval.set(Calendar.MONTH, Calendar.OCTOBER);
119: } else if (month.equals("nov")) {
120: retval.set(Calendar.MONTH, Calendar.NOVEMBER);
121: } else if (month.equals("dec")) {
122: retval.set(Calendar.MONTH, Calendar.DECEMBER);
123: }
124: lexer.match(' ');
125: String s2 = lexer.number();
126: int yr = Integer.parseInt(s2);
127: retval.set(Calendar.YEAR, yr);
128: return retval;
129:
130: } catch (ParseException ex) {
131: throw createParseException(errorMsg);
132: } catch (NumberFormatException nfe) {
133: throw createParseException(errorMsg);
134: }
135: }
136:
137: /**
138: * Sets the time field. This has the format hour:minute:second.
139: * @param calendar input data for setting the time
140: * @exception ParseException if a parsing error occurs
141: */
142: protected void time(Calendar calendar) throws ParseException {
143: String errorMsg = "error processing time ";
144: try {
145: String s = lexer.number();
146: int hour = Integer.parseInt(s);
147: calendar.set(Calendar.HOUR_OF_DAY, hour);
148: lexer.match(':');
149: s = lexer.number();
150: int min = Integer.parseInt(s);
151: calendar.set(Calendar.MINUTE, min);
152: lexer.match(':');
153: s = lexer.number();
154: int sec = Integer.parseInt(s);
155: calendar.set(Calendar.SECOND, sec);
156: } catch (ParseException ex) {
157: throw createParseException(errorMsg);
158: } catch (NumberFormatException nfe) {
159: throw createParseException(errorMsg);
160: }
161: }
162:
163: /** Default constructor. */
164: protected HeaderParser() {
165: }
166:
167: /**
168: * Sets the header to be parsed.
169: * @param header the string to be processed.
170: */
171: public void setHeaderToParse(String header) throws ParseException {
172: if (this .lexer == null)
173: this .lexer = new Lexer("command_keywordLexer", header);
174: else
175: throw createParseException("header already set");
176: }
177:
178: /**
179: * Creates new HeaderParser.
180: * @param header String to parse.
181: */
182: protected HeaderParser(String header) {
183: this .lexer = new Lexer("command_keywordLexer", header);
184: }
185:
186: /**
187: * Constructor with initial lexer engine.
188: * @param lexer initial lexer engine
189: */
190: protected HeaderParser(Lexer lexer) {
191: this .lexer = lexer;
192: this .lexer.selectLexer("command_keywordLexer");
193: }
194:
195: /**
196: * Parses the SIP header from the buffer and return a parsed
197: * structure.
198: * @return the parsed header value
199: * @throws ParseException if there was an error parsing.
200: */
201: public Header parse() throws ParseException {
202: String name = lexer.getNextToken(':');
203: lexer.consume(1);
204: String body = lexer.getLine().trim();
205: // we dont set any fields because the header is
206: // ok
207: ExtensionHeader retval = new ExtensionHeader(name, body, body);
208: return retval;
209:
210: }
211:
212: /**
213: * Parses the header name until the colon and chew WS after that.
214: * @param tok the separator token
215: */
216: protected void headerName(int tok) throws ParseException {
217: this .lexer.match(tok);
218: this .lexer.SPorHT();
219: this .lexer.match(':');
220: this.lexer.SPorHT();
221: }
222: }
|