1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.cubby.tags;
17
18 import static java.lang.Boolean.TRUE;
19 import static javax.servlet.jsp.PageContext.REQUEST_SCOPE;
20 import static org.seasar.cubby.CubbyConstants.ATTR_CONTEXT_PATH;
21 import static org.seasar.cubby.CubbyConstants.ATTR_ERRORS;
22 import static org.seasar.cubby.CubbyConstants.ATTR_PARAMS;
23 import static org.seasar.cubby.CubbyConstants.ATTR_VALIDATION_FAIL;
24 import static org.seasar.cubby.internal.util.LogMessages.format;
25
26 import java.util.Collection;
27 import java.util.Map;
28 import java.util.Map.Entry;
29
30 import javax.servlet.jsp.JspContext;
31 import javax.servlet.jsp.PageContext;
32 import javax.servlet.jsp.tagext.SimpleTag;
33 import javax.servlet.jsp.tagext.SimpleTagSupport;
34
35 import org.seasar.cubby.CubbyConstants;
36 import org.seasar.cubby.action.ActionErrors;
37 import org.seasar.cubby.controller.FormWrapper;
38 import org.seasar.cubby.internal.util.StringUtils;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42
43
44
45
46
47 class TagUtils {
48
49
50 private static final Logger logger = LoggerFactory
51 .getLogger(TagUtils.class);
52
53
54
55
56
57
58
59
60 public static ActionErrors errors(final JspContext context) {
61 return (ActionErrors) context.getAttribute(ATTR_ERRORS, REQUEST_SCOPE);
62 }
63
64
65
66
67
68
69
70
71
72
73 @SuppressWarnings("unchecked")
74 private static Object[] paramValues(final JspContext context,
75 final String name) {
76 final Map<String, Object[]> valuesMap = Map.class.cast(context
77 .getAttribute(ATTR_PARAMS, REQUEST_SCOPE));
78 final Object[] values;
79 if (valuesMap == null || !valuesMap.containsKey(name)) {
80 values = new Object[0];
81 } else {
82 values = valuesMap.get(name);
83 }
84 return values;
85 }
86
87
88
89
90
91
92
93
94
95
96
97
98 public static Object[] multipleFormValues(final JspContext context,
99 final FormWrapper formWrapper, final String name) {
100 return multipleFormValues(context, formWrapper, name, null);
101 }
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116 public static Object[] multipleFormValues(final JspContext context,
117 final FormWrapper formWrapper, final String name,
118 final String checkedValue) {
119 final Object[] values;
120 if (isValidationFail(context)) {
121 values = paramValues(context, name);
122 } else {
123 if (checkedValue != null) {
124 values = new Object[] { checkedValue };
125 } else {
126 if (!formWrapper.hasValues(name)) {
127 if (logger.isDebugEnabled()) {
128 logger.debug(format("DCUB0023", name));
129 }
130 return null;
131 }
132 values = formWrapper.getValues(name);
133 }
134 }
135 return values;
136 }
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153 public static Object formValue(final JspContext context,
154 final FormWrapper formWrapper, final String name,
155 final Integer index, final Object specifiedValue) {
156 final Object value;
157
158 if (isValidationFail(context)) {
159 if (specifiedValue == null) {
160 final Object[] values = paramValues(context, name);
161 value = value(values, index);
162 } else {
163 final Object[] values = paramValues(context, name);
164 if (values.length == 0) {
165 value = specifiedValue;
166 } else {
167 value = value(values, index);
168 }
169 }
170 } else {
171 if (specifiedValue != null) {
172 value = specifiedValue;
173 } else {
174 if (!formWrapper.hasValues(name)) {
175 logger.debug(format("DCUB0023", name));
176 return null;
177 }
178 value = value(formWrapper.getValues(name), index);
179 }
180 }
181
182 return value;
183 }
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198 private static Object value(final Object[] values, final Integer index) {
199 final Object value;
200 if (values == null) {
201 value = "";
202 } else {
203 if (index == null) {
204 value = getElement(values, 0);
205 } else {
206 value = getElement(values, index);
207 }
208 }
209 return value;
210 }
211
212
213
214
215
216
217
218
219
220
221
222
223
224 private static Object getElement(final Object[] values, final Integer index) {
225 final Object value;
226 if (values.length <= index) {
227 value = "";
228 } else {
229 value = values[index];
230 }
231 return value;
232 }
233
234
235
236
237
238
239
240
241
242 private static boolean isValidationFail(final JspContext context) {
243 return TRUE.equals(context.getAttribute(ATTR_VALIDATION_FAIL,
244 REQUEST_SCOPE));
245 }
246
247 public static final Object REMOVE_ATTRIBUTE = new Object();
248
249
250
251
252
253
254
255
256
257
258
259 public static String toAttr(final Map<String, Object> map) {
260 final StringBuilder builder = new StringBuilder();
261 for (final Entry<String, Object> entry : map.entrySet()) {
262 final String key = entry.getKey();
263 if (entry.getValue() == REMOVE_ATTRIBUTE) {
264 continue;
265 }
266 builder.append(key);
267 builder.append("=\"");
268 builder.append(escapeHtml(entry.getValue()));
269 builder.append("\" ");
270 }
271 return builder.toString();
272 }
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288 public static boolean contains(final Object obj, final String str) {
289 if (obj instanceof Collection<?>) {
290 return ((Collection<?>) obj).contains(str);
291 } else if (obj.getClass().isArray()) {
292 for (final Object value : (Object[]) obj) {
293 if (equalsAsString(value, str)) {
294 return true;
295 }
296 }
297 return false;
298 } else {
299 return equalsAsString(obj, str);
300 }
301 }
302
303
304
305
306
307
308
309
310
311
312
313 private static boolean equalsAsString(final Object obj1, final Object obj2) {
314 if (obj1 == obj2) {
315 return true;
316 } else if (obj1 == null) {
317 return false;
318 } else {
319 return obj1.toString().equals(obj2.toString());
320 }
321 }
322
323
324
325
326
327
328
329
330
331 public static void addCSSClassName(
332 final Map<String, Object> dynamicAttributes, final String className) {
333 String classValue = (String) dynamicAttributes.get("class");
334 if (StringUtils.isEmpty(classValue)) {
335 classValue = className;
336 } else {
337 classValue = classValue + " " + className;
338 }
339 dynamicAttributes.put("class", classValue);
340 }
341
342
343
344
345
346
347
348
349 public static FormWrapper getFormWrapper(final SimpleTag tag) {
350 final FormTag formTag = (FormTag) SimpleTagSupport
351 .findAncestorWithClass(tag, FormTag.class);
352 if (formTag == null) {
353 return null;
354 }
355 return formTag.getFormWrapper();
356 }
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395 public static String escapeHtml(final Object str) {
396 if (str == null) {
397 return "";
398 }
399 String text = str.toString();
400 text = StringUtils.replace(text, "&", "&");
401 text = StringUtils.replace(text, "<", "<");
402 text = StringUtils.replace(text, ">", ">");
403 text = StringUtils.replace(text, "\"", """);
404 text = StringUtils.replace(text, "'", "'");
405 return text;
406 }
407
408
409
410
411
412
413
414
415 public static String toString(final Object object) {
416 return object == null ? "" : object.toString();
417 }
418
419
420
421
422
423
424
425
426 public static String getContextPath(final JspContext jspContext) {
427 final String contextPath = (String) jspContext.getAttribute(
428 ATTR_CONTEXT_PATH, PageContext.REQUEST_SCOPE);
429 if ("/".equals(contextPath)) {
430 return "";
431 }
432 return contextPath;
433 }
434
435 }