1 package org.apache.turbine.util.uri;
2
3
4 /*
5 * Licensed to the Apache Software Foundation (ASF) under one
6 * or more contributor license agreements. See the NOTICE file
7 * distributed with this work for additional information
8 * regarding copyright ownership. The ASF licenses this file
9 * to you under the Apache License, Version 2.0 (the
10 * "License"); you may not use this file except in compliance
11 * with the License. You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing,
16 * software distributed under the License is distributed on an
17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 * KIND, either express or implied. See the License for the
19 * specific language governing permissions and limitations
20 * under the License.
21 */
22
23
24 import java.util.Iterator;
25
26 import org.apache.commons.lang.StringUtils;
27 import org.apache.fulcrum.parser.ParameterParser;
28 import org.apache.turbine.util.RunData;
29 import org.apache.turbine.util.ServerData;
30
31 /**
32 * This class allows you to keep all the information needed for a single
33 * link at one place. It keeps your query data, path info, the server
34 * scheme, name, port and the script path. It is tuned for usage with a
35 * Template System e.g. Velocity.
36 *
37 * If you must generate a Turbine Link in a Template System, use this class.
38 *
39 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
40 * @version $Id: TemplateURI.java 1071038 2011-02-15 20:32:27Z tv $
41 */
42
43 public class TemplateURI
44 extends TurbineURI
45 {
46 /**
47 * Empty C'tor. Uses Turbine.getDefaultServerData().
48 *
49 */
50 public TemplateURI()
51 {
52 super();
53 }
54
55 /**
56 * Constructor with a RunData object
57 *
58 * @param runData A RunData object
59 */
60 public TemplateURI(RunData runData)
61 {
62 super(runData);
63 }
64
65 /**
66 * Constructor, set explicit redirection
67 *
68 * @param runData A RunData object
69 * @param redirect True if redirection allowed.
70 */
71 public TemplateURI(RunData runData, boolean redirect)
72 {
73 super(runData, redirect);
74 }
75
76 /**
77 * Constructor, set Template
78 *
79 * @param runData A RunData object
80 * @param template A Template Name
81 */
82 public TemplateURI(RunData runData, String template)
83 {
84 super(runData);
85 setTemplate(template);
86 }
87
88 /**
89 * Constructor, set Template, set explicit redirection
90 *
91 * @param runData A RunData object
92 * @param template A Template Name
93 * @param redirect True if redirection allowed.
94 */
95 public TemplateURI(RunData runData, String template, boolean redirect)
96 {
97 super(runData, redirect);
98 setTemplate(template);
99 }
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 }