KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jstl > rt > FmtParseNumberTag


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

46 public class FmtParseNumberTag extends BodyTagSupport JavaDoc {
47   private static L10N L = new L10N(FmtParseNumberTag.class);
48   
49   private String JavaDoc _value;
50   private String JavaDoc _type;
51   
52   private String JavaDoc _pattern;
53   
54   private Object JavaDoc _parseLocale;
55   private boolean _integerOnly = false;
56
57   private String JavaDoc _var;
58   private String JavaDoc _scope;
59
60   /**
61    * Sets the formatting value.
62    *
63    * @param value the value.
64    */

65   public void setValue(String JavaDoc value)
66   {
67     _value = value;
68   }
69
70   /**
71    * Sets the formatting type.
72    *
73    * @param type the type.
74    */

75   public void setType(String JavaDoc type)
76   {
77     _type = type;
78   }
79
80   /**
81    * Sets the number pattern.
82    *
83    * @param pattern the number pattern.
84    */

85   public void setPattern(String JavaDoc pattern)
86   {
87     _pattern = pattern;
88   }
89
90   /**
91    * Sets the parse locale
92    *
93    * @param locale the locale
94    */

95   public void setParseLocale(Object JavaDoc locale)
96   {
97     _parseLocale = locale;
98   }
99
100   /**
101    * Sets true if integer only parsing.
102    *
103    * @param integerOnly the JSP-EL expression for the number pattern.
104    */

105   public void setIntegerOnly(boolean integerOnly)
106   {
107     _integerOnly = integerOnly;
108   }
109
110   /**
111    * Sets the variable name.
112    *
113    * @param var the variable name to store the value in.
114    */

115   public void setVar(String JavaDoc var)
116   {
117     _var = var;
118   }
119
120   /**
121    * Sets the variable scope.
122    *
123    * @param scope the variable scope to store the value in.
124    */

125   public void setScope(String JavaDoc scope)
126   {
127     _scope = scope;
128   }
129
130   /**
131    * Process the tag.
132    */

133   public int doEndTag()
134     throws JspException JavaDoc
135   {
136     try {
137       PageContextImpl pc = (PageContextImpl) pageContext;
138       
139       JspWriter JavaDoc out = pc.getOut();
140
141       NumberFormat JavaDoc format = getFormat();
142
143       String JavaDoc string;
144
145       if (_value != null)
146         string = _value;
147       else
148         string = bodyContent.getString().trim();
149
150       Number JavaDoc value = format.parse(string);
151
152       if (_var == null)
153         out.print(value);
154       else
155         CoreSetTag.setValue(pageContext, _var, _scope, value);
156     } catch (IOException JavaDoc e) {
157     } catch (ParseException JavaDoc e) {
158       throw new JspException JavaDoc(e);
159     }
160
161     return EVAL_PAGE;
162   }
163
164   protected NumberFormat JavaDoc getFormat()
165     throws JspException JavaDoc
166   {
167     PageContextImpl pc = (PageContextImpl) pageContext;
168       
169     NumberFormat JavaDoc format = null;
170
171     Locale JavaDoc locale = null;
172
173     if (_parseLocale != null) {
174       Object JavaDoc localeObj = _parseLocale;
175
176       if (localeObj instanceof Locale JavaDoc)
177         locale = (Locale JavaDoc) localeObj;
178       else if (localeObj instanceof String JavaDoc)
179         locale = pc.getLocale((String JavaDoc) localeObj, null);
180     }
181     
182     if (locale == null)
183       locale = pc.getLocale();
184
185     if (_type == null || _type.equals("") || _type.equals("number")) {
186       if (locale != null)
187         format = NumberFormat.getInstance(locale);
188       else
189         format = NumberFormat.getInstance();
190
191       DecimalFormat JavaDoc decimalFormat = (DecimalFormat JavaDoc) format;
192
193       if (_pattern != null)
194         decimalFormat.applyPattern(_pattern);
195     }
196     else if (_type.equals("percent")) {
197       if (locale != null)
198         format = NumberFormat.getPercentInstance(locale);
199       else
200         format = NumberFormat.getPercentInstance();
201     }
202     else if (_type.equals("currency")) {
203       if (locale != null)
204         format = NumberFormat.getCurrencyInstance(locale);
205       else
206         format = NumberFormat.getCurrencyInstance(locale);
207     }
208     else
209       throw new JspException JavaDoc(L.l("unknown formatNumber type `{0}'", _type));
210
211     format.setParseIntegerOnly(_integerOnly);
212
213     return format;
214   }
215 }
216
Popular Tags