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 {
44 }
45
46
47 public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value)
48 {
49 if (facesContext == null)
50 {
51 throw new NullPointerException("facesContext");
52 }
53 if (uiComponent == null)
54 {
55 throw new NullPointerException("uiComponent");
56 }
57
58 if (value != null)
59 {
60 value = value.trim();
61 if (value.length() > 0)
62 {
63 try
64 {
65 value = fixLocale(facesContext, value);
66 return this.stringToDouble(value);
67 }
68 catch (NumberFormatException e)
69 {
70 throw new ConverterException(_MessageUtils.getErrorMessage(facesContext,
71 DOUBLE_ID,
72 new Object[]{value,"4214",_MessageUtils.getLabel(facesContext, uiComponent)}), e);
73 }
74
75 }
76 }
77 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 Locale loc = facesContext.getViewRoot().getLocale();
94 if (loc == null || loc == Locale.US)
95 {
96
97 return value;
98 }
99
100
101
102
103 DecimalFormatSymbols dfs = new DecimalFormatSymbols(loc);
104
105 char decSep = dfs.getDecimalSeparator();
106
107
108
109 if (decSep != '.' && value.lastIndexOf(decSep) >= 0)
110 {
111 StringBuffer sbVal = new StringBuffer();
112
113
114 for (int i = 0; i < value.length(); i++)
115 {
116 if (value.charAt(i) == decSep)
117 {
118 sbVal.append('.');
119 continue;
120 }
121
122
123 sbVal.append(value.charAt(i));
124 }
125
126 value = sbVal.toString();
127 }
128
129
130 return value;
131 }
132
133 private Double stringToDouble(String value)
134 {
135
136
137
138
139
140 if (value.length() >= 23)
141 {
142 StringBuffer normalized = new StringBuffer();
143 for (int i=0; i< value.length(); i++)
144 {
145 char c = value.charAt(i);
146 if ( c != '.')
147 {
148 normalized.append(c);
149 }
150 }
151
152 String normalizedString = normalized.toString();
153 if (normalizedString.contains("22250738585072012") && normalizedString.contains("e-"))
154 {
155
156 throw new NumberFormatException("Not Allowed! This value could possibly kill the VM!");
157 }
158 }
159
160
161 return Double.valueOf(value);
162 }
163
164 public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value)
165 {
166 if (facesContext == null)
167 {
168 throw new NullPointerException("facesContext");
169 }
170 if (uiComponent == null)
171 {
172 throw new NullPointerException("uiComponent");
173 }
174
175 if (value == null)
176 {
177 return "";
178 }
179 if (value instanceof String)
180 {
181 return (String)value;
182 }
183 try
184 {
185 return Double.toString(((Number)value).doubleValue());
186 }
187 catch (Exception e)
188 {
189 throw new ConverterException(_MessageUtils.getErrorMessage(facesContext, STRING_ID,
190 new Object[]{value,_MessageUtils.getLabel(facesContext, uiComponent)}),e);
191 }
192 }
193 }