KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > datatype > convertor > FormattingIntegerConvertor


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 java.util.Locale JavaDoc;
19 import java.text.DecimalFormat JavaDoc;
20 import java.text.ParseException JavaDoc;
21
22 /**
23  * A Convertor for {@link Integer}s backed by the
24  * {@link java.text.DecimalFormat DecimalFormat} class.
25  *
26  * <p>This class is mostly the same as the {@link FormattingDecimalConvertor},
27  * so see there for more information.
28  *
29  * @author <a HREF="mailto:antonio@apache.org">Antonio Gallardo</a>
30  * @version $Id: FormattingIntegerConvertor.java 157080 2005-03-11 14:04:07Z sylvain $
31 */

32 public class FormattingIntegerConvertor extends FormattingDecimalConvertor {
33
34     public FormattingIntegerConvertor() {
35         super();
36     }
37
38     public ConversionResult convertFromString(String JavaDoc value, Locale JavaDoc locale, Convertor.FormatCache formatCache) {
39         // Some locales (e.g. "fr") produce non-breaking spaces sent back as space by the browser
40
value = value.replace(' ', (char)160);
41         DecimalFormat JavaDoc decimalFormat = getDecimalFormat(locale, formatCache);
42         try {
43             Number JavaDoc decimalValue = decimalFormat.parse(value);
44             if (decimalValue instanceof Integer JavaDoc)
45                 return new ConversionResult(decimalValue);
46             else
47                 return new ConversionResult(new Integer JavaDoc(decimalValue.intValue()));
48         } catch (ParseException JavaDoc e) {
49             return ConversionResult.create("integer");
50         }
51     }
52
53     protected int getDefaultVariant() {
54         return INTEGER;
55     }
56
57     public Class JavaDoc getTypeClass() {
58         return Integer JavaDoc.class;
59     }
60 }
61
Popular Tags