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.DecimalFormat;
27 import java.text.DecimalFormatSymbols;
28 import java.text.NumberFormat;
29 import java.text.ParseException;
30 import java.util.Locale;
31
32
33
34
35
36
37
38 @JSFConverter
39 public class DoubleConverter
40 implements Converter
41 {
42
43 public static final String CONVERTER_ID = "javax.faces.Double";
44 public static final String STRING_ID = "javax.faces.converter.STRING";
45 public static final String DOUBLE_ID = "javax.faces.converter.DoubleConverter.DOUBLE";
46
47
48 public DoubleConverter()
49 {
50 }
51
52
53 public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value)
54 {
55 if (facesContext == null) throw new NullPointerException("facesContext");
56 if (uiComponent == null) throw new NullPointerException("uiComponent");
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) throw new NullPointerException("facesContext");
167 if (uiComponent == null) throw new NullPointerException("uiComponent");
168
169 if (value == null)
170 {
171 return "";
172 }
173 if (value instanceof String)
174 {
175 return (String)value;
176 }
177 try
178 {
179 return Double.toString(((Number)value).doubleValue());
180 }
181 catch (Exception e)
182 {
183 throw new ConverterException(_MessageUtils.getErrorMessage(facesContext, STRING_ID, new Object[]{value,_MessageUtils.getLabel(facesContext, uiComponent)}),e);
184 }
185 }
186 }