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 | |
private static final long serialVersionUID = -1800859871401901842L; |
29 | |
|
30 | 0 | static final List<String> TRUE_VALUES = Arrays.asList(new String[] { "yes", "y", "true", "t", "on", "1", "enabled" }); |
31 | 0 | static final List<String> FALSE_VALUES = Arrays.asList(new String[] { "no", "n", "false", "f", "off", "0", "disabled" }); |
32 | |
|
33 | |
protected Object convertToObject(String target) { |
34 | 0 | if (Formatter.isEmptyValue(target)) |
35 | 0 | return Boolean.FALSE; |
36 | |
|
37 | 0 | String stringValue = target.getClass().isArray() ? unwrapString(target) : (String) target; |
38 | 0 | stringValue = stringValue.trim().toLowerCase(); |
39 | |
|
40 | 0 | if (TRUE_VALUES.contains(stringValue)) |
41 | 0 | return Boolean.TRUE; |
42 | 0 | if (FALSE_VALUES.contains(stringValue)) |
43 | 0 | return Boolean.FALSE; |
44 | |
|
45 | 0 | throw new FormatException("converting", RiceKeyConstants.ERROR_BOOLEAN, stringValue); |
46 | |
} |
47 | |
|
48 | |
public Object format(Object target) { |
49 | 0 | if (target == null) |
50 | 0 | return "No"; |
51 | 0 | if (target instanceof String) { |
52 | 0 | return target; |
53 | |
} |
54 | |
|
55 | 0 | boolean isTrue = ((Boolean) target).booleanValue(); |
56 | |
|
57 | 0 | return isTrue ? "Yes" : "No"; |
58 | |
} |
59 | |
|
60 | |
protected Object getNullObjectValue() { |
61 | 0 | return Boolean.FALSE; |
62 | |
} |
63 | |
|
64 | |
} |