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:
026: package gov.nist.siplite.parser;
027:
028: import gov.nist.core.*;
029: import gov.nist.siplite.header.*;
030:
031: /**
032: * Parser for the SIP status line.
033: *
034: * @version JAIN-SIP-1.1
035: *
036: *
037: * <a href="{@docRoot}/uncopyright.html">This code is in the public domain.</a>
038: *
039: */
040: public class StatusLineParser extends Parser {
041: /**
042: * Constructor with initial status line string.
043: * @param statusLine initial status line
044: */
045: public StatusLineParser(String statusLine) {
046: this .lexer = new Lexer("status_lineLexer", statusLine);
047: }
048:
049: /**
050: * Constructor with initial lexer engine.
051: * @param lexer initial lexer engine
052: */
053: public StatusLineParser(Lexer lexer) {
054: this .lexer = lexer;
055: this .lexer.selectLexer("status_lineLexer");
056: }
057:
058: /**
059: * Gets the status code from the parsed line.
060: * @return the status code
061: * @exception ParseException if a parsing error occurs
062: */
063: protected int statusCode() throws ParseException {
064: String scode = this .lexer.number();
065: if (debug)
066: dbg_enter("statusCode");
067: try {
068: int retval = Integer.parseInt(scode);
069: return retval;
070: } catch (NumberFormatException ex) {
071: throw new ParseException(lexer.getBuffer() + ":"
072: + ex.getMessage(), lexer.getPtr());
073: } finally {
074: if (debug)
075: dbg_leave("statusCode");
076: }
077:
078: }
079:
080: /**
081: * Gets the reason phrase.
082: * @return the reason phrase
083: * @exception ParseException of a parsing error occurs
084: */
085: protected String reasonPhrase() throws ParseException {
086: return this .lexer.getRest().trim();
087: }
088:
089: /**
090: * Parses the status line message.
091: * @return Header the status line object
092: * @throws ParseException if errors occur during the parsing
093: */
094: public StatusLine parse() throws ParseException {
095: try {
096: if (debug)
097: dbg_enter("parse");
098: StatusLine retval = new StatusLine();
099: String version = this .sipVersion();
100: retval.setSipVersion(version);
101: lexer.SPorHT();
102: int scode = statusCode();
103: retval.setStatusCode(scode);
104: lexer.SPorHT();
105: String rp = reasonPhrase();
106: retval.setReasonPhrase(rp);
107: lexer.SPorHT();
108: return retval;
109: } finally {
110: if (debug)
111: dbg_leave("parse");
112: }
113: }
114:
115: }
|