View Javadoc

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