KickJava   Java API By Example, From Geeks To Geeks.

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


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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

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

50 public class ParseNumberTag extends BodyTagSupport JavaDoc {
51   private static L10N L = new L10N(ParseNumberTag.class);
52   
53   private Expr _valueExpr;
54   private Expr _typeExpr;
55   
56   private Expr _patternExpr;
57   
58   private Expr _parseLocaleExpr;
59   private Expr _integerOnlyExpr;
60
61   private String JavaDoc _var;
62   private String JavaDoc _scope;
63
64   /**
65    * Sets the formatting value.
66    *
67    * @param value the JSP-EL expression for the value.
68    */

69   public void setValue(Expr value)
70   {
71     _valueExpr = value;
72   }
73
74   /**
75    * Sets the formatting type.
76    *
77    * @param type the JSP-EL expression for the type.
78    */

79   public void setType(Expr type)
80   {
81     _typeExpr = type;
82   }
83
84   /**
85    * Sets the number pattern.
86    *
87    * @param pattern the JSP-EL expression for the number pattern.
88    */

89   public void setPattern(Expr pattern)
90   {
91     _patternExpr = pattern;
92   }
93
94   /**
95    * Sets the parse locale
96    *
97    * @param locale the JSP-EL expression for the number pattern.
98    */

99   public void setParseLocale(Expr locale)
100   {
101     _parseLocaleExpr = locale;
102   }
103
104   /**
105    * Sets true if integer only parsing.
106    *
107    * @param integerOnly the JSP-EL expression for the number pattern.
108    */

109   public void setIntegerOnly(Expr integerOnly)
110   {
111     _integerOnlyExpr = integerOnly;
112   }
113
114   /**
115    * Sets the variable name.
116    *
117    * @param var the variable name to store the value in.
118    */

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

129   public void setScope(String JavaDoc scope)
130   {
131     _scope = scope;
132   }
133
134   /**
135    * Process the tag.
136    */

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