KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > 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.forms.datatype.convertor;
17
18 import org.xml.sax.ContentHandler JavaDoc;
19 import org.xml.sax.SAXException JavaDoc;
20
21 import java.util.Locale JavaDoc;
22 import java.text.DecimalFormat JavaDoc;
23 import java.text.NumberFormat JavaDoc;
24 import java.text.ParseException JavaDoc;
25 import java.math.BigDecimal JavaDoc;
26 import java.math.BigInteger JavaDoc;
27
28 /**
29  * A Convertor for {@link BigDecimal}s backed by the
30  * {@link java.text.DecimalFormat DecimalFormat} class.
31  *
32  * <p>It can be configured to use one of these variants: integer,
33  * number, currency or percent.
34  *
35  * <p>Alternatively, a <strong>formatting pattern</strong> can be used. This can either be a locale-dependent
36  * or locale-independent formatting pattern. When looking up a formatting pattern, a mechansim
37  * similar to resource bundle lookup is used. Suppose the locale is nl-BE, then first a formatting
38  * pattern for nl-BE will be sought, then one for nl, and if that is not
39  * found, finally the locale-independent formatting pattern will be used.
40  *
41  * @version $Id: FormattingDecimalConvertor.java 157080 2005-03-11 14:04:07Z sylvain $
42  */

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