    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.template.TurbineTemplate;
    028import org.apache.turbine.util.RunData;
    029import org.apache.turbine.util.uri.URIConstants;
    030
    031
    032/**
    033 * This is a wrapper for Template specific information.  It's part of
    034 * the RunData object and can extract the information it needs to do
    035 * the job directly from the data.getParameters().
    036 *
    037 * @author <a href="mailto:mbryson@mindspring.com">Dave Bryson</a>
    038 * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
    039 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
    040 * @version $Id: TemplateInfo.java 1706239 2015-10-01 13:18:35Z tv $
    041 */
    042public class TemplateInfo
    043{
    044
    045    /** Constants for tempStorage hash map. */
    046    public static final String NAVIGATION_TEMPLATE = "00navigation_template00";
    047    /** Constants for tempStorage hash map. */
    048    public static final String LAYOUT_TEMPLATE = "00layout_template00";
    049    /** Constants for tempStorage hash map. */
    050    public static final String SERVICE_NAME = "template_service";
    051
    052    /* Handle to the RunData object. */
    053    private RunData data = null;
    054
    055    /* Place to store information about templates. */
    056    private Map<String, Object> tempStorage = null;
    057
    058    /**
    059     * Constructor
    060     *
    061     * @param data A Turbine RunData object.
    062     */
    063    public TemplateInfo(RunData data)
    064    {
    065        this.data = data;
    066        tempStorage = new HashMap<String, Object>(10);
    067    }
    068
    069    /**
    070     * Get the value of navigationTemplate.
    071     *
    072     * @return A String with the value of navigationTemplate.
    073     */
    074    public String getNavigationTemplate()
    075    {
    076        return getString(TemplateInfo.NAVIGATION_TEMPLATE);
    077    }
    078
    079    /**
    080     * Set the value of navigationTemplate.
    081     *
    082     * @param v Value to assign to navigationTemplate.
    083     */
    084    public void setNavigationTemplate(String v)
    085    {
    086        setTemp(TemplateInfo.NAVIGATION_TEMPLATE, v);
    087    }
    088
    089    /**
    090     * Get the value of screen for the RunData parameters.  This
    091     * information comes from PathInfo or a QueryString.
    092     *
    093     * @return A String with the value of screen.
    094     */
    095    public String getScreenTemplate()
    096    {
    097        return data.getParameters().getString(URIConstants.CGI_TEMPLATE_PARAM, null);
    098    }
    099
    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}