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