KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > modules > input > RequestParameterModule


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
17 package org.apache.cocoon.components.modules.input;
18
19 import org.apache.avalon.framework.configuration.Configuration;
20 import org.apache.avalon.framework.configuration.ConfigurationException;
21 import org.apache.avalon.framework.thread.ThreadSafe;
22 import org.apache.cocoon.environment.ObjectModelHelper;
23 import org.apache.cocoon.environment.Request;
24
25 import java.util.Enumeration JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.LinkedList JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.SortedSet JavaDoc;
31 import java.util.TreeSet JavaDoc;
32
33 /**
34  * RequestParameterModule accesses request parameters. If the
35  * parameter name contains an askerisk "*" this is considered a
36  * wildcard and all parameters that would match this wildcard are
37  * considered to be part of an array of that name for
38  * getAttributeValues. Only one "*" is allowed. Wildcard matches take
39  * precedence over real arrays. In that case only the first value of
40  * such array is returned.
41  *
42  * @author <a HREF="mailto:haul@apache.org">Christian Haul</a>
43  * @version CVS $Id: RequestParameterModule.java 30932 2004-07-29 17:35:38Z vgritsenko $
44  */

45 public class RequestParameterModule extends AbstractInputModule implements ThreadSafe {
46
47     public Object JavaDoc getAttribute( String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel ) throws ConfigurationException {
48
49         String JavaDoc pname = (String JavaDoc) this.settings.get("parameter", name);
50         if ( modeConf != null ) {
51             pname = modeConf.getAttribute( "parameter", pname );
52             // preferred
53
pname = modeConf.getChild("parameter").getValue(pname);
54         }
55         return ObjectModelHelper.getRequest(objectModel).getParameter( pname );
56     }
57
58
59     public Iterator JavaDoc getAttributeNames( Configuration modeConf, Map JavaDoc objectModel ) throws ConfigurationException {
60
61         return new IteratorHelper(ObjectModelHelper.getRequest(objectModel).getParameterNames());
62     }
63
64
65     public Object JavaDoc[] getAttributeValues( String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel )
66         throws ConfigurationException {
67
68         Request request = ObjectModelHelper.getRequest(objectModel);
69         String JavaDoc wildcard = (String JavaDoc) this.settings.get("parameter", name);
70         if ( modeConf != null ) {
71             wildcard = modeConf.getAttribute( "parameter", wildcard );
72             // preferred
73
wildcard = modeConf.getChild("parameter").getValue(wildcard);
74         }
75         int wildcardIndex = wildcard.indexOf( "*" );
76         if ( wildcardIndex != -1 ) {
77             // "*" contained in parameter name => combine all
78
// parameters' values that match prefix, suffix
79

80             // split the parameter's name so that the "*" could be
81
// determined by looking at the parameters' names that
82
// start with the prefix and end with the suffix
83
//
84
String JavaDoc prefix = wildcard.substring( 0, wildcardIndex );
85             String JavaDoc suffix;
86             if ( wildcard.length() >= wildcardIndex + 1 ) {
87                 suffix = wildcard.substring( wildcardIndex + 1 );
88             } else {
89                 suffix = "";
90             }
91             SortedSet JavaDoc names = new TreeSet JavaDoc();
92             Enumeration JavaDoc allNames = request.getParameterNames();
93
94             while (allNames.hasMoreElements()) {
95                 String JavaDoc pname = (String JavaDoc) allNames.nextElement();
96                 if ( pname.startsWith( prefix ) && pname.endsWith( suffix ) ) {
97                     names.add(pname);
98                 }
99             }
100
101             List JavaDoc values = new LinkedList JavaDoc();
102             Iterator JavaDoc j = names.iterator();
103             while (j.hasNext()){
104                 String JavaDoc pname = (String JavaDoc) j.next();
105                 values.add( request.getParameter( pname ) );
106             }
107
108             return values.toArray();
109
110         } else {
111             // no "*" in parameter name => just return all values of
112
// this one parameter.
113

114             return request.getParameterValues( wildcard );
115
116         }
117
118     }
119
120 }
121
Popular Tags