Debian Codegraph @

java/gov/nist/javax/sip/parser/HeaderParser.java

raw
1 /*
2 * Conditions Of Use
3 *
4 * This software was developed by employees of the National Institute of
5 * Standards and Technology (NIST), an agency of the Federal Government.
6 * Pursuant to title 15 Untied States Code Section 105, works of NIST
7 * employees are not subject to copyright protection in the United States
8 * and are considered to be in the public domain. As a result, a formal
9 * license is not needed to use the software.
10 *
11 * This software is provided by NIST as a service and is expressly
12 * provided "AS IS." NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
13 * OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
14 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
15 * AND DATA ACCURACY. NIST does not warrant or make any representations
16 * regarding the use of the software or the results thereof, including but
17 * not limited to the correctness, accuracy, reliability or usefulness of
18 * the software.
19 *
20 * Permission to use this software is contingent upon your acceptance
21 * of the terms of this agreement
22 *
23 * .
24 *
25 */
26 package gov.nist.javax.sip.parser;
27
28 import gov.nist.javax.sip.header.*;
29 import java.util.*;
30 import java.text.ParseException;
31
32 /**
33 * Generic header parser class. The parsers for various headers extend this
34 * class. To create a parser for a new header, extend this class and change
35 * the createParser class.
36 *
37 * @version 1.2 $Revision: 1.9 $ $Date: 2009/07/17 18:58:00 $
38 *
39 * @author M. Ranganathan <br/>
40 *
41 *
42 */
43 public class HeaderParser extends Parser {
44
45 /**
46 * Parse the weekday field
47 * @return an integer with the calendar content for wkday.
48 */
49 protected int wkday() throws ParseException {
50 dbg_enter("wkday");
51 try {
52 String tok = lexer.ttoken();
53 String id = tok.toLowerCase();
54
55 if (TokenNames.MON.equalsIgnoreCase(id))
56 return Calendar.MONDAY;
57 else if (TokenNames.TUE.equalsIgnoreCase(id))
58 return Calendar.TUESDAY;
59 else if (TokenNames.WED.equalsIgnoreCase(id))
60 return Calendar.WEDNESDAY;
61 else if (TokenNames.THU.equalsIgnoreCase(id))
62 return Calendar.THURSDAY;
63 else if (TokenNames.FRI.equalsIgnoreCase(id))
64 return Calendar.FRIDAY;
65 else if (TokenNames.SAT.equalsIgnoreCase(id))
66 return Calendar.SATURDAY;
67 else if (TokenNames.SUN.equalsIgnoreCase(id))
68 return Calendar.SUNDAY;
69 else
70 throw createParseException("bad wkday");
71 } finally {
72 dbg_leave("wkday");
73 }
74
75 }
76
77 /**
78 * parse and return a date field.
79 * @return a date structure with the parsed value.
80 */
81 protected Calendar date() throws ParseException {
82 try {
83 Calendar retval = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
84 String s1 = lexer.number();
85 int day = Integer.parseInt(s1);
86 if (day <= 0 || day > 31)
87 throw createParseException("Bad day ");
88 retval.set(Calendar.DAY_OF_MONTH, day);
89 lexer.match(' ');
90 String month = lexer.ttoken().toLowerCase();
91 if (month.equals("jan")) {
92 retval.set(Calendar.MONTH, Calendar.JANUARY);
93 } else if (month.equals("feb")) {
94 retval.set(Calendar.MONTH, Calendar.FEBRUARY);
95 } else if (month.equals("mar")) {
96 retval.set(Calendar.MONTH, Calendar.MARCH);
97 } else if (month.equals("apr")) {
98 retval.set(Calendar.MONTH, Calendar.APRIL);
99 } else if (month.equals("may")) {
100 retval.set(Calendar.MONTH, Calendar.MAY);
101 } else if (month.equals("jun")) {
102 retval.set(Calendar.MONTH, Calendar.JUNE);
103 } else if (month.equals("jul")) {
104 retval.set(Calendar.MONTH, Calendar.JULY);
105 } else if (month.equals("aug")) {
106 retval.set(Calendar.MONTH, Calendar.AUGUST);
107 } else if (month.equals("sep")) {
108 retval.set(Calendar.MONTH, Calendar.SEPTEMBER);
109 } else if (month.equals("oct")) {
110 retval.set(Calendar.MONTH, Calendar.OCTOBER);
111 } else if (month.equals("nov")) {
112 retval.set(Calendar.MONTH, Calendar.NOVEMBER);
113 } else if (month.equals("dec")) {
114 retval.set(Calendar.MONTH, Calendar.DECEMBER);
115 }
116 lexer.match(' ');
117 String s2 = lexer.number();
118 int yr = Integer.parseInt(s2);
119 retval.set(Calendar.YEAR, yr);
120 return retval;
121
122 } catch (Exception ex) {
123 throw createParseException("bad date field");
124 }
125
126 }
127
128 /**
129 * Set the time field. This has the format hour:minute:second
130 */
131 protected void time(Calendar calendar) throws ParseException {
132 try {
133 String s = lexer.number();
134 int hour = Integer.parseInt(s);
135 calendar.set(Calendar.HOUR_OF_DAY, hour);
136 lexer.match(':');
137 s = lexer.number();
138 int min = Integer.parseInt(s);
139 calendar.set(Calendar.MINUTE, min);
140 lexer.match(':');
141 s = lexer.number();
142 int sec = Integer.parseInt(s);
143 calendar.set(Calendar.SECOND, sec);
144 } catch (Exception ex) {
145 throw createParseException("error processing time ");
146
147 }
148
149 }
150
151 /**
152 * Creates new HeaderParser
153 * @param String to parse.
154 */
155 protected HeaderParser(String header) {
156 this.lexer = new Lexer("command_keywordLexer", header);
157 }
158
159 protected HeaderParser(Lexer lexer) {
160 this.lexer = lexer;
161 this.lexer.selectLexer("command_keywordLexer");
162 }
163
164 /**
165 * Parse the SIP header from the buffer and return a parsed
166 * structure.
167 * @throws ParseException if there was an error parsing.
168 */
169 public SIPHeader parse() throws ParseException {
170 String name = lexer.getNextToken(':');
171 lexer.consume(1);
172 String body = lexer.getLine().trim();
173 // we dont set any fields because the header is
174 // ok
175 ExtensionHeaderImpl retval = new ExtensionHeaderImpl(name);
176 retval.setValue(body);
177 return retval;
178
179 }
180
181 /**
182 * Parse the header name until the colon and chew WS after that.
183 */
184 protected void headerName(int tok) throws ParseException {
185 this.lexer.match(tok);
186 this.lexer.SPorHT();
187 this.lexer.match(':');
188 this.lexer.SPorHT();
189 }
190 }