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