Source code: com/tripi/asp/util/ParseQueryString.java
1 /**
2 * ArrowHead ASP Server
3 * This is a source file for the ArrowHead ASP Server - an 100% Java
4 * VBScript interpreter and ASP server.
5 *
6 * For more information, see http://www.tripi.com/arrowhead
7 *
8 * Copyright (C) 2002 Terence Haddock
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 */
25 package com.tripi.asp.util;
26
27 import java.io.IOException;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.Map;
31
32 import jregex.Matcher;
33 import jregex.Pattern;
34 import jregex.RETokenizer;
35
36 import org.apache.log4j.Category;
37
38 /**
39 * This class implements parsing for query string variables.
40 *
41 * @author Terence Haddock
42 * @version 0.9
43 */
44 public class ParseQueryString
45 {
46 /** Debugging category */
47 private static final Category DBG = Category.getInstance(ParseQueryString.class);
48
49 /** Pattern used to tokenize the list */
50 protected static final Pattern tokenizerPattern = new Pattern("&");
51
52 /** Pattern used to find arguments/variables */
53 protected static final Pattern variablesPattern = new Pattern("^([^=]*)(=(.*))?$");
54
55 /**
56 * Function to perform a parse on a query string. This function returns a map
57 * containing String keys which are the variable names, and Set values
58 * containing the values for the string.
59 * @param queryString string to parse
60 * @return map containing the results.
61 */
62 public static Map parse(String queryString)
63 {
64 return parse(new RETokenizer(tokenizerPattern, queryString));
65 }
66
67 /**
68 * Function to perform a parse on a query string. This function returns a map
69 * containing String keys which are the variable names, and Set values
70 * containing the values for the string.
71 * @param reader reader
72 * @param length length of input
73 * @return map containing the results
74 * @throws IOException on I/O error.
75 */
76 public static Map parse(java.io.Reader reader, int length) throws IOException
77 {
78 return parse(new RETokenizer(tokenizerPattern, reader, length));
79 }
80
81 /**
82 * Internal function to perform the parsing.
83 * @param tok tokenizer
84 * @return parse results.
85 */
86 private static Map parse(RETokenizer tok)
87 {
88 Map tmpMap = new java.util.HashMap();
89
90 while (tok.hasMore())
91 {
92 String value = tok.nextToken();
93 if (DBG.isDebugEnabled()) DBG.debug("Token: " + value);
94 Matcher m = variablesPattern.matcher(value);
95 if (m.matches())
96 {
97 String arg = Tools.urlDecode(m.group(1));
98 String val = m.group(3);
99 if (val == null) val = "";
100 val = Tools.urlDecode(val);
101 if (DBG.isDebugEnabled()) DBG.debug("Arg: " + arg + "/ val: " + val);
102 List list = (List)tmpMap.get(arg);
103 if (list == null)
104 {
105 list = new java.util.ArrayList();
106 tmpMap.put(arg, list);
107 }
108 list.add(val);
109 }
110 }
111 Map retMap = new java.util.HashMap();
112 for (Iterator i = tmpMap.keySet().iterator(); i.hasNext();)
113 {
114 String key = (String)i.next();
115 List list = (List)tmpMap.get(key);
116 String strArray[] = new String[list.size()];
117 int x = 0;
118 for (Iterator j = list.iterator(); j.hasNext();)
119 {
120 strArray[x++] = (String)j.next();
121 }
122 retMap.put(key, strArray);
123 }
124 return retMap;
125 }
126 }
127