KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > datatype > convertor > FormattingDecimalConvertor


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 package org.apache.cocoon.woody.datatype.convertor;
17
18 import org.outerj.i18n.I18nSupport;
19 import org.outerj.i18n.DecimalFormat;
20
21 import java.util.Locale JavaDoc;
22 import java.text.ParseException JavaDoc;
23 import java.math.BigDecimal JavaDoc;
24 import java.math.BigInteger JavaDoc;
25
26 /**
27  * A Convertor for {@link BigDecimal}s backed by the
28  * {@link java.text.DecimalFormat DecimalFormat} class.
29  *
30  * <p>It can be configured to use one of these variants: integer,
31  * number, currency or percent.
32  *
33  * <p>Alternatively, a <strong>formatting pattern</strong> can be used. This can either be a locale-dependent
34  * or locale-independent formatting pattern. When looking up a formatting pattern, a mechansim
35  * similar to resource bundle lookup is used. Suppose the locale is nl-BE, then first a formatting
36  * pattern for nl-BE will be sought, then one for nl, and if that is not
37  * found, finally the locale-independent formatting pattern will be used.
38  *
39  * <p>Note: the earlier statement about the fact that this class uses java.text.DecimalFormat
40  * is not entirely correct. In fact, it uses a small wrapper class that will either delegate to
41  * java.text.DecimalFormat or com.ibm.icu.text.DecimalFormat. The com.ibm version will automatically
42  * be used if it is present on the classpath, otherwise the java.text version will be used.
43  *
44  * @version CVS $Id: FormattingDecimalConvertor.java 36217 2004-08-11 11:42:01Z cziegeler $
45  */

46 public class FormattingDecimalConvertor implements Convertor {
47     private int variant;
48     /** Locale-specific formatting patterns. */
49     private LocaleMap localizedPatterns;
50     /** Non-locale specific formatting pattern. */
51     private String JavaDoc nonLocalizedPattern;
52
53     public static final int INTEGER = 0;
54     public static final int NUMBER = 1;
55     public static final int CURRENCY = 2;
56     public static final int PERCENT = 3;
57
58     public FormattingDecimalConvertor() {
59         this.variant = getDefaultVariant();
60         this.localizedPatterns = new LocaleMap();
61     }
62
63     protected int getDefaultVariant() {
64         return NUMBER;
65     }
66
67     public Object JavaDoc convertFromString(String JavaDoc value, Locale JavaDoc locale, Convertor.FormatCache formatCache) {
68         DecimalFormat decimalFormat = getDecimalFormat(locale, formatCache);
69         Number JavaDoc decimalValue;
70         try {
71             decimalValue = decimalFormat.parse(value);
72         } catch (ParseException JavaDoc e) {
73             return null;
74         }
75
76         if (decimalValue instanceof BigDecimal JavaDoc) {
77             // no need for conversion
78
} else if (decimalValue instanceof Integer JavaDoc) {
79                 decimalValue = new BigDecimal JavaDoc(decimalValue .intValue());
80         } else if (decimalValue instanceof Long JavaDoc) {
81                 decimalValue = new BigDecimal JavaDoc(decimalValue.longValue());
82         } else if (decimalValue instanceof Double JavaDoc) {
83                 decimalValue = new BigDecimal JavaDoc(decimalValue.doubleValue());
84         } else if (decimalValue instanceof BigInteger JavaDoc) {
85                 decimalValue = new BigDecimal JavaDoc((BigInteger JavaDoc)decimalValue);
86         } else {
87                 return null;
88         }
89
90             return decimalValue;
91     }
92
93     public String JavaDoc convertToString(Object JavaDoc value, Locale JavaDoc locale, Convertor.FormatCache formatCache) {
94         DecimalFormat decimalFormat = getDecimalFormat(locale, formatCache);
95         return decimalFormat.format(value);
96     }
97
98     protected final DecimalFormat getDecimalFormat(Locale JavaDoc locale, Convertor.FormatCache formatCache) {
99         DecimalFormat decimalFormat = null;
100         if (formatCache != null)
101             decimalFormat = (DecimalFormat)formatCache.get();
102         if (decimalFormat == null) {
103             decimalFormat = getDecimalFormat(locale);
104             if (formatCache != null)
105                 formatCache.store(decimalFormat);
106         }
107         return decimalFormat;
108     }
109
110     private DecimalFormat getDecimalFormat(Locale JavaDoc locale) {
111         DecimalFormat decimalFormat = null;
112
113         switch (variant) {
114             case INTEGER:
115                 decimalFormat = I18nSupport.getInstance().getIntegerFormat(locale);
116                 break;
117             case NUMBER:
118                 decimalFormat = I18nSupport.getInstance().getNumberFormat(locale);
119                 break;
120             case CURRENCY:
121                 decimalFormat = I18nSupport.getInstance().getCurrencyFormat(locale);
122                 break;
123             case PERCENT:
124                 decimalFormat = I18nSupport.getInstance().getPercentFormat(locale);
125                 break;
126         }
127
128         String JavaDoc pattern = (String JavaDoc)localizedPatterns.get(locale);
129
130         if (pattern != null)
131             decimalFormat.applyLocalizedPattern(pattern);
132         else if (nonLocalizedPattern != null)
133             decimalFormat.applyPattern(nonLocalizedPattern);
134
135         return decimalFormat;
136     }
137
138     public void setVariant(int variant) {
139         if (variant != INTEGER && variant != NUMBER && variant != CURRENCY && variant != PERCENT)
140             throw new IllegalArgumentException JavaDoc("Invalid value for variant parameter.");
141         this.variant = variant;
142     }
143
144     public void addFormattingPattern(Locale JavaDoc locale, String JavaDoc pattern) {
145         localizedPatterns.put(locale, pattern);
146     }
147
148     public void setNonLocalizedPattern(String JavaDoc pattern) {
149         this.nonLocalizedPattern = pattern;
150     }
151
152     public Class JavaDoc getTypeClass() {
153         return java.math.BigDecimal JavaDoc.class;
154     }
155 }
156
Popular Tags