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