KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > http > ParameterParser


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.http;
21
22 import java.io.IOException JavaDoc;
23 import java.text.MessageFormat JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import java.util.Vector JavaDoc;
26
27 /**
28  *
29  * @author Lee David Painter <a HREF="mailto:lee@3sp.com">&lt;lee@3sp.com&gt;</a>
30  */

31 class ParameterParser {
32
33     /** String to be parsed */
34     private char[] chars = null;
35
36     /** Current position in the string */
37     private int pos = 0;
38
39     /** Maximum position in the string */
40     private int len = 0;
41
42     /** Start of a token */
43     private int i1 = 0;
44
45     /** End of a token */
46     private int i2 = 0;
47
48     /** Default ParameterParser constructor */
49     public ParameterParser() {
50         super();
51     }
52
53     /** Are there any characters left to parse? */
54     private boolean hasChar() {
55         return this.pos < this.len;
56     }
57
58     /** A helper method to process the parsed token. */
59     private String JavaDoc getToken(boolean quoted) {
60         // Trim leading white spaces
61
while ((i1 < i2) && (Character.isWhitespace(chars[i1]))) {
62             i1++;
63         }
64         // Trim trailing white spaces
65
while ((i2 > i1) && (Character.isWhitespace(chars[i2 - 1]))) {
66             i2--;
67         }
68         // Strip away quotes if necessary
69
if (quoted) {
70             if (((i2 - i1) >= 2) && (chars[i1] == '"') && (chars[i2 - 1] == '"')) {
71                 i1++;
72                 i2--;
73             }
74         }
75         String JavaDoc result = null;
76         if (i2 > i1) {
77             result = new String JavaDoc(chars, i1, i2 - i1);
78         }
79         return result;
80     }
81
82     /** Is given character present in the array of characters? */
83     private boolean isOneOf(char ch, char[] charray) {
84         boolean result = false;
85         for (int i = 0; i < charray.length; i++) {
86             if (ch == charray[i]) {
87                 result = true;
88                 break;
89             }
90         }
91         return result;
92     }
93
94     /**
95      * Parse out a token until any of the given terminators is encountered.
96      */

97     private String JavaDoc parseToken(final char[] terminators) {
98         char ch;
99         i1 = pos;
100         i2 = pos;
101         while (hasChar()) {
102             ch = chars[pos];
103             if (isOneOf(ch, terminators)) {
104                 break;
105             }
106             i2++;
107             pos++;
108         }
109         return getToken(false);
110     }
111
112     /**
113      * Parse out a token until any of the given terminators is encountered.
114      * Special characters in quoted tokens are escaped.
115      */

116     private String JavaDoc parseQuotedToken(final char[] terminators) {
117         char ch;
118         i1 = pos;
119         i2 = pos;
120         boolean quoted = false;
121         while (hasChar()) {
122             ch = chars[pos];
123             if (!quoted && isOneOf(ch, terminators)) {
124                 break;
125             }
126             if (ch == '"') {
127                 quoted = !quoted;
128             }
129             i2++;
130             pos++;
131         }
132         return getToken(true);
133     }
134
135     /**
136      * Extracts a list of {@link NameValuePair}s from the given string.
137      *
138      * @param str the string that contains a sequence of name/value pairs
139      * @return a list of {@link NameValuePair}s
140      *
141      */

142     public Vector JavaDoc parse(final String JavaDoc str, char separator) {
143
144         if (str == null) {
145             return new Vector JavaDoc();
146         }
147         return parse(str.toCharArray(), separator);
148     }
149
150     /**
151      * Extracts a list of {@link NameValuePair}s from the given array of
152      * characters.
153      *
154      * @param chars the array of characters that contains a sequence of
155      * name/value pairs
156      *
157      * @return a list of {@link NameValuePair}s
158      */

159     public Vector JavaDoc parse(final char[] chars, char separator) {
160
161         if (chars == null) {
162             return new Vector JavaDoc();
163         }
164         return parse(chars, 0, chars.length, separator);
165     }
166
167     /**
168      * Extracts a list of {@link NameValuePair}s from the given array of
169      * characters.
170      *
171      * @param chars the array of characters that contains a sequence of
172      * name/value pairs
173      * @param offset - the initial offset.
174      * @param length - the length.
175      *
176      * @return a list of {@link NameValuePair}s
177      */

178     public Vector JavaDoc parse(final char[] chars, int offset, int length, char separator) {
179
180         if (chars == null) {
181             return new Vector JavaDoc();
182         }
183         Vector JavaDoc params = new Vector JavaDoc();
184         this.chars = chars;
185         this.pos = offset;
186         this.len = length;
187
188         String JavaDoc paramName = null;
189         String JavaDoc paramValue = null;
190         while (hasChar()) {
191             paramName = parseToken(new char[] { '=', separator });
192             paramValue = null;
193             if (hasChar() && (chars[pos] == '=')) {
194                 pos++; // skip '='
195
paramValue = parseQuotedToken(new char[] { separator });
196             }
197             if (hasChar() && (chars[pos] == separator)) {
198                 pos++; // skip separator
199
}
200             if ((paramName != null) && (paramName.length() > 0)) {
201                 params.addElement(new NameValuePair(paramName, paramValue));
202             }
203         }
204         return params;
205     }
206
207     public static Hashtable JavaDoc extractParams(String JavaDoc challengeStr) throws IOException JavaDoc {
208         if (challengeStr == null) {
209             throw new IllegalArgumentException JavaDoc(Messages.getString("ParameterParser.challendMayNotBeNull")); //$NON-NLS-1$
210
}
211         int idx = challengeStr.indexOf(' ');
212         if (idx == -1) {
213             throw new IOException JavaDoc(MessageFormat.format(Messages.getString("ParameterParser.invalidChallenge"), new Object JavaDoc[] { challengeStr })); //$NON-NLS-1$
214
}
215         Hashtable JavaDoc map = new Hashtable JavaDoc();
216         ParameterParser parser = new ParameterParser();
217         Vector JavaDoc params = parser.parse(challengeStr.substring(idx + 1, challengeStr.length()), ',');
218         for (int i = 0; i < params.size(); i++) {
219             NameValuePair param = (NameValuePair) params.elementAt(i);
220             map.put(param.getName().toLowerCase(), param.getValue());
221         }
222         return map;
223     }
224 }
225
Popular Tags