001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.util.upload;
018:
019: import java.util.HashMap;
020: import java.util.Map;
021:
022: /**
023: * A simple parser intended to parse sequences of name/value pairs. Parameter
024: * values are exptected to be enclosed in quotes if they contain unsafe
025: * characters, such as '=' characters or separators. Parameter values are
026: * optional and can be omitted.
027: *
028: * <p>
029: * <code>param1 = value; param2 = "anything goes; really"; param3</code>
030: * </p>
031: *
032: * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
033: */
034:
035: public class ParameterParser {
036: /**
037: * String to be parsed.
038: */
039: private char[] chars = null;
040:
041: /**
042: * Current position in the string.
043: */
044: private int pos = 0;
045:
046: /**
047: * Maximum position in the string.
048: */
049: private int len = 0;
050:
051: /**
052: * Start of a token.
053: */
054: private int i1 = 0;
055:
056: /**
057: * End of a token.
058: */
059: private int i2 = 0;
060:
061: /**
062: * Whether names stored in the map should be converted to lower case.
063: */
064: private boolean lowerCaseNames = false;
065:
066: /**
067: * Default ParameterParser constructor.
068: */
069: public ParameterParser() {
070: super ();
071: }
072:
073: /**
074: * Are there any characters left to parse?
075: *
076: * @return <tt>true</tt> if there are unparsed characters, <tt>false</tt>
077: * otherwise.
078: */
079: private boolean hasChar() {
080: return this .pos < this .len;
081: }
082:
083: /**
084: * A helper method to process the parsed token. This method removes leading
085: * and trailing blanks as well as enclosing quotation marks, when necessary.
086: *
087: * @param quoted
088: * <tt>true</tt> if quotation marks are expected,
089: * <tt>false</tt> otherwise.
090: * @return the token
091: */
092: private String getToken(boolean quoted) {
093: // Trim leading white spaces
094: while ((i1 < i2) && (Character.isWhitespace(chars[i1]))) {
095: i1++;
096: }
097: // Trim trailing white spaces
098: while ((i2 > i1) && (Character.isWhitespace(chars[i2 - 1]))) {
099: i2--;
100: }
101: // Strip away quotation marks if necessary
102: if (quoted) {
103: if (((i2 - i1) >= 2) && (chars[i1] == '"')
104: && (chars[i2 - 1] == '"')) {
105: i1++;
106: i2--;
107: }
108: }
109: String result = null;
110: if (i2 > i1) {
111: result = new String(chars, i1, i2 - i1);
112: }
113: return result;
114: }
115:
116: /**
117: * Tests if the given character is present in the array of characters.
118: *
119: * @param ch
120: * the character to test for presense in the array of characters
121: * @param charray
122: * the array of characters to test against
123: *
124: * @return <tt>true</tt> if the character is present in the array of
125: * characters, <tt>false</tt> otherwise.
126: */
127: private boolean isOneOf(char ch, final char[] charray) {
128: boolean result = false;
129: for (int i = 0; i < charray.length; i++) {
130: if (ch == charray[i]) {
131: result = true;
132: break;
133: }
134: }
135: return result;
136: }
137:
138: /**
139: * Parses out a token until any of the given terminators is encountered.
140: *
141: * @param terminators
142: * the array of terminating characters. Any of these characters
143: * when encountered signify the end of the token
144: *
145: * @return the token
146: */
147: private String parseToken(final char[] terminators) {
148: char ch;
149: i1 = pos;
150: i2 = pos;
151: while (hasChar()) {
152: ch = chars[pos];
153: if (isOneOf(ch, terminators)) {
154: break;
155: }
156: i2++;
157: pos++;
158: }
159: return getToken(false);
160: }
161:
162: /**
163: * Parses out a token until any of the given terminators is encountered
164: * outside the quotation marks.
165: *
166: * @param terminators
167: * the array of terminating characters. Any of these characters
168: * when encountered outside the quotation marks signify the end
169: * of the token
170: *
171: * @return the token
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: * Returns <tt>true</tt> if parameter names are to be converted to lower
197: * case when name/value pairs are parsed.
198: *
199: * @return <tt>true</tt> if parameter names are to be converted to lower
200: * case when name/value pairs are parsed. Otherwise returns
201: * <tt>false</tt>
202: */
203: public boolean isLowerCaseNames() {
204: return this .lowerCaseNames;
205: }
206:
207: /**
208: * Sets the flag if parameter names are to be converted to lower case when
209: * name/value pairs are parsed.
210: *
211: * @param b
212: * <tt>true</tt> if parameter names are to be converted to
213: * lower case when name/value pairs are parsed. <tt>false</tt>
214: * otherwise.
215: */
216: public void setLowerCaseNames(boolean b) {
217: this .lowerCaseNames = b;
218: }
219:
220: /**
221: * Extracts a map of name/value pairs from the given string. Names are
222: * expected to be unique.
223: *
224: * @param str
225: * the string that contains a sequence of name/value pairs
226: * @param separator
227: * the name/value pairs separator
228: *
229: * @return a map of name/value pairs
230: */
231: public Map parse(final String str, char separator) {
232: if (str == null) {
233: return new HashMap();
234: }
235: return parse(str.toCharArray(), separator);
236: }
237:
238: /**
239: * Extracts a map of name/value pairs from the given array of characters.
240: * Names are expected to be unique.
241: *
242: * @param chars
243: * the array of characters that contains a sequence of name/value
244: * pairs
245: * @param separator
246: * the name/value pairs separator
247: *
248: * @return a map of name/value pairs
249: */
250: public Map parse(final char[] chars, char separator) {
251: if (chars == null) {
252: return new HashMap();
253: }
254: return parse(chars, 0, chars.length, separator);
255: }
256:
257: /**
258: * Extracts a map of name/value pairs from the given array of characters.
259: * Names are expected to be unique.
260: *
261: * @param chars
262: * the array of characters that contains a sequence of name/value
263: * pairs
264: * @param offset -
265: * the initial offset.
266: * @param length -
267: * the length.
268: * @param separator
269: * the name/value pairs separator
270: *
271: * @return a map of name/value pairs
272: */
273: public Map parse(final char[] chars, int offset, int length,
274: char separator) {
275:
276: if (chars == null) {
277: return new HashMap();
278: }
279: HashMap params = new HashMap();
280: this .chars = chars;
281: this .pos = offset;
282: this .len = length;
283:
284: String paramName = null;
285: String paramValue = null;
286: while (hasChar()) {
287: paramName = parseToken(new char[] { '=', separator });
288: paramValue = null;
289: if (hasChar() && (chars[pos] == '=')) {
290: pos++; // skip '='
291: paramValue = parseQuotedToken(new char[] { separator });
292: }
293: if (hasChar() && (chars[pos] == separator)) {
294: pos++; // skip separator
295: }
296: if ((paramName != null) && (paramName.length() > 0)) {
297: if (this.lowerCaseNames) {
298: paramName = paramName.toLowerCase();
299: }
300: params.put(paramName, paramValue);
301: }
302: }
303: return params;
304: }
305: }
|