View Javadoc

1   package org.apache.turbine.util.template;
2   
3   /*
4    * Copyright 2001-2004 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.Map;
20  import java.util.HashMap;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  
25  import org.apache.turbine.services.template.TurbineTemplate;
26  import org.apache.turbine.util.RunData;
27  import org.apache.turbine.util.uri.URIConstants;
28  
29  
30  /***
31   * This is a wrapper for Template specific information.  It's part of
32   * the RunData object and can extract the information it needs to do
33   * the job directly from the data.getParameters().
34   *
35   * @author <a href="mailto:mbryson@mindspring.com">Dave Bryson</a>
36   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
37   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
38   * @version $Id: TemplateInfo.java,v 1.8.2.2 2004/05/20 03:27:24 seade Exp $
39   */
40  public class TemplateInfo
41  {
42      /*** Logging */
43      private static Log log = LogFactory.getLog(TemplateInfo.class);
44  
45  
46      /* Constants for tempStorage hash map. */
47      public static final String NAVIGATION_TEMPLATE = "00navigation_template00";
48      public static final String LAYOUT_TEMPLATE = "00layout_template00";
49      public static final String SERVICE_NAME = "template_service";
50  
51      /* Handle to the RunData object. */
52      private RunData data = null;
53  
54      /* Place to store information about templates. */
55      private Map tempStorage = null;
56  
57      /***
58       * Constructor
59       *
60       * @param RunData A Turbine Rundata object.
61       */
62      public TemplateInfo(RunData data)
63      {
64          this.data = data;
65          tempStorage = new HashMap(10);
66      }
67  
68      /***
69       * Get the value of navigationTemplate.
70       *
71       * @return A String with the value of navigationTemplate.
72       */
73      public String getNavigationTemplate()
74      {
75          return getString(TemplateInfo.NAVIGATION_TEMPLATE);
76      }
77  
78      /***
79       * Set the value of navigationTemplate.
80       *
81       * @param v Value to assign to navigationTemplate.
82       */
83      public void setNavigationTemplate(String v)
84      {
85          setTemp(TemplateInfo.NAVIGATION_TEMPLATE, v);
86      }
87  
88      /***
89       * Get the value of screen for the RunData parameters.  This
90       * information comes from PathInfo or a QueryString. 
91       *
92       * @return A String with the value of screen.
93       */
94      public String getScreenTemplate()
95      {
96          return data.getParameters().getString(URIConstants.CGI_TEMPLATE_PARAM, null);
97      }
98  
99      /***
100      * Set the value of screen.  This is really just a method to hide
101      * using the RunData Parameter.
102      *
103      * @param v Value to assign to screen.
104      */
105     public void setScreenTemplate(String v)
106     {
107         data.getParameters().setString(URIConstants.CGI_TEMPLATE_PARAM, v);
108 
109         // We have changed the screen template so
110         // we should now update the layout template
111         // as well. We will use the template service
112         // to help us out.
113         try
114         {
115             setLayoutTemplate(TurbineTemplate.getLayoutTemplateName(v));
116         }
117         catch (Exception e)
118         {
119             /*
120              * do nothing.
121              */
122         }
123     }
124 
125     /***
126      * Get the value of layout.
127      *
128      * @return A String with the value of layout.
129      */
130     public String getLayoutTemplate()
131     {
132         String value = getString(TemplateInfo.LAYOUT_TEMPLATE);
133         return value;
134     }
135 
136     /***
137      * Set the value of layout.
138      *
139      * @param v Value to assign to layout.
140      */
141     public void setLayoutTemplate(String v)
142     {
143         setTemp(TemplateInfo.LAYOUT_TEMPLATE, v);
144     }
145 
146     /***
147      * Get the value of Template context.  This will be cast to the
148      * proper Context by its Service.
149      *
150      * @param name The name of the template context.
151      * @return An Object with the Value of context.
152      */
153     public Object getTemplateContext(String name)
154     {
155         return getTemp(name);
156     }
157 
158     /***
159      * Set the value of context.
160      *
161      * @param name The name of the template context.
162      * @param v Value to assign to context.
163      */
164     public void setTemplateContext(String name, Object v)
165     {
166         setTemp(name, v);
167     }
168 
169     /***
170      * Get the value of service.
171      *
172      * @return A String with the value of service.
173      */
174     public String getService()
175     {
176         return getString(TemplateInfo.SERVICE_NAME);
177     }
178 
179     /***
180      * Set the value of service.
181      *
182      * @param v Value to assign to service.
183      */
184     public void setService(String v)
185     {
186         setTemp(TemplateInfo.SERVICE_NAME, v);
187     }
188 
189     /***
190      * Get an object from temporary storage.
191      *
192      * @param name A String with the name of the object.
193      * @return An Object.
194      */
195     public Object getTemp(String name)
196     {
197         return tempStorage.get(name);
198     }
199 
200     /***
201      * Get an object from temporary storage, or a default value.
202      *
203      * @param name A String with the name of the object.
204      * @param def An Object, the default value.
205      * @return An Object.
206      */
207     public Object getTemp(String name, Object def)
208     {
209         try
210         {
211             Object val = tempStorage.get(name);
212             return (val != null) ? val : def;
213         }
214         catch (Exception e)
215         {
216             return def;
217         }
218     }
219 
220     /***
221      * Put an object into temporary storage.
222      *
223      * @param name A String with the name of the object.
224      * @param value An Object, the value.
225      */
226     public void setTemp(String name, Object value)
227     {
228         tempStorage.put(name, value);
229     }
230 
231     /***
232      * Return a String[] from the temp hash map.
233      *
234      * @param name A String with the name of the object.
235      * @return A String[].
236      */
237     public String[] getStringArray(String name)
238     {
239         String[] value = null;
240         Object object = getTemp(name, null);
241         if (object != null)
242         {
243             value = (String[]) object;
244         }
245         return value;
246     }
247 
248     /***
249      * Return a String from the temp hash map.
250      *
251      * @param name A String with the name of the object.
252      * @return A String.
253      */
254     public String getString(String name)
255     {
256         String value = null;
257         Object object = getTemp(name, null);
258         if (object != null)
259         {
260             value = (String) object;
261         }
262         return value;
263     }
264 
265     /***
266      * Remove an object from the  temporary storage.
267      *
268      * @param name A String with the name of the object.
269      * @return The object that was removed or <code>null</code>
270      *         if the name was not a key.
271      */
272     public Object removeTemp(String name)
273     {
274         return tempStorage.remove(name);
275     }
276 
277     /*
278      * Returns all the available names in the temporary storage.
279      *
280      * @return A object array with the keys.
281      */
282     public Object[] getTempKeys()
283     {
284         return tempStorage.keySet().toArray();
285     }
286 }