    001    package 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
    024    import java.util.HashMap;
    025    import java.util.Map;
    026
    027    import org.apache.turbine.services.template.TurbineTemplate;
    028    import org.apache.turbine.util.RunData;
    029    import 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 1073174 2011-02-21 22:18:45Z tv $
    041     */
    042    public class TemplateInfo
    043    {
    044
    045        /* Constants for tempStorage hash map. */
    046        public static final String NAVIGATION_TEMPLATE = "00navigation_template00";
    047        public static final String LAYOUT_TEMPLATE = "00layout_template00";
    048        public static final String SERVICE_NAME = "template_service";
    049
    050        /* Handle to the RunData object. */
    051        private RunData data = null;
    052
    053        /* Place to store information about templates. */
    054        private Map<String, Object> tempStorage = null;
    055
    056        /**
    057         * Constructor
    058         *
    059         * @param RunData A Turbine Rundata object.
    060         */
    061        public TemplateInfo(RunData data)
    062        {
    063            this.data = data;
    064            tempStorage = new HashMap<String, Object>(10);
    065        }
    066
    067        /**
    068         * Get the value of navigationTemplate.
    069         *
    070         * @return A String with the value of navigationTemplate.
    071         */
    072        public String getNavigationTemplate()
    073        {
    074            return getString(TemplateInfo.NAVIGATION_TEMPLATE);
    075        }
    076
    077        /**
    078         * Set the value of navigationTemplate.
    079         *
    080         * @param v Value to assign to navigationTemplate.
    081         */
    082        public void setNavigationTemplate(String v)
    083        {
    084            setTemp(TemplateInfo.NAVIGATION_TEMPLATE, v);
    085        }
    086
    087        /**
    088         * Get the value of screen for the RunData parameters.  This
    089         * information comes from PathInfo or a QueryString.
    090         *
    091         * @return A String with the value of screen.
    092         */
    093        public String getScreenTemplate()
    094        {
    095            return data.getParameters().getString(URIConstants.CGI_TEMPLATE_PARAM, null);
    096        }
    097
    098        /**
    099         * Set the value of screen.  This is really just a method to hide
    100         * using the RunData Parameter.
    101         *
    102         * @param v Value to assign to screen.
    103         */
    104        public void setScreenTemplate(String v)
    105        {
    106            data.getParameters().setString(URIConstants.CGI_TEMPLATE_PARAM, v);
    107
    108            // We have changed the screen template so
    109            // we should now update the layout template
    110            // as well. We will use the template service
    111            // to help us out.
    112            try
    113            {
    114                setLayoutTemplate(TurbineTemplate.getLayoutTemplateName(v));
    115            }
    116            catch (Exception e)
    117            {
    118                /*
    119                 * do nothing.
    120                 */
    121            }
    122        }
    123
    124        /**
    125         * Get the value of layout.
    126         *
    127         * @return A String with the value of layout.
    128         */
    129        public String getLayoutTemplate()
    130        {
    131            String value = getString(TemplateInfo.LAYOUT_TEMPLATE);
    132            return value;
    133        }
    134
    135        /**
    136         * Set the value of layout.
    137         *
    138         * @param v Value to assign to layout.
    139         */
    140        public void setLayoutTemplate(String v)
    141        {
    142            setTemp(TemplateInfo.LAYOUT_TEMPLATE, v);
    143        }
    144
    145        /**
    146         * Get the value of Template context.  This will be cast to the
    147         * proper Context by its Service.
    148         *
    149         * @param name The name of the template context.
    150         * @return An Object with the Value of context.
    151         */
    152        public Object getTemplateContext(String name)
    153        {
    154            return getTemp(name);
    155        }
    156
    157        /**
    158         * Set the value of context.
    159         *
    160         * @param name The name of the template context.
    161         * @param v Value to assign to context.
    162         */
    163        public void setTemplateContext(String name, Object v)
    164        {
    165            setTemp(name, v);
    166        }
    167
    168        /**
    169         * Get the value of service.
    170         *
    171         * @return A String with the value of service.
    172         */
    173        public String getService()
    174        {
    175            return getString(TemplateInfo.SERVICE_NAME);
    176        }
    177
    178        /**
    179         * Set the value of service.
    180         *
    181         * @param v Value to assign to service.
    182         */
    183        public void setService(String v)
    184        {
    185            setTemp(TemplateInfo.SERVICE_NAME, v);
    186        }
    187
    188        /**
    189         * Get an object from temporary storage.
    190         *
    191         * @param name A String with the name of the object.
    192         * @return An Object.
    193         */
    194        public Object getTemp(String name)
    195        {
    196            return tempStorage.get(name);
    197        }
    198
    199        /**
    200         * Get an object from temporary storage, or a default value.
    201         *
    202         * @param name A String with the name of the object.
    203         * @param def An Object, the default value.
    204         * @return An Object.
    205         */
    206        public Object getTemp(String name, Object def)
    207        {
    208            try
    209            {
    210                Object val = tempStorage.get(name);
    211                return (val != null) ? val : def;
    212            }
    213            catch (Exception e)
    214            {
    215                return def;
    216            }
    217        }
    218
    219        /**
    220         * Put an object into temporary storage.
    221         *
    222         * @param name A String with the name of the object.
    223         * @param value An Object, the value.
    224         */
    225        public void setTemp(String name, Object value)
    226        {
    227            tempStorage.put(name, value);
    228        }
    229
    230        /**
    231         * Return a String[] from the temp hash map.
    232         *
    233         * @param name A String with the name of the object.
    234         * @return A String[].
    235         */
    236        public String[] getStringArray(String name)
    237        {
    238            String[] value = null;
    239            Object object = getTemp(name, null);
    240            if (object != null)
    241            {
    242                value = (String[]) object;
    243            }
    244            return value;
    245        }
    246
    247        /**
    248         * Return a String from the temp hash map.
    249         *
    250         * @param name A String with the name of the object.
    251         * @return A String.
    252         */
    253        public String getString(String name)
    254        {
    255            String value = null;
    256            Object object = getTemp(name, null);
    257            if (object != null)
    258            {
    259                value = (String) object;
    260            }
    261            return value;
    262        }
    263
    264        /**
    265         * Remove an object from the  temporary storage.
    266         *
    267         * @param name A String with the name of the object.
    268         * @return The object that was removed or <code>null</code>
    269         *         if the name was not a key.
    270         */
    271        public Object removeTemp(String name)
    272        {
    273            return tempStorage.remove(name);
    274        }
    275
    276        /*
    277         * Returns all the available names in the temporary storage.
    278         *
    279         * @return A object array with the keys.
    280         */
    281        public Object[] getTempKeys()
    282        {
    283            return tempStorage.keySet().toArray();
    284        }
    285    }