KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > environment > wrapper > RequestParameters


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.environment.wrapper;
17
18 import java.io.Serializable JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Enumeration JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25
26 /**
27  * This class is used by the <code>RequestWrapper</code>. It parses
28  * a query string and creates a parameter representation required
29  * for the <code>Request</code> object.
30  *
31  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
32  * @version CVS $Id: RequestParameters.java 124685 2005-01-08 22:20:56Z antonio $
33  */

34 public final class RequestParameters
35 implements Serializable JavaDoc {
36
37     /** The parameter names are the keys and the value is a List object */
38     private Map JavaDoc names;
39
40     /**
41      * Decode the string
42      */

43     private String JavaDoc parseName(String JavaDoc s) {
44         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
45         for (int i = 0; i < s.length(); i++) {
46             char c = s.charAt(i);
47             switch (c) {
48                 case '+':
49                     sb.append(' ');
50                     break;
51                 case '%':
52                     try {
53                         sb.append((char) Integer.parseInt(s.substring(i+1, i+3),
54                               16));
55                         i += 2;
56                     } catch (NumberFormatException JavaDoc e) {
57                         throw new IllegalArgumentException JavaDoc();
58                     } catch (StringIndexOutOfBoundsException JavaDoc e) {
59                         String JavaDoc rest = s.substring(i);
60                         sb.append(rest);
61                         if (rest.length()==2)
62                             i++;
63                     }
64
65                     break;
66                 default:
67                     sb.append(c);
68                     break;
69             }
70         }
71         return sb.toString();
72     }
73
74     /**
75      * Construct a new object from a queryString
76      */

77     public RequestParameters(String JavaDoc queryString) {
78         this.names = new HashMap JavaDoc(5);
79         if (queryString != null) {
80             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(queryString, "&");
81             while (st.hasMoreTokens()) {
82                 String JavaDoc pair = st.nextToken();
83                 int pos = pair.indexOf('=');
84                 if (pos != -1) {
85                     this.setParameter(this.parseName(pair.substring(0, pos)),
86                                       this.parseName(pair.substring(pos+1, pair.length())));
87                 }
88             }
89         }
90     }
91
92     /**
93      * Add a parameter.
94      * The parameter is added with the given value.
95      * @param name The name of the parameter.
96      * @param value The value of the parameter.
97      */

98     private void setParameter(String JavaDoc name, String JavaDoc value) {
99         ArrayList JavaDoc list;
100         if (names.containsKey(name)) {
101             list = (ArrayList JavaDoc)names.get(name);
102         } else {
103             list = new ArrayList JavaDoc(3);
104             names.put(name, list);
105         }
106         list.add(value);
107     }
108
109     /**
110      * Get the value of a parameter.
111      * @param name The name of the parameter.
112      * @return The value of the first parameter with the name
113      * or <CODE>null</CODE>
114      */

115     public String JavaDoc getParameter(String JavaDoc name) {
116         if (names.containsKey(name)) {
117             return (String JavaDoc)((ArrayList JavaDoc)names.get(name)).get(0);
118         }
119         return null;
120     }
121
122     /**
123      * Get the value of a parameter.
124      * @param name The name of the parameter.
125      * @param defaultValue The default value if the parameter does not exist.
126      * @return The value of the first parameter with the name
127      * or <CODE>defaultValue</CODE>
128      */

129     public String JavaDoc getParameter(String JavaDoc name, String JavaDoc defaultValue) {
130         if (names.containsKey(name)) {
131             return (String JavaDoc)((ArrayList JavaDoc)names.get(name)).get(0);
132         }
133         return defaultValue;
134     }
135
136     /**
137      * Get all values of a parameter.
138      * @param name The name of the parameter.
139      * @return Array of the (String) values or null if the parameter
140      * is not defined.
141      */

142     public String JavaDoc[] getParameterValues(String JavaDoc name) {
143         if (names.containsKey(name)) {
144             String JavaDoc values[] = null;
145             ArrayList JavaDoc list = (ArrayList JavaDoc)names.get(name);
146             Iterator JavaDoc iter = list.iterator();
147             while (iter.hasNext()) {
148                 if (values == null) {
149                     values = new String JavaDoc[1];
150                 } else {
151                     String JavaDoc[] copy = new String JavaDoc[values.length+1];
152                     System.arraycopy(values, 0, copy, 0, values.length);
153                     values = copy;
154                 }
155                 values[values.length-1] = (String JavaDoc)iter.next();
156             }
157             return values;
158         }
159         return null;
160     }
161
162     /**
163      * Get all parameter names.
164      * @return Enumeration for the (String) parameter names.
165      */

166     public Enumeration JavaDoc getParameterNames() {
167         return new EnumerationFromIterator(names.keySet().iterator());
168     }
169
170     final static class EnumerationFromIterator implements Enumeration JavaDoc {
171         private Iterator JavaDoc iter;
172         EnumerationFromIterator(Iterator JavaDoc iter) {
173             this.iter = iter;
174         }
175
176         public boolean hasMoreElements() {
177             return iter.hasNext();
178         }
179         public Object JavaDoc nextElement() { return iter.next(); }
180     }
181
182 }
183
Popular Tags