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