1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package javax.faces.convert;
20
21 import javax.faces.component.UIComponent;
22 import javax.faces.context.FacesContext;
23
24 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFConverter;
25
26
27
28
29
30
31
32 @JSFConverter
33 public class IntegerConverter
34 implements Converter
35 {
36
37 public static final String CONVERTER_ID = "javax.faces.Integer";
38 public static final String STRING_ID = "javax.faces.converter.STRING";
39 public static final String INTEGER_ID = "javax.faces.converter.IntegerConverter.INTEGER";
40
41
42 public IntegerConverter()
43 {
44 }
45
46
47 public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value)
48 {
49 if (facesContext == null) throw new NullPointerException("facesContext");
50 if (uiComponent == null) throw new NullPointerException("uiComponent");
51
52 if (value != null)
53 {
54 value = value.trim();
55 if (value.length() > 0)
56 {
57 try
58 {
59 return Integer.valueOf(value);
60 }
61 catch (NumberFormatException e)
62 {
63 throw new ConverterException(_MessageUtils.getErrorMessage(facesContext,
64 INTEGER_ID,
65 new Object[]{value,"21",_MessageUtils.getLabel(facesContext, uiComponent)}), e);
66 }
67 }
68 }
69 return null;
70 }
71
72 public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value)
73 {
74 if (facesContext == null) throw new NullPointerException("facesContext");
75 if (uiComponent == null) throw new NullPointerException("uiComponent");
76
77 if (value == null)
78 {
79 return "";
80 }
81 if (value instanceof String)
82 {
83 return (String)value;
84 }
85 try
86 {
87 return Integer.toString(((Number)value).intValue());
88 }
89 catch (Exception e)
90 {
91 throw new ConverterException(_MessageUtils.getErrorMessage(facesContext, STRING_ID, new Object[]{value,_MessageUtils.getLabel(facesContext, uiComponent)}),e);
92 }
93 }
94 }