1
2
3
4
5
6
7
8
9
10
11
12 package org.displaytag.util;
13
14 import java.util.HashMap;
15 import java.util.Iterator;
16 import java.util.Map;
17 import java.util.Set;
18 import java.util.StringTokenizer;
19
20 import org.apache.commons.lang.ObjectUtils;
21 import org.apache.commons.lang.StringEscapeUtils;
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.commons.lang.UnhandledException;
24 import org.apache.commons.lang.builder.EqualsBuilder;
25 import org.apache.commons.lang.builder.HashCodeBuilder;
26
27
28
29
30
31
32 public class DefaultHref implements Href
33 {
34
35
36
37
38 private static final long serialVersionUID = 899149338534L;
39
40
41
42
43 private String url;
44
45
46
47
48 private Map parameters;
49
50
51
52
53 private String anchor;
54
55
56
57
58
59 public DefaultHref(String baseUrl)
60 {
61 this.parameters = new HashMap();
62 setFullUrl(baseUrl);
63 }
64
65
66
67
68 public void setFullUrl(String baseUrl)
69 {
70 this.url = null;
71 this.anchor = null;
72 String noAnchorUrl;
73 int anchorposition = baseUrl.indexOf('#');
74
75
76 if (anchorposition != -1)
77 {
78 noAnchorUrl = baseUrl.substring(0, anchorposition);
79 this.anchor = baseUrl.substring(anchorposition + 1);
80 }
81 else
82 {
83 noAnchorUrl = baseUrl;
84 }
85
86 if (noAnchorUrl.indexOf('?') == -1)
87 {
88
89 this.url = noAnchorUrl;
90 return;
91 }
92
93
94 StringTokenizer tokenizer = new StringTokenizer(noAnchorUrl, "?");
95
96 if (baseUrl.startsWith("?"))
97 {
98
99 url = TagConstants.EMPTY_STRING;
100 }
101 else
102 {
103
104 url = tokenizer.nextToken();
105 }
106
107 if (!tokenizer.hasMoreTokens())
108 {
109 return;
110 }
111
112
113 StringTokenizer paramTokenizer = new StringTokenizer(tokenizer.nextToken(), "&");
114
115
116 while (paramTokenizer.hasMoreTokens())
117 {
118
119 String[] keyValue = StringUtils.split(paramTokenizer.nextToken(), '=');
120
121
122 String escapedKey = StringEscapeUtils.escapeHtml(keyValue[0]);
123 String escapedValue = keyValue.length > 1
124 ? StringEscapeUtils.escapeHtml(keyValue[1])
125 : TagConstants.EMPTY_STRING;
126
127 if (!this.parameters.containsKey(escapedKey))
128 {
129
130 this.parameters.put(escapedKey, escapedValue);
131 }
132 else
133 {
134
135 Object previousValue = this.parameters.get(escapedKey);
136 if (previousValue != null && previousValue.getClass().isArray())
137 {
138 Object[] previousArray = (Object[]) previousValue;
139 Object[] newArray = new Object[previousArray.length + 1];
140
141 int j;
142
143 for (j = 0; j < previousArray.length; j++)
144 {
145 newArray[j] = previousArray[j];
146 }
147
148 newArray[j] = escapedValue;
149 this.parameters.put(escapedKey, newArray);
150 }
151 else
152 {
153 this.parameters.put(escapedKey, new Object[]{previousValue, escapedValue});
154 }
155 }
156 }
157 }
158
159
160
161
162
163
164
165 public Href addParameter(String name, Object value)
166 {
167 this.parameters.put(name, ObjectUtils.toString(value, null));
168 return this;
169 }
170
171
172
173
174
175 public void removeParameter(String name)
176 {
177
178 this.parameters.remove(StringEscapeUtils.escapeHtml(name));
179 }
180
181
182
183
184
185
186
187 public Href addParameter(String name, int value)
188 {
189 this.parameters.put(name, new Integer(value));
190 return this;
191 }
192
193
194
195
196
197 public Map getParameterMap()
198 {
199 Map copyMap = new HashMap(this.parameters.size());
200 copyMap.putAll(this.parameters);
201 return copyMap;
202 }
203
204
205
206
207
208
209 public void setParameterMap(Map parametersMap)
210 {
211
212 this.parameters = new HashMap(parametersMap.size());
213
214
215 addParameterMap(parametersMap);
216 }
217
218
219
220
221
222
223 public void addParameterMap(Map parametersMap)
224 {
225
226 if (parametersMap == null)
227 {
228 return;
229 }
230
231
232 Iterator mapIterator = parametersMap.entrySet().iterator();
233 while (mapIterator.hasNext())
234 {
235 Map.Entry entry = (Map.Entry) mapIterator.next();
236 String key = StringEscapeUtils.escapeHtml((String) entry.getKey());
237
238
239 if (!this.parameters.containsKey(key))
240 {
241 Object value = entry.getValue();
242
243 if (value != null)
244 {
245 if (value.getClass().isArray())
246 {
247 String[] values = (String[]) value;
248 for (int i = 0; i < values.length; i++)
249 {
250 values[i] = StringEscapeUtils.escapeHtml(values[i]);
251 }
252 }
253 else
254 {
255 value = StringEscapeUtils.escapeHtml(value.toString());
256 }
257 }
258
259 this.parameters.put(key, value);
260 }
261 }
262 }
263
264
265
266
267
268 public String getBaseUrl()
269 {
270 return this.url;
271 }
272
273
274
275
276
277 public String getAnchor()
278 {
279 return this.anchor;
280 }
281
282
283
284
285
286 public void setAnchor(String name)
287 {
288 this.anchor = name;
289 }
290
291
292
293
294
295 public String toString()
296 {
297 StringBuffer buffer = new StringBuffer(30);
298
299 buffer.append(this.url);
300
301 if (this.parameters.size() > 0)
302 {
303 buffer.append('?');
304 Set parameterSet = this.parameters.entrySet();
305
306 Iterator iterator = parameterSet.iterator();
307
308 while (iterator.hasNext())
309 {
310 Map.Entry entry = (Map.Entry) iterator.next();
311
312 Object key = entry.getKey();
313 Object value = entry.getValue();
314
315 if (value == null)
316 {
317 buffer.append(key).append('=');
318 }
319 else if (value.getClass().isArray())
320 {
321 Object[] values = (Object[]) value;
322 for (int i = 0; i < values.length; i++)
323 {
324 if (i > 0)
325 {
326 buffer.append(TagConstants.AMPERSAND);
327 }
328
329 buffer.append(key).append('=').append(values[i]);
330 }
331 }
332 else
333 {
334 buffer.append(key).append('=').append(value);
335 }
336
337 if (iterator.hasNext())
338 {
339 buffer.append(TagConstants.AMPERSAND);
340 }
341 }
342 }
343
344 if (this.anchor != null)
345 {
346 buffer.append('#');
347 buffer.append(this.anchor);
348 }
349
350 return buffer.toString();
351 }
352
353
354
355
356 public Object clone()
357 {
358 final DefaultHref href;
359 try
360 {
361 href = (DefaultHref) super.clone();
362 }
363 catch (CloneNotSupportedException e)
364 {
365 throw new UnhandledException(e);
366 }
367
368 href.parameters = new HashMap(this.parameters);
369 return href;
370 }
371
372
373
374
375 public boolean equals(Object object)
376 {
377 if (!(object instanceof DefaultHref))
378 {
379 return false;
380 }
381 DefaultHref rhs = (DefaultHref) object;
382 return new EqualsBuilder().append(this.parameters, rhs.parameters).append(this.url, rhs.url).append(
383 this.anchor,
384 rhs.anchor).isEquals();
385 }
386
387
388
389
390 public int hashCode()
391 {
392 return new HashCodeBuilder(1313733113, -431360889)
393 .append(this.parameters)
394 .append(this.url)
395 .append(this.anchor)
396 .toHashCode();
397 }
398 }