KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > faces > convert > BigDecimalConverter


1 /*
2  * Copyright 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 javax.faces.convert;
17
18 import javax.faces.component.UIComponent;
19 import javax.faces.context.FacesContext;
20 import java.math.BigDecimal JavaDoc;
21
22 /**
23  * $Log: BigDecimalConverter.java,v $
24  * Revision 1.8 2004/07/01 22:00:51 mwessendorf
25  * ASF switch
26  *
27  * Revision 1.7 2004/06/07 13:40:37 mwessendorf
28  * solved Feature Request #966892
29  *
30  * Revision 1.6 2004/03/26 12:08:41 manolito
31  * Exceptions in getAsString now catched and
32  * more relaxed Number casting in all number converters
33  *
34  * @author Thomas Spiegl (latest modification by $Author: mwessendorf $)
35  * @version $Revision: 1.8 $ $Date: 2004/07/01 22:00:51 $
36  */

37 public class BigDecimalConverter
38         implements Converter
39 {
40     private static final String JavaDoc CONVERSION_MESSAGE_ID = "javax.faces.convert.BigDecimalConverter.CONVERSION";
41
42     // FIELDS
43
public static final String JavaDoc CONVERTER_ID = "javax.faces.BigDecimal";
44
45     // CONSTRUCTORS
46
public BigDecimalConverter()
47     {
48     }
49
50     // METHODS
51
public Object JavaDoc getAsObject(FacesContext facesContext, UIComponent uiComponent, String JavaDoc value)
52     {
53         if (facesContext == null) throw new NullPointerException JavaDoc("facesContext");
54         if (uiComponent == null) throw new NullPointerException JavaDoc("uiComponent");
55
56         if (value != null)
57         {
58             {
59                 value = value.trim();
60                 if (value.length() > 0)
61                 {
62                     try
63                     {
64                         return new BigDecimal JavaDoc(value.trim());
65                     }
66                     catch (NumberFormatException JavaDoc e)
67                     {
68                         throw new ConverterException(_MessageUtils.getErrorMessage(facesContext,
69                                                                                    CONVERSION_MESSAGE_ID,
70                                                                                    new Object JavaDoc[]{uiComponent.getId(),value}), e);
71                     }
72                 }
73             }
74         }
75         return null;
76     }
77
78     public String JavaDoc getAsString(FacesContext facesContext, UIComponent uiComponent, Object JavaDoc value)
79     {
80         if (facesContext == null) throw new NullPointerException JavaDoc("facesContext");
81         if (uiComponent == null) throw new NullPointerException JavaDoc("uiComponent");
82
83         if (value == null)
84         {
85             return "";
86         }
87         if (value instanceof String JavaDoc)
88         {
89             return (String JavaDoc)value;
90         }
91         try
92         {
93             return ((BigDecimal JavaDoc)value).toString();
94         }
95         catch (Exception JavaDoc e)
96         {
97             throw new ConverterException(e);
98         }
99     }
100 }
101
Popular Tags