1 3 package jodd.servlet; 4 5 import java.util.List ; 6 import java.util.Map ; 7 8 14 public class HtmlFormUtil { 15 16 private static final String EMPTY = ""; 17 private static final String CHECKED = "checked"; 18 private static final String IGNORE = " _i=\""; 19 private static final String TRUE = "true"; 20 private static final String ON = "on"; 21 private static final String YES = "yes"; 22 private static final String ENDQUOTE = "\" "; 23 private static final String SELECTED = "selected"; 24 25 27 30 public static Object array(Object [] 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 list(List 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 map(Map map, String key) { 58 if (map == null) { 59 return null; 60 } 61 return map.get(key); 62 } 63 64 65 67 70 private static boolean isTrueString(String value) { 71 return (value.equalsIgnoreCase(ON) || 72 value.equalsIgnoreCase(TRUE) || 73 value.equalsIgnoreCase(YES)); 74 } 75 76 public static String checked(boolean data) { 77 if (data == true) { 78 return CHECKED; 79 } 80 return EMPTY; 81 } 82 83 public static String checked(Boolean data) { 84 if ((data != null) && (data.booleanValue() == true)) { 85 return CHECKED; 86 } 87 return EMPTY; 88 } 89 90 public static String checked(Object data) { 91 if (data == null) { 92 return EMPTY; 93 } 94 String dataValue = data.toString(); 95 if (isTrueString(dataValue)) { 96 return CHECKED; 97 } 98 return EMPTY; 99 } 100 101 106 public static String checkedExist(Object data) { 107 return data != null ? CHECKED : EMPTY; 108 } 109 123 public static String checked(Object data, String value) { 124 if (data == null) { 125 return EMPTY; 126 } 127 String dataValue = data.toString(); 128 if (dataValue.equals(value)) { 129 return CHECKED; 130 } 131 return EMPTY; 132 } 133 134 135 137 147 public static String checkedValue(Object data, String value) { 148 return value + ENDQUOTE + checked(data, value) + IGNORE; 149 } 150 151 156 public static String checkedValue(boolean data, String value) { 157 return value + ENDQUOTE + checked(data) + IGNORE; 158 } 159 160 165 public static String checkedValue(Boolean data, String value) { 166 return value + ENDQUOTE + checked(data) + IGNORE; 167 } 168 169 174 public static String checkedValue(Object data) { 175 return TRUE + ENDQUOTE + checked(data) + IGNORE; 176 } 177 178 public static String checkedValueExist(Object data) { 179 return TRUE + ENDQUOTE + checkedExist(data) + IGNORE; 180 } 181 182 183 185 198 public static String selected(Object data, String value) { 199 if (data == null) { 200 return EMPTY; 201 } 202 String dataValue = data.toString(); 203 if (dataValue.equals(value)) { 204 return SELECTED; 205 } 206 return EMPTY; 207 } 208 209 211 221 public static String selectedValue(Object data, String value) { 222 return value + ENDQUOTE + selected(data, value) + IGNORE; 223 } 224 225 227 public static String multiSelected(Object [] data, String value) { 228 if (data == null) { 229 return EMPTY; 230 } 231 for (int i = 0; i < data.length; i++) { 232 String dataValue = data[i].toString(); 233 if (dataValue.equals(value)) { 234 return SELECTED; 235 } 236 } 237 return EMPTY; 238 } 239 public static String multiSelected(int[] data, String value) { 240 if (data == null) { 241 return EMPTY; 242 } 243 for (int i = 0; i < data.length; i++) { 244 String dataValue = Integer.toString(data[i]); 245 if (dataValue.equals(value)) { 246 return SELECTED; 247 } 248 } 249 return EMPTY; 250 } 251 public static String multiSelected(long[] data, String value) { 252 if (data == null) { 253 return EMPTY; 254 } 255 for (int i = 0; i < data.length; i++) { 256 String dataValue = Long.toString(data[i]); 257 if (dataValue.equals(value)) { 258 return SELECTED; 259 } 260 } 261 return EMPTY; 262 } 263 264 public static String multiSelected(List data, String value) { 265 if (data == null) { 266 return EMPTY; 267 } 268 if (data.contains(value)) { 269 return SELECTED; 270 } 271 return EMPTY; 272 } 273 274 276 public static String multiSelectedValue(Object [] data, String value) { 277 return value + ENDQUOTE + multiSelected(data, value) + IGNORE; 278 } 279 public static String multiSelectedValue(int[] data, String value) { 280 return value + ENDQUOTE + multiSelected(data, value) + IGNORE; 281 } 282 public static String multiSelectedValue(long[] data, String value) { 283 return value + ENDQUOTE + multiSelected(data, value) + IGNORE; 284 } 285 286 public static String multiSelectedValue(List data, String value) { 287 return value + ENDQUOTE + multiSelected(data, value) + IGNORE; 288 } 289 290 291 } 292 | Popular Tags |