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.core.Debug;
28import gov.nist.core.LexerCore;
29import gov.nist.core.ParserCore;
30import gov.nist.core.Token;
31import java.text.ParseException;
32
33/**
34 * Base parser class.
35 *
36 * @version 1.2 $Revision: 1.10 $ $Date: 2009/07/17 18:58:01 $
37 *
38 * @author M. Ranganathan   <br/>
39 *
40 *
41 */
42public abstract class Parser extends ParserCore implements TokenTypes {
43
44    protected ParseException createParseException(String exceptionString) {
45        return new ParseException(
46            lexer.getBuffer() + ":" + exceptionString,
47            lexer.getPtr());
48    }
49
50    protected Lexer getLexer() {
51        return (Lexer) this.lexer;
52    }
53
54    protected String sipVersion() throws ParseException {
55        if (debug)
56            dbg_enter("sipVersion");
57        try {
58            Token tok = lexer.match(SIP);
59            if (!tok.getTokenValue().equalsIgnoreCase("SIP"))
60                createParseException("Expecting SIP");
61            lexer.match('/');
62            tok = lexer.match(ID);
63            if (!tok.getTokenValue().equals("2.0"))
64                createParseException("Expecting SIP/2.0");
65
66            return "SIP/2.0";
67        } finally {
68            if (debug)
69                dbg_leave("sipVersion");
70        }
71    }
72
73    /**
74     * parses a method. Consumes if a valid method has been found.
75     */
76    protected String method() throws ParseException {
77        try {
78            if (debug)
79                dbg_enter("method");
80            Token[] tokens = this.lexer.peekNextToken(1);
81            Token token = (Token) tokens[0];
82            if (token.getTokenType() == INVITE
83                || token.getTokenType() == ACK
84                || token.getTokenType() == OPTIONS
85                || token.getTokenType() == BYE
86                || token.getTokenType() == REGISTER
87                || token.getTokenType() == CANCEL
88                || token.getTokenType() == SUBSCRIBE
89                || token.getTokenType() == NOTIFY
90                || token.getTokenType() == PUBLISH
91                || token.getTokenType() == MESSAGE
92                || token.getTokenType() == ID) {
93                lexer.consume();
94                return token.getTokenValue();
95            } else {
96                throw createParseException("Invalid Method");
97            }
98        } finally {
99            if (Debug.debug)
100                dbg_leave("method");
101        }
102    }
103
104    /**
105     * Verifies that a given string matches the 'token' production in RFC3261
106     *
107     * @param token
108     * @throws ParseException - if there are invalid characters
109     *
110     * @author JvB
111     */
112    public static final void checkToken( String token ) throws ParseException {
113
114        if (token == null || token.length()==0 ) {
115            throw new ParseException("null or empty token", -1 );
116        } else {
117            // JvB: check that it is a valid token
118            for ( int i=0; i<token.length(); ++i ) {
119                if ( !LexerCore.isTokenChar( token.charAt(i) )) {
120                    throw new ParseException( "Invalid character(s) in string (not allowed in 'token')", i );
121                }
122            }
123        }
124    }
125}
126/*
127 * $Log: Parser.java,v $
128 * Revision 1.10  2009/07/17 18:58:01  emcho
129 * Converts indentation tabs to spaces so that we have a uniform indentation policy in the whole project.
130 *
131 * Revision 1.9  2008/01/18 11:19:24  jbemmel
132 * added a method to check strings for valid token characters
133 *
134 * Revision 1.8  2007/02/23 14:56:05  belangery
135 * Added performance improvement around header name lowercase conversion.
136 *
137 * Revision 1.7  2006/09/27 15:02:43  mranga
138 * Issue number:
139 * Obtained from:
140 * Submitted by:
141 * Reviewed by:   mranga
142 * rfc 2543 transaction matching. fix for MESSAGE request type parsing.
143 * CVS: ----------------------------------------------------------------------
144 * CVS: Issue number:
145 * CVS:   If this change addresses one or more issues,
146 * CVS:   then enter the issue number(s) here.
147 * CVS: Obtained from:
148 * CVS:   If this change has been taken from another system,
149 * CVS:   then name the system in this line, otherwise delete it.
150 * CVS: Submitted by:
151 * CVS:   If this code has been contributed to the project by someone else; i.e.,
152 * CVS:   they sent us a patch or a set of diffs, then include their name/email
153 * CVS:   address here. If this is your work then delete this line.
154 * CVS: Reviewed by:
155 * CVS:   If we are doing pre-commit code reviews and someone else has
156 * CVS:   reviewed your changes, include their name(s) here.
157 * CVS:   If you have not had it reviewed then delete this line.
158 *
159 * Revision 1.6  2006/07/13 09:02:18  mranga
160 * Issue number:
161 * Obtained from:
162 * Submitted by:  jeroen van bemmel
163 * Reviewed by:   mranga
164 * Moved some changes from jain-sip-1.2 to java.net
165 *
166 * CVS: ----------------------------------------------------------------------
167 * CVS: Issue number:
168 * CVS:   If this change addresses one or more issues,
169 * CVS:   then enter the issue number(s) here.
170 * CVS: Obtained from:
171 * CVS:   If this change has been taken from another system,
172 * CVS:   then name the system in this line, otherwise delete it.
173 * CVS: Submitted by:
174 * CVS:   If this code has been contributed to the project by someone else; i.e.,
175 * CVS:   they sent us a patch or a set of diffs, then include their name/email
176 * CVS:   address here. If this is your work then delete this line.
177 * CVS: Reviewed by:
178 * CVS:   If we are doing pre-commit code reviews and someone else has
179 * CVS:   reviewed your changes, include their name(s) here.
180 * CVS:   If you have not had it reviewed then delete this line.
181 *
182 * Revision 1.5  2006/06/19 06:47:27  mranga
183 * javadoc fixups
184 *
185 * Revision 1.4  2006/06/16 15:26:28  mranga
186 * Added NIST disclaimer to all public domain files. Clean up some javadoc. Fixed a leak
187 *
188 * Revision 1.3  2005/11/21 23:24:49  jeroen
189 * "SIP" is case insensitive
190 *
191 * Revision 1.2  2005/10/27 20:49:00  jeroen
192 * added support for RFC3903 PUBLISH
193 *
194 * Revision 1.1.1.1  2005/10/04 17:12:35  mranga
195 *
196 * Import
197 *
198 *
199 * Revision 1.3  2004/01/22 13:26:31  sverker
200 * Issue number:
201 * Obtained from:
202 * Submitted by:  sverker
203 * Reviewed by:   mranga
204 *
205 * Major reformat of code to conform with style guide. Resolved compiler and javadoc warnings. Added CVS tags.
206 *
207 * CVS: ----------------------------------------------------------------------
208 * CVS: Issue number:
209 * CVS:   If this change addresses one or more issues,
210 * CVS:   then enter the issue number(s) here.
211 * CVS: Obtained from:
212 * CVS:   If this change has been taken from another system,
213 * CVS:   then name the system in this line, otherwise delete it.
214 * CVS: Submitted by:
215 * CVS:   If this code has been contributed to the project by someone else; i.e.,
216 * CVS:   they sent us a patch or a set of diffs, then include their name/email
217 * CVS:   address here. If this is your work then delete this line.
218 * CVS: Reviewed by:
219 * CVS:   If we are doing pre-commit code reviews and someone else has
220 * CVS:   reviewed your changes, include their name(s) here.
221 * CVS:   If you have not had it reviewed then delete this line.
222 *
223 */
224