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