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.cocoon.environment.wrapper;
018:
019: import java.io.Serializable;
020: import java.util.ArrayList;
021: import java.util.Enumeration;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.Map;
025: import java.util.StringTokenizer;
026:
027: /**
028: * This class is used by the <code>RequestWrapper</code>. It parses
029: * a query string and creates a parameter representation required
030: * for the <code>Request</code> object.
031: *
032: * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
033: * @version CVS $Id: RequestParameters.java 433543 2006-08-22 06:22:54Z crossley $
034: */
035: public final class RequestParameters implements Serializable {
036:
037: /** The parameter names are the keys and the value is a List object */
038: private Map names;
039:
040: /**
041: * Decode the string
042: */
043: private String parseName(String s) {
044: StringBuffer sb = new StringBuffer();
045: for (int i = 0; i < s.length(); i++) {
046: char c = s.charAt(i);
047: switch (c) {
048: case '+':
049: sb.append(' ');
050: break;
051: case '%':
052: try {
053: sb.append((char) Integer.parseInt(s.substring(
054: i + 1, i + 3), 16));
055: i += 2;
056: } catch (NumberFormatException e) {
057: throw new IllegalArgumentException();
058: } catch (StringIndexOutOfBoundsException e) {
059: String rest = s.substring(i);
060: sb.append(rest);
061: if (rest.length() == 2)
062: i++;
063: }
064:
065: break;
066: default:
067: sb.append(c);
068: break;
069: }
070: }
071: return sb.toString();
072: }
073:
074: /**
075: * Construct a new object from a queryString
076: */
077: public RequestParameters(String queryString) {
078: this .names = new HashMap(5);
079: if (queryString != null) {
080: StringTokenizer st = new StringTokenizer(queryString, "&");
081: while (st.hasMoreTokens()) {
082: String pair = st.nextToken();
083: int pos = pair.indexOf('=');
084: if (pos != -1) {
085: this .setParameter(this .parseName(pair.substring(0,
086: pos)), this .parseName(pair.substring(
087: pos + 1, pair.length())));
088: }
089: }
090: }
091: }
092:
093: /**
094: * Add a parameter.
095: * The parameter is added with the given value.
096: * @param name The name of the parameter.
097: * @param value The value of the parameter.
098: */
099: private void setParameter(String name, String value) {
100: ArrayList list;
101: if (names.containsKey(name)) {
102: list = (ArrayList) names.get(name);
103: } else {
104: list = new ArrayList(3);
105: names.put(name, list);
106: }
107: list.add(value);
108: }
109:
110: /**
111: * Get the value of a parameter.
112: * @param name The name of the parameter.
113: * @return The value of the first parameter with the name
114: * or <CODE>null</CODE>
115: */
116: public String getParameter(String name) {
117: if (names.containsKey(name)) {
118: return (String) ((ArrayList) names.get(name)).get(0);
119: }
120: return null;
121: }
122:
123: /**
124: * Get the value of a parameter.
125: * @param name The name of the parameter.
126: * @param defaultValue The default value if the parameter does not exist.
127: * @return The value of the first parameter with the name
128: * or <CODE>defaultValue</CODE>
129: */
130: public String getParameter(String name, String defaultValue) {
131: if (names.containsKey(name)) {
132: return (String) ((ArrayList) names.get(name)).get(0);
133: }
134: return defaultValue;
135: }
136:
137: /**
138: * Get all values of a parameter.
139: * @param name The name of the parameter.
140: * @return Array of the (String) values or null if the parameter
141: * is not defined.
142: */
143: public String[] getParameterValues(String name) {
144: if (names.containsKey(name)) {
145: String values[] = null;
146: ArrayList list = (ArrayList) names.get(name);
147: Iterator iter = list.iterator();
148: while (iter.hasNext()) {
149: if (values == null) {
150: values = new String[1];
151: } else {
152: String[] copy = new String[values.length + 1];
153: System.arraycopy(values, 0, copy, 0, values.length);
154: values = copy;
155: }
156: values[values.length - 1] = (String) iter.next();
157: }
158: return values;
159: }
160: return null;
161: }
162:
163: /**
164: * Get all parameter names.
165: * @return Enumeration for the (String) parameter names.
166: */
167: public Enumeration getParameterNames() {
168: return new EnumerationFromIterator(names.keySet().iterator());
169: }
170:
171: final static class EnumerationFromIterator implements Enumeration {
172: private Iterator iter;
173:
174: EnumerationFromIterator(Iterator iter) {
175: this .iter = iter;
176: }
177:
178: public boolean hasMoreElements() {
179: return iter.hasNext();
180: }
181:
182: public Object nextElement() {
183: return iter.next();
184: }
185: }
186:
187: }
|