1
2
3
4 package org.rcfaces.core.internal.taglib;
5
6 import java.util.Locale;
7
8 import javax.el.ELContext;
9 import javax.el.ValueExpression;
10 import javax.faces.context.FacesContext;
11 import javax.faces.convert.Converter;
12 import javax.faces.convert.NumberConverter;
13 import javax.servlet.jsp.JspException;
14
15 import org.rcfaces.core.converter.AbstractNumberConverter;
16 import org.rcfaces.core.internal.converter.LocaleConverter;
17
18
19
20
21
22
23 public class ConvertNumberTag extends CameliaConverterTag {
24 private static final String REVISION = "$Revision: 1.1 $";
25
26 private static final long serialVersionUID = 9211078836220394550L;
27
28 private static final String DEFAULT_NUMBER_TYPE = "number";
29
30 private ValueExpression currencyCode;
31
32 private ValueExpression currencySymbol;
33
34 private ValueExpression groupingUsed;
35
36 private ValueExpression integerOnly;
37
38 private ValueExpression maxFractionDigits;
39
40 private ValueExpression maxIntegerDigits;
41
42 private ValueExpression minFractionDigits;
43
44 private ValueExpression minIntegerDigits;
45
46 private ValueExpression locale;
47
48 private ValueExpression pattern;
49
50 private ValueExpression type;
51
52 private ValueExpression defaultValue;
53
54 public ConvertNumberTag() {
55 initializeFields();
56 }
57
58 public void release() {
59 super.release();
60 initializeFields();
61 }
62
63 private void initializeFields() {
64 currencyCode = null;
65 currencySymbol = null;
66 groupingUsed = null;
67 integerOnly = null;
68 maxFractionDigits = null;
69 maxIntegerDigits = null;
70 minFractionDigits = null;
71 locale = null;
72 pattern = null;
73 type = null;
74 }
75
76 public void setCurrencyCode(ValueExpression currencyCode) {
77 this.currencyCode = currencyCode;
78 }
79
80 public void setCurrencySymbol(ValueExpression currencySymbol) {
81 this.currencySymbol = currencySymbol;
82 }
83
84 public void setGroupingUsed(ValueExpression groupingUsed) {
85 this.groupingUsed = groupingUsed;
86 }
87
88 public void setIntegerOnly(ValueExpression integerOnly) {
89 this.integerOnly = integerOnly;
90 }
91
92 public void setMaxFractionDigits(ValueExpression maxFractionDigits) {
93 this.maxFractionDigits = maxFractionDigits;
94 }
95
96 public void setMaxIntegerDigits(ValueExpression maxIntegerDigits) {
97 this.maxIntegerDigits = maxIntegerDigits;
98 }
99
100 public void setMinFractionDigits(ValueExpression minFractionDigits) {
101 this.minFractionDigits = minFractionDigits;
102 }
103
104 public void setMinIntegerDigits(ValueExpression minIntegerDigits) {
105 this.minIntegerDigits = minIntegerDigits;
106 }
107
108 public void setLocale(ValueExpression locale) {
109 this.locale = locale;
110 }
111
112 public void setPattern(ValueExpression pattern) {
113 this.pattern = pattern;
114 }
115
116 public void setType(ValueExpression type) {
117 this.type = type;
118 }
119
120 public final void setDefaultValue(ValueExpression defaultValue) {
121 this.defaultValue = defaultValue;
122 }
123
124 public int doStartTag() throws JspException {
125 setConverterId(getDefaultConverterId());
126
127 return super.doStartTag();
128 }
129
130 protected String getDefaultConverterId() {
131 return "org.rcfaces.Number";
132 }
133
134 protected Converter createConverter() throws JspException {
135
136 NumberConverter result = (NumberConverter) super.createConverter();
137
138 FacesContext facesContext = FacesContext.getCurrentInstance();
139
140 ELContext elContext = facesContext.getELContext();
141
142 if (currencyCode != null) {
143 String c = null;
144
145 if (currencyCode.isLiteralText()) {
146 c = currencyCode.getExpressionString();
147
148 } else {
149 c = (String) currencyCode.getValue(elContext);
150 }
151
152 result.setCurrencyCode(c);
153 }
154
155 if (currencySymbol != null) {
156
157 if (currencySymbol.isLiteralText()) {
158 result.setCurrencySymbol(currencySymbol.getExpressionString());
159
160 } else {
161 result.setCurrencySymbol((String) currencySymbol
162 .getValue(elContext));
163 }
164 }
165
166 if (pattern != null) {
167 if (pattern.isLiteralText()) {
168 result.setPattern(pattern.getExpressionString());
169
170 } else {
171 result.setPattern((String) pattern.getValue(elContext));
172 }
173 }
174
175 String t = null;
176 if (type != null) {
177 if (type.isLiteralText()) {
178 t = type.getExpressionString();
179
180 } else {
181 t = (String) type.getValue(elContext);
182 }
183 }
184
185 if (t == null) {
186 t = DEFAULT_NUMBER_TYPE;
187 }
188
189 result.setType(t);
190
191 if (groupingUsed != null) {
192 boolean gu;
193
194 if (groupingUsed.isLiteralText()) {
195 gu = Boolean.valueOf(type.getExpressionString()).booleanValue();
196
197 } else {
198 gu = ((Boolean) type.getValue(elContext)).booleanValue();
199 }
200
201 result.setGroupingUsed(gu);
202
203 }
204
205 if (integerOnly != null) {
206 boolean io;
207
208 if (integerOnly.isLiteralText()) {
209 io = Boolean.valueOf(integerOnly.getExpressionString())
210 .booleanValue();
211
212 } else {
213 io = ((Boolean) integerOnly.getValue(elContext)).booleanValue();
214 }
215
216 result.setIntegerOnly(io);
217 }
218
219 if (maxFractionDigits != null) {
220 int mfd;
221
222 if (groupingUsed.isLiteralText()) {
223 mfd = Integer.parseInt(integerOnly.getExpressionString());
224
225 } else {
226 mfd = ((Integer) integerOnly.getValue(elContext)).intValue();
227 }
228
229 result.setMaxFractionDigits(mfd);
230 }
231
232 if (maxIntegerDigits != null) {
233 int mid;
234
235 if (maxIntegerDigits.isLiteralText()) {
236 mid = Integer.parseInt(maxIntegerDigits.getExpressionString());
237
238 } else {
239 mid = ((Integer) maxIntegerDigits.getValue(elContext))
240 .intValue();
241 }
242
243 result.setMaxIntegerDigits(mid);
244 }
245
246 if (minFractionDigits != null) {
247 int mfd;
248
249 if (minFractionDigits.isLiteralText()) {
250 mfd = Integer.parseInt(minFractionDigits.getExpressionString());
251
252 } else {
253 mfd = ((Integer) minFractionDigits.getValue(elContext))
254 .intValue();
255 }
256
257 result.setMinFractionDigits(mfd);
258 }
259
260 if (minIntegerDigits != null) {
261 int mid;
262
263 if (minIntegerDigits.isLiteralText()) {
264 mid = Integer.parseInt(minIntegerDigits.getExpressionString());
265
266 } else {
267 mid = ((Integer) minIntegerDigits.getValue(elContext))
268 .intValue();
269 }
270
271 result.setMinIntegerDigits(mid);
272 }
273
274 if (locale != null) {
275 Locale loc;
276
277 if (locale.isLiteralText()) {
278 loc = (Locale) LocaleConverter.SINGLETON.getAsObject(
279 facesContext, null, locale.getExpressionString());
280
281 } else {
282 loc = (Locale) locale.getValue(elContext);
283 }
284
285 result.setLocale(loc);
286 }
287
288 if (result instanceof AbstractNumberConverter) {
289 if (defaultValue != null) {
290 Object defValue;
291
292 if (locale.isLiteralText()) {
293 defValue = defaultValue.getExpressionString();
294
295 } else {
296 defValue = defaultValue.getValue(elContext);
297 }
298
299 ((AbstractNumberConverter) result).setDefaultValue(String
300 .valueOf(defValue));
301 }
302 }
303
304 return result;
305 }
306 }