KickJava   Java API By Example, From Geeks To Geeks.

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


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  * RequestAttributeModule accesses request attributes. If the
35  * attribute name contains an askerisk "*" this is considered a
36  * wildcard and all attributes that would match this wildcard are
37  * considered to be part of an array of that name for
38  * getAttributeValues. Only one "*" is allowed.
39  *
40  * @author <a HREF="mailto:haul@apache.org">Christian Haul</a>
41  * @version CVS $Id: RequestAttributeModule.java 37203 2004-08-30 14:19:06Z cziegeler $
42  */

43 public class RequestAttributeModule extends AbstractInputModule implements ThreadSafe {
44
45     public Object JavaDoc getAttribute( String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel )
46         throws ConfigurationException {
47
48         String JavaDoc pname = (String JavaDoc) this.settings.get("parameter", name);
49         if ( modeConf != null ) {
50             pname = modeConf.getAttribute( "parameter", pname );
51             // preferred
52
pname = modeConf.getChild("parameter").getValue(pname);
53         }
54         return ObjectModelHelper.getRequest(objectModel).getAttribute( pname );
55     }
56
57
58     /* (non-Javadoc)
59      * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeNames(org.apache.avalon.framework.configuration.Configuration, java.util.Map)
60      */

61     public Iterator JavaDoc getAttributeNames( Configuration modeConf, Map JavaDoc objectModel )
62     throws ConfigurationException {
63         return new IteratorHelper(ObjectModelHelper.getRequest(objectModel).getAttributeNames());
64     }
65
66
67     public Object JavaDoc[] getAttributeValues( String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel )
68     throws ConfigurationException {
69         final Request request = ObjectModelHelper.getRequest(objectModel);
70         
71         String JavaDoc wildcard = (String JavaDoc) this.settings.get("parameter",name);
72         if ( modeConf != null ) {
73             wildcard = modeConf.getAttribute( "parameter", wildcard );
74             // preferred
75
wildcard = modeConf.getChild("parameter").getValue(wildcard);
76         }
77         int wildcardIndex = wildcard.indexOf( "*" );
78         if ( wildcardIndex != -1 ) {
79             // "*" contained in attribute name => combine all
80
// attributes' values that match prefix, suffix
81

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

116             Object JavaDoc value = request.getAttribute( wildcard );
117             if ( value != null && !value.getClass().isArray() ) {
118                 Object JavaDoc[] values = new Object JavaDoc[1];
119                 values[0] = value;
120                 return values;
121             } else {
122                 return (Object JavaDoc[]) value;
123             }
124
125         }
126
127     }
128
129
130
131 }
132
Popular Tags