1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.core.web.format; |
17 | |
|
18 | |
import java.util.Arrays; |
19 | |
import java.util.List; |
20 | |
|
21 | |
import org.kuali.rice.core.util.RiceKeyConstants; |
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | 0 | public class LittleBooleanFormatter extends Formatter { |
27 | |
|
28 | 0 | static final List TRUE_VALUES = Arrays.asList(new String[] { "yes", "y", "true", "t", "on", "1", "enabled" }); |
29 | 0 | static final List FALSE_VALUES = Arrays.asList(new String[] { "no", "n", "false", "f", "off", "0", "disabled" }); |
30 | |
|
31 | |
protected Object convertToObject(String target) { |
32 | 0 | if (Formatter.isEmptyValue(target)) |
33 | 0 | return Boolean.FALSE; |
34 | |
|
35 | 0 | String stringValue = target.getClass().isArray() ? unwrapString(target) : (String) target; |
36 | 0 | stringValue = stringValue.trim().toLowerCase(); |
37 | |
|
38 | 0 | if (TRUE_VALUES.contains(stringValue)) |
39 | 0 | return Boolean.TRUE; |
40 | 0 | if (FALSE_VALUES.contains(stringValue)) |
41 | 0 | return Boolean.FALSE; |
42 | |
|
43 | 0 | throw new FormatException("converting", RiceKeyConstants.ERROR_BOOLEAN, stringValue); |
44 | |
} |
45 | |
|
46 | |
public Object format(Object target) { |
47 | 0 | if (target == null) |
48 | 0 | return "No"; |
49 | 0 | if (target instanceof String) { |
50 | 0 | return target; |
51 | |
} |
52 | |
|
53 | 0 | boolean isTrue = ((Boolean) target).booleanValue(); |
54 | |
|
55 | 0 | return isTrue ? "Yes" : "No"; |
56 | |
} |
57 | |
|
58 | |
protected Object getNullObjectValue() { |
59 | 0 | return Boolean.FALSE; |
60 | |
} |
61 | |
|
62 | |
} |