KickJava   Java API By Example, From Geeks To Geeks.

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


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

17 package org.apache.excalibur.source;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.ArrayList 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 import org.apache.avalon.framework.configuration.Configuration;
27 import org.apache.avalon.framework.configuration.ConfigurationException;
28 import org.apache.avalon.framework.parameters.Parameters;
29
30 /**
31  * This class holds parameters for a <code>Source</code> object.
32  * It differs from the usual Parameters object because it can hold
33  * more than one value for a parameter, as is the case for HTTP
34  * request parameters.
35  * <p>
36  * Only particular kinds of <code>Source</code> implementations, such as
37  * {@link org.apache.excalibur.source.impl.URLSource} support this kind of
38  * parameters, passed as the {@link SourceResolver#URI_PARAMETERS} entry
39  * in the <code>parameters</code> argument of
40  * {@link SourceResolver#resolveURI(String, String, Map)}.
41  *
42  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
43  * @version $Id: SourceParameters.java,v 1.4 2004/02/28 11:47:26 cziegeler Exp $
44  */

45 public final class SourceParameters
46     implements Serializable JavaDoc, Cloneable JavaDoc
47 {
48     /** The parameter names are the keys and the value is a List object */
49     private Map JavaDoc names = new HashMap JavaDoc( 5 );
50
51     /**
52      * Decode the string
53      */

54     private String JavaDoc parseName( String JavaDoc s )
55     {
56         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
57         for( int i = 0; i < s.length(); i++ )
58         {
59             char c = s.charAt( i );
60             switch( c )
61             {
62                 case '+':
63                     sb.append( ' ' );
64                     break;
65                 case '%':
66                     try
67                     {
68                         sb.append( (char)Integer.parseInt( s.substring( i + 1, i + 3 ),
69                                                            16 ) );
70                         i += 2;
71                     }
72                     catch( NumberFormatException JavaDoc e )
73                     {
74                         throw new IllegalArgumentException JavaDoc();
75                     }
76                     catch( StringIndexOutOfBoundsException JavaDoc e )
77                     {
78                         String JavaDoc rest = s.substring( i );
79                         sb.append( rest );
80                         if( rest.length() == 2 )
81                             i++;
82                     }
83
84                     break;
85                 default:
86                     sb.append( c );
87                     break;
88             }
89         }
90         return sb.toString();
91     }
92
93     /**
94      * Create a new parameters object from the
95      * children of the configuration.
96      * If no children are available <code>null</code>
97      * is returned.
98      */

99     public static SourceParameters create( Configuration conf )
100     {
101         Configuration[] children = conf.getChildren();
102         if( children != null && children.length > 0 )
103         {
104             SourceParameters pars = new SourceParameters();
105             String JavaDoc name;
106             String JavaDoc value;
107             for( int i = 0; i < children.length; i++ )
108             {
109                 name = children[ i ].getName();
110                 try
111                 {
112                     value = children[ i ].getValue();
113                 }
114                 catch( ConfigurationException local )
115                 {
116                     value = ""; // ignore exception
117
}
118                 pars.setParameter( name, value );
119             }
120             return pars;
121         }
122         return null;
123     }
124
125     /**
126      * Standard Constructor creating an empty parameters object
127      */

128     public SourceParameters()
129     {
130     }
131
132     /**
133      * Construct a new object from a queryString
134      */

135     public SourceParameters( String JavaDoc queryString )
136     {
137         if( queryString != null )
138         {
139             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc( queryString, "&" );
140             while( st.hasMoreTokens() )
141             {
142                 String JavaDoc pair = st.nextToken();
143                 int pos = pair.indexOf( '=' );
144                 if( pos != -1 )
145                 {
146                     setParameter( parseName( pair.substring( 0, pos ) ),
147                                   parseName( pair.substring( pos + 1, pair.length() ) ) );
148                 }
149             }
150         }
151     }
152
153     /**
154      * Add a parameter.
155      * The parameter is added with the given value.
156      * @param name The name of the parameter.
157      * @param value The value of the parameter.
158      */

159     public void setParameter( String JavaDoc name, String JavaDoc value )
160     {
161         ArrayList JavaDoc list;
162         if( names.containsKey( name ) == true )
163         {
164             list = (ArrayList JavaDoc)names.get( name );
165         }
166         else
167         {
168             list = new ArrayList JavaDoc( 3 );
169             names.put( name, list );
170         }
171         list.add( value );
172     }
173
174     /**
175      * Get the value of a parameter.
176      * @param name The name of the parameter.
177      * @return The value of the first parameter with the name
178      * or <CODE>null</CODE>
179      */

180     public String JavaDoc getParameter( String JavaDoc name )
181     {
182         if( names.containsKey( name ) == true )
183         {
184             return (String JavaDoc)( (ArrayList JavaDoc)names.get( name ) ).get( 0 );
185         }
186         return null;
187     }
188
189     /**
190      * Get the value of a parameter.
191      * @param name The name of the parameter.
192      * @param defaultValue The default value if the parameter does not exist.
193      * @return The value of the first parameter with the name
194      * or <CODE>defaultValue</CODE>
195      */

196     public String JavaDoc getParameter( String JavaDoc name, String JavaDoc defaultValue )
197     {
198         if( names.containsKey( name ) == true )
199         {
200             return (String JavaDoc)( (ArrayList JavaDoc)names.get( name ) ).get( 0 );
201         }
202         return defaultValue;
203     }
204
205     /**
206      * Get the integer value of a parameter.
207      * @param name The name of the parameter.
208      * @param defaultValue The default value if the parameter does not exist.
209      * @return The value of the first parameter with the name
210      * or <CODE>defaultValue</CODE>
211      */

212     public int getParameterAsInteger( String JavaDoc name, int defaultValue )
213     {
214         if( names.containsKey( name ) == true )
215         {
216             return new Integer JavaDoc( (String JavaDoc)( (ArrayList JavaDoc)names.get( name ) ).get( 0 ) ).intValue();
217         }
218         return defaultValue;
219     }
220
221     /**
222      * Get the boolean value of a parameter.
223      * @param name The name of the parameter.
224      * @param defaultValue The default value if the parameter does not exist.
225      * @return The value of the first parameter with the name
226      * or <CODE>defaultValue</CODE>
227      */

228     public boolean getParameterAsBoolean( String JavaDoc name, boolean defaultValue )
229     {
230         if( names.containsKey( name ) == true )
231         {
232             return new Boolean JavaDoc( (String JavaDoc)( (ArrayList JavaDoc)names.get( name ) ).get( 0 ) ).booleanValue();
233         }
234         return defaultValue;
235     }
236
237     /**
238      * Test if a value for this parameter exists.
239      * @param name The name of the parameter.
240      * @return <CODE>true</CODE> if a value exists, otherwise <CODE>false</CODE>
241      */

242     public boolean containsParameter( String JavaDoc name )
243     {
244         return names.containsKey( name );
245     }
246
247     /**
248      * Get all values of a parameter.
249      * @param name The name of the parameter.
250      * @return Iterator for the (String) values or null if the parameter
251      * is not defined.
252      */

253     public Iterator JavaDoc getParameterValues( String JavaDoc name )
254     {
255         if( names.containsKey( name ) == true )
256         {
257             ArrayList JavaDoc list = (ArrayList JavaDoc)names.get( name );
258             return list.iterator();
259         }
260         return null;
261     }
262
263     /**
264      * Get all parameter names.
265      * @return Iterator for the (String) parameter names.
266      */

267     public Iterator JavaDoc getParameterNames()
268     {
269         return names.keySet().iterator();
270     }
271
272     /**
273      * Create a Parameters object.
274      * The first value of each parameter is added to the Parameters object.
275      * @return An Parameters object - if no parameters are defined this is an
276      * empty object.
277      */

278     public Parameters getFirstParameters()
279     {
280         Parameters result = new Parameters();
281         Iterator JavaDoc iter = this.getParameterNames();
282         String JavaDoc parName;
283         while( iter.hasNext() )
284         {
285             parName = (String JavaDoc)iter.next();
286             result.setParameter( parName, this.getParameter( parName ) );
287         }
288         return result;
289     }
290
291     /**
292      * Build a query string.
293      * The query string can e.g. be used for http connections.
294      * @return A query string which contains for each parameter/value pair
295      * a part, like "parameter=value" separated by "&".
296      * If no parameter is defined <CODE>null</CODE> is returned.
297      */

298     public String JavaDoc getQueryString()
299     {
300         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
301         Iterator JavaDoc iter = this.names.keySet().iterator();
302         Iterator JavaDoc listIterator;
303         String JavaDoc key;
304         String JavaDoc value;
305         boolean first = true;
306         while( iter.hasNext() == true )
307         {
308             key = (String JavaDoc)iter.next();
309             listIterator = ( (ArrayList JavaDoc)names.get( key ) ).iterator();
310             while( listIterator.hasNext() == true )
311             {
312                 if( first == false ) result.append( '&' );
313                 value = (String JavaDoc)listIterator.next();
314                 result.append( key ).append( '=' ).append( value );
315                 first = false;
316             }
317         }
318         return ( result.length() == 0 ? null : result.toString() );
319     }
320
321     /**
322      * Build a query string and encode each parameter value.
323      * The query string can e.g. be used for http connections.
324      * @return A query string which contains for each parameter/value pair
325      * a part, like "parameter=value" separated by "&".
326      * If no parameter is defined <CODE>null</CODE> is returned.
327      */

328     public String JavaDoc getEncodedQueryString()
329     {
330         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
331         Iterator JavaDoc iter = this.names.keySet().iterator();
332         Iterator JavaDoc listIterator;
333         String JavaDoc key;
334         String JavaDoc value;
335         boolean first = true;
336         while( iter.hasNext() == true )
337         {
338             key = (String JavaDoc)iter.next();
339             listIterator = ( (ArrayList JavaDoc)names.get( key ) ).iterator();
340             while( listIterator.hasNext() == true )
341             {
342                 if( first == false ) result.append( '&' );
343                 value = (String JavaDoc)listIterator.next();
344                 result.append( key ).append( '=' ).append( SourceUtil.encode( value ) );
345                 first = false;
346             }
347         }
348         return ( result.length() == 0 ? null : result.toString() );
349     }
350
351     /**
352      * Add all parameters from the incoming parameters object.
353      */

354     public void add( SourceParameters parameters )
355     {
356         if( null != parameters )
357         {
358             Iterator JavaDoc names = parameters.getParameterNames();
359             Iterator JavaDoc values;
360             String JavaDoc name;
361             String JavaDoc value;
362             while( names.hasNext() == true )
363             {
364                 name = (String JavaDoc)names.next();
365                 values = parameters.getParameterValues( name );
366                 while( values.hasNext() == true )
367                 {
368                     value = (String JavaDoc)values.next();
369                     this.setParameter( name, value );
370                 }
371             }
372         }
373     }
374
375     /**
376      * Overriding toString
377      */

378     public String JavaDoc toString()
379     {
380         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc( "SourceParameters: {" );
381         Iterator JavaDoc names = this.getParameterNames();
382         String JavaDoc name;
383         boolean firstName = true;
384         Iterator JavaDoc values;
385         String JavaDoc value;
386         boolean firstValue;
387         while( names.hasNext() == true )
388         {
389             name = (String JavaDoc)names.next();
390             if( firstName == false )
391             {
392                 buffer.append( ", " );
393             }
394             else
395             {
396                 firstName = false;
397             }
398             buffer.append( name ).append( " = (" );
399             values = this.getParameterValues( name );
400             firstValue = true;
401             while( values.hasNext() == true )
402             {
403                 value = (String JavaDoc)values.next();
404                 if( firstValue == false )
405                 {
406                     buffer.append( ", " );
407                 }
408                 else
409                 {
410                     firstValue = false;
411                 }
412                 buffer.append( value );
413             }
414             buffer.append( ')' );
415         }
416         buffer.append( '}' );
417         return buffer.toString();
418     }
419
420     /**
421      * Returns a copy of the parameters object.
422      */

423     public Object JavaDoc clone()
424     {
425         SourceParameters newObject = new SourceParameters();
426         Iterator JavaDoc names = this.getParameterNames();
427         Iterator JavaDoc values;
428         String JavaDoc name, value;
429         while( names.hasNext() )
430         {
431             name = (String JavaDoc)names.next();
432             values = this.getParameterValues( name );
433             while( values.hasNext() )
434             {
435                 value = (String JavaDoc)values.next();
436                 newObject.setParameter( name, value );
437             }
438         }
439         return newObject;
440     }
441
442     /**
443      * Test if there are any parameters.
444      */

445     public boolean hasParameters()
446     {
447         return ( this.names.size() > 0 );
448     }
449
450     /**
451      * Set the value of this parameter to the given value.
452      * Remove all other values for this parameter.
453      */

454     public void setSingleParameterValue( String JavaDoc name, String JavaDoc value )
455     {
456         this.removeParameter( name );
457         this.setParameter( name, value );
458     }
459
460     /**
461      * Remove all values for this parameter
462      */

463     public void removeParameter( String JavaDoc name )
464     {
465         if( this.names.containsKey( name ) )
466         {
467             this.names.remove( name );
468         }
469     }
470 }
471
Popular Tags