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 gov.nist.core.*;
30import java.text.ParseException;
31import javax.sip.*;
32
33/**
34 * Parser for Warning header.
35 *
36 * @version 1.2
37 *
38 * @author Olivier Deruelle
39 * @author M. Ranganathan
40 *
41 *
42 *
43 * @version 1.0
44 */
45public class WarningParser extends HeaderParser {
46
47    /**
48     * Constructor
49     *
50     * @param warning -
51     *            Warning header to parse
52     */
53    public WarningParser(String warning) {
54        super(warning);
55    }
56
57    /**
58     * Cosntructor
59     *
60     * @param lexer -
61     *            the lexer to use.
62     */
63    protected WarningParser(Lexer lexer) {
64        super(lexer);
65    }
66
67    /**
68     * parse the String message
69     *
70     * @return SIPHeader (WarningList object)
71     * @throws SIPParseException
72     *             if the message does not respect the spec.
73     */
74    public SIPHeader parse() throws ParseException {
75        WarningList warningList = new WarningList();
76        if (debug)
77            dbg_enter("WarningParser.parse");
78
79        try {
80            headerName(TokenTypes.WARNING);
81
82            while (lexer.lookAhead(0) != '\n') {
83                Warning warning = new Warning();
84                warning.setHeaderName(SIPHeaderNames.WARNING);
85
86                // Parsing the 3digits code
87                this.lexer.match(TokenTypes.ID);
88                Token token = lexer.getNextToken();
89                try {
90                    int code = Integer.parseInt(token.getTokenValue());
91                    warning.setCode(code);
92                } catch (NumberFormatException ex) {
93                    throw createParseException(ex.getMessage());
94                } catch (InvalidArgumentException ex) {
95                    throw createParseException(ex.getMessage());
96                }
97                this.lexer.SPorHT();
98
99                // Parsing the agent
100                this.lexer.match(TokenTypes.ID);
101                token = lexer.getNextToken();
102                // Bug reported by zvali@dev.java.net
103                if (lexer.lookAhead(0) == ':') {
104                    this.lexer.match(':');
105                    this.lexer.match(TokenTypes.ID);
106                    Token token2 = lexer.getNextToken();
107                    warning.setAgent(token.getTokenValue() + ":"
108                            + token2.getTokenValue());
109                } else {
110                    warning.setAgent(token.getTokenValue());
111                }
112
113                this.lexer.SPorHT();
114
115                // Parsing the text
116                String text = this.lexer.quotedString();
117                warning.setText(text);
118                this.lexer.SPorHT();
119
120                warningList.add(warning);
121
122                while (lexer.lookAhead(0) == ',') {
123                    this.lexer.match(',');
124                    this.lexer.SPorHT();
125
126                    warning = new Warning();
127
128                    // Parsing the 3digits code
129                    this.lexer.match(TokenTypes.ID);
130                    Token tok = lexer.getNextToken();
131                    try {
132                        int code = Integer.parseInt(tok.getTokenValue());
133                        warning.setCode(code);
134                    } catch (NumberFormatException ex) {
135                        throw createParseException(ex.getMessage());
136                    } catch (InvalidArgumentException ex) {
137                        throw createParseException(ex.getMessage());
138                    }
139                    this.lexer.SPorHT();
140
141                    // Parsing the agent
142                    this.lexer.match(TokenTypes.ID);
143                    tok = lexer.getNextToken();
144
145                    // Bug reported by zvali@dev.java.net
146
147                    if (lexer.lookAhead(0) == ':') {
148                        this.lexer.match(':');
149                        this.lexer.match(TokenTypes.ID);
150                        Token token2 = lexer.getNextToken();
151                        warning.setAgent(tok.getTokenValue() + ":"
152                                + token2.getTokenValue());
153                    } else {
154                        warning.setAgent(tok.getTokenValue());
155                    }
156
157                    this.lexer.SPorHT();
158
159                    // Parsing the text
160                    text = this.lexer.quotedString();
161                    warning.setText(text);
162                    this.lexer.SPorHT();
163
164                    warningList.add(warning);
165                }
166
167            }
168        } finally {
169            if (debug)
170                dbg_leave("WarningParser.parse");
171        }
172
173        return warningList;
174    }
175
176    /**
177     * public static void main(String args[]) throws ParseException { String
178     * warning[] = { "Warning: 307 isi.edu \"Session parameter 'foo' not
179     * understood\"\n", "Warning: 301 isi.edu \"Incompatible network address
180     * type 'E.164'\"\n", "Warning: 312 ii.edu \"Soda\", "+ " 351 i.edu \"I
181     * network address 'E.164'\" , 323 ii.edu \"Sodwea\"\n", "Warning: 392
182     * 192.168.89.71:5060 \"Noisy feedback tells: pid=936
183     * req_src_ip=192.168.89.20 in_uri=sip:xxx@yyyy.org:5061
184     * out_uri=sip:xxx@yyyy.org:5061 via_cnt==1\"\n" };
185     *
186     * for (int i = 0; i < warning.length; i++ ) { WarningParser parser = new
187     * WarningParser(warning[i]); WarningList warningList= (WarningList)
188     * parser.parse(); System.out.println("encoded = " + warningList.encode()); }
189     *  }
190     */
191
192}
193