KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > servlet > HtmlFormUtil


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.servlet;
4
5 import java.util.List JavaDoc;
6 import java.util.Map JavaDoc;
7
8 /**
9  * Various utilities for raw population of HTML forms.
10  * Text encoding is provided by {@link HtmlEncode} class.
11  * Generally, it is advisible to use Objects as attributes, not primitives,
12  * althought it would be possible to use them.
13  */

14 public class HtmlFormUtil {
15
16     private static final String JavaDoc EMPTY = "";
17     private static final String JavaDoc CHECKED = "checked";
18     private static final String JavaDoc IGNORE = " _i=\"";
19     private static final String JavaDoc TRUE = "true";
20     private static final String JavaDoc ON = "on";
21     private static final String JavaDoc YES = "yes";
22     private static final String JavaDoc ENDQUOTE = "\" ";
23     private static final String JavaDoc SELECTED = "selected";
24
25     // ---------------------------------------------------------------- array
26

27     /**
28      * Performs safe array lookup.
29      */

30     public static Object JavaDoc array(Object JavaDoc[] array, int index) {
31         if ((array == null) || (index >= array.length) || (index < 0)) {
32             return null;
33         }
34         return array[index];
35     }
36     public static int array(int[] array, int index) {
37         if ((array == null) || (index >= array.length) || (index < 0)) {
38             return 0;
39         }
40         return array[index];
41     }
42     public static long array(long[] array, int index) {
43         if ((array == null) || (index >= array.length) || (index < 0)) {
44             return 0;
45         }
46         return array[index];
47     }
48
49
50     public static Object JavaDoc list(List JavaDoc list, int index) {
51         if ((list == null) || (index >= list.size()) || (index < 0)) {
52             return null;
53         }
54         return list.get(index);
55     }
56
57     public static Object JavaDoc map(Map JavaDoc map, String JavaDoc key) {
58         if (map == null) {
59             return null;
60         }
61         return map.get(key);
62     }
63
64
65     // ---------------------------------------------------------------- checked
66

67     /**
68      * Determines if string equals to "on", "true" or "yes". Case is ignored.
69      */

70     private static boolean isTrueString(String JavaDoc value) {
71         return (value.equalsIgnoreCase(ON) ||
72                 value.equalsIgnoreCase(TRUE) ||
73                 value.equalsIgnoreCase(YES));
74     }
75
76     public static String JavaDoc checked(boolean data) {
77         if (data == true) {
78             return CHECKED;
79         }
80         return EMPTY;
81     }
82
83     public static String JavaDoc checked(Boolean JavaDoc data) {
84         if ((data != null) && (data.booleanValue() == true)) {
85             return CHECKED;
86         }
87         return EMPTY;
88     }
89
90     public static String JavaDoc checked(Object JavaDoc data) {
91         if (data == null) {
92             return EMPTY;
93         }
94         String JavaDoc dataValue = data.toString();
95         if (isTrueString(dataValue)) {
96             return CHECKED;
97         }
98         return EMPTY;
99     }
100
101     /**
102      * Checks if object exists. Since non-checked checkboxes are not sended through request,
103      * it is assumed that existance of an object means that check box is checked.
104      * @see #checked(Object, String)
105      */

106     public static String JavaDoc checkedExist(Object JavaDoc data) {
107         return data != null ? CHECKED : EMPTY;
108     }
109     /**
110      * Checks if string representation of an object equals to specified value.
111      * If it does, 'checked' is returned, otherwise an empty string.
112      * <p>
113      *
114      * Usage:<br>
115      * <code>
116      * &lt;input type="radio" name="r2" value="value2" &lt;%=FormUtil.checked(value, "value2")%&gt;&gt;
117      * </code>
118      *
119      * <p>
120      * May be used for CHECKBOX, RADIO form elements.
121      * @see #checkedExist(Object)
122      */

123     public static String JavaDoc checked(Object JavaDoc data, String JavaDoc value) {
124         if (data == null) {
125             return EMPTY;
126         }
127         String JavaDoc dataValue = data.toString();
128         if (dataValue.equals(value)) {
129             return CHECKED;
130         }
131         return EMPTY;
132     }
133
134
135     // ---------------------------------------------------------------- checked value
136

137     /**
138      * Shortcut for {@link #checked(Object, String)}. Allows user to write value only once in
139      * HTML form.
140      * <p>
141      *
142      * Usage:<br>
143      * <code>
144      * &lt;input type="radio" name="r2" value="&lt;%=FormUtil.checkedValue(value, "value2")%&gt;"&gt;
145      * </code>
146      */

147     public static String JavaDoc checkedValue(Object JavaDoc data, String JavaDoc value) {
148         return value + ENDQUOTE + checked(data, value) + IGNORE;
149     }
150
151     /**
152      * Shortcut for {@link #checked(boolean)}.
153      *
154      * @see #checkedValue(Object, String)
155      */

156     public static String JavaDoc checkedValue(boolean data, String JavaDoc value) {
157         return value + ENDQUOTE + checked(data) + IGNORE;
158     }
159
160     /**
161      * Shortcut for {@link #checked(boolean)}.
162      *
163      * @see #checkedValue(Object, String)
164      */

165     public static String JavaDoc checkedValue(Boolean JavaDoc data, String JavaDoc value) {
166         return value + ENDQUOTE + checked(data) + IGNORE;
167     }
168
169     /**
170      * Shortcut for {@link #checked(Object)} assumming that value equals to "true".
171      *
172      * @see #checkedValue(Object, String)
173      */

174     public static String JavaDoc checkedValue(Object JavaDoc data) {
175         return TRUE + ENDQUOTE + checked(data) + IGNORE;
176     }
177
178     public static String JavaDoc checkedValueExist(Object JavaDoc data) {
179         return TRUE + ENDQUOTE + checkedExist(data) + IGNORE;
180     }
181
182
183     // ---------------------------------------------------------------- selected
184

185     /**
186      * Checks if objects string representation equals to specified value.
187      * If it does, 'selected' is returned, otherwise an empty string.
188      * <p>
189      *
190      * Usage:
191      * <code>
192      * &lt;option value="option1" &lt;%=FormUtil.selected(value, "option1")%&gt;&gt;option #1&lt;/option&gt;
193      * </code>
194      * <p>
195      *
196      * May be used for OPTION form elements.
197      */

198     public static String JavaDoc selected(Object JavaDoc data, String JavaDoc value) {
199         if (data == null) {
200             return EMPTY;
201         }
202         String JavaDoc dataValue = data.toString();
203         if (dataValue.equals(value)) {
204             return SELECTED;
205         }
206         return EMPTY;
207     }
208
209     // ---------------------------------------------------------------- selected value
210

211     /**
212      * Shortcut for {@link #selected(Object, String)}. Allows user to write value only once in
213      * HTML form.
214      * <p>
215      *
216      * Usage:
217      * <code>
218      * &lt;option value="&lt;%=FormUtil.selectedValue(value, "option1")%&gt;"&gt;option #1&lt;/option&gt;
219      * </code>
220      */

221     public static String JavaDoc selectedValue(Object JavaDoc data, String JavaDoc value) {
222         return value + ENDQUOTE + selected(data, value) + IGNORE;
223     }
224
225     // ---------------------------------------------------------------- multiple selected
226

227     public static String JavaDoc multiSelected(Object JavaDoc[] data, String JavaDoc value) {
228         if (data == null) {
229             return EMPTY;
230         }
231         for (int i = 0; i < data.length; i++) {
232             String JavaDoc dataValue = data[i].toString();
233             if (dataValue.equals(value)) {
234                 return SELECTED;
235             }
236         }
237         return EMPTY;
238     }
239     public static String JavaDoc multiSelected(int[] data, String JavaDoc value) {
240         if (data == null) {
241             return EMPTY;
242         }
243         for (int i = 0; i < data.length; i++) {
244             String JavaDoc dataValue = Integer.toString(data[i]);
245             if (dataValue.equals(value)) {
246                 return SELECTED;
247             }
248         }
249         return EMPTY;
250     }
251     public static String JavaDoc multiSelected(long[] data, String JavaDoc value) {
252         if (data == null) {
253             return EMPTY;
254         }
255         for (int i = 0; i < data.length; i++) {
256             String JavaDoc dataValue = Long.toString(data[i]);
257             if (dataValue.equals(value)) {
258                 return SELECTED;
259             }
260         }
261         return EMPTY;
262     }
263
264     public static String JavaDoc multiSelected(List JavaDoc data, String JavaDoc value) {
265         if (data == null) {
266             return EMPTY;
267         }
268         if (data.contains(value)) {
269             return SELECTED;
270         }
271         return EMPTY;
272     }
273
274     // ---------------------------------------------------------------- multiple selected value
275

276     public static String JavaDoc multiSelectedValue(Object JavaDoc[] data, String JavaDoc value) {
277         return value + ENDQUOTE + multiSelected(data, value) + IGNORE;
278     }
279     public static String JavaDoc multiSelectedValue(int[] data, String JavaDoc value) {
280         return value + ENDQUOTE + multiSelected(data, value) + IGNORE;
281     }
282     public static String JavaDoc multiSelectedValue(long[] data, String JavaDoc value) {
283         return value + ENDQUOTE + multiSelected(data, value) + IGNORE;
284     }
285
286     public static String JavaDoc multiSelectedValue(List JavaDoc data, String JavaDoc value) {
287         return value + ENDQUOTE + multiSelected(data, value) + IGNORE;
288     }
289
290
291 }
292
Popular Tags