KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > 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.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 Long}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  * @version $Id: FormattingLongConvertor.java 157080 2005-03-11 14:04:07Z sylvain $
30  */

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