Source code: javax/faces/convert/BigIntegerConverter.java
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;
21
22 /**
23 * @author Thomas Spiegl (latest modification by $Author: grantsmith $)
24 * @version $Revision: 169646 $ $Date: 2005-05-11 11:34:57 -0400 (Wed, 11 May 2005) $
25 */
26 public class BigIntegerConverter
27 implements Converter
28 {
29 private static final String CONVERSION_MESSAGE_ID = "javax.faces.convert.BigIntegerConverter.CONVERSION";
30
31 // FIELDS
32 public static final String CONVERTER_ID = "javax.faces.BigInteger";
33
34 // CONSTRUCTORS
35 public BigIntegerConverter()
36 {
37 }
38
39 // METHODS
40 public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value)
41 {
42 if (facesContext == null) throw new NullPointerException("facesContext");
43 if (uiComponent == null) throw new NullPointerException("uiComponent");
44
45 if (value != null)
46 {
47 value = value.trim();
48 if (value.length() > 0)
49 {
50 try
51 {
52 return new BigInteger(value.trim());
53 }
54 catch (NumberFormatException e)
55 {
56 throw new ConverterException(_MessageUtils.getErrorMessage(facesContext,
57 CONVERSION_MESSAGE_ID,
58 new Object[]{uiComponent.getId(),value}), e);
59 }
60 }
61 }
62 return null;
63 }
64
65 public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value)
66 {
67 if (facesContext == null) throw new NullPointerException("facesContext");
68 if (uiComponent == null) throw new NullPointerException("uiComponent");
69
70 if (value == null)
71 {
72 return "";
73 }
74 if (value instanceof String)
75 {
76 return (String)value;
77 }
78 try
79 {
80 return ((BigInteger)value).toString();
81 }
82 catch (Exception e)
83 {
84 throw new ConverterException(e);
85 }
86 }
87
88 }