1/*2 * Copyright 2000-2001,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 at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 */1617packageorg.apache.jetspeed.modules.parameters;
1819// Turbine support20import org.apache.turbine.util.RunData;
21import org.apache.turbine.services.velocity.TurbineVelocity;
22import org.apache.turbine.services.localization.LocalizationTool;
23// Java stuff24import java.util.Map;
2526// jetspeed stuff27import org.apache.jetspeed.services.TemplateLocator;
28import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
29import org.apache.jetspeed.services.logging.JetspeedLogger;
30import org.apache.jetspeed.util.template.BaseJetspeedLink;
3132// Velocity Stuff33import org.apache.velocity.context.Context;
3435/***36 * Generic Velocity-based presentation style. The following default objects are put in the context:37 * <UL>38 * <LI>data - rundata object</LI>39 * <LI>name - name of the parameter</LI>40 * <LI>value - current value of the parameter</LI>41 * <LI>parms - map of additional style parameters</LI>42 * </UL>43 * 44 * <P>Supporting Velocity templates should be placed in ${velocity-templates-root}/parameters folder.</p>45 * 46 * <P>It may be used directly with "template" as the only required parameter. This is useful when the47 * no additional objects are needed by the template.</P>48 * 49 * <P>If additional objects need to be put in the context, a new class extending VelocityParameterPresentationStyle50 * should be created. Override buildContext to place custom objects in the Velocity context.</P>51 * 52 * <P>If "template" parameter is not specified, it is assumed that the template name is "classname.vm".</P>53 * 54 * @author <a href="mailto:morciuch@apache.org">Mark Orciuch</a>55 * @version $Id: VelocityParameterPresentationStyle.java,v 1.6 2004/02/23 03:01:20 jford Exp $56 */5758publicclassVelocityParameterPresentationStyleextendsParameterPresentationStyle59 {
6061/***62 * Static initialization of the logger for this class63 */64privatestaticfinalJetspeedLogger logger = JetspeedLogFactoryService.getLogger(VelocityParameterPresentationStyle.class.getName());
6566/***67 * Returns presentation control68 * 69 * @param data - rundata object70 * @param name - parameter name71 * @param value - current parameter value72 * @param parms - additional style parameters73 * @return string74 */75public String getContent(RunData data, String name, String value, Map parms)
76 {
77 String result = null;
7879// create a blank context80 Context context = TurbineVelocity.getContext();
8182// Put basics in the context83 context.put("data", data);
84 context.put("name", name);
85 context.put("value", value);
86 context.put("parms", parms);
87 context.put("events", this.getJavascriptEvents());
88 LocalizationTool lt = new LocalizationTool();
89 lt.init(data);
90 context.put("l10n", lt);
91 context.put("jslink", newBaseJetspeedLink(data));
9293try94 {
95// Add custom objects to the context96this.buildContext(data, name, value, parms, context);
9798// Build default template name (classname + .vm)99 String className = this.getClass().getName();
100int pos = className.lastIndexOf(".");
101 pos = pos < 0 ? 0 : pos + 1;
102 className = className.substring(pos);
103104// Render the template105 String template = (String) this.getParm("template", className + ".vm");
106 String templatePath = TemplateLocator.locateParameterTemplate(data, template);
107 result = TurbineVelocity.handleRequest(context, templatePath);
108 }
109catch (Exception e)
110 {
111 logger.error("Exception", e);
112// Fallback to input text box presentation style113 result = "<input type=\"text\" name=\"" + name + "\" value=\"" + value + "\"";
114 }
115116 TurbineVelocity.requestFinished(context);
117118return result;
119120 }
121122/***123 * Override this method to put your own objects in the Velocity context124 * 125 * @param data126 * @param name127 * @param value128 * @param parms129 * @param context130 */131publicvoid buildContext(RunData data, String name, String value, Map parms, Context context)
132 {
133134 }
135 }