1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 package nl.openedge.baritus.converters;
32
33 /***
34 * <p>{@link Converter} implementation that converts an incoming
35 * String into a <code>java.lang.Boolean</code> object,
36 * throwing a {@link ConversionException} if a conversion
37 * error occurs.</p>
38 *
39 * @author Eelco Hillenius
40 */
41
42 public final class BooleanConverter implements Converter
43 {
44
45 /***
46 * Create a {@link Converter} that will throw a {@link ConversionException}
47 * if a conversion error occurs.
48 */
49 public BooleanConverter()
50 {
51
52 }
53
54 /***
55 * Convert the specified input object into an output object of the
56 * specified type.
57 *
58 * @param type Data type to which this value should be converted
59 * @param value The input value to be converted
60 *
61 * @exception ConversionException if conversion cannot be performed
62 * successfully
63 */
64 public Object convert(Class type, Object value)
65 {
66
67 if (value == null)
68 {
69 return null;
70 }
71
72 if (value instanceof Boolean)
73 {
74 return (value);
75 }
76
77 try
78 {
79 String stringValue = value.toString();
80 if (stringValue.equalsIgnoreCase("yes") ||
81 stringValue.equalsIgnoreCase("y") ||
82 stringValue.equalsIgnoreCase("true") ||
83 stringValue.equalsIgnoreCase("on") ||
84 stringValue.equalsIgnoreCase("1")) {
85 return (Boolean.TRUE);
86 }
87 else if (stringValue.equalsIgnoreCase("no") ||
88 stringValue.equalsIgnoreCase("n") ||
89 stringValue.equalsIgnoreCase("false") ||
90 stringValue.equalsIgnoreCase("off") ||
91 stringValue.equalsIgnoreCase("0")) {
92 return (Boolean.FALSE);
93 }
94 else
95 {
96 throw new ConversionException(stringValue);
97 }
98 }
99 catch (ClassCastException e)
100 {
101 throw new ConversionException(e);
102 }
103
104 }
105
106
107 }