| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.seasar.teeda.core.util; |
| 17 | |
|
| 18 | |
import javax.faces.component.UIComponent; |
| 19 | |
import javax.faces.component.ValueHolder; |
| 20 | |
import javax.faces.convert.Converter; |
| 21 | |
import javax.faces.el.ValueBinding; |
| 22 | |
import javax.faces.internal.ConverterResource; |
| 23 | |
|
| 24 | |
import org.seasar.teeda.core.JsfConstants; |
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
public class ConverterUtil { |
| 30 | |
|
| 31 | 0 | private ConverterUtil() { |
| 32 | 0 | } |
| 33 | |
|
| 34 | |
public static Converter getConverter(final UIComponent component) { |
| 35 | 263 | Converter converter = null; |
| 36 | 263 | final ValueBinding vb = component |
| 37 | |
.getValueBinding(JsfConstants.VALUE_ATTR); |
| 38 | 263 | if (vb != null) { |
| 39 | 34 | String expression = vb.getExpressionString(); |
| 40 | 34 | converter = ConverterResource.getConverter(expression); |
| 41 | |
} |
| 42 | 263 | if (converter != null) { |
| 43 | 2 | return converter; |
| 44 | |
} |
| 45 | 261 | if (component instanceof ValueHolder) { |
| 46 | 261 | return ((ValueHolder) component).getConverter(); |
| 47 | |
} |
| 48 | 0 | return null; |
| 49 | |
} |
| 50 | |
|
| 51 | |
public static int convertToInt(Object value) { |
| 52 | 8 | if (value instanceof Number) { |
| 53 | 3 | return ((Number) value).intValue(); |
| 54 | 5 | } else if (value instanceof String) { |
| 55 | |
try { |
| 56 | 4 | return Integer.parseInt((String) value); |
| 57 | 1 | } catch (NumberFormatException e) { |
| 58 | 1 | throw new IllegalArgumentException("Cannot convert " |
| 59 | |
+ value.toString() + " to int"); |
| 60 | |
} |
| 61 | |
} else { |
| 62 | 1 | throw new IllegalArgumentException("Cannot convert " |
| 63 | |
+ value.toString() + " to int"); |
| 64 | |
} |
| 65 | |
} |
| 66 | |
|
| 67 | |
public static boolean convertToBoolean(Object value) { |
| 68 | 4 | if (value instanceof Boolean) { |
| 69 | 1 | return ((Boolean) value).booleanValue(); |
| 70 | 3 | } else if (value instanceof String) { |
| 71 | 2 | return Boolean.valueOf((String) value).booleanValue(); |
| 72 | |
} else { |
| 73 | 1 | throw new IllegalArgumentException("Cannot convert " |
| 74 | |
+ value.toString() + " to boolean"); |
| 75 | |
} |
| 76 | |
} |
| 77 | |
|
| 78 | |
public static long convertToLong(Object value) { |
| 79 | 8 | if (value instanceof Number) { |
| 80 | 3 | return ((Number) value).longValue(); |
| 81 | 5 | } else if (value instanceof String) { |
| 82 | |
try { |
| 83 | 4 | return Long.parseLong((String) value); |
| 84 | 1 | } catch (NumberFormatException e) { |
| 85 | 1 | throw new IllegalArgumentException("Cannot convert " |
| 86 | |
+ value.toString() + " to long"); |
| 87 | |
} |
| 88 | |
} else { |
| 89 | 1 | throw new IllegalArgumentException("Cannot convert " |
| 90 | |
+ value.toString() + " to long"); |
| 91 | |
} |
| 92 | |
} |
| 93 | |
|
| 94 | |
public static double convertToDouble(Object value) { |
| 95 | 8 | if (value instanceof Number) { |
| 96 | 3 | return ((Number) value).doubleValue(); |
| 97 | 5 | } else if (value instanceof String) { |
| 98 | |
try { |
| 99 | 4 | return Double.parseDouble((String) value); |
| 100 | 1 | } catch (NumberFormatException e) { |
| 101 | 1 | throw new IllegalArgumentException("Cannot convert " |
| 102 | |
+ value.toString() + " to double"); |
| 103 | |
} |
| 104 | |
} else { |
| 105 | 1 | throw new IllegalArgumentException("Cannot convert " |
| 106 | |
+ value.toString() + " to double"); |
| 107 | |
} |
| 108 | |
} |
| 109 | |
|
| 110 | |
} |