StatusLineParser.java revision 600c7a4bbc7348167293eac928192e695b4ad5ba
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*/
26package gov.nist.javax.sip.parser;
27import gov.nist.javax.sip.header.*;
28import java.text.ParseException;
29
30/**
31 * Parser for the SIP status line.
32 *
33 * @version 1.2 $Revision: 1.7 $ $Date: 2009/07/17 18:58:05 $
34 *
35 * @author M. Ranganathan   <br/>
36 *
37 *
38 *
39 */
40public class StatusLineParser extends Parser {
41    public StatusLineParser(String statusLine) {
42        this.lexer = new Lexer("status_lineLexer", statusLine);
43    }
44
45    public StatusLineParser(Lexer lexer) {
46        this.lexer = lexer;
47        this.lexer.selectLexer("status_lineLexer");
48    }
49
50    protected int statusCode() throws ParseException {
51        String scode = this.lexer.number();
52        if (debug)
53            dbg_enter("statusCode");
54        try {
55            int retval = Integer.parseInt(scode);
56            return retval;
57        } catch (NumberFormatException ex) {
58            throw new ParseException(
59                lexer.getBuffer() + ":" + ex.getMessage(),
60                lexer.getPtr());
61        } finally {
62            if (debug)
63                dbg_leave("statusCode");
64        }
65
66    }
67
68    protected String reasonPhrase() throws ParseException {
69        return this.lexer.getRest().trim();
70    }
71
72    public StatusLine parse() throws ParseException {
73        try {
74            if (debug)
75                dbg_enter("parse");
76            StatusLine retval = new StatusLine();
77            String version = this.sipVersion();
78            retval.setSipVersion(version);
79            lexer.SPorHT();
80            int scode = statusCode();
81            retval.setStatusCode(scode);
82            lexer.SPorHT();
83            String rp = reasonPhrase();
84            retval.setReasonPhrase(rp);
85            lexer.SPorHT();
86            return retval;
87        } finally {
88            if (debug)
89                dbg_leave("parse");
90        }
91    }
92
93    /**
94        public static void main(String[] args)  throws ParseException {
95            String[] statusLines = {
96             "SIP/2.0 200 OK\n",
97             "BOO 200 OK\n",
98             "SIP/2.0 500 OK bad things happened \n"
99            };
100            for (int i = 0 ; i < statusLines.length; i++) {
101               try {
102               StatusLineParser slp = new StatusLineParser(statusLines[i]);
103               StatusLine sl = slp.parse();
104               System.out.println("encoded = " + sl.encode());
105               } catch (ParseException ex) {
106                System.out.println("error message " + ex.getMessage());
107               }
108            }
109        }
110    */
111}
112/*
113 * $Log: StatusLineParser.java,v $
114 * Revision 1.7  2009/07/17 18:58:05  emcho
115 * Converts indentation tabs to spaces so that we have a uniform indentation policy in the whole project.
116 *
117 * Revision 1.6  2006/07/13 09:02:20  mranga
118 * Issue number:
119 * Obtained from:
120 * Submitted by:  jeroen van bemmel
121 * Reviewed by:   mranga
122 * Moved some changes from jain-sip-1.2 to java.net
123 *
124 * CVS: ----------------------------------------------------------------------
125 * CVS: Issue number:
126 * CVS:   If this change addresses one or more issues,
127 * CVS:   then enter the issue number(s) here.
128 * CVS: Obtained from:
129 * CVS:   If this change has been taken from another system,
130 * CVS:   then name the system in this line, otherwise delete it.
131 * CVS: Submitted by:
132 * CVS:   If this code has been contributed to the project by someone else; i.e.,
133 * CVS:   they sent us a patch or a set of diffs, then include their name/email
134 * CVS:   address here. If this is your work then delete this line.
135 * CVS: Reviewed by:
136 * CVS:   If we are doing pre-commit code reviews and someone else has
137 * CVS:   reviewed your changes, include their name(s) here.
138 * CVS:   If you have not had it reviewed then delete this line.
139 *
140 * Revision 1.3  2006/06/19 06:47:27  mranga
141 * javadoc fixups
142 *
143 * Revision 1.2  2006/06/16 15:26:28  mranga
144 * Added NIST disclaimer to all public domain files. Clean up some javadoc. Fixed a leak
145 *
146 * Revision 1.1.1.1  2005/10/04 17:12:36  mranga
147 *
148 * Import
149 *
150 *
151 * Revision 1.4  2004/01/22 13:26:32  sverker
152 * Issue number:
153 * Obtained from:
154 * Submitted by:  sverker
155 * Reviewed by:   mranga
156 *
157 * Major reformat of code to conform with style guide. Resolved compiler and javadoc warnings. Added CVS tags.
158 *
159 * CVS: ----------------------------------------------------------------------
160 * CVS: Issue number:
161 * CVS:   If this change addresses one or more issues,
162 * CVS:   then enter the issue number(s) here.
163 * CVS: Obtained from:
164 * CVS:   If this change has been taken from another system,
165 * CVS:   then name the system in this line, otherwise delete it.
166 * CVS: Submitted by:
167 * CVS:   If this code has been contributed to the project by someone else; i.e.,
168 * CVS:   they sent us a patch or a set of diffs, then include their name/email
169 * CVS:   address here. If this is your work then delete this line.
170 * CVS: Reviewed by:
171 * CVS:   If we are doing pre-commit code reviews and someone else has
172 * CVS:   reviewed your changes, include their name(s) here.
173 * CVS:   If you have not had it reviewed then delete this line.
174 *
175 */
176