KickJava   Java API By Example, From Geeks To Geeks.

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


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 Long}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  * @version CVS $Id: FormattingLongConvertor.java 30932 2004-07-29 17:35:38Z vgritsenko $
31  */

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