View Javadoc

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