Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
TemplateURI |
|
| 1.411764705882353;1,412 |
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 | 0 | super(); |
53 | 0 | } |
54 | ||
55 | /** | |
56 | * Constructor with a RunData object | |
57 | * | |
58 | * @param runData A RunData object | |
59 | */ | |
60 | public TemplateURI(RunData runData) | |
61 | { | |
62 | 9 | super(runData); |
63 | 9 | } |
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 | 0 | super(runData, redirect); |
74 | 0 | } |
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 | 0 | super(runData); |
85 | 0 | setTemplate(template); |
86 | 0 | } |
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 | 0 | super(runData, redirect); |
98 | 0 | setTemplate(template); |
99 | 0 | } |
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 | 0 | this(runData, template); |
111 | 0 | setAction(action); |
112 | 0 | } |
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 | 0 | this(runData, template, redirect); |
125 | 0 | setAction(action); |
126 | 0 | } |
127 | ||
128 | /** | |
129 | * Constructor with a ServerData object | |
130 | * | |
131 | * @param serverData A ServerData object | |
132 | */ | |
133 | public TemplateURI(ServerData serverData) | |
134 | { | |
135 | 0 | super(serverData); |
136 | 0 | } |
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 | 0 | super(serverData, redirect); |
147 | 0 | } |
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 | 0 | super(serverData); |
158 | 0 | setTemplate(template); |
159 | 0 | } |
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 | 0 | super(serverData, redirect); |
171 | 0 | setTemplate(template); |
172 | 0 | } |
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 | 0 | this(serverData, template); |
184 | 0 | setAction(action); |
185 | 0 | } |
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 | 0 | this(serverData, template, redirect); |
198 | 0 | setAction(action); |
199 | 0 | } |
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 | 0 | this(); |
210 | 0 | setTemplate(template); |
211 | 0 | setAction(action); |
212 | 0 | } |
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 | 0 | if(StringUtils.isNotEmpty(template)) |
226 | { | |
227 | 0 | add(PATH_INFO, CGI_TEMPLATE_PARAM, template); |
228 | } | |
229 | else | |
230 | { | |
231 | 0 | clearTemplate(); |
232 | } | |
233 | 0 | } |
234 | ||
235 | /** | |
236 | * Clears the template= value for this URL. | |
237 | * | |
238 | */ | |
239 | public void clearTemplate() | |
240 | { | |
241 | 0 | removePathInfo(CGI_TEMPLATE_PARAM); |
242 | 0 | } |
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 | 0 | for(Iterator<?> it = pp.keySet().iterator(); it.hasNext();) |
270 | { | |
271 | 0 | String key = (String) it.next(); |
272 | ||
273 | 0 | if (!key.equalsIgnoreCase(CGI_ACTION_PARAM) && |
274 | !key.equalsIgnoreCase(CGI_SCREEN_PARAM) && | |
275 | !key.equalsIgnoreCase(CGI_TEMPLATE_PARAM)) | |
276 | { | |
277 | 0 | String[] values = pp.getStrings(key); |
278 | 0 | if(values != null) |
279 | { | |
280 | 0 | for (int i = 0; i < values.length; i++) |
281 | { | |
282 | 0 | add(type, key, values[i]); |
283 | } | |
284 | } | |
285 | else | |
286 | { | |
287 | 0 | add(type, key, ""); |
288 | } | |
289 | } | |
290 | 0 | } |
291 | 0 | } |
292 | } |