PVisitedNetworkIDParser.java revision 600c7a4bbc7348167293eac928192e695b4ad5ba
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*/
26/*******************************************
27 * PRODUCT OF PT INOVACAO - EST DEPARTMENT *
28 *******************************************/
29
30package gov.nist.javax.sip.parser.ims;
31
32import gov.nist.core.Token;
33import gov.nist.javax.sip.header.SIPHeader;
34import gov.nist.javax.sip.header.ims.PVisitedNetworkID;
35import gov.nist.javax.sip.header.ims.PVisitedNetworkIDList;
36import gov.nist.javax.sip.parser.Lexer;
37import gov.nist.javax.sip.parser.ParametersParser;
38import gov.nist.javax.sip.parser.TokenTypes;
39
40import java.text.ParseException;
41
42/**
43 * P-Visited-Network-ID header parser.
44 *
45 * <pre>
46 * P-Visited-Network-ID   = "P-Visited-Network-ID" HCOLON
47 *                          vnetwork-spec
48 *                          *(COMMA vnetwork-spec)
49 * vnetwork-spec          = (token / quoted-string)
50 *                          *(SEMI vnetwork-param)
51 * vnetwork-param         = generic-param
52 * </pre>
53 *
54 * @author ALEXANDRE MIGUEL SILVA SANTOS
55 */
56
57/*
58
59 */
60
61public class PVisitedNetworkIDParser extends ParametersParser implements TokenTypes {
62
63    /**
64     * Constructor
65     */
66    public PVisitedNetworkIDParser(String networkID) {
67        super(networkID);
68
69    }
70
71    protected PVisitedNetworkIDParser(Lexer lexer) {
72        super(lexer);
73
74    }
75
76
77
78
79    public SIPHeader parse() throws ParseException {
80
81        PVisitedNetworkIDList visitedNetworkIDList = new PVisitedNetworkIDList();
82
83        if (debug)
84            dbg_enter("VisitedNetworkIDParser.parse");
85
86        try {
87            this.lexer.match(TokenTypes.P_VISITED_NETWORK_ID);
88            this.lexer.SPorHT();
89            this.lexer.match(':');
90            this.lexer.SPorHT();
91
92            while (true) {
93
94                PVisitedNetworkID visitedNetworkID = new PVisitedNetworkID();
95
96                if (this.lexer.lookAhead(0) == '\"')
97                    parseQuotedString(visitedNetworkID);
98                else
99                    parseToken(visitedNetworkID);
100
101                visitedNetworkIDList.add(visitedNetworkID);
102
103                this.lexer.SPorHT();
104                char la = lexer.lookAhead(0);
105                if (la == ',') {
106                    this.lexer.match(',');
107                    this.lexer.SPorHT();
108                } else if (la == '\n')
109                    break;
110                else
111                    throw createParseException("unexpected char = " + la);
112            }
113            return visitedNetworkIDList;
114        } finally {
115            if (debug)
116                dbg_leave("VisitedNetworkIDParser.parse");
117        }
118
119    }
120
121    protected void parseQuotedString(PVisitedNetworkID visitedNetworkID) throws ParseException {
122
123        if (debug)
124            dbg_enter("parseQuotedString");
125
126        try {
127
128            StringBuffer retval = new StringBuffer();
129
130            if (this.lexer.lookAhead(0) != '\"')
131                throw createParseException("unexpected char");
132            this.lexer.consume(1);
133
134            while (true) {
135                char next = this.lexer.getNextChar();
136                if (next == '\"') {
137                    // Got to the terminating quote.
138                    break;
139                } else if (next == '\0') {
140                    throw new ParseException("unexpected EOL", 1);
141                } else if (next == '\\') {
142                    retval.append(next);
143                    next = this.lexer.getNextChar();
144                    retval.append(next);
145                } else {
146                    retval.append(next);
147                }
148            }
149
150            visitedNetworkID.setVisitedNetworkID(retval.toString());
151            super.parse(visitedNetworkID);
152
153
154
155        }finally {
156            if (debug)
157                dbg_leave("parseQuotedString.parse");
158        }
159
160    }
161
162    protected void parseToken(PVisitedNetworkID visitedNetworkID) throws ParseException
163    {
164        // issued by Miguel Freitas
165
166        lexer.match(TokenTypes.ID);
167        Token token = lexer.getNextToken();
168        //String value = token.getTokenValue();
169        visitedNetworkID.setVisitedNetworkID(token);
170        super.parse(visitedNetworkID);
171
172    }
173
174
175}
176