1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package javax.faces.convert; |
17 | |
|
18 | |
import java.util.Locale; |
19 | |
|
20 | |
import javax.faces.component.UIComponent; |
21 | |
import javax.faces.context.FacesContext; |
22 | |
import javax.faces.internal.ConvertUtil; |
23 | |
import javax.faces.internal.FacesMessageUtil; |
24 | |
|
25 | |
import org.seasar.framework.util.AssertionUtil; |
26 | |
import org.seasar.framework.util.NumberConversionUtil; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
public class IntegerConverter implements Converter { |
32 | |
|
33 | |
public static final String CONVERTER_ID = "javax.faces.Integer"; |
34 | |
|
35 | 2 | public static final String CONVERSION_OBJECT_ID = IntegerConverter.class |
36 | |
.getName() |
37 | |
+ ".CONVERSION"; |
38 | |
|
39 | 1 | public static final String CONVERSION_STRING_ID = IntegerConverter.class |
40 | |
.getName() |
41 | |
+ ".CONVERSION_STRING"; |
42 | |
|
43 | 35 | public IntegerConverter() { |
44 | 35 | } |
45 | |
|
46 | |
public Object getAsObject(FacesContext context, UIComponent component, |
47 | |
String value) throws ConverterException { |
48 | 29 | AssertionUtil.assertNotNull("FacesContext", context); |
49 | 28 | AssertionUtil.assertNotNull("UIComponent", component); |
50 | 27 | if (value == null) { |
51 | 1 | return null; |
52 | |
} |
53 | 26 | value = value.trim(); |
54 | 26 | if (value.length() < 1) { |
55 | 1 | return null; |
56 | |
} |
57 | 25 | Locale locale = context.getViewRoot().getLocale(); |
58 | 25 | value = NumberConversionUtil.removeDelimeter(value, locale); |
59 | |
try { |
60 | 25 | return Integer.valueOf(value); |
61 | 16 | } catch (Exception e) { |
62 | 16 | Object[] args = ConvertUtil.createExceptionMessageArgs(component, |
63 | |
value); |
64 | 16 | throw new ConverterException(FacesMessageUtil.getMessage(context, |
65 | |
getObjectMessageId(), args), e); |
66 | |
} |
67 | |
} |
68 | |
|
69 | |
public String getAsString(FacesContext context, UIComponent component, |
70 | |
Object value) throws ConverterException { |
71 | 6 | AssertionUtil.assertNotNull("FacesContext", context); |
72 | 5 | AssertionUtil.assertNotNull("UIComponent", component); |
73 | 4 | if (value == null) { |
74 | 1 | return ""; |
75 | |
} |
76 | |
try { |
77 | 3 | return (value instanceof String) ? (String) value : (Integer |
78 | |
.toString(((Integer) value).intValue())); |
79 | 1 | } catch (Exception e) { |
80 | 1 | Object[] args = ConvertUtil.createExceptionMessageArgs(component, |
81 | |
value); |
82 | 1 | throw new ConverterException(FacesMessageUtil.getMessage(context, |
83 | |
getStringMessageId(), args), e); |
84 | |
} |
85 | |
} |
86 | |
|
87 | |
protected String getObjectMessageId() { |
88 | 16 | return CONVERSION_OBJECT_ID; |
89 | |
} |
90 | |
|
91 | |
protected String getStringMessageId() { |
92 | 1 | return CONVERSION_STRING_ID; |
93 | |
} |
94 | |
|
95 | |
} |