KickJava   Java API By Example, From Geeks To Geeks.

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


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.BigInteger JavaDoc;
21
22 /**
23  * $Log: BigIntegerConverter.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 BigIntegerConverter
38         implements Converter
39 {
40     private static final String JavaDoc CONVERSION_MESSAGE_ID = "javax.faces.convert.BigIntegerConverter.CONVERSION";
41
42     // FIELDS
43
public static final String JavaDoc CONVERTER_ID = "javax.faces.BigInteger";
44
45     // CONSTRUCTORS
46
public BigIntegerConverter()
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             value = value.trim();
59             if (value.length() > 0)
60             {
61                 try
62                 {
63                     return new BigInteger JavaDoc(value.trim());
64                 }
65                 catch (NumberFormatException JavaDoc e)
66                 {
67                     throw new ConverterException(_MessageUtils.getErrorMessage(facesContext,
68                                                                                CONVERSION_MESSAGE_ID,
69                                                                                new Object JavaDoc[]{uiComponent.getId(),value}), e);
70                 }
71             }
72         }
73         return null;
74     }
75
76     public String JavaDoc getAsString(FacesContext facesContext, UIComponent uiComponent, Object JavaDoc value)
77     {
78         if (facesContext == null) throw new NullPointerException JavaDoc("facesContext");
79         if (uiComponent == null) throw new NullPointerException JavaDoc("uiComponent");
80
81         if (value == null)
82         {
83             return "";
84         }
85         if (value instanceof String JavaDoc)
86         {
87             return (String JavaDoc)value;
88         }
89         try
90         {
91             return ((BigInteger JavaDoc)value).toString();
92         }
93         catch (Exception JavaDoc e)
94         {
95             throw new ConverterException(e);
96         }
97     }
98
99 }
100
Popular Tags