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 CharacterConverter |
34 | |
implements Converter |
35 | |
{ |
36 | |
|
37 | |
public static final String CONVERTER_ID = "javax.faces.Character"; |
38 | |
public static final String STRING_ID = "javax.faces.converter.STRING"; |
39 | |
public static final String CHARACTER_ID = "javax.faces.converter.CharacterConverter.CHARACTER"; |
40 | |
|
41 | |
|
42 | |
public CharacterConverter() |
43 | 0 | { |
44 | 0 | } |
45 | |
|
46 | |
|
47 | |
public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value) |
48 | |
{ |
49 | 0 | if (facesContext == null) throw new NullPointerException("facesContext"); |
50 | 0 | if (uiComponent == null) throw new NullPointerException("uiComponent"); |
51 | |
|
52 | 0 | if (value != null) |
53 | |
{ |
54 | 0 | value = value.trim(); |
55 | 0 | if (value.length() > 0) |
56 | |
{ |
57 | |
try { |
58 | 0 | return new Character(value.charAt(0)); |
59 | 0 | }catch(Exception e) { |
60 | 0 | throw new ConverterException(_MessageUtils.getErrorMessage(facesContext, |
61 | |
CHARACTER_ID, |
62 | |
new Object[]{value,_MessageUtils.getLabel(facesContext, uiComponent)}), e); |
63 | |
} |
64 | |
} |
65 | |
} |
66 | 0 | return null; |
67 | |
} |
68 | |
|
69 | |
public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) |
70 | |
{ |
71 | 0 | if (facesContext == null) throw new NullPointerException("facesContext"); |
72 | 0 | if (uiComponent == null) throw new NullPointerException("uiComponent"); |
73 | |
|
74 | 0 | if (value == null) |
75 | |
{ |
76 | 0 | return ""; |
77 | |
} |
78 | 0 | if (value instanceof String) |
79 | |
{ |
80 | 0 | return (String)value; |
81 | |
} |
82 | |
try |
83 | |
{ |
84 | 0 | return ((Character)value).toString(); |
85 | |
} |
86 | 0 | catch (Exception e) |
87 | |
{ |
88 | 0 | throw new ConverterException(_MessageUtils.getErrorMessage(facesContext, STRING_ID, new Object[]{value,_MessageUtils.getLabel(facesContext, uiComponent)}),e); |
89 | |
} |
90 | |
} |
91 | |
|
92 | |
} |