KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > i18n > 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.i18n;
18
19 import java.io.IOException JavaDoc;
20 import java.text.DecimalFormat JavaDoc;
21 import java.text.DecimalFormatSymbols JavaDoc;
22 import java.text.Format JavaDoc;
23 import java.text.NumberFormat JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.Locale JavaDoc;
26
27 /** Formats a {@link Number} instance using a {@link java.util.Locale} and the
28   * {@link NumberFormat} or a {@link DecimalFormat} if a pattern is specified.
29   *
30   * @author James Strachan
31   * @version $Revision: 1.5 $
32   */

33 public class FormatNumberTag extends FormatTagSupport {
34     
35     protected static final String JavaDoc _tagname = "i18n:formatNumber";
36
37     private NumberFormat JavaDoc format;
38     private String JavaDoc pattern;
39
40
41     // Tag interface
42
//-------------------------------------------------------------------------
43
public void release() {
44         super.release();
45         format = null;
46         pattern = null;
47     }
48     
49     // Properties
50
//-------------------------------------------------------------------------
51

52     /** If no {@link NumberFormat} is configured then use a
53       * {@link DecimalFormat} instance if a pattern is configured else
54       * use the default NumberFormat for the {@link java.util.Locale}
55       */

56     public Format JavaDoc getFormat() {
57         if ( format == null ) {
58             String JavaDoc pattern = getPattern();
59             if ( pattern != null ) {
60                 format = getPatternFormat( pattern );
61             }
62             if ( pattern == null ) {
63                 format = getNumberFormat();
64             }
65         }
66         return format;
67     }
68
69     public void setFormat( NumberFormat JavaDoc format ) {
70         this.format = format;
71     }
72
73     public String JavaDoc getPattern() {
74         return pattern;
75     }
76     
77     public void setPattern( String JavaDoc pattern ) {
78         this.pattern = pattern;
79     }
80
81     // Implementation methods
82
//-------------------------------------------------------------------------
83
protected NumberFormat JavaDoc getPatternFormat( String JavaDoc pattern ) {
84         // XXXX: Use a Locale based cache for these objects as per Craig's
85
// XXXX: suggestion
86
return new DecimalFormat JavaDoc( pattern, new DecimalFormatSymbols JavaDoc( getLocale() ) );
87     }
88     
89     /** @return the default number formatter for the current Locale
90       */

91     protected NumberFormat JavaDoc getNumberFormat() {
92         return NumberFormat.getInstance( getLocale() );
93         // XXXX: what's the difference with the following:=
94
// return NumberFormat.getNumberInstance( getLocale() );
95
}
96     
97 }
98
Popular Tags