KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jstl > rt > 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.rt;
30
31 import com.caucho.jsp.BodyContentImpl;
32 import com.caucho.jsp.PageContextImpl;
33 import com.caucho.util.L10N;
34
35 import javax.servlet.jsp.JspException JavaDoc;
36 import javax.servlet.jsp.JspWriter JavaDoc;
37 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.text.DecimalFormat JavaDoc;
40 import java.text.DecimalFormatSymbols JavaDoc;
41 import java.text.NumberFormat JavaDoc;
42 import java.util.Locale JavaDoc;
43
44 /**
45  * Looks up an i18n message from a bundle and prints it.
46  */

47 public class FormatNumberTag extends BodyTagSupport JavaDoc {
48   private static L10N L = new L10N(FormatNumberTag.class);
49   
50   private double _value;
51   private boolean _hasValue;
52   
53   private String JavaDoc _type;
54   
55   private String JavaDoc _pattern;
56   
57   private String JavaDoc _currencyCode;
58   private String JavaDoc _currencySymbol;
59   
60   private boolean _groupingUsed = true;
61   
62   private int _maxIntegerDigits = -1;
63   private int _minIntegerDigits = -1;
64   private int _maxFractionDigits = -1;
65   private int _minFractionDigits = -1;
66
67   private String JavaDoc _var;
68   private String JavaDoc _scope;
69
70   /**
71    * Sets the formatting value.
72    *
73    * @param value the value.
74    */

75   public void setValue(double value)
76   {
77     _value = value;
78     _hasValue = true;
79   }
80
81   /**
82    * Sets the formatting type.
83    *
84    * @param type the type.
85    */

86   public void setType(String JavaDoc type)
87   {
88     _type = type;
89   }
90
91   /**
92    * Sets the number pattern.
93    *
94    * @param pattern the number pattern.
95    */

96   public void setPattern(String JavaDoc pattern)
97   {
98     _pattern = pattern;
99   }
100
101   /**
102    * Sets the currency code.
103    *
104    * @param currencyCode the currency code.
105    */

106   public void setCurrencyCode(String JavaDoc currency)
107   {
108     _currencyCode = currency;
109   }
110
111   /**
112    * Sets the currency symbol.
113    *
114    * @param currencySymbol the currency symbol.
115    */

116   public void setCurrencySymbol(String JavaDoc currencySymbol)
117   {
118     _currencySymbol = currencySymbol;
119   }
120
121   /**
122    * Sets the groupingUsed expression
123    *
124    * @param groupingUsed true if grouping is used
125    */

126   public void setGroupingUsed(boolean groupingUsed)
127   {
128     _groupingUsed = groupingUsed;
129   }
130
131   /**
132    * Sets the minimum digits allowed in the integer portion.
133    *
134    * @param minIntegerDigits the digits.
135    */

136   public void setMinIntegerDigits(int minIntegerDigits)
137   {
138     _minIntegerDigits = minIntegerDigits;
139   }
140
141   /**
142    * Sets the maximum digits allowed in the integer portion.
143    *
144    * @param maxIntegerDigits the digits.
145    */

146   public void setMaxIntegerDigits(int maxIntegerDigits)
147   {
148     _maxIntegerDigits = maxIntegerDigits;
149   }
150
151   /**
152    * Sets the minimum digits allowed in the fraction portion.
153    *
154    * @param minFractionDigits the digits.
155    */

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

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

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

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

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