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_OUTPUT_VALUES;
21 import static org.seasar.cubby.CubbyConstants.ATTR_PARAMS;
22 import static org.seasar.cubby.CubbyConstants.ATTR_VALIDATION_FAIL;
23
24 import java.util.Collection;
25 import java.util.Map;
26 import java.util.Map.Entry;
27
28 import javax.servlet.jsp.JspContext;
29 import javax.servlet.jsp.PageContext;
30
31 import org.seasar.cubby.action.ActionErrors;
32 import org.seasar.cubby.util.CubbyFunctions;
33 import org.seasar.framework.util.StringUtil;
34
35
36
37
38
39
40 class TagUtils {
41
42 public static ActionErrors errors(final JspContext context) {
43 return (ActionErrors) context.getAttribute("errors", REQUEST_SCOPE);
44 }
45
46 @SuppressWarnings("unchecked")
47 private static Object[] paramValues(final JspContext context,
48 final String name) {
49 final Map<String, Object[]> valuesMap = (Map<String, Object[]>) context
50 .getAttribute(ATTR_PARAMS, REQUEST_SCOPE);
51 final Object[] values;
52 if (valuesMap == null || !valuesMap.containsKey(name)) {
53 values = new Object[0];
54 } else {
55 values = valuesMap.get(name);
56 }
57 return values;
58 }
59
60 private static Object[] formValues(final Map<String, String[]> valuesMap,
61 final String name) {
62 final Object[] values;
63 if (valuesMap == null || !valuesMap.containsKey(name)) {
64 values = new Object[0];
65 } else {
66 values = valuesMap.get(name);
67 }
68 return values;
69 }
70
71 public static Object[] multipleFormValues(final JspContext context,
72 final Map<String, String[]> outputValuesMap, final String name) {
73 return multipleFormValues(context, outputValuesMap, name, null);
74 }
75
76 public static Object[] multipleFormValues(final JspContext context,
77 final Map<String, String[]> outputValuesMap, final String name,
78 final String checkedValue) {
79 final Object[] values;
80 if (isValidationFail(context)) {
81 values = paramValues(context, name);
82 } else {
83 if (checkedValue != null) {
84 values = new Object[] { checkedValue };
85 } else {
86 values = formValues(outputValuesMap, name);
87 }
88 }
89 return values;
90 }
91
92 public static Object formValue(final JspContext context,
93 final Map<String, String[]> outputValuesMap, final String name,
94 final Integer index, final Object specifiedValue) {
95 final Object value;
96
97 if (isValidationFail(context)) {
98 final Object[] values = paramValues(context, name);
99 value = value(values, index);
100 } else {
101 if (specifiedValue == null) {
102 final Object[] values = formValues(outputValuesMap, name);
103 value = value(values, index);
104 } else {
105 value = specifiedValue;
106 }
107 }
108
109 return value;
110 }
111
112 private static Object value(final Object[] values, final Integer index) {
113 final Object value;
114 if (values == null) {
115 value = "";
116 } else {
117 if (index == null) {
118 value = getElement(values, 0);
119 } else {
120 value = getElement(values, index);
121 }
122 }
123 return value;
124 }
125
126 private static Object getElement(final Object[] values, final Integer index) {
127 final Object value;
128 if (values.length <= index) {
129 value = "";
130 } else {
131 value = values[index];
132 }
133 return value;
134 }
135
136 private static boolean isValidationFail(final JspContext context) {
137 return TRUE.equals(context.getAttribute(ATTR_VALIDATION_FAIL,
138 REQUEST_SCOPE));
139 }
140
141 @SuppressWarnings("unchecked")
142 public static Map<String, String[]> outputValues(final JspContext context) {
143 final Map<String, String[]> outputValues = (Map<String, String[]>) context
144 .getAttribute(ATTR_OUTPUT_VALUES, PageContext.PAGE_SCOPE);
145 return outputValues;
146 }
147
148 public static String toAttr(final Map<String, Object> map) {
149 final StringBuilder sb = new StringBuilder();
150 for (final Entry<String, Object> entry : map.entrySet()) {
151 final String key = entry.getKey();
152 if ("value".equals(key) || "checkedValue".equals(key)) {
153 continue;
154 }
155 sb.append(key);
156 sb.append("=\"");
157 sb.append(CubbyFunctions.escapeHtml(entry.getValue()));
158 sb.append("\" ");
159 }
160 return sb.toString();
161 }
162
163 public static boolean isChecked(final String value, final Object values) {
164 if (values instanceof Collection) {
165 return ((Collection<?>) values).contains(value);
166 } else if (values.getClass().isArray()) {
167 for (final Object v : (Object[]) values) {
168 if (equalsValue(v, value)) {
169 return true;
170 }
171 }
172 return false;
173 } else {
174 return equalsValue(values, value);
175 }
176 }
177
178 private static boolean equalsValue(final Object values, final Object value) {
179 if (values == value) {
180 return true;
181 } else if (values == null) {
182 return false;
183 } else {
184 return values.toString().equals(value.toString());
185 }
186 }
187
188 public static void addClassName(final Map<String, Object> dyn, final String className) {
189 String classValue = (String) dyn.get("class");
190 if (StringUtil.isEmpty(classValue)) {
191 classValue = className;
192 } else {
193 classValue = classValue + " " + className;
194 }
195 dyn.put("class", classValue);
196 }
197
198 }