KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > html > FormatNumber


1 /*
2  * Copyright 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  * $Header:$
17  */

18 package org.apache.beehive.netui.tags.html;
19
20 import org.apache.beehive.netui.util.internal.InternalStringBuilder;
21
22 import org.apache.beehive.netui.util.Bundle;
23
24 import javax.servlet.jsp.JspException JavaDoc;
25 import javax.servlet.jsp.tagext.JspTag JavaDoc;
26 import javax.servlet.jsp.tagext.SimpleTagSupport JavaDoc;
27 import java.text.DecimalFormat JavaDoc;
28 import java.util.Locale JavaDoc;
29
30 /**
31  * A formatter used to format numbers. This formatter uses patterns that conform to
32  * <code>java.text.NumberFormat</code> pattern syntax. FormatNumber calls <code>toString()</code> on
33  * the object to be formatted to get the value the pattern is applied to.
34  *
35  * The valid FormatNumber types are:
36  * <ul>
37  * <li>number</li>
38  * <li>currency</li>
39  * <li>percent</li>
40  * </ul>
41  * @jsptagref.tagdescription A formatter used to format numbers.
42  *
43  * <p>The &lt;netui:formatNumber> tag formats the output of its parent tag. For example:
44  *
45  * <pre> &lt;netui:span value="${pageFlow.price}">
46  * &lt;netui:formatNumber country="FR" language="fr" type="currency" />
47  * &lt;/netui:span></pre>
48  *
49  * <p>The <code>pattern</code> attribute conforms to
50  * {@link java.text.DecimalFormat java.text.DecimalFormat} pattern syntax.
51  *
52  * <p>The <code>pattern</code> attribute uses the comma as a grouping separater.
53  * If many different grouping sizes are specified in one <code>pattern</code>,
54  * the right-most grouping interval will be used throughout; the other grouping intervals
55  * will be ignored. For example, the following format patterns all produce the same result.
56  * If the number to format is 123456789, each will produce 123,456,789.
57  * <blockquote>
58  * <ul>
59  * <li>pattern="#,##,###,###"</li>
60  * <li>pattern="######,###"</li>
61  * <li>pattern="##,####,###"</li>
62  * </ul>
63  * </blockquote>
64  *
65  * <p>The <code>type</code> attribute specifies three common
66  * kinds of formatting to be applied to the number.
67  * The valid values for the <code>type</code> attribute are:
68  * <blockquote>
69  * <ul>
70  * <li><code>number</code></li>
71  * <li><code>currency</code></li>
72  * <li><code>percent</code></li>
73  * </ul>
74  * </blockquote>
75  *
76  * <p>The <code>country</code> attribute takes an upper-case,
77  * two-letter code as defined by ISO-3166.
78  *
79  * <p>The <code>language</code> attribute takes a lower-case,
80  * two-letter code as defined by ISO-639.
81  * @example In this first example, the value "12345678" is formatted
82  * to 12,345,678.00.
83  * <pre> &lt;netui:span value="12345678">
84  * &lt;netui:formatNumber pattern="#,###.00" />
85  * &lt;/netui:span></pre>
86  *
87  * <p>In the next sample, the value ".33" is formatted to 33%.</p>
88  * <pre> &lt;netui:span value=".33">
89  * &lt;netui:formatNumber type="percent" />
90  * &lt;/netui:span></pre>
91  *
92  * <p>In the next sample, the value "14.317" is formatted
93  * to $14.32.</p>
94  * <pre> &lt;netui:span value="14.317">
95  * &lt;netui:formatNumber country="US" language="en" type="currency" />
96  * &lt;/netui:span></pre>
97  * @netui:tag name="formatNumber" body-content="empty" description="A formatter used to format numbers."
98  */

99 public class FormatNumber extends FormatTag
100 {
101     /**
102      * The type of number format to be used.
103      */

104     protected String JavaDoc _type;
105
106     /**
107      * Return the name of the Tag.
108      */

109     public String JavaDoc getTagName()
110     {
111         return "FormatNumber";
112     }
113
114     /**
115      * Sets the type of number format to be used (number, currency, or percent).
116      * @param type the number format type.
117      * @jsptagref.attributedescription The type of the format to apply. Possible values are <code>number</code>, <code>currency</code>, or <code>percent</code>.
118      * @jsptagref.databindable false
119      * @jsptagref.attributesyntaxvalue <i>string_type</i>
120      * @netui:attribute required="false" rtexprvalue="true"
121      * description="The type of the format to apply. Possible values are number, currency, or percent."
122      */

123     public void setType(String JavaDoc type)
124             throws JspException JavaDoc
125     {
126         _type = setRequiredValueAttribute(type, "type");
127         if (_type != null) {
128             if (!type.equals("number") && !type.equals("currency") && !type.equals("percent")) {
129                 String JavaDoc s = Bundle.getString("Tags_NumberFormatWrongType");
130                 registerTagError(s, null);
131             }
132         }
133     }
134
135     /**
136      * Create the internal Formatter instance and perform the formatting.
137      * @throws JspException if a JSP exception has occurred
138      */

139     public void doTag()
140             throws JspException JavaDoc
141     {
142         JspTag JavaDoc parentTag = SimpleTagSupport.findAncestorWithClass(this, IFormattable.class);
143
144         // if there are errors we need to either add these to the parent AbstractBastTag or report an error.
145
if (hasErrors()) {
146             if (parentTag instanceof IFormattable) {
147                 IFormattable parent = (IFormattable) parentTag;
148                 parent.formatterHasError();
149             }
150             reportErrors();
151             return;
152         }
153
154         // if there are no errors then add this to the parent as a formatter.
155
if (parentTag instanceof IFormattable) {
156             NumberFormatter formatter = new NumberFormatter();
157             formatter.setPattern(_pattern);
158             formatter.setType(_type);
159             formatter.setLocale(getLocale());
160             IFormattable parent = (IFormattable) parentTag;
161             parent.addFormatter(formatter);
162         }
163         else {
164             String JavaDoc s = Bundle.getString("Tags_FormattableParentRequired");
165             registerTagError(s, null);
166             reportErrors();
167         }
168     }
169
170     /**
171      * Internal FormatTag.Formatter which uses NumberFormat.
172      */

173     public static class NumberFormatter extends FormatTag.Formatter
174     {
175         private String JavaDoc type;
176         private Locale JavaDoc locale;
177
178         public void setType(String JavaDoc type)
179         {
180             this.type = type;
181         }
182
183         public void setLocale(Locale JavaDoc locale)
184         {
185             this.locale = locale;
186         }
187
188         public String JavaDoc format(Object JavaDoc dataToFormat) throws JspException JavaDoc
189         {
190             if (dataToFormat == null) {
191                 return null;
192             }
193             InternalStringBuilder formattedString = new InternalStringBuilder(32);
194             DecimalFormat JavaDoc numberFormat = null;
195
196             // get the number format. The type has been validated when it was set on the tag.
197
if (locale == null) {
198                 if ((type == null) || (type.equals("number"))) {
199                     numberFormat = (DecimalFormat JavaDoc) java.text.NumberFormat.getNumberInstance();
200                 }
201                 else if (type.equals("currency")) {
202                     numberFormat = (DecimalFormat JavaDoc) java.text.NumberFormat.getCurrencyInstance();
203                 }
204                 else if (type.equals("percent")) {
205                     numberFormat = (DecimalFormat JavaDoc) java.text.NumberFormat.getPercentInstance();
206                 }
207                 else {
208                     assert(false) : "Invalid type was found:" + type;
209                 }
210             }
211             else {
212                 if ((type == null) || (type.equals("number"))) {
213                     numberFormat = (DecimalFormat JavaDoc) java.text.NumberFormat.getNumberInstance(locale);
214                 }
215                 else if (type.equals("currency")) {
216                     numberFormat = (DecimalFormat JavaDoc) java.text.NumberFormat.getCurrencyInstance(locale);
217                 }
218                 else if (type.equals("percent")) {
219                     numberFormat = (DecimalFormat JavaDoc) java.text.NumberFormat.getPercentInstance(locale);
220                 }
221                 else {
222                     assert(false) : "Invalid type was found:" + type;
223                 }
224             }
225
226             // format the number, apply the pattern specified
227
try {
228                 if (getPattern() != null)
229                     numberFormat.applyPattern(getPattern());
230             }
231             catch (Exception JavaDoc e) {
232                 throw new JspException JavaDoc(Bundle.getString("Tags_NumberFormatPatternException", e.getMessage()), e);
233             }
234
235             // parse the number
236
if (dataToFormat.toString().length() == 0) {
237                 return "";
238             }
239             try {
240                 double number = Double.parseDouble(dataToFormat.toString());
241                 formattedString.append(numberFormat.format(number));
242             }
243             catch (Exception JavaDoc e) {
244                 throw new JspException JavaDoc(Bundle.getString("Tags_NumberFormatParseException", e.getMessage()), e);
245             }
246
247             return formattedString.toString();
248
249         }
250     }
251 }
252
Popular Tags