1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts.taglib.html;
22
23 import org.apache.commons.beanutils.BeanUtils;
24 import org.apache.struts.Globals;
25 import org.apache.struts.taglib.TagUtils;
26 import org.apache.struts.util.MessageResources;
27
28 import javax.servlet.jsp.JspException;
29 import javax.servlet.jsp.PageContext;
30
31 import java.lang.reflect.InvocationTargetException;
32
33
34
35
36
37
38
39
40
41
42
43 public class MultiboxTag extends BaseHandlerTag {
44
45
46
47 protected static MessageResources messages =
48 MessageResources.getMessageResources(Constants.Package
49 + ".LocalStrings");
50
51
52
53
54
55
56
57 protected String constant = null;
58
59
60
61
62 protected String name = Constants.BEAN_KEY;
63
64
65
66
67 protected String property = null;
68
69
70
71
72
73 protected String value = null;
74
75 public String getName() {
76 return (this.name);
77 }
78
79 public void setName(String name) {
80 this.name = name;
81 }
82
83
84
85
86
87
88 public String getProperty() {
89 return (this.property);
90 }
91
92
93
94
95
96
97 public void setProperty(String property) {
98 this.property = property;
99 }
100
101
102
103
104 public String getValue() {
105 return (this.value);
106 }
107
108
109
110
111
112
113 public void setValue(String value) {
114 this.value = value;
115 }
116
117
118
119
120
121
122
123
124 public int doStartTag() throws JspException {
125
126 this.constant = null;
127
128 return (EVAL_BODY_TAG);
129 }
130
131
132
133
134
135
136
137 public int doAfterBody() throws JspException {
138 if (bodyContent != null) {
139 this.constant = bodyContent.getString().trim();
140 }
141
142 if ("".equals(this.constant)) {
143 this.constant = null;
144 }
145
146 return SKIP_BODY;
147 }
148
149
150
151
152
153
154 public int doEndTag() throws JspException {
155
156 StringBuffer results = new StringBuffer("<input type=\"checkbox\"");
157
158 prepareAttribute(results, "name", prepareName());
159 prepareAttribute(results, "accesskey", getAccesskey());
160 prepareAttribute(results, "tabindex", getTabindex());
161
162 String value = prepareValue(results);
163
164 prepareChecked(results, value);
165 results.append(prepareEventHandlers());
166 results.append(prepareStyles());
167 prepareOtherAttributes(results);
168 results.append(getElementClose());
169
170 TagUtils.getInstance().write(pageContext, results.toString());
171
172 return EVAL_PAGE;
173 }
174
175
176
177
178
179
180 protected String prepareName()
181 throws JspException {
182 return property;
183 }
184
185
186
187
188
189
190 protected String prepareValue(StringBuffer results)
191 throws JspException {
192 String value = (this.value == null) ? this.constant : this.value;
193
194 if (value == null) {
195 JspException e =
196 new JspException(messages.getMessage("multiboxTag.value"));
197
198 pageContext.setAttribute(Globals.EXCEPTION_KEY, e,
199 PageContext.REQUEST_SCOPE);
200 throw e;
201 }
202
203 prepareAttribute(results, "value", TagUtils.getInstance().filter(value));
204
205 return value;
206 }
207
208
209
210
211
212
213 protected void prepareChecked(StringBuffer results, String value)
214 throws JspException {
215 Object bean = TagUtils.getInstance().lookup(pageContext, name, null);
216 String[] values = null;
217
218 if (bean == null) {
219 throw new JspException(messages.getMessage("getter.bean", name));
220 }
221
222 try {
223 values = BeanUtils.getArrayProperty(bean, property);
224
225 if (values == null) {
226 values = new String[0];
227 }
228 } catch (IllegalAccessException e) {
229 throw new JspException(messages.getMessage("getter.access",
230 property, name));
231 } catch (InvocationTargetException e) {
232 Throwable t = e.getTargetException();
233
234 throw new JspException(messages.getMessage("getter.result",
235 property, t.toString()));
236 } catch (NoSuchMethodException e) {
237 throw new JspException(messages.getMessage("getter.method",
238 property, name));
239 }
240
241 for (int i = 0; i < values.length; i++) {
242 if (value.equals(values[i])) {
243 results.append(" checked=\"checked\"");
244
245 break;
246 }
247 }
248 }
249
250
251
252
253 public void release() {
254 super.release();
255 constant = null;
256 name = Constants.BEAN_KEY;
257 property = null;
258 value = null;
259 }
260 }