Revision 44669 trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.file/org.gvsig.fmap.dal.file.csv/src/main/java/org/gvsig/fmap/dal/store/csv/AutomaticDetectionOfTypes.java
AutomaticDetectionOfTypes.java | ||
---|---|---|
7 | 7 |
import java.util.Locale; |
8 | 8 |
import org.gvsig.fmap.dal.DataTypes; |
9 | 9 |
import org.gvsig.tools.ToolsLocator; |
10 |
import org.gvsig.tools.dataTypes.DataTypeUtils; |
|
10 | 11 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
12 |
import org.gvsig.tools.dataTypes.Coercion; |
|
13 |
import org.gvsig.tools.dataTypes.CoercionContext; |
|
11 | 14 |
|
12 | 15 |
/** |
13 | 16 |
* |
... | ... | |
25 | 28 |
public boolean possibleInt = true; |
26 | 29 |
public boolean possibleFloat = true; |
27 | 30 |
public boolean possibleDouble = true; |
31 |
public boolean possibleDecimal = true; |
|
28 | 32 |
public boolean possibleLong = true; |
29 | 33 |
public boolean possibleURL = true; |
30 | 34 |
public boolean possibleDate = true; |
... | ... | |
64 | 68 |
locale = Locale.getDefault(); |
65 | 69 |
} |
66 | 70 |
DataTypesManager typeManager = ToolsLocator.getDataTypesManager(); |
67 |
DataTypesManager.CoercionWithLocale toDouble = (DataTypesManager.CoercionWithLocale) typeManager.getCoercion(DataTypes.DOUBLE); |
|
68 |
DataTypesManager.CoercionWithLocale toFloat = (DataTypesManager.CoercionWithLocale) typeManager.getCoercion(DataTypes.FLOAT); |
|
69 |
DataTypesManager.CoercionWithLocale toDate = (DataTypesManager.CoercionWithLocale) typeManager.getCoercion(DataTypes.DATE); |
|
70 |
DataTypesManager.CoercionWithLocale toInt = (DataTypesManager.CoercionWithLocale) typeManager.getCoercion(DataTypes.INT); |
|
71 |
DataTypesManager.CoercionWithLocale toLong = (DataTypesManager.CoercionWithLocale) typeManager.getCoercion(DataTypes.LONG); |
|
72 |
DataTypesManager.Coercion toGeom = typeManager.getCoercion(DataTypes.GEOMETRY); |
|
71 |
Coercion toDecimal = typeManager.getCoercion(DataTypes.DECIMAL); |
|
72 |
Coercion toDouble = typeManager.getCoercion(DataTypes.DOUBLE); |
|
73 |
Coercion toFloat = typeManager.getCoercion(DataTypes.FLOAT); |
|
74 |
Coercion toDate = typeManager.getCoercion(DataTypes.DATE); |
|
75 |
Coercion toInt = typeManager.getCoercion(DataTypes.INT); |
|
76 |
Coercion toLong = typeManager.getCoercion(DataTypes.LONG); |
|
77 |
Coercion toGeom = typeManager.getCoercion(DataTypes.GEOMETRY); |
|
73 | 78 |
|
79 |
CoercionContext coercionContext = DataTypeUtils.coerceContextLocale(locale); |
|
74 | 80 |
List<String> row = rows.nextRowValues(); |
75 | 81 |
lineno++; |
76 | 82 |
|
... | ... | |
81 | 87 |
} |
82 | 88 |
String rawvalue = row.get(i); |
83 | 89 |
PossibleDataType possibleDataType = possibleDataTypes.get(i); |
90 |
if (possibleDataType.possibleDecimal) { |
|
91 |
try { |
|
92 |
toDecimal.coerce(rawvalue, coercionContext); |
|
93 |
possibleDataType.possibleDecimal = true; |
|
94 |
} catch (Exception ex) { |
|
95 |
possibleDataType.possibleDecimal = false; |
|
96 |
} |
|
97 |
} |
|
84 | 98 |
if (possibleDataType.possibleDouble) { |
85 | 99 |
try { |
86 |
toDouble.coerce(rawvalue, locale);
|
|
100 |
toDouble.coerce(rawvalue, coercionContext);
|
|
87 | 101 |
possibleDataType.possibleDouble = true; |
88 | 102 |
} catch (Exception ex) { |
89 | 103 |
possibleDataType.possibleDouble = false; |
... | ... | |
91 | 105 |
} |
92 | 106 |
if (possibleDataType.possibleFloat) { |
93 | 107 |
try { |
94 |
toFloat.coerce(rawvalue, locale);
|
|
108 |
toFloat.coerce(rawvalue, coercionContext);
|
|
95 | 109 |
possibleDataType.possibleFloat = true; |
96 | 110 |
} catch (Exception ex) { |
97 | 111 |
possibleDataType.possibleFloat = false; |
... | ... | |
105 | 119 |
} |
106 | 120 |
if (possibleDataType.possibleDate) { |
107 | 121 |
try { |
108 |
toDate.coerce(rawvalue, locale);
|
|
122 |
toDate.coerce(rawvalue, coercionContext);
|
|
109 | 123 |
possibleDataType.possibleDate = true; |
110 | 124 |
} catch (Exception ex) { |
111 | 125 |
possibleDataType.possibleDate = false; |
... | ... | |
141 | 155 |
types[n++] = DataTypes.LONG; |
142 | 156 |
continue; |
143 | 157 |
} |
158 |
if (possibleDataType.possibleDecimal) { |
|
159 |
// Preferimos un Decimal que un Float/Double |
|
160 |
types[n++] = DataTypes.DECIMAL; |
|
161 |
continue; |
|
162 |
} |
|
144 | 163 |
if (possibleDataType.possibleFloat) { |
145 | 164 |
// Forzamos los float a double para evitar perder precision |
146 | 165 |
types[n++] = DataTypes.DOUBLE; |
Also available in: Unified diff