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;
27
28import gov.nist.javax.sip.header.*;
29import java.text.ParseException;
30import javax.sip.*;
31
32/**
33 * Parser for TimeStamp header.
34 *
35 * @version 1.2 $Revision: 1.9 $ $Date: 2009/10/18 13:46:39 $
36 *
37 * @author Olivier Deruelle   <br/>
38 * @author M. Ranganathan   <br/>
39 *
40 *
41 */
42public class TimeStampParser extends HeaderParser {
43
44    /**
45     * Creates a new instance of TimeStampParser
46     * @param timeStamp the header to parse
47     */
48    public TimeStampParser(String timeStamp) {
49        super(timeStamp);
50    }
51
52    /**
53     * Constructor
54     * @param lexer the lexer to use to parse the header
55     */
56    protected TimeStampParser(Lexer lexer) {
57        super(lexer);
58    }
59
60    /**
61     * parse the String message
62     * @return SIPHeader (TimeStamp object)
63     * @throws SIPParseException if the message does not respect the spec.
64     */
65    public SIPHeader parse() throws ParseException {
66
67        if (debug)
68            dbg_enter("TimeStampParser.parse");
69        TimeStamp timeStamp = new TimeStamp();
70        try {
71            headerName(TokenTypes.TIMESTAMP);
72
73            timeStamp.setHeaderName(SIPHeaderNames.TIMESTAMP);
74
75            this.lexer.SPorHT();
76            String firstNumber = this.lexer.number();
77
78            try {
79
80                if (lexer.lookAhead(0) == '.') {
81                    this.lexer.match('.');
82                    String secondNumber = this.lexer.number();
83
84                    String s = firstNumber + "." + secondNumber;
85                    float ts = Float.parseFloat(s);
86                    timeStamp.setTimeStamp(ts);
87                } else {
88                    long ts = Long.parseLong(firstNumber);
89                    timeStamp.setTime(ts);
90                }
91
92
93            } catch (NumberFormatException ex) {
94                throw createParseException(ex.getMessage());
95            } catch (InvalidArgumentException ex) {
96                throw createParseException(ex.getMessage());
97            }
98
99            this.lexer.SPorHT();
100            if (lexer.lookAhead(0) != '\n') {
101                firstNumber = this.lexer.number();
102
103                try {
104
105                    if (lexer.lookAhead(0) == '.') {
106                        this.lexer.match('.');
107                        String secondNumber = this.lexer.number();
108
109                        String s = firstNumber + "." + secondNumber;
110                        float ts = Float.parseFloat(s);
111                        timeStamp.setDelay(ts);
112                    } else {
113                        int ts = Integer.parseInt(firstNumber);
114                        timeStamp.setDelay(ts);
115                    }
116
117
118                } catch (NumberFormatException ex) {
119                    throw createParseException(ex.getMessage());
120                } catch (InvalidArgumentException ex) {
121                    throw createParseException(ex.getMessage());
122                }
123            }
124
125        } finally {
126            if (debug)
127                dbg_leave("TimeStampParser.parse");
128        }
129
130        return timeStamp;
131    }
132
133
134
135
136}
137/*
138 * $Log: TimeStampParser.java,v $
139 * Revision 1.9  2009/10/18 13:46:39  deruelle_jean
140 * FindBugs Fixes (Category Performance Warnings)
141 *
142 * Issue number:
143 * Obtained from:
144 * Submitted by: Jean Deruelle
145 * Reviewed by:
146 *
147 * Revision 1.8  2009/07/17 18:58:06  emcho
148 * Converts indentation tabs to spaces so that we have a uniform indentation policy in the whole project.
149 *
150 * Revision 1.7  2006/08/15 21:44:50  mranga
151 * Issue number:
152 * Obtained from:
153 * Submitted by:  mranga
154 * Reviewed by:   mranga
155 * Incorporating the latest API changes from Phelim
156 * CVS: ----------------------------------------------------------------------
157 * CVS: Issue number:
158 * CVS:   If this change addresses one or more issues,
159 * CVS:   then enter the issue number(s) here.
160 * CVS: Obtained from:
161 * CVS:   If this change has been taken from another system,
162 * CVS:   then name the system in this line, otherwise delete it.
163 * CVS: Submitted by:
164 * CVS:   If this code has been contributed to the project by someone else; i.e.,
165 * CVS:   they sent us a patch or a set of diffs, then include their name/email
166 * CVS:   address here. If this is your work then delete this line.
167 * CVS: Reviewed by:
168 * CVS:   If we are doing pre-commit code reviews and someone else has
169 * CVS:   reviewed your changes, include their name(s) here.
170 * CVS:   If you have not had it reviewed then delete this line.
171 *
172 * Revision 1.6  2006/07/13 09:02:14  mranga
173 * Issue number:
174 * Obtained from:
175 * Submitted by:  jeroen van bemmel
176 * Reviewed by:   mranga
177 * Moved some changes from jain-sip-1.2 to java.net
178 *
179 * CVS: ----------------------------------------------------------------------
180 * CVS: Issue number:
181 * CVS:   If this change addresses one or more issues,
182 * CVS:   then enter the issue number(s) here.
183 * CVS: Obtained from:
184 * CVS:   If this change has been taken from another system,
185 * CVS:   then name the system in this line, otherwise delete it.
186 * CVS: Submitted by:
187 * CVS:   If this code has been contributed to the project by someone else; i.e.,
188 * CVS:   they sent us a patch or a set of diffs, then include their name/email
189 * CVS:   address here. If this is your work then delete this line.
190 * CVS: Reviewed by:
191 * CVS:   If we are doing pre-commit code reviews and someone else has
192 * CVS:   reviewed your changes, include their name(s) here.
193 * CVS:   If you have not had it reviewed then delete this line.
194 *
195 * Revision 1.5  2006/06/19 06:47:27  mranga
196 * javadoc fixups
197 *
198 * Revision 1.4  2006/06/16 15:26:28  mranga
199 * Added NIST disclaimer to all public domain files. Clean up some javadoc. Fixed a leak
200 *
201 * Revision 1.3  2006/05/25 23:46:23  mranga
202 * Added @author NIST to all API files. Moved a package around in the tck directory.
203 *
204 * Ranga.
205 *
206 * Revision 1.2  2006/05/18 10:08:43  mranga
207 * Fixes null pointer in comparison when host is not specified. Add methods to allow longs and ints as args in TimeStamp Header.
208 *
209 * Ranga.
210 *
211 * Revision 1.1.1.1  2005/10/04 17:12:36  mranga
212 *
213 * Import
214 *
215 *
216 * Revision 1.4  2004/01/22 13:26:32  sverker
217 * Issue number:
218 * Obtained from:
219 * Submitted by:  sverker
220 * Reviewed by:   mranga
221 *
222 * Major reformat of code to conform with style guide. Resolved compiler and javadoc warnings. Added CVS tags.
223 *
224 * CVS: ----------------------------------------------------------------------
225 * CVS: Issue number:
226 * CVS:   If this change addresses one or more issues,
227 * CVS:   then enter the issue number(s) here.
228 * CVS: Obtained from:
229 * CVS:   If this change has been taken from another system,
230 * CVS:   then name the system in this line, otherwise delete it.
231 * CVS: Submitted by:
232 * CVS:   If this code has been contributed to the project by someone else; i.e.,
233 * CVS:   they sent us a patch or a set of diffs, then include their name/email
234 * CVS:   address here. If this is your work then delete this line.
235 * CVS: Reviewed by:
236 * CVS:   If we are doing pre-commit code reviews and someone else has
237 * CVS:   reviewed your changes, include their name(s) here.
238 * CVS:   If you have not had it reviewed then delete this line.
239 *
240 */
241