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