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.text.DecimalFormatSymbols; |
27 | |
import java.util.Locale; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
@JSFConverter |
33 | |
public class DoubleConverter |
34 | |
implements Converter |
35 | |
{ |
36 | |
|
37 | |
public static final String CONVERTER_ID = "javax.faces.Double"; |
38 | |
public static final String STRING_ID = "javax.faces.converter.STRING"; |
39 | |
public static final String DOUBLE_ID = "javax.faces.converter.DoubleConverter.DOUBLE"; |
40 | |
|
41 | |
|
42 | |
public DoubleConverter() |
43 | 6 | { |
44 | 6 | } |
45 | |
|
46 | |
|
47 | |
public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value) |
48 | |
{ |
49 | 20 | if (facesContext == null) |
50 | |
{ |
51 | 0 | throw new NullPointerException("facesContext"); |
52 | |
} |
53 | 20 | if (uiComponent == null) |
54 | |
{ |
55 | 0 | throw new NullPointerException("uiComponent"); |
56 | |
} |
57 | |
|
58 | 20 | if (value != null) |
59 | |
{ |
60 | 20 | value = value.trim(); |
61 | 20 | if (value.length() > 0) |
62 | |
{ |
63 | |
try |
64 | |
{ |
65 | 20 | value = fixLocale(facesContext, value); |
66 | 20 | return this.stringToDouble(value); |
67 | |
} |
68 | 12 | catch (NumberFormatException e) |
69 | |
{ |
70 | 12 | throw new ConverterException(_MessageUtils.getErrorMessage(facesContext, |
71 | |
DOUBLE_ID, |
72 | |
new Object[]{value,"4214",_MessageUtils.getLabel(facesContext, uiComponent)}), e); |
73 | |
} |
74 | |
|
75 | |
} |
76 | |
} |
77 | 0 | return null; |
78 | |
} |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
private String fixLocale(FacesContext facesContext, String value) |
92 | |
{ |
93 | 20 | Locale loc = facesContext.getViewRoot().getLocale(); |
94 | 20 | if (loc == null || loc == Locale.US) |
95 | |
{ |
96 | |
|
97 | 14 | return value; |
98 | |
} |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | 6 | DecimalFormatSymbols dfs = new DecimalFormatSymbols(loc); |
104 | |
|
105 | 6 | char decSep = dfs.getDecimalSeparator(); |
106 | |
|
107 | |
|
108 | |
|
109 | 6 | if (decSep != '.' && value.lastIndexOf(decSep) >= 0) |
110 | |
{ |
111 | 4 | StringBuffer sbVal = new StringBuffer(); |
112 | |
|
113 | |
|
114 | 34 | for (int i = 0; i < value.length(); i++) |
115 | |
{ |
116 | 30 | if (value.charAt(i) == decSep) |
117 | |
{ |
118 | 4 | sbVal.append('.'); |
119 | 4 | continue; |
120 | |
} |
121 | |
|
122 | |
|
123 | 26 | sbVal.append(value.charAt(i)); |
124 | |
} |
125 | |
|
126 | 4 | value = sbVal.toString(); |
127 | |
} |
128 | |
|
129 | |
|
130 | 6 | return value; |
131 | |
} |
132 | |
|
133 | |
private Double stringToDouble(String value) |
134 | |
{ |
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | 20 | if (value.length() >= 23) |
141 | |
{ |
142 | 10 | StringBuffer normalized = new StringBuffer(); |
143 | 290 | for (int i=0; i< value.length(); i++) |
144 | |
{ |
145 | 280 | char c = value.charAt(i); |
146 | 280 | if ( c != '.') |
147 | |
{ |
148 | 270 | normalized.append(c); |
149 | |
} |
150 | |
} |
151 | |
|
152 | 10 | String normalizedString = normalized.toString(); |
153 | 10 | if (normalizedString.contains("22250738585072012") && normalizedString.contains("e-")) |
154 | |
{ |
155 | |
|
156 | 10 | throw new NumberFormatException("Not Allowed! This value could possibly kill the VM!"); |
157 | |
} |
158 | |
} |
159 | |
|
160 | |
|
161 | 10 | return Double.valueOf(value); |
162 | |
} |
163 | |
|
164 | |
public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) |
165 | |
{ |
166 | 0 | if (facesContext == null) |
167 | |
{ |
168 | 0 | throw new NullPointerException("facesContext"); |
169 | |
} |
170 | 0 | if (uiComponent == null) |
171 | |
{ |
172 | 0 | throw new NullPointerException("uiComponent"); |
173 | |
} |
174 | |
|
175 | 0 | if (value == null) |
176 | |
{ |
177 | 0 | return ""; |
178 | |
} |
179 | 0 | if (value instanceof String) |
180 | |
{ |
181 | 0 | return (String)value; |
182 | |
} |
183 | |
try |
184 | |
{ |
185 | 0 | return Double.toString(((Number)value).doubleValue()); |
186 | |
} |
187 | 0 | catch (Exception e) |
188 | |
{ |
189 | 0 | throw new ConverterException(_MessageUtils.getErrorMessage(facesContext, STRING_ID, |
190 | |
new Object[]{value,_MessageUtils.getLabel(facesContext, uiComponent)}),e); |
191 | |
} |
192 | |
} |
193 | |
} |