KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jstl > el > FormatNumberTag


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jstl.el;
30
31 import com.caucho.el.Expr;
32 import com.caucho.jsp.BodyContentImpl;
33 import com.caucho.jsp.PageContextImpl;
34 import com.caucho.util.L10N;
35
36 import javax.el.ELContext;
37 import javax.servlet.jsp.JspException JavaDoc;
38 import javax.servlet.jsp.JspWriter JavaDoc;
39 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
40 import java.text.DecimalFormat JavaDoc;
41 import java.text.DecimalFormatSymbols JavaDoc;
42 import java.text.NumberFormat JavaDoc;
43 import java.util.Locale JavaDoc;
44
45 /**
46  * Looks up an i18n message from a bundle and prints it.
47  */

48 public class FormatNumberTag extends BodyTagSupport JavaDoc {
49   private static L10N L = new L10N(FormatNumberTag.class);
50   
51   private Expr _valueExpr;
52   private Expr _typeExpr;
53   
54   private Expr _patternExpr;
55   
56   private Expr _currencyCodeExpr;
57   private Expr _currencySymbolExpr;
58   
59   private Expr _groupingUsedExpr;
60   
61   private Expr _maxIntegerDigitsExpr;
62   private Expr _minIntegerDigitsExpr;
63   private Expr _maxFractionDigitsExpr;
64   private Expr _minFractionDigitsExpr;
65
66   private String JavaDoc _var;
67   private String JavaDoc _scope;
68
69   /**
70    * Sets the formatting value.
71    *
72    * @param value the JSP-EL expression for the value.
73    */

74   public void setValue(Expr value)
75   {
76     _valueExpr = value;
77   }
78
79   /**
80    * Sets the formatting type.
81    *
82    * @param type the JSP-EL expression for the type.
83    */

84   public void setType(Expr type)
85   {
86     _typeExpr = type;
87   }
88
89   /**
90    * Sets the number pattern.
91    *
92    * @param pattern the JSP-EL expression for the number pattern.
93    */

94   public void setPattern(Expr pattern)
95   {
96     _patternExpr = pattern;
97   }
98
99   /**
100    * Sets the currency code.
101    *
102    * @param currencyCode the JSP-EL expression for the currency code.
103    */

104   public void setCurrencyCode(Expr currencyCode)
105   {
106     _currencyCodeExpr = currencyCode;
107   }
108
109   /**
110    * Sets the currency symbol.
111    *
112    * @param currencySymbol the JSP-EL expression for the currency symbol.
113    */

114   public void setCurrencySymbol(Expr currencySymbol)
115   {
116     _currencySymbolExpr = currencySymbol;
117   }
118
119   /**
120    * Sets the groupingUsed expression
121    *
122    * @param groupingUsed the JSP-EL expression for the grouping pattern.
123    */

124   public void setGroupingUsed(Expr groupingUsed)
125   {
126     _groupingUsedExpr = groupingUsed;
127   }
128
129   /**
130    * Sets the minimum digits allowed in the integer portion.
131    *
132    * @param minIntegerDigits the JSP-EL expression for the digits.
133    */

134   public void setMinIntegerDigits(Expr minIntegerDigits)
135   {
136     _minIntegerDigitsExpr = minIntegerDigits;
137   }
138
139   /**
140    * Sets the maximum digits allowed in the integer portion.
141    *
142    * @param maxIntegerDigits the JSP-EL expression for the digits.
143    */

144   public void setMaxIntegerDigits(Expr maxIntegerDigits)
145   {
146     _maxIntegerDigitsExpr = maxIntegerDigits;
147   }
148
149   /**
150    * Sets the minimum digits allowed in the fraction portion.
151    *
152    * @param minFractionDigits the JSP-EL expression for the digits.
153    */

154   public void setMinFractionDigits(Expr minFractionDigits)
155   {
156     _minFractionDigitsExpr = minFractionDigits;
157   }
158
159   /**
160    * Sets the maximum digits allowed in the fraction portion.
161    *
162    * @param maxFractionDigits the JSP-EL expression for the digits.
163    */

164   public void setMaxFractionDigits(Expr maxFractionDigits)
165   {
166     _maxFractionDigitsExpr = maxFractionDigits;
167   }
168
169   /**
170    * Sets the variable name.
171    *
172    * @param var the variable name to store the value in.
173    */

174   public void setVar(String JavaDoc var)
175   {
176     _var = var;
177   }
178
179   /**
180    * Sets the variable scope.
181    *
182    * @param scope the variable scope to store the value in.
183    */

184   public void setScope(String JavaDoc scope)
185   {
186     _scope = scope;
187   }
188
189   /**
190    * Process the tag.
191    */

192   public int doEndTag()
193     throws JspException JavaDoc
194   {
195     try {
196       PageContextImpl pageContext = (PageContextImpl) this.pageContext;
197       ELContext env = pageContext.getELContext();
198       
199       JspWriter JavaDoc out = pageContext.getOut();
200
201       double number;
202
203       BodyContentImpl body = (BodyContentImpl) getBodyContent();
204
205       if (_valueExpr != null)
206         number = _valueExpr.evalDouble(env);
207       else if (body != null) {
208     String JavaDoc value = body.getTrimString();
209
210     if (! value.equals(""))
211       number = Double.parseDouble(value);
212     else
213       number = 0.0;
214       }
215       else
216         number = 0.0;
217       
218       if (Double.isNaN(number))
219         number = 0;
220       
221       NumberFormat JavaDoc format = getFormat();
222
223       String JavaDoc value;
224       if (format != null)
225         value = format.format(number);
226       else
227         value = String.valueOf(number);
228
229       if (_var == null)
230         out.print(value);
231       else
232         CoreSetTag.setValue(pageContext, _var, _scope, value);
233     } catch (Exception JavaDoc e) {
234       throw new JspException JavaDoc(e);
235     }
236
237     return EVAL_PAGE;
238   }
239
240   protected NumberFormat JavaDoc getFormat()
241     throws JspException JavaDoc
242   {
243     try {
244       PageContextImpl pageContext = (PageContextImpl) this.pageContext;
245       ELContext env = pageContext.getELContext();
246       
247       NumberFormat JavaDoc format = null;
248
249       Locale JavaDoc locale = pageContext.getLocale();
250
251       String JavaDoc type = null;
252       if (_typeExpr != null)
253     type = _typeExpr.evalString(env);
254
255       if (type == null || type.equals("") || type.equals("number")) {
256     if (locale != null)
257       format = NumberFormat.getInstance(locale);
258     else
259       format = NumberFormat.getInstance();
260
261     DecimalFormat JavaDoc decimalFormat = (DecimalFormat JavaDoc) format;
262
263     if (_patternExpr != null)
264       decimalFormat.applyPattern(_patternExpr.evalString(env));
265       }
266       else if (type.equals("percent")) {
267     if (locale != null)
268       format = NumberFormat.getPercentInstance(locale);
269     else
270       format = NumberFormat.getPercentInstance();
271       }
272       else if (type.equals("currency")) {
273     if (locale != null)
274       format = NumberFormat.getCurrencyInstance(locale);
275     else
276       format = NumberFormat.getCurrencyInstance();
277
278     if ((_currencyCodeExpr != null || _currencySymbolExpr != null) &&
279         format instanceof DecimalFormat JavaDoc) {
280       DecimalFormat JavaDoc dFormat = (DecimalFormat JavaDoc) format;
281       DecimalFormatSymbols JavaDoc dSymbols;
282
283       dSymbols = dFormat.getDecimalFormatSymbols();
284
285       if (_currencyCodeExpr != null && dSymbols != null)
286         dSymbols.setInternationalCurrencySymbol(_currencyCodeExpr.evalString(env));
287
288       if (_currencySymbolExpr != null && dSymbols != null)
289         dSymbols.setCurrencySymbol(_currencySymbolExpr.evalString(env));
290
291       dFormat.setDecimalFormatSymbols(dSymbols);
292     }
293       }
294       else
295     throw new JspException JavaDoc(L.l("unknown formatNumber type `{0}'",
296                    type));
297
298       if (_groupingUsedExpr != null)
299     format.setGroupingUsed(_groupingUsedExpr.evalBoolean(env));
300
301       if (_minIntegerDigitsExpr != null)
302     format.setMinimumIntegerDigits((int) _minIntegerDigitsExpr.evalLong(env));
303       
304       if (_maxIntegerDigitsExpr != null)
305     format.setMaximumIntegerDigits((int) _maxIntegerDigitsExpr.evalLong(env));
306
307       if (_minFractionDigitsExpr != null)
308     format.setMinimumFractionDigits((int) _minFractionDigitsExpr.evalLong(env));
309       
310       if (_maxFractionDigitsExpr != null)
311     format.setMaximumFractionDigits((int) _maxFractionDigitsExpr.evalLong(env));
312
313       return format;
314     } catch (Exception JavaDoc e) {
315       throw new JspException JavaDoc(e);
316     }
317   }
318 }
319
Popular Tags