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.TurbineServices;
22  import org.apache.turbine.services.jsp.JspService;
23  
24  // Java stuff
25  import java.util.Map;
26  
27  // jetspeed stuff
28  import org.apache.jetspeed.services.TemplateLocator;
29  import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
30  import org.apache.jetspeed.services.logging.JetspeedLogger;
31  
32  /***
33   * Generic jsp-based presentation style. The following default objects are put in the context:
34   * <UL>
35   * <LI>data - rundata object</LI>
36   * <LI>name - name of the parameter</LI>
37   * <LI>value - current value of the parameter</LI>
38   * <LI>parms - map of additional style parameters</LI>
39   * </UL>
40   * 
41   * <P>Supporting jsp templates should be placed in ${velocity-templates-root}/parameters folder.</p>
42   * 
43   * <P>It may be used directly with "template" as the only required parameter. This is useful when the
44   * no additional objects are needed by the template.</P>
45   * 
46   * <P>If additional objects need to be put in the context, a new class extending JspParameterPresentationStyle
47   * should be created. Override buildContext to place custom objects in the jsp context.</P>
48   * 
49   * <P>If "template" parameter is not specified, it is assumed that the template name is "classname.vm".</P>
50   * 
51   * @author <a href="mailto:morciuch@apache.org">Mark Orciuch</a>
52   * @version $Id: JspParameterPresentationStyle.java,v 1.4 2004/02/23 03:01:20 jford Exp $
53   */
54  
55  public class JspParameterPresentationStyle extends ParameterPresentationStyle
56  {
57  
58      /***
59       * Static initialization of the logger for this class
60       */    
61      private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(JspParameterPresentationStyle.class.getName());     
62      
63      /***
64       * Returns presentation control
65       * 
66       * @param data - rundata object
67       * @param name - parameter name
68       * @param value - current parameter value
69       * @param parms - additional style parameters
70       * @return string
71       */
72      public String getContent(RunData data, String name, String value, Map parms)
73      {
74          String result = null;
75  
76          // Get reference to jsp service
77          JspService jspService = (JspService) TurbineServices.getInstance().getService(JspService.SERVICE_NAME);
78  
79          // Put basics in the context
80          data.getRequest().setAttribute("data", data);
81          data.getRequest().setAttribute("name", name);
82          data.getRequest().setAttribute("value", value);
83          data.getRequest().setAttribute("parms", parms);
84          data.getRequest().setAttribute("events", this.getJavascriptEvents());
85  
86          try
87          {
88              // Add custom objects to the context
89              this.buildContext(data, name, value, parms);
90  
91              // Build default template name (classname + .vm)
92              String className = this.getClass().getName();
93              int pos = className.lastIndexOf(".");
94              pos = pos < 0 ? 0 : pos + 1;
95              className = className.substring(pos);
96  
97              // Render the template
98              String template = (String) this.getParm("template", className + ".jsp");
99              String templatePath = TemplateLocator.locateParameterTemplate(data, template);
100             jspService.handleRequest(data, templatePath);
101             result = "";
102         }
103         catch (Exception e)
104         {
105             logger.error("Exception", e);
106             // Fallback to input text box presentation style
107             result = "<input type=\"text\" name=\"" + name + "\" value=\"" + value + "\"";
108         }
109 
110         return result;
111 
112     }
113 
114     /***
115      * Override this method to put your own objects in the Velocity context
116      * 
117      * @param data
118      * @param name
119      * @param value
120      * @param parms
121      * @param context
122      */
123     public void buildContext(RunData data, String name, String value, Map parms)
124     {
125 
126     }
127 }