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 {
46 }
47
48
49 public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value)
50 {
51 if (facesContext == null)
52 {
53 throw new NullPointerException("facesContext");
54 }
55 if (uiComponent == null)
56 {
57 throw new NullPointerException("uiComponent");
58 }
59
60 if (value != null)
61 {
62 {
63 value = value.trim();
64 if (value.length() > 0)
65 {
66 try
67 {
68 return new BigDecimal(value.trim());
69 }
70 catch (NumberFormatException e)
71 {
72 throw new ConverterException(_MessageUtils.getErrorMessage(facesContext,
73 DECIMAL_ID,
74 new Object[]{value,new BigDecimal(4815.16).toString(),
75 _MessageUtils.getLabel(facesContext, uiComponent)}), e);
76 }
77 }
78 }
79 }
80 return null;
81 }
82
83 public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value)
84 {
85 if (facesContext == null)
86 {
87 throw new NullPointerException("facesContext");
88 }
89 if (uiComponent == null)
90 {
91 throw new NullPointerException("uiComponent");
92 }
93
94 if (value == null)
95 {
96 return "";
97 }
98 if (value instanceof String)
99 {
100 return (String)value;
101 }
102 try
103 {
104 return ((BigDecimal)value).toString();
105 }
106 catch (Exception e)
107 {
108 throw new ConverterException(_MessageUtils.getErrorMessage(facesContext, STRING_ID,
109 new Object[]{value,_MessageUtils.getLabel(facesContext, uiComponent)}),e);
110 }
111 }
112 }