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.core.*;
028: import java.util.Vector;
029:
030: import com.sun.midp.log.Logging;
031: import com.sun.midp.log.LogChannels;
032:
033: /**
034: * Base parser class.
035: *
036: * @version JAIN-SIP-1.1
037: *
038: *
039: * <a href="{@docRoot}/uncopyright.html">This code is in the public domain.</a>
040: *
041: */
042:
043: public abstract class Parser extends ParserCore implements TokenTypes {
044: /** Default constructor. */
045: protected Parser() {
046: }
047:
048: /**
049: * Creates a ParseException with the provided message.
050: * @param exceptionString message for cause of execption
051: * @return the parser exception
052: */
053: protected ParseException createParseException(String exceptionString) {
054: return new ParseException(lexer.getBuffer() + ":"
055: + exceptionString, lexer.getPtr());
056: }
057:
058: /**
059: * Gets the current lexer engine.
060: * @return the current lexer engine
061: */
062: protected Lexer getLexer() {
063: return (Lexer) this .lexer;
064: }
065:
066: /**
067: * Gets the current SIP version string.
068: * @return the SIP version string.
069: * @exception ParseException if a parsing error occurs
070: */
071: protected String sipVersion() throws ParseException {
072: if (debug)
073: dbg_enter("sipVersion");
074: try {
075: Token tok = lexer.match(SIP);
076: if (!tok.getTokenValue().equals("SIP"))
077: createParseException("Expecting SIP");
078: lexer.match('/');
079: tok = lexer.match(ID);
080: if (!tok.getTokenValue().equals("2.0"))
081: createParseException("Expecting SIP/2.0");
082:
083: return "SIP/2.0";
084: } finally {
085: if (debug)
086: dbg_leave("sipVersion");
087: }
088: }
089:
090: /**
091: * Parses a method. Consumes if a valid method has been found.
092: * @return the parsed method string
093: * @exception ParseException if a parsing error occurs
094: */
095: protected String method() throws ParseException {
096: try {
097: if (debug)
098: dbg_enter("method");
099: Vector tokens = this .lexer.peekNextToken(1);
100: Token token = (Token) tokens.elementAt(0);
101: int tokenType = token.getTokenType();
102:
103: if (tokenType == INVITE || tokenType == ACK
104: || tokenType == OPTIONS || tokenType == BYE
105: || tokenType == REGISTER || tokenType == CANCEL
106: || tokenType == SUBSCRIBE || tokenType == NOTIFY
107: || tokenType == ID || tokenType == MESSAGE
108: || tokenType == INFO || tokenType == PUBLISH
109: || tokenType == UPDATE || tokenType == PRACK
110: || tokenType == REFER) {
111: lexer.consume();
112: return token.getTokenValue();
113: } else {
114: throw createParseException("Invalid Method");
115: }
116: } finally {
117: if (debug)
118: dbg_leave("method");
119: }
120: }
121: }
|