KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.apache.cocoon.acting;
17
18 import org.apache.avalon.framework.configuration.Configuration;
19 import org.apache.avalon.framework.configuration.ConfigurationException;
20 import org.apache.avalon.framework.parameters.Parameters;
21 import org.apache.avalon.framework.thread.ThreadSafe;
22
23 import org.apache.cocoon.environment.Redirector;
24 import org.apache.cocoon.environment.SourceResolver;
25 import org.apache.cocoon.components.modules.input.InputModuleHelper;
26
27 import org.apache.commons.lang.BooleanUtils;
28
29 import java.util.HashMap JavaDoc;
30 import java.util.Map JavaDoc;
31
32 /**
33  * Simple helper action to allow passing sitemap variables to InputModules.
34  * Sitemap evaluation of input modules using the curly bracket syntax e.g.
35  * {defaults:skin} suffers from the fact that it is not
36  * possible to use a sitemap variable as part of the invocation like
37  * {defaults:{1})}. This action takes three parameters, the name
38  * of the input module, the attribute name, and whether to call getAttribute() or
39  * getAttributeValues(). Thus the above becomes
40  * <pre>
41  * &lt;map:act type="inputmodule"&gt;
42  * &lt;map:parameter name="module" value="defaults"/&gt;
43  * &lt;map:parameter name="attribute" value="{1}"/&gt;
44  * &lt;map:parameter name="single-value" value="false"/&gt;
45  *
46  * &lt;!-- do something with the result: "{1}" --&gt;
47  *
48  * &lt;/map:act&gt;
49  * </pre>
50  * The action invokes the
51  * {@link org.apache.cocoon.components.modules.input.InputModule#getAttributeValues(String, Configuration, Map) getAttributeValues()}
52  * method and returns all results numbered from "0". If no result exists,
53  * "null" is returned and the nested block is skipped.
54  * The name of the input module to use may be preconfigured when
55  * declaring the action in your sitemap:
56  * <pre>
57  * &lt;map:action name="inputmodule"
58  * SRC="org.apache.cocoon.acting.InputModuleAction"
59  * logger="sitemap.action.inputmodule"&gt;
60  * &lt;module&gt;defaults&lt;/module&gt;
61  * &lt;single-value&gt;false&lt;/single-value&gt;
62  * &lt;/map:action&gt;
63  * </pre>
64  *
65  *
66  * @see org.apache.cocoon.components.modules.input.InputModule
67  *
68  * @author <a HREF="mailto:haul@apache.org">Christian Haul</a>
69  * @version CVS $Id: InputModuleAction.java 123776 2004-12-31 09:17:48Z antonio $
70  */

71 public class InputModuleAction extends ConfigurableServiceableAction implements ThreadSafe {
72
73     /* (non-Javadoc)
74      * @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)
75      */

76     public Map JavaDoc act(Redirector redirector, SourceResolver resolver,
77                    Map JavaDoc objectModel, String JavaDoc source, Parameters parameters)
78         throws Exception JavaDoc {
79
80         HashMap JavaDoc map = null;
81         Configuration conf = null;
82         String JavaDoc module = parameters.getParameter("module", (String JavaDoc) this.settings.get("module"));
83         String JavaDoc attrib =
84             parameters.getParameter("attribute", (String JavaDoc) this.settings.get("attribute"));
85         boolean single =
86             parameters.getParameterAsBoolean(
87                 "single-value",
88                 ((Boolean JavaDoc) this.settings.get("single-value")).booleanValue());
89
90         if (module != null && attrib != null) {
91             InputModuleHelper mhelper = new InputModuleHelper();
92             mhelper.setup(manager);
93             Object JavaDoc[] result = null;
94             if (!single) {
95                 result = mhelper.getAttributeValues(objectModel, conf, module, attrib, null);
96             } else {
97                 Object JavaDoc tmp = mhelper.getAttribute(objectModel, conf, module, attrib, null);
98                 if (tmp != null){
99                     result = new Object JavaDoc[1];
100                     result[0] = tmp;
101                 }
102             }
103             mhelper.releaseAll();
104
105             if (result != null && result.length != 0) {
106                 map = new HashMap JavaDoc();
107                 for (int i = 0; i < result.length; i++) {
108                     map.put(Integer.toString(i), result[i]);
109                 }
110             }
111         } else {
112             if (getLogger().isErrorEnabled()) {
113                 getLogger().error(
114                     "Parameter is missing: module=" + module + " attribute=" + attrib);
115             }
116         }
117         return map;
118     }
119
120     /* (non-Javadoc)
121      * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
122      */

123     public void configure(Configuration conf) throws ConfigurationException {
124         super.configure(conf);
125         String JavaDoc tmp = (String JavaDoc) this.settings.get("single-value", "false");
126         this.settings.put("single-value", BooleanUtils.toBooleanObject(tmp));
127     }
128
129 }
130
Popular Tags