    001    package org.apache.turbine.util.uri;
    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.Iterator;
    025
    026    import org.apache.commons.lang.StringUtils;
    027    import org.apache.fulcrum.parser.ParameterParser;
    028    import org.apache.turbine.util.RunData;
    029    import org.apache.turbine.util.ServerData;
    030
    031    /**
    032     * This class allows you to keep all the information needed for a single
    033     * link at one place. It keeps your query data, path info, the server
    034     * scheme, name, port and the script path. It is tuned for usage with a
    035     * Template System e.g. Velocity.
    036     *
    037     * If you must generate a Turbine Link in a Template System, use this class.
    038     *
    039     * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
    040     * @version $Id: TemplateURI.java 1071038 2011-02-15 20:32:27Z tv $
    041     */
    042
    043    public class TemplateURI
    044            extends TurbineURI
    045    {
    046        /**
    047         * Empty C'tor. Uses Turbine.getDefaultServerData().
    048         *
    049         */
    050        public TemplateURI()
    051        {
    052            super();
    053        }
    054
    055        /**
    056         * Constructor with a RunData object
    057         *
    058         * @param runData A RunData object
    059         */
    060        public TemplateURI(RunData runData)
    061        {
    062            super(runData);
    063        }
    064
    065        /**
    066         * Constructor, set explicit redirection
    067         *
    068         * @param runData A RunData object
    069         * @param redirect True if redirection allowed.
    070         */
    071        public TemplateURI(RunData runData, boolean redirect)
    072        {
    073            super(runData, redirect);
    074        }
    075
    076        /**
    077         * Constructor, set Template
    078         *
    079         * @param runData A RunData object
    080         * @param template A Template Name
    081         */
    082        public TemplateURI(RunData runData, String template)
    083        {
    084            super(runData);
    085            setTemplate(template);
    086        }
    087
    088        /**
    089         * Constructor, set Template, set explicit redirection
    090         *
    091         * @param runData A RunData object
    092         * @param template A Template Name
    093         * @param redirect True if redirection allowed.
    094         */
    095        public TemplateURI(RunData runData, String template, boolean redirect)
    096        {
    097            super(runData, redirect);
    098            setTemplate(template);
    099        }
    100
    101        /**
    102         * Constructor, set Template and Action
    103         *
    104         * @param runData A RunData object
    105         * @param template A Template Name
    106         * @param action An Action Name
    107         */
    108        public TemplateURI(RunData runData, String template, String action)
    109        {
    110            this(runData, template);
    111            setAction(action);
    112        }
    113
    114        /**
    115         * Constructor, set Template and Action, set explicit redirection
    116         *
    117         * @param runData A RunData object
    118         * @param template A Template Name
    119         * @param action An Action Name
    120         * @param redirect True if redirection allowed.
    121         */
    122        public TemplateURI(RunData runData, String template, String action, boolean redirect)
    123        {
    124            this(runData, template, redirect);
    125            setAction(action);
    126        }
    127
    128        /**
    129         * Constructor with a ServerData object
    130         *
    131         * @param serverData A ServerData object
    132         */
    133        public TemplateURI(ServerData serverData)
    134        {
    135            super(serverData);
    136        }
    137
    138        /**
    139         * Constructor, set explicit redirection
    140         *
    141         * @param serverData A ServerData object
    142         * @param redirect True if redirection allowed.
    143         */
    144        public TemplateURI(ServerData serverData, boolean redirect)
    145        {
    146            super(serverData, redirect);
    147        }
    148
    149        /**
    150         * Constructor, set Template
    151         *
    152         * @param serverData A ServerData object
    153         * @param template A Template Name
    154         */
    155        public TemplateURI(ServerData serverData, String template)
    156        {
    157            super(serverData);
    158            setTemplate(template);
    159        }
    160
    161        /**
    162         * Constructor, set Template, set explicit redirection
    163         *
    164         * @param serverData A ServerData object
    165         * @param template A Template Name
    166         * @param redirect True if redirection allowed.
    167         */
    168        public TemplateURI(ServerData serverData, String template, boolean redirect)
    169        {
    170            super(serverData, redirect);
    171            setTemplate(template);
    172        }
    173
    174        /**
    175         * Constructor, set Template and Action
    176         *
    177         * @param serverData A ServerData object
    178         * @param template A Template Name
    179         * @param action An Action Name
    180         */
    181        public TemplateURI(ServerData serverData, String template, String action)
    182        {
    183            this(serverData, template);
    184            setAction(action);
    185        }
    186
    187        /**
    188         * Constructor, set Template and Action, set explicit redirection
    189         *
    190         * @param serverData A ServerData object
    191         * @param template A Template Name
    192         * @param action An Action Name
    193         * @param redirect True if redirection allowed.
    194         */
    195        public TemplateURI(ServerData serverData, String template, String action, boolean redirect)
    196        {
    197            this(serverData, template, redirect);
    198            setAction(action);
    199        }
    200
    201        /**
    202         * Constructor, user Turbine.getDefaultServerData(), set Template and Action
    203         *
    204         * @param template A Template Name
    205         * @param action An Action Name
    206         */
    207        public TemplateURI(String template, String action)
    208        {
    209            this();
    210            setTemplate(template);
    211            setAction(action);
    212        }
    213
    214        /**
    215         * Sets the template= value for this URL.
    216         *
    217         * By default it adds the information to the path_info instead
    218         * of the query data. An empty value (null or "") cleans out
    219         * an existing value.
    220         *
    221         * @param template A String with the template value.
    222         */
    223        public void setTemplate(String template)
    224        {
    225            if(StringUtils.isNotEmpty(template))
    226            {
    227                add(PATH_INFO, CGI_TEMPLATE_PARAM, template);
    228            }
    229            else
    230            {
    231                clearTemplate();
    232            }
    233        }
    234
    235        /**
    236         * Clears the template= value for this URL.
    237         *
    238         */
    239        public void clearTemplate()
    240        {
    241            removePathInfo(CGI_TEMPLATE_PARAM);
    242        }
    243
    244        /*
    245         * ========================================================================
    246         *
    247         * Protected / Private Methods
    248         *
    249         * ========================================================================
    250         *
    251         */
    252
    253        /**
    254         * Method for a quick way to add all the parameters in a
    255         * ParameterParser.
    256         *
    257         * <p>If the type is P (0), then add name/value to the pathInfo
    258         * hashtable.
    259         *
    260         * <p>If the type is Q (1), then add name/value to the queryData
    261         * hashtable.
    262         *
    263         * @param type Type of insertion (@see #add(char type, String name, String value))
    264         * @param pp A ParameterParser.
    265         */
    266        protected void add(int type,
    267                ParameterParser pp)
    268        {
    269            for(Iterator<?> it = pp.keySet().iterator(); it.hasNext();)
    270            {
    271                String key = (String) it.next();
    272
    273                if (!key.equalsIgnoreCase(CGI_ACTION_PARAM) &&
    274                        !key.equalsIgnoreCase(CGI_SCREEN_PARAM) &&
    275                        !key.equalsIgnoreCase(CGI_TEMPLATE_PARAM))
    276                {
    277                    String[] values = pp.getStrings(key);
    278                    if(values != null)
    279                    {
    280                        for (int i = 0; i < values.length; i++)
    281                        {
    282                            add(type, key, values[i]);
    283                        }
    284                    }
    285                    else
    286                    {
    287                        add(type, key, "");
    288                    }
    289                }
    290            }
    291        }
    292    }