1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.smartlab.web.bean;
24
25 import java.util.Locale;
26
27
28
29
30
31
32 public class BooleanConverter extends Converter {
33
34 private boolean value;
35
36
37
38
39
40
41
42 public BooleanConverter(boolean value) {
43 this.value = value;
44 }
45
46
47
48
49
50 public Object convert(Class type, Object value, Locale locale) throws ConversionException {
51 if (type.equals(Boolean.class) || type.equals(Boolean.TYPE)) {
52 if (value == null || value.toString().trim().length() == 0) {
53 value = "off";
54 }
55 if (value.toString().equalsIgnoreCase("on") || value.toString().equalsIgnoreCase("y")
56 || value.toString().equalsIgnoreCase("yes") || value.toString().equalsIgnoreCase("1")
57 || value.toString().equalsIgnoreCase("true")) {
58 return Boolean.TRUE;
59 } else if (value.toString().equalsIgnoreCase("off") || value.toString().equalsIgnoreCase("n")
60 || value.toString().equalsIgnoreCase("no") || value.toString().equalsIgnoreCase("0")
61 || value.toString().equalsIgnoreCase("false")) {
62 return Boolean.FALSE;
63 } else {
64 throw new ConversionException("Unable to convert to boolean `" + value + "`");
65 }
66 } else {
67 return value.toString();
68 }
69 }
70
71
72
73
74
75
76 protected boolean hasDefault() {
77 return true;
78 }
79
80
81
82
83
84
85 protected boolean getDefault() {
86 return value;
87 }
88 }