1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.custom.convertboolean;
21
22 import javax.faces.component.UIComponent;
23 import javax.faces.component.StateHolder;
24 import javax.faces.context.FacesContext;
25 import javax.faces.convert.Converter;
26 import javax.faces.convert.ConverterException;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 public class BooleanConverter implements Converter, StateHolder
50 {
51
52 private String trueValue = "true";
53 private String falseValue = "false";
54
55 private boolean isTransient;
56
57 public static final String CONVERTER_ID = "org.apache.myfaces.custom.convertboolean.BooleanConverter";
58
59 public BooleanConverter()
60 {
61
62 }
63
64
65
66
67
68
69 public String getFalseValue()
70 {
71 return falseValue;
72 }
73
74 public void setFalseValue(String falseValue)
75 {
76 this.falseValue = falseValue;
77 }
78
79
80
81
82
83
84 public String getTrueValue()
85 {
86 return trueValue;
87 }
88
89 public void setTrueValue(String trueValue)
90 {
91 this.trueValue = trueValue;
92 }
93
94 public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value)
95 throws ConverterException
96 {
97 if (facesContext == null)
98 {
99 throw new NullPointerException("facesContext");
100 }
101 if (uiComponent == null)
102 {
103 throw new NullPointerException("uiComponent");
104 }
105
106 if (value != null)
107 {
108 value = value.trim();
109 if (value.length() > 0)
110 {
111 try
112 {
113 return Boolean.valueOf(value.equals(trueValue));
114 }
115 catch (Exception e)
116 {
117 throw new ConverterException(e);
118 }
119 }
120 }
121 return null;
122 }
123
124 public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value)
125 throws ConverterException
126 {
127 if (facesContext == null)
128 {
129 throw new NullPointerException("facesContext");
130 }
131 if (uiComponent == null)
132 {
133 throw new NullPointerException("uiComponent");
134 }
135
136 if (value == null)
137 {
138 return "";
139 }
140 if (value instanceof String)
141 {
142 return (String) value;
143 }
144 try
145 {
146 return ((Boolean) value).booleanValue() ? trueValue : falseValue;
147 }
148 catch (Exception e)
149 {
150 throw new ConverterException(e);
151 }
152 }
153
154
155
156 public boolean isTransient()
157 {
158 return this.isTransient;
159 }
160
161 public void setTransient(boolean newTransientValue)
162 {
163 this.isTransient = newTransientValue;
164 }
165
166 public void restoreState(FacesContext context, Object state)
167 {
168 Object values[] = (Object[]) state;
169 this.trueValue = (String) values[0];
170 this.falseValue = (String) values[1];
171 }
172
173 public Object saveState(FacesContext context)
174 {
175 Object[] values = new Object[2];
176 values[0] = this.trueValue;
177 values[1] = this.falseValue;
178 return values;
179 }
180
181
182 }