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