KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > 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.woody.datatype.convertor;
17
18 import org.outerj.i18n.DecimalFormat;
19
20 import java.util.Locale JavaDoc;
21 import java.text.ParseException JavaDoc;
22
23 /**
24  * A Convertor for {@link Integer}s backed by the
25  * {@link java.text.DecimalFormat DecimalFormat} class.
26  *
27  * <p>This class is mostly the same as the {@link FormattingDecimalConvertor},
28  * so see there for more information.
29  *
30  * @author <a HREF="mailto:antonio@apache.org">Antonio Gallardo</a>
31  * @version CVS $Id: FormattingIntegerConvertor.java 30932 2004-07-29 17:35:38Z vgritsenko $
32 */

33 public class FormattingIntegerConvertor extends FormattingDecimalConvertor {
34
35     public FormattingIntegerConvertor() {
36         super();
37     }
38
39     public Object JavaDoc convertFromString(String JavaDoc value, Locale JavaDoc locale, Convertor.FormatCache formatCache) {
40         DecimalFormat decimalFormat = getDecimalFormat(locale, formatCache);
41         try {
42             Number JavaDoc decimalValue = decimalFormat.parse(value);
43             if (decimalValue instanceof Integer JavaDoc)
44                 return decimalValue;
45             else
46                 return new Integer JavaDoc(decimalValue.intValue());
47         } catch (ParseException JavaDoc e) {
48             return null;
49         }
50     }
51
52     protected int getDefaultVariant() {
53         return INTEGER;
54     }
55
56     public Class JavaDoc getTypeClass() {
57         return Integer JavaDoc.class;
58     }
59 }
60
Popular Tags