001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.acting;
018:
019: import org.apache.avalon.framework.configuration.Configuration;
020: import org.apache.avalon.framework.configuration.ConfigurationException;
021: import org.apache.avalon.framework.parameters.Parameters;
022: import org.apache.avalon.framework.thread.ThreadSafe;
023:
024: import org.apache.cocoon.environment.Redirector;
025: import org.apache.cocoon.environment.SourceResolver;
026: import org.apache.cocoon.components.modules.input.InputModuleHelper;
027:
028: import org.apache.commons.lang.BooleanUtils;
029:
030: import java.util.HashMap;
031: import java.util.Map;
032:
033: /**
034: * Simple helper action to allow passing sitemap variables to InputModules.
035: * Sitemap evaluation of input modules using the curly bracket syntax e.g.
036: * {defaults:skin} suffers from the fact that it is not
037: * possible to use a sitemap variable as part of the invocation like
038: * {defaults:{1})}. This action takes three parameters, the name
039: * of the input module, the attribute name, and whether to call getAttribute() or
040: * getAttributeValues(). Thus the above becomes
041: * <pre>
042: * <map:act type="inputmodule">
043: * <map:parameter name="module" value="defaults"/>
044: * <map:parameter name="attribute" value="{1}"/>
045: * <map:parameter name="single-value" value="false"/>
046: *
047: * <!-- do something with the result: "{1}" -->
048: *
049: * </map:act>
050: * </pre>
051: * The action invokes the
052: * {@link org.apache.cocoon.components.modules.input.InputModule#getAttributeValues(String, Configuration, Map) getAttributeValues()}
053: * method and returns all results numbered from "0". If no result exists,
054: * "null" is returned and the nested block is skipped.
055: * The name of the input module to use may be preconfigured when
056: * declaring the action in your sitemap:
057: * <pre>
058: * <map:action name="inputmodule"
059: * src="org.apache.cocoon.acting.InputModuleAction"
060: * logger="sitemap.action.inputmodule">
061: * <module>defaults</module>
062: * <single-value>false</single-value>
063: * </map:action>
064: * </pre>
065: *
066: *
067: * @see org.apache.cocoon.components.modules.input.InputModule
068: *
069: * @author <a href="mailto:haul@apache.org">Christian Haul</a>
070: * @version CVS $Id: InputModuleAction.java 433543 2006-08-22 06:22:54Z crossley $
071: */
072: public class InputModuleAction extends ConfigurableServiceableAction
073: implements ThreadSafe {
074:
075: /* (non-Javadoc)
076: * @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)
077: */
078: public Map act(Redirector redirector, SourceResolver resolver,
079: Map objectModel, String source, Parameters parameters)
080: throws Exception {
081:
082: HashMap map = null;
083: Configuration conf = null;
084: String module = parameters.getParameter("module",
085: (String) this .settings.get("module"));
086: String attrib = parameters.getParameter("attribute",
087: (String) this .settings.get("attribute"));
088: boolean single = parameters.getParameterAsBoolean(
089: "single-value", ((Boolean) this .settings
090: .get("single-value")).booleanValue());
091:
092: if (module != null && attrib != null) {
093: InputModuleHelper mhelper = new InputModuleHelper();
094: mhelper.setup(manager);
095: Object[] result = null;
096: if (!single) {
097: result = mhelper.getAttributeValues(objectModel, conf,
098: module, attrib, null);
099: } else {
100: Object tmp = mhelper.getAttribute(objectModel, conf,
101: module, attrib, null);
102: if (tmp != null) {
103: result = new Object[1];
104: result[0] = tmp;
105: }
106: }
107: mhelper.releaseAll();
108:
109: if (result != null && result.length != 0) {
110: map = new HashMap();
111: for (int i = 0; i < result.length; i++) {
112: map.put(Integer.toString(i), result[i]);
113: }
114: }
115: } else {
116: if (getLogger().isErrorEnabled()) {
117: getLogger().error(
118: "Parameter is missing: module=" + module
119: + " attribute=" + attrib);
120: }
121: }
122: return map;
123: }
124:
125: /* (non-Javadoc)
126: * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
127: */
128: public void configure(Configuration conf)
129: throws ConfigurationException {
130: super .configure(conf);
131: String tmp = (String) this .settings
132: .get("single-value", "false");
133: this .settings.put("single-value", BooleanUtils
134: .toBooleanObject(tmp));
135: }
136:
137: }
|