KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > tag > el > fmt > FormatNumberTag


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.standard.tag.el.fmt;
18
19 import javax.servlet.jsp.JspException JavaDoc;
20
21 import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
22 import org.apache.taglibs.standard.tag.common.fmt.FormatNumberSupport;
23
24 /**
25  * <p>A handler for &lt;formatNumber&gt; that accepts attributes as Strings
26  * and evaluates them as expressions at runtime.</p>
27  *
28  * @author Jan Luehe
29  */

30
31 public class FormatNumberTag extends FormatNumberSupport {
32
33     //*********************************************************************
34
// 'Private' state (implementation details)
35

36     private String JavaDoc value_; // stores EL-based property
37
private String JavaDoc type_; // stores EL-based property
38
private String JavaDoc pattern_; // stores EL-based property
39
private String JavaDoc currencyCode_; // stores EL-based property
40
private String JavaDoc currencySymbol_; // stores EL-based property
41
private String JavaDoc groupingUsed_; // stores EL-based property
42
private String JavaDoc maxIntegerDigits_; // stores EL-based property
43
private String JavaDoc minIntegerDigits_; // stores EL-based property
44
private String JavaDoc maxFractionDigits_; // stores EL-based property
45
private String JavaDoc minFractionDigits_; // stores EL-based property
46

47
48     //*********************************************************************
49
// Constructor
50

51     /**
52      * Constructs a new FormatNumberTag. As with TagSupport, subclasses
53      * should not provide other constructors and are expected to call
54      * the superclass constructor
55      */

56     public FormatNumberTag() {
57         super();
58         init();
59     }
60
61
62     //*********************************************************************
63
// Tag logic
64

65     // evaluates expression and chains to parent
66
public int doStartTag() throws JspException JavaDoc {
67
68         // evaluate any expressions we were passed, once per invocation
69
evaluateExpressions();
70
71     // chain to the parent implementation
72
return super.doStartTag();
73     }
74
75     // Releases any resources we may have (or inherit)
76
public void release() {
77         super.release();
78         init();
79     }
80
81
82     //*********************************************************************
83
// Accessor methods
84

85     // for EL-based attribute
86
public void setValue(String JavaDoc value_) {
87         this.value_ = value_;
88     this.valueSpecified = true;
89     }
90
91     // for EL-based attribute
92
public void setType(String JavaDoc type_) {
93         this.type_ = type_;
94     }
95
96     // for EL-based attribute
97
public void setPattern(String JavaDoc pattern_) {
98         this.pattern_ = pattern_;
99     }
100
101     // for EL-based attribute
102
public void setCurrencyCode(String JavaDoc currencyCode_) {
103         this.currencyCode_ = currencyCode_;
104     }
105
106     // for EL-based attribute
107
public void setCurrencySymbol(String JavaDoc currencySymbol_) {
108         this.currencySymbol_ = currencySymbol_;
109     }
110
111     // for EL-based attribute
112
public void setGroupingUsed(String JavaDoc groupingUsed_) {
113         this.groupingUsed_ = groupingUsed_;
114     this.groupingUsedSpecified = true;
115     }
116
117     // for EL-based attribute
118
public void setMaxIntegerDigits(String JavaDoc maxIntegerDigits_) {
119         this.maxIntegerDigits_ = maxIntegerDigits_;
120     this.maxIntegerDigitsSpecified = true;
121     }
122
123     // for EL-based attribute
124
public void setMinIntegerDigits(String JavaDoc minIntegerDigits_) {
125         this.minIntegerDigits_ = minIntegerDigits_;
126     this.minIntegerDigitsSpecified = true;
127     }
128
129     // for EL-based attribute
130
public void setMaxFractionDigits(String JavaDoc maxFractionDigits_) {
131         this.maxFractionDigits_ = maxFractionDigits_;
132     this.maxFractionDigitsSpecified = true;
133     }
134
135     // for EL-based attribute
136
public void setMinFractionDigits(String JavaDoc minFractionDigits_) {
137         this.minFractionDigits_ = minFractionDigits_;
138     this.minFractionDigitsSpecified = true;
139     }
140
141
142     //*********************************************************************
143
// Private (utility) methods
144

145     // (re)initializes state (during release() or construction)
146
private void init() {
147         // null implies "no expression"
148
value_ = type_ = pattern_ = null;
149     currencyCode_ = currencySymbol_ = null;
150     groupingUsed_ = null;
151     maxIntegerDigits_ = minIntegerDigits_ = null;
152     maxFractionDigits_ = minFractionDigits_ = null;
153     }
154
155     // Evaluates expressions as necessary
156
private void evaluateExpressions() throws JspException JavaDoc {
157     Object JavaDoc obj = null;
158
159         /*
160          * Note: we don't check for type mismatches here; we assume
161          * the expression evaluator will return the expected type
162          * (by virtue of knowledge we give it about what that type is).
163          * A ClassCastException here is truly unexpected, so we let it
164          * propagate up.
165          */

166
167     // 'value' attribute
168
if (value_ != null) {
169         value = ExpressionEvaluatorManager.evaluate(
170             "value", value_, Object JavaDoc.class, this, pageContext);
171     }
172
173     // 'type' attribute
174
if (type_ != null) {
175         type = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
176             "type", type_, String JavaDoc.class, this, pageContext);
177     }
178
179     // 'pattern' attribute
180
if (pattern_ != null) {
181         pattern = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
182             "pattern", pattern_, String JavaDoc.class, this, pageContext);
183     }
184
185     // 'currencyCode' attribute
186
if (currencyCode_ != null) {
187         currencyCode = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
188             "currencyCode", currencyCode_, String JavaDoc.class, this,
189         pageContext);
190     }
191
192     // 'currencySymbol' attribute
193
if (currencySymbol_ != null) {
194         currencySymbol = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
195             "currencySymbol", currencySymbol_, String JavaDoc.class, this,
196         pageContext);
197     }
198
199     // 'groupingUsed' attribute
200
if (groupingUsed_ != null) {
201         obj = ExpressionEvaluatorManager.evaluate(
202             "groupingUsed", groupingUsed_, Boolean JavaDoc.class, this,
203         pageContext);
204         if (obj != null) {
205         isGroupingUsed = ((Boolean JavaDoc) obj).booleanValue();
206         }
207     }
208
209     // 'maxIntegerDigits' attribute
210
if (maxIntegerDigits_ != null) {
211         obj = ExpressionEvaluatorManager.evaluate(
212             "maxIntegerDigits", maxIntegerDigits_, Integer JavaDoc.class, this,
213         pageContext);
214         if (obj != null) {
215         maxIntegerDigits = ((Integer JavaDoc) obj).intValue();
216         }
217     }
218
219     // 'minIntegerDigits' attribute
220
if (minIntegerDigits_ != null) {
221         obj = ExpressionEvaluatorManager.evaluate(
222             "minIntegerDigits", minIntegerDigits_, Integer JavaDoc.class, this,
223         pageContext);
224         if (obj != null) {
225         minIntegerDigits = ((Integer JavaDoc) obj).intValue();
226         }
227     }
228
229     // 'maxFractionDigits' attribute
230
if (maxFractionDigits_ != null) {
231         obj = ExpressionEvaluatorManager.evaluate(
232             "maxFractionDigits", maxFractionDigits_, Integer JavaDoc.class, this,
233         pageContext);
234         if (obj != null) {
235         maxFractionDigits = ((Integer JavaDoc) obj).intValue();
236         }
237     }
238
239     // 'minFractionDigits' attribute
240
if (minFractionDigits_ != null) {
241         obj = ExpressionEvaluatorManager.evaluate(
242             "minFractionDigits", minFractionDigits_, Integer JavaDoc.class, this,
243         pageContext);
244         if (obj != null) {
245         minFractionDigits = ((Integer JavaDoc) obj).intValue();
246         }
247     }
248     }
249 }
250
251
Popular Tags