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 | |
import java.math.BigDecimal; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
@JSFConverter |
35 | |
public class BigDecimalConverter |
36 | |
implements Converter |
37 | |
{ |
38 | |
|
39 | |
public static final String CONVERTER_ID = "javax.faces.BigDecimal"; |
40 | |
public static final String STRING_ID = "javax.faces.converter.STRING"; |
41 | |
public static final String DECIMAL_ID = "javax.faces.converter.BigDecimalConverter.DECIMAL"; |
42 | |
|
43 | |
|
44 | |
public BigDecimalConverter() |
45 | 0 | { |
46 | 0 | } |
47 | |
|
48 | |
|
49 | |
public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value) |
50 | |
{ |
51 | 0 | if (facesContext == null) throw new NullPointerException("facesContext"); |
52 | 0 | if (uiComponent == null) throw new NullPointerException("uiComponent"); |
53 | |
|
54 | 0 | if (value != null) |
55 | |
{ |
56 | |
{ |
57 | 0 | value = value.trim(); |
58 | 0 | if (value.length() > 0) |
59 | |
{ |
60 | |
try |
61 | |
{ |
62 | 0 | return new BigDecimal(value.trim()); |
63 | |
} |
64 | 0 | catch (NumberFormatException e) |
65 | |
{ |
66 | 0 | throw new ConverterException(_MessageUtils.getErrorMessage(facesContext, |
67 | |
DECIMAL_ID, |
68 | |
new Object[]{value,"4815,16",_MessageUtils.getLabel(facesContext, uiComponent)}), e); |
69 | |
} |
70 | |
} |
71 | |
} |
72 | |
} |
73 | 0 | return null; |
74 | |
} |
75 | |
|
76 | |
public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) |
77 | |
{ |
78 | 0 | if (facesContext == null) throw new NullPointerException("facesContext"); |
79 | 0 | if (uiComponent == null) throw new NullPointerException("uiComponent"); |
80 | |
|
81 | 0 | if (value == null) |
82 | |
{ |
83 | 0 | return ""; |
84 | |
} |
85 | 0 | if (value instanceof String) |
86 | |
{ |
87 | 0 | return (String)value; |
88 | |
} |
89 | |
try |
90 | |
{ |
91 | 0 | return ((BigDecimal)value).toString(); |
92 | |
} |
93 | 0 | catch (Exception e) |
94 | |
{ |
95 | 0 | throw new ConverterException(_MessageUtils.getErrorMessage(facesContext, STRING_ID, new Object[]{value,_MessageUtils.getLabel(facesContext, uiComponent)}),e); |
96 | |
} |
97 | |
} |
98 | |
} |