1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.fileupload;
18
19 import java.io.UnsupportedEncodingException;
20 import java.util.HashMap;
21 import java.util.Locale;
22 import java.util.Map;
23
24 import org.apache.commons.fileupload.util.mime.MimeUtility;
25
26
27
28
29
30
31
32
33
34
35
36
37 public class ParameterParser {
38
39
40
41
42 private char[] chars = null;
43
44
45
46
47 private int pos = 0;
48
49
50
51
52 private int len = 0;
53
54
55
56
57 private int i1 = 0;
58
59
60
61
62 private int i2 = 0;
63
64
65
66
67 private boolean lowerCaseNames = false;
68
69
70
71
72 public ParameterParser() {
73 super();
74 }
75
76
77
78
79
80
81
82 private boolean hasChar() {
83 return this.pos < this.len;
84 }
85
86
87
88
89
90
91
92
93
94
95 private String getToken(boolean quoted) {
96
97 while ((i1 < i2) && (Character.isWhitespace(chars[i1]))) {
98 i1++;
99 }
100
101 while ((i2 > i1) && (Character.isWhitespace(chars[i2 - 1]))) {
102 i2--;
103 }
104
105 if (quoted
106 && ((i2 - i1) >= 2)
107 && (chars[i1] == '"')
108 && (chars[i2 - 1] == '"')) {
109 i1++;
110 i2--;
111 }
112 String result = null;
113 if (i2 > i1) {
114 result = new String(chars, i1, i2 - i1);
115 }
116 return result;
117 }
118
119
120
121
122
123
124
125
126
127
128 private boolean isOneOf(char ch, final char[] charray) {
129 boolean result = false;
130 for (char element : charray) {
131 if (ch == element) {
132 result = true;
133 break;
134 }
135 }
136 return result;
137 }
138
139
140
141
142
143
144
145
146
147
148 private String parseToken(final char[] terminators) {
149 char ch;
150 i1 = pos;
151 i2 = pos;
152 while (hasChar()) {
153 ch = chars[pos];
154 if (isOneOf(ch, terminators)) {
155 break;
156 }
157 i2++;
158 pos++;
159 }
160 return getToken(false);
161 }
162
163
164
165
166
167
168
169
170
171
172
173 private String parseQuotedToken(final char[] terminators) {
174 char ch;
175 i1 = pos;
176 i2 = pos;
177 boolean quoted = false;
178 boolean charEscaped = false;
179 while (hasChar()) {
180 ch = chars[pos];
181 if (!quoted && isOneOf(ch, terminators)) {
182 break;
183 }
184 if (!charEscaped && ch == '"') {
185 quoted = !quoted;
186 }
187 charEscaped = (!charEscaped && ch == '\\');
188 i2++;
189 pos++;
190
191 }
192 return getToken(true);
193 }
194
195
196
197
198
199
200
201
202
203 public boolean isLowerCaseNames() {
204 return this.lowerCaseNames;
205 }
206
207
208
209
210
211
212
213
214
215 public void setLowerCaseNames(boolean b) {
216 this.lowerCaseNames = b;
217 }
218
219
220
221
222
223
224
225
226
227
228
229 public Map<String, String> parse(final String str, char[] separators) {
230 if (separators == null || separators.length == 0) {
231 return new HashMap<String, String>();
232 }
233 char separator = separators[0];
234 if (str != null) {
235 int idx = str.length();
236 for (char separator2 : separators) {
237 int tmp = str.indexOf(separator2);
238 if (tmp != -1 && tmp < idx) {
239 idx = tmp;
240 separator = separator2;
241 }
242 }
243 }
244 return parse(str, separator);
245 }
246
247
248
249
250
251
252
253
254
255
256 public Map<String, String> parse(final String str, char separator) {
257 if (str == null) {
258 return new HashMap<String, String>();
259 }
260 return parse(str.toCharArray(), separator);
261 }
262
263
264
265
266
267
268
269
270
271
272
273 public Map<String, String> parse(final char[] charArray, char separator) {
274 if (charArray == null) {
275 return new HashMap<String, String>();
276 }
277 return parse(charArray, 0, charArray.length, separator);
278 }
279
280
281
282
283
284
285
286
287
288
289
290
291
292 public Map<String, String> parse(
293 final char[] charArray,
294 int offset,
295 int length,
296 char separator) {
297
298 if (charArray == null) {
299 return new HashMap<String, String>();
300 }
301 HashMap<String, String> params = new HashMap<String, String>();
302 this.chars = charArray;
303 this.pos = offset;
304 this.len = length;
305
306 String paramName = null;
307 String paramValue = null;
308 while (hasChar()) {
309 paramName = parseToken(new char[] {
310 '=', separator });
311 paramValue = null;
312 if (hasChar() && (charArray[pos] == '=')) {
313 pos++;
314 paramValue = parseQuotedToken(new char[] {
315 separator });
316
317 if (paramValue != null) {
318 try {
319 paramValue = MimeUtility.decodeText(paramValue);
320 } catch (UnsupportedEncodingException e) {
321
322 }
323 }
324 }
325 if (hasChar() && (charArray[pos] == separator)) {
326 pos++;
327 }
328 if ((paramName != null) && (paramName.length() > 0)) {
329 if (this.lowerCaseNames) {
330 paramName = paramName.toLowerCase(Locale.ENGLISH);
331 }
332
333 params.put(paramName, paramValue);
334 }
335 }
336 return params;
337 }
338
339 }