View Javadoc

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