| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package javax.faces.convert; |
| 17 | |
|
| 18 | |
import java.util.Locale; |
| 19 | |
|
| 20 | |
import javax.faces.component.UIComponent; |
| 21 | |
import javax.faces.context.FacesContext; |
| 22 | |
import javax.faces.internal.ConvertUtil; |
| 23 | |
import javax.faces.internal.FacesMessageUtil; |
| 24 | |
|
| 25 | |
import org.seasar.framework.util.AssertionUtil; |
| 26 | |
import org.seasar.framework.util.NumberConversionUtil; |
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
public class DoubleConverter implements Converter { |
| 32 | |
|
| 33 | |
public static final String CONVERTER_ID = "javax.faces.DoubleTime"; |
| 34 | |
|
| 35 | 2 | public static final String CONVERSION_OBJECT_ID = DoubleConverter.class |
| 36 | |
.getName() |
| 37 | |
+ ".CONVERSION"; |
| 38 | |
|
| 39 | 1 | public static final String CONVERSION_STRING_ID = DoubleConverter.class |
| 40 | |
.getName() |
| 41 | |
+ ".CONVERSION_STRING"; |
| 42 | |
|
| 43 | 15 | public DoubleConverter() { |
| 44 | 15 | } |
| 45 | |
|
| 46 | |
public Object getAsObject(FacesContext context, UIComponent component, |
| 47 | |
String value) throws ConverterException { |
| 48 | 7 | AssertionUtil.assertNotNull("FacesContext", context); |
| 49 | 6 | AssertionUtil.assertNotNull("UIComponent", component); |
| 50 | 5 | if (value == null) { |
| 51 | 1 | return null; |
| 52 | |
} |
| 53 | |
|
| 54 | 4 | value = value.trim(); |
| 55 | 4 | if (value.length() < 1) { |
| 56 | 1 | return null; |
| 57 | |
} |
| 58 | 3 | Locale locale = context.getViewRoot().getLocale(); |
| 59 | 3 | value = NumberConversionUtil.removeDelimeter(value, locale); |
| 60 | |
try { |
| 61 | 3 | return Double.valueOf(value); |
| 62 | 1 | } catch (Exception e) { |
| 63 | 1 | Object[] args = ConvertUtil.createExceptionMessageArgs(component, |
| 64 | |
value); |
| 65 | 1 | throw new ConverterException(FacesMessageUtil.getMessage(context, |
| 66 | |
getObjectMessageId(), args), e); |
| 67 | |
} |
| 68 | |
} |
| 69 | |
|
| 70 | |
public String getAsString(FacesContext context, UIComponent component, |
| 71 | |
Object value) throws ConverterException { |
| 72 | 6 | AssertionUtil.assertNotNull("FacesContext", context); |
| 73 | 5 | AssertionUtil.assertNotNull("UIComponent", component); |
| 74 | 4 | if (value == null) { |
| 75 | 1 | return ""; |
| 76 | |
} |
| 77 | |
try { |
| 78 | 3 | return (value instanceof String) ? (String) value : (Double |
| 79 | |
.toString(((Double) value).doubleValue())); |
| 80 | 1 | } catch (Exception e) { |
| 81 | 1 | Object[] args = ConvertUtil.createExceptionMessageArgs(component, |
| 82 | |
value); |
| 83 | 1 | throw new ConverterException(FacesMessageUtil.getMessage(context, |
| 84 | |
getStringMessageId(), args), e); |
| 85 | |
} |
| 86 | |
} |
| 87 | |
|
| 88 | |
protected String getObjectMessageId() { |
| 89 | 1 | return CONVERSION_OBJECT_ID; |
| 90 | |
} |
| 91 | |
|
| 92 | |
protected String getStringMessageId() { |
| 93 | 1 | return CONVERSION_STRING_ID; |
| 94 | |
} |
| 95 | |
|
| 96 | |
} |