View Javadoc

1   package org.apache.turbine.util.uri;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.Iterator;
20  
21  import org.apache.commons.lang.StringUtils;
22  
23  import org.apache.turbine.util.RunData;
24  import org.apache.turbine.util.ServerData;
25  import org.apache.turbine.util.parser.ParameterParser;
26  
27  /***
28   * This class allows you to keep all the information needed for a single
29   * link at one place. It keeps your query data, path info, the server
30   * scheme, name, port and the script path. It is tuned for usage with a
31   * Template System e.g. Velocity.
32   *
33   * If you must generate a Turbine Link in a Template System, use this class.
34   *
35   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
36   * @version $Id: TemplateURI.java 264148 2005-08-29 14:21:04Z henning $
37   */
38  
39  public class TemplateURI
40          extends TurbineURI
41  {
42      /***
43       * Empty C'tor. Uses Turbine.getDefaultServerData().
44       *
45       */
46      public TemplateURI()
47      {
48          super();
49      }
50  
51      /***
52       * Constructor with a RunData object
53       *
54       * @param runData A RunData object
55       */
56      public TemplateURI(RunData runData)
57      {
58          super(runData);
59      }
60  
61      /***
62       * Constructor, set explicit redirection
63       *
64       * @param runData A RunData object
65       * @param redirect True if redirection allowed.
66       */
67      public TemplateURI(RunData runData, boolean redirect)
68      {
69          super(runData, redirect);
70      }
71  
72      /***
73       * Constructor, set Template
74       *
75       * @param runData A RunData object
76       * @param template A Template Name
77       */
78      public TemplateURI(RunData runData, String template)
79      {
80          super(runData);
81          setTemplate(template);
82      }
83  
84      /***
85       * Constructor, set Template, set explicit redirection
86       *
87       * @param runData A RunData object
88       * @param template A Template Name
89       * @param redirect True if redirection allowed.
90       */
91      public TemplateURI(RunData runData, String template, boolean redirect)
92      {
93          super(runData, redirect);
94          setTemplate(template);
95      }
96  
97      /***
98       * Constructor, set Template and Action
99       *
100      * @param runData A RunData object
101      * @param template A Template Name
102      * @param action An Action Name
103      */
104     public TemplateURI(RunData runData, String template, String action)
105     {
106         this(runData, template);
107         setAction(action);
108     }
109 
110     /***
111      * Constructor, set Template and Action, set explicit redirection
112      *
113      * @param runData A RunData object
114      * @param template A Template Name
115      * @param action An Action Name
116      * @param redirect True if redirection allowed.
117      */
118     public TemplateURI(RunData runData, String template, String action, boolean redirect)
119     {
120         this(runData, template, redirect);
121         setAction(action);
122     }
123 
124     /***
125      * Constructor with a ServerData object
126      *
127      * @param serverData A ServerData object
128      */
129     public TemplateURI(ServerData serverData)
130     {
131         super(serverData);
132     }
133 
134     /***
135      * Constructor, set explicit redirection
136      *
137      * @param serverData A ServerData object
138      * @param redirect True if redirection allowed.
139      */
140     public TemplateURI(ServerData serverData, boolean redirect)
141     {
142         super(serverData, redirect);
143     }
144 
145     /***
146      * Constructor, set Template
147      *
148      * @param serverData A ServerData object
149      * @param template A Template Name
150      */
151     public TemplateURI(ServerData serverData, String template)
152     {
153         super(serverData);
154         setTemplate(template);
155     }
156 
157     /***
158      * Constructor, set Template, set explicit redirection
159      *
160      * @param serverData A ServerData object
161      * @param template A Template Name
162      * @param redirect True if redirection allowed.
163      */
164     public TemplateURI(ServerData serverData, String template, boolean redirect)
165     {
166         super(serverData, redirect);
167         setTemplate(template);
168     }
169 
170     /***
171      * Constructor, set Template and Action
172      *
173      * @param serverData A ServerData object
174      * @param template A Template Name
175      * @param action An Action Name
176      */
177     public TemplateURI(ServerData serverData, String template, String action)
178     {
179         this(serverData, template);
180         setAction(action);
181     }
182 
183     /***
184      * Constructor, set Template and Action, set explicit redirection
185      *
186      * @param serverData A ServerData object
187      * @param template A Template Name
188      * @param action An Action Name
189      * @param redirect True if redirection allowed.
190      */
191     public TemplateURI(ServerData serverData, String template, String action, boolean redirect)
192     {
193         this(serverData, template, redirect);
194         setAction(action);
195     }
196 
197     /***
198      * Constructor, user Turbine.getDefaultServerData(), set Template and Action
199      *
200      * @param template A Template Name
201      * @param action An Action Name
202      */
203     public TemplateURI(String template, String action)
204     {
205         this();
206         setTemplate(template);
207         setAction(action);
208     }
209 
210     /***
211      * Sets the template= value for this URL.
212      *
213      * By default it adds the information to the path_info instead
214      * of the query data. An empty value (null or "") cleans out
215      * an existing value.
216      *
217      * @param template A String with the template value.
218      */
219     public void setTemplate(String template)
220     {
221         if(StringUtils.isNotEmpty(template))
222         {
223             add(PATH_INFO, CGI_TEMPLATE_PARAM, template);
224         }
225         else
226         {
227             clearTemplate();
228         }
229     }
230 
231     /***
232      * Clears the template= value for this URL.
233      *
234      */
235     public void clearTemplate()
236     {
237         removePathInfo(CGI_TEMPLATE_PARAM);
238     }
239 
240     /*
241      * ========================================================================
242      *
243      * Protected / Private Methods
244      *
245      * ========================================================================
246      *
247      */
248 
249     /***
250      * Method for a quick way to add all the parameters in a
251      * ParameterParser.
252      *
253      * <p>If the type is P (0), then add name/value to the pathInfo
254      * hashtable.
255      *
256      * <p>If the type is Q (1), then add name/value to the queryData
257      * hashtable.
258      *
259      * @param type Type of insertion (@see #add(char type, String name, String value))
260      * @param pp A ParameterParser.
261      */
262     protected void add(int type,
263             ParameterParser pp)
264     {
265         for(Iterator it = pp.keySet().iterator(); it.hasNext();)
266         {
267             String key = (String) it.next();
268 
269             if (!key.equalsIgnoreCase(CGI_ACTION_PARAM) &&
270                     !key.equalsIgnoreCase(CGI_SCREEN_PARAM) &&
271                     !key.equalsIgnoreCase(CGI_TEMPLATE_PARAM))
272             {
273                 String[] values = pp.getStrings(key);
274                 for (int i = 0; i < values.length; i++)
275                 {
276                     add(type, key, values[i]);
277                 }
278             }
279         }
280     }
281 }