KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > acting > SetterAction


1 /*
2  * Copyright 2005 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 package org.apache.cocoon.acting;
17
18 import java.util.Map JavaDoc;
19
20 import org.apache.avalon.framework.parameters.ParameterException;
21 import org.apache.avalon.framework.parameters.Parameterizable;
22 import org.apache.avalon.framework.parameters.Parameters;
23 import org.apache.avalon.framework.thread.ThreadSafe;
24 import org.apache.cocoon.environment.ObjectModelHelper;
25 import org.apache.cocoon.environment.Redirector;
26 import org.apache.cocoon.environment.SourceResolver;
27
28 /**
29  * This action can be used to set information in either the object model,
30  * the request or the session.
31  * All parameters set for this action are set in the according location
32  * whereas the parameter name is the key and the value of the parameter
33  * will be set as a string value for this key.
34  *
35  * @version SVN $Id: SetterAction.java 289173 2005-09-15 08:07:37Z cziegeler $
36  */

37 public class SetterAction
38     extends AbstractAction
39     implements Parameterizable, ThreadSafe {
40
41     public static final int MODE_OBJECT_MODEL = 1;
42     public static final int MODE_REQUEST_ATTR = 2;
43     public static final int MODE_SESSION_ATTR = 3;
44     
45     public static final String JavaDoc MODEDEF_OBJECT_MODEL = "object-model";
46     public static final String JavaDoc MODEDEF_REQUEST_ATTR = "request-attribute";
47     public static final String JavaDoc MODEDEF_SESSION_ATTR = "session-attribute";
48
49     protected int mode = MODE_OBJECT_MODEL;
50
51     /**
52      * @see Parameterizable#parameterize(org.apache.avalon.framework.parameters.Parameters)
53      * @throws ParameterException
54      */

55     public void parameterize(Parameters params)
56     throws ParameterException {
57         String JavaDoc modeDef = params.getParameter("mode", null);
58         if ( modeDef != null ) {
59             if ( MODEDEF_OBJECT_MODEL.equals(modeDef) ) {
60                 this.mode = MODE_OBJECT_MODEL;
61             } else if ( MODEDEF_REQUEST_ATTR.equals(modeDef) ) {
62                 this.mode = MODE_REQUEST_ATTR;
63             } else if ( MODEDEF_SESSION_ATTR.equals(modeDef) ) {
64                 this.mode = MODE_SESSION_ATTR;
65             } else {
66                 throw new ParameterException("Unknown mode: " + this.mode);
67             }
68         }
69     }
70
71     /**
72      * @see org.apache.cocoon.acting.Action#act(org.apache.cocoon.environment.Redirector, org.apache.cocoon.environment.SourceResolver, java.util.Map, java.lang.String, org.apache.avalon.framework.parameters.Parameters)
73      */

74     public Map JavaDoc act(Redirector redirector,
75                    SourceResolver resolver,
76                    Map JavaDoc objectModel,
77                    String JavaDoc source,
78                    Parameters parameters)
79     throws Exception JavaDoc {
80         final String JavaDoc[] names = parameters.getNames();
81         for(int i = 0; i < names.length; i++) {
82             final String JavaDoc name = names[i];
83             if ( this.mode == MODE_OBJECT_MODEL ) {
84                 objectModel.put(name, parameters.getParameter(name));
85             } else if ( this.mode == MODE_REQUEST_ATTR ) {
86                 ObjectModelHelper.getRequest(objectModel).setAttribute(name, parameters.getParameter(name));
87             } else if ( this.mode == MODE_SESSION_ATTR ) {
88                 ObjectModelHelper.getRequest(objectModel).getSession().setAttribute(name, parameters.getParameter(name));
89             }
90         }
91         return EMPTY_MAP;
92     }
93 }
94
Popular Tags