View Javadoc

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 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.jetspeed.modules.parameters; 
18  
19  // Turbine support
20  import org.apache.turbine.util.RunData;
21  import org.apache.turbine.services.velocity.TurbineVelocity;
22  import org.apache.turbine.services.localization.LocalizationTool;
23  // Java stuff
24  import java.util.Map;
25  
26  // jetspeed stuff
27  import org.apache.jetspeed.services.TemplateLocator;
28  import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
29  import org.apache.jetspeed.services.logging.JetspeedLogger;
30  import org.apache.jetspeed.util.template.BaseJetspeedLink;
31  
32  // Velocity Stuff
33  import org.apache.velocity.context.Context;
34  
35  /***
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 the
47   * 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 VelocityParameterPresentationStyle
50   * 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   */
57  
58  public class VelocityParameterPresentationStyle extends ParameterPresentationStyle
59  {
60  
61      /***
62       * Static initialization of the logger for this class
63       */    
64      private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(VelocityParameterPresentationStyle.class.getName());     
65      
66      /***
67       * Returns presentation control
68       * 
69       * @param data - rundata object
70       * @param name - parameter name
71       * @param value - current parameter value
72       * @param parms - additional style parameters
73       * @return string
74       */
75      public String getContent(RunData data, String name, String value, Map parms)
76      {
77          String result = null;
78  
79          // create a blank context
80          Context context = TurbineVelocity.getContext();
81  
82          // Put basics in the context
83          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", new BaseJetspeedLink(data));
92  
93          try
94          {
95              // Add custom objects to the context
96              this.buildContext(data, name, value, parms, context);
97  
98              // Build default template name (classname + .vm)
99              String className = this.getClass().getName();
100             int pos = className.lastIndexOf(".");
101             pos = pos < 0 ? 0 : pos + 1;
102             className = className.substring(pos);
103 
104             // Render the template
105             String template = (String) this.getParm("template", className + ".vm");
106             String templatePath = TemplateLocator.locateParameterTemplate(data, template);
107             result = TurbineVelocity.handleRequest(context, templatePath);
108         }
109         catch (Exception e)
110         {
111             logger.error("Exception", e);
112             // Fallback to input text box presentation style
113             result = "<input type=\"text\" name=\"" + name + "\" value=\"" + value + "\"";
114         }
115 
116         TurbineVelocity.requestFinished(context);       
117 
118         return result;
119 
120     }
121 
122     /***
123      * Override this method to put your own objects in the Velocity context
124      * 
125      * @param data
126      * @param name
127      * @param value
128      * @param parms
129      * @param context
130      */
131     public void buildContext(RunData data, String name, String value, Map parms, Context context)
132     {
133 
134     }
135 }