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