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