001package org.apache.turbine.util.template;
002
003
004/*
005 * Licensed to the Apache Software Foundation (ASF) under one
006 * or more contributor license agreements.  See the NOTICE file
007 * distributed with this work for additional information
008 * regarding copyright ownership.  The ASF licenses this file
009 * to you under the Apache License, Version 2.0 (the
010 * "License"); you may not use this file except in compliance
011 * with the License.  You may obtain a copy of the License at
012 *
013 *   http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing,
016 * software distributed under the License is distributed on an
017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
018 * KIND, either express or implied.  See the License for the
019 * specific language governing permissions and limitations
020 * under the License.
021 */
022
023
024import java.util.HashMap;
025import java.util.Map;
026
027import org.apache.turbine.services.TurbineServices;
028import org.apache.turbine.services.template.TemplateService;
029import org.apache.turbine.util.RunData;
030import org.apache.turbine.util.uri.URIConstants;
031
032
033/**
034 * This is a wrapper for Template specific information.  It's part of
035 * the RunData object and can extract the information it needs to do
036 * the job directly from the data.getParameters().
037 *
038 * @author <a href="mailto:mbryson@mindspring.com">Dave Bryson</a>
039 * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
040 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
041 * @version $Id$
042 */
043public class TemplateInfo
044{
045
046    /** Constants for tempStorage hash map. */
047    public static final String NAVIGATION_TEMPLATE = "00navigation_template00";
048    /** Constants for tempStorage hash map. */
049    public static final String LAYOUT_TEMPLATE = "00layout_template00";
050    /** Constants for tempStorage hash map. */
051    public static final String SERVICE_NAME = "template_service";
052
053    /* Handle to the RunData object. */
054    private RunData data = null;
055
056    /* Place to store information about templates. */
057    private Map<String, Object> tempStorage = null;
058
059    /**
060     * Constructor
061     *
062     * @param data A Turbine RunData object.
063     */
064    public TemplateInfo(RunData data)
065    {
066        this.data = data;
067        tempStorage = new HashMap<>(10);
068    }
069
070    /**
071     * Get the value of navigationTemplate.
072     *
073     * @return A String with the value of navigationTemplate.
074     */
075    public String getNavigationTemplate()
076    {
077        return getString(TemplateInfo.NAVIGATION_TEMPLATE);
078    }
079
080    /**
081     * Set the value of navigationTemplate.
082     *
083     * @param v Value to assign to navigationTemplate.
084     */
085    public void setNavigationTemplate(String v)
086    {
087        setTemp(TemplateInfo.NAVIGATION_TEMPLATE, v);
088    }
089
090    /**
091     * Get the value of screen for the RunData parameters.  This
092     * information comes from PathInfo or a QueryString.
093     *
094     * @return A String with the value of screen.
095     */
096    public String getScreenTemplate()
097    {
098        return data.getParameters().getString(URIConstants.CGI_TEMPLATE_PARAM, null);
099    }
100
101    /**
102     * Set the value of screen.  This is really just a method to hide
103     * using the RunData Parameter.
104     *
105     * @param v Value to assign to screen.
106     */
107    public void setScreenTemplate(String v)
108    {
109        data.getParameters().setString(URIConstants.CGI_TEMPLATE_PARAM, v);
110
111        // We have changed the screen template so
112        // we should now update the layout template
113        // as well. We will use the template service
114        // to help us out.
115        try
116        {
117            TemplateService templateService = (TemplateService)TurbineServices.getInstance().getService(TemplateService.SERVICE_NAME);
118            setLayoutTemplate(templateService.getLayoutTemplateName(v));
119        }
120        catch (Exception e)
121        {
122            /*
123             * do nothing.
124             */
125        }
126    }
127
128    /**
129     * Get the value of layout.
130     *
131     * @return A String with the value of layout.
132     */
133    public String getLayoutTemplate()
134    {
135        String value = getString(TemplateInfo.LAYOUT_TEMPLATE);
136        return value;
137    }
138
139    /**
140     * Set the value of layout.
141     *
142     * @param v Value to assign to layout.
143     */
144    public void setLayoutTemplate(String v)
145    {
146        setTemp(TemplateInfo.LAYOUT_TEMPLATE, v);
147    }
148
149    /**
150     * Get the value of Template context.  This will be cast to the
151     * proper Context by its Service.
152     *
153     * @param name The name of the template context.
154     * @return An Object with the Value of context.
155     */
156    public Object getTemplateContext(String name)
157    {
158        return getTemp(name);
159    }
160
161    /**
162     * Set the value of context.
163     *
164     * @param name The name of the template context.
165     * @param v Value to assign to context.
166     */
167    public void setTemplateContext(String name, Object v)
168    {
169        setTemp(name, v);
170    }
171
172    /**
173     * Get the value of service.
174     *
175     * @return A String with the value of service.
176     */
177    public String getService()
178    {
179        return getString(TemplateInfo.SERVICE_NAME);
180    }
181
182    /**
183     * Set the value of service.
184     *
185     * @param v Value to assign to service.
186     */
187    public void setService(String v)
188    {
189        setTemp(TemplateInfo.SERVICE_NAME, v);
190    }
191
192    /**
193     * Get an object from temporary storage.
194     *
195     * @param name A String with the name of the object.
196     * @return An Object.
197     */
198    public Object getTemp(String name)
199    {
200        return tempStorage.get(name);
201    }
202
203    /**
204     * Get an object from temporary storage, or a default value.
205     *
206     * @param name A String with the name of the object.
207     * @param def An Object, the default value.
208     * @return An Object.
209     */
210    public Object getTemp(String name, Object def)
211    {
212        try
213        {
214            Object val = tempStorage.get(name);
215            return (val != null) ? val : def;
216        }
217        catch (Exception e)
218        {
219            return def;
220        }
221    }
222
223    /**
224     * Put an object into temporary storage.
225     *
226     * @param name A String with the name of the object.
227     * @param value An Object, the value.
228     */
229    public void setTemp(String name, Object value)
230    {
231        tempStorage.put(name, value);
232    }
233
234    /**
235     * Return a String[] from the temp hash map.
236     *
237     * @param name A String with the name of the object.
238     * @return A String[].
239     */
240    public String[] getStringArray(String name)
241    {
242        String[] value = null;
243        Object object = getTemp(name, null);
244        if (object != null)
245        {
246            value = (String[]) object;
247        }
248        return value;
249    }
250
251    /**
252     * Return a String from the temp hash map.
253     *
254     * @param name A String with the name of the object.
255     * @return A String.
256     */
257    public String getString(String name)
258    {
259        String value = null;
260        Object object = getTemp(name, null);
261        if (object != null)
262        {
263            value = (String) object;
264        }
265        return value;
266    }
267
268    /**
269     * Remove an object from the  temporary storage.
270     *
271     * @param name A String with the name of the object.
272     * @return The object that was removed or <code>null</code>
273     *         if the name was not a key.
274     */
275    public Object removeTemp(String name)
276    {
277        return tempStorage.remove(name);
278    }
279
280    /**
281     * Returns all the available names in the temporary storage.
282     *
283     * @return A object array with the keys.
284     */
285    public Object[] getTempKeys()
286    {
287        return tempStorage.keySet().toArray();
288    }
289}