KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > source > SourceParameters


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.source;
9
10 import org.apache.avalon.framework.parameters.Parameters;
11 import java.io.*;
12 import java.util.*;
13
14 /**
15  * This class holds parameters for a <code>Source</code> object.
16  * It differs from the usual Parameters object because in can hold
17  * more than one value for a parameter.
18  *
19  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
20  * @version $Id: SourceParameters.java,v 1.4 2001/12/11 09:53:36 jefft Exp $
21  */

22 public final class SourceParameters
23 implements Serializable {
24
25     /** The parameter names are the keys and the value is a List object */
26     private Map names = new HashMap(5);
27
28     /**
29      * Decode the string
30      */

31     private String JavaDoc parseName(String JavaDoc s) {
32         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
33         for (int i = 0; i < s.length(); i++) {
34             char c = s.charAt(i);
35             switch (c) {
36                 case '+':
37                     sb.append(' ');
38                     break;
39                 case '%':
40                     try {
41                         sb.append((char) Integer.parseInt(s.substring(i+1, i+3),
42                               16));
43                         i += 2;
44                     } catch (NumberFormatException JavaDoc e) {
45                         throw new IllegalArgumentException JavaDoc();
46                     } catch (StringIndexOutOfBoundsException JavaDoc e) {
47                         String JavaDoc rest = s.substring(i);
48                         sb.append(rest);
49                         if (rest.length()==2)
50                             i++;
51                     }
52
53                     break;
54                 default:
55                     sb.append(c);
56                     break;
57             }
58         }
59         return sb.toString();
60     }
61
62     /**
63      * Standard Constructor
64      */

65     public SourceParameters() {
66     }
67
68     /**
69      * Construct a new object from a queryString
70      */

71     public SourceParameters(String JavaDoc queryString) {
72         if (queryString != null) {
73             StringTokenizer st = new StringTokenizer(queryString, "&");
74             while (st.hasMoreTokens()) {
75                 String JavaDoc pair = (String JavaDoc)st.nextToken();
76                 int pos = pair.indexOf('=');
77                 if (pos != -1) {
78                     this.setParameter(this.parseName(pair.substring(0, pos)),
79                                       this.parseName(pair.substring(pos+1, pair.length())));
80                 }
81             }
82         }
83     }
84
85     /**
86      * Add a parameter.
87      * The parameter is added with the given value.
88      * @param name The name of the parameter.
89      * @param value The value of the parameter.
90      */

91     private void setParameter(String JavaDoc name, String JavaDoc value) {
92         ArrayList list;
93         if (names.containsKey(name) == true) {
94             list = (ArrayList)names.get(name);
95         } else {
96             list = new ArrayList(3);
97             names.put(name, list);
98         }
99         list.add(value);
100     }
101
102     /**
103      * Get the value of a parameter.
104      * @param name The name of the parameter.
105      * @return The value of the first parameter with the name
106      * or <CODE>null</CODE>
107      */

108     public String JavaDoc getParameter(String JavaDoc name) {
109         if (names.containsKey(name) == true) {
110             return (String JavaDoc)((ArrayList)names.get(name)).get(0);
111         }
112         return null;
113     }
114
115     /**
116      * Get the value of a parameter.
117      * @param name The name of the parameter.
118      * @param defaultValue The default value if the parameter does not exist.
119      * @return The value of the first parameter with the name
120      * or <CODE>defaultValue</CODE>
121      */

122     public String JavaDoc getParameter(String JavaDoc name, String JavaDoc defaultValue) {
123         if (names.containsKey(name) == true) {
124             return (String JavaDoc)((ArrayList)names.get(name)).get(0);
125         }
126         return defaultValue;
127     }
128
129     /**
130      * Get the integer value of a parameter.
131      * @param name The name of the parameter.
132      * @param defaultValue The default value if the parameter does not exist.
133      * @return The value of the first parameter with the name
134      * or <CODE>defaultValue</CODE>
135      */

136     public int getParameterAsInteger(String JavaDoc name, int defaultValue) {
137         if (names.containsKey(name) == true) {
138             return new Integer JavaDoc((String JavaDoc)((ArrayList)names.get(name)).get(0)).intValue();
139         }
140         return defaultValue;
141     }
142
143     /**
144      * Get the boolean value of a parameter.
145      * @param name The name of the parameter.
146      * @param defaultValue The default value if the parameter does not exist.
147      * @return The value of the first parameter with the name
148      * or <CODE>defaultValue</CODE>
149      */

150     public boolean getParameterAsBoolean(String JavaDoc name, boolean defaultValue) {
151         if (names.containsKey(name) == true) {
152             return new Boolean JavaDoc((String JavaDoc)((ArrayList)names.get(name)).get(0)).booleanValue();
153         }
154         return defaultValue;
155     }
156
157     /**
158      * Test if a value for this parameter exists.
159      * @param name The name of the parameter.
160      * @return <CODE>true</CODE> if a value exists, otherwise <CODE>false</CODE>
161      */

162     public boolean containsParameter(String JavaDoc name) {
163         return names.containsKey(name);
164     }
165
166     /**
167      * Get all values of a parameter.
168      * @param name The name of the parameter.
169      * @return Iterator for the (String) values or null if the parameter
170      * is not defined.
171      */

172     public Iterator getParameterValues(String JavaDoc name) {
173         if (names.containsKey(name) == true) {
174             ArrayList list = (ArrayList)names.get(name);
175             return list.iterator();
176         }
177         return null;
178     }
179     /**
180      * Get all parameter names.
181      * @return Iterator for the (String) parameter names.
182      */

183     public Iterator getParameterNames() {
184         return names.keySet().iterator();
185     }
186
187     /**
188      * Create a Parameters object.
189      * The first value of each parameter is added to the Parameters object.
190      * @return An Parameters object - if no parameters are defined this is an
191      * empty object.
192      */

193     public Parameters getFirstParameters() {
194         Parameters result = new Parameters();
195         Iterator iter = this.getParameterNames();
196         String JavaDoc parName;
197         while (iter.hasNext()) {
198             parName = (String JavaDoc)iter.next();
199             result.setParameter(parName, this.getParameter(parName));
200         }
201         return result;
202     }
203
204     /**
205      * Build a query string.
206      * The query string can e.g. be used for http connections.
207      * @return A query string which contains for each parameter/value pair
208      * a part, like "parameter=value" separated by "&".
209      * If no parameter is defined <CODE>null</CODE> is returned.
210      */

211     public String JavaDoc getQueryString() {
212         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
213         Iterator iter = this.names.keySet().iterator();
214         Iterator listIterator;
215         String JavaDoc key;
216         String JavaDoc value;
217         boolean first = true;
218         while (iter.hasNext() == true) {
219             key = (String JavaDoc)iter.next();
220             listIterator = ((ArrayList)names.get(key)).iterator();
221             while (listIterator.hasNext() == true) {
222                 if (first == false) result.append('&');
223                 value = (String JavaDoc)listIterator.next();
224                 result.append(key).append('=').append(value);
225                 first = false;
226             }
227         }
228         return (result.length() == 0 ? null : result.toString());
229     }
230
231     /**
232      * Build a query string and encode each parameter value
233      * The query string can e.g. be used for http connections.
234      * @return A query string which contains for each parameter/value pair
235      * a part, like "parameter=value" separated by "&".
236      * If no parameter is defined <CODE>null</CODE> is returned.
237      */

238     public String JavaDoc getEncodedQueryString() {
239         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
240         Iterator iter = this.names.keySet().iterator();
241         Iterator listIterator;
242         String JavaDoc key;
243         String JavaDoc value;
244         boolean first = true;
245         while (iter.hasNext() == true) {
246             key = (String JavaDoc)iter.next();
247             listIterator = ((ArrayList)names.get(key)).iterator();
248             while (listIterator.hasNext() == true) {
249                 if (first == false) result.append('&');
250                 value = (String JavaDoc)listIterator.next();
251                 result.append(key).append('=').append(SourceUtil.encode(value));
252                 first = false;
253             }
254         }
255         return (result.length() == 0 ? null : result.toString());
256     }
257
258     /**
259      * Add all parameters from the incoming parameters object.
260      */

261     public void add(SourceParameters parameters) {
262         Iterator names = parameters.getParameterNames();
263         Iterator values;
264         String JavaDoc name;
265         String JavaDoc value;
266         while (names.hasNext() == true) {
267             name = (String JavaDoc)names.next();
268             values = parameters.getParameterValues(name);
269             while (values.hasNext() == true) {
270                 value = (String JavaDoc)values.next();
271                 this.setParameter(name, value);
272             }
273         }
274     }
275
276     /**
277      * Overriding toString
278      */

279     public String JavaDoc toString() {
280         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("SourceParameters: {");
281         Iterator names = this.getParameterNames();
282         String JavaDoc name;
283         boolean firstName = true;
284         Iterator values;
285         String JavaDoc value;
286         boolean firstValue;
287         while (names.hasNext() == true) {
288             name = (String JavaDoc)names.next();
289             if (firstName == false) {
290                 buffer.append(", ");
291             } else {
292                 firstName = false;
293             }
294             buffer.append(name).append(" = (");
295             values = this.getParameterValues(name);
296             firstValue = true;
297             while (values.hasNext() == true) {
298                 value = (String JavaDoc)values.next();
299                 if (firstValue == false) {
300                     buffer.append(", ");
301                 } else {
302                     firstValue = false;
303                 }
304                 buffer.append(value);
305             }
306             buffer.append(')');
307         }
308         buffer.append('}');
309         return buffer.toString();
310     }
311
312     /**
313      * Returns a copy of the parameters object.
314      */

315     public Object JavaDoc clone() {
316         SourceParameters newObject = new SourceParameters();
317         Iterator names = this.getParameterNames();
318         Iterator values;
319         String JavaDoc name, value;
320         while (names.hasNext() == true) {
321             name = (String JavaDoc)names.next();
322             values = this.getParameterValues(name);
323             while (values.hasNext() == true) {
324                 value = (String JavaDoc)values.next();
325                 newObject.setParameter(name, value);
326             }
327         }
328         return newObject;
329     }
330
331     /**
332      * Test if there are any parameters.
333      */

334     public boolean hasParameters() {
335         return (this.names.size() > 0);
336     }
337
338     /**
339      * Set the value of this parameter to the given value.
340      * Remove all other values for this parameter.
341      */

342     public void setSingleParameterValue(String JavaDoc name, String JavaDoc value) {
343         this.removeParameter(name);
344         this.setParameter(name, value);
345     }
346
347     /**
348      * Remove all values for this parameter
349      */

350     public void removeParameter(String JavaDoc name) {
351         if (this.names.containsKey(name) == true) {
352             this.names.remove(name);
353         }
354     }
355
356 }
357
Popular Tags