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