Revision 43983 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/CSVStoreProvider.java
CSVStoreProvider.java | ||
---|---|---|
1137 | 1137 |
} |
1138 | 1138 |
} |
1139 | 1139 |
|
1140 |
private static class PossibleDataType { |
|
1141 |
|
|
1142 |
public boolean possibleInt = true; |
|
1143 |
public boolean possibleFloat = true; |
|
1144 |
public boolean possibleDouble = true; |
|
1145 |
public boolean possibleLong = true; |
|
1146 |
public boolean possibleURL = true; |
|
1147 |
public boolean possibleDate = true; |
|
1148 |
public boolean possibleGeometry = true; |
|
1149 |
} |
|
1150 |
|
|
1151 | 1140 |
private int[] automaticDetectionOfTypes(String[] headers) throws IOException { |
1152 | 1141 |
boolean automatic_types_detection = CSVStoreParameters.getAutomaticTypesDetection(getCSVParameters()); |
1153 | 1142 |
if (!automatic_types_detection) { |
1154 | 1143 |
return null; |
1155 | 1144 |
} |
1156 |
List<PossibleDataType> possibleDataTypes; |
|
1157 |
Locale locale; |
|
1158 | 1145 |
int[] types = null; |
1159 | 1146 |
|
1160 | 1147 |
FileReader in = null; |
... | ... | |
1163 | 1150 |
try { |
1164 | 1151 |
in = new FileReader(this.getCSVParameters().getFile()); |
1165 | 1152 |
reader = getSimpleReader(in); |
1166 |
if (CSVStoreParameters.isFirstLineHeader(getCSVParameters())) { |
|
1167 |
reader.read(); |
|
1168 |
} |
|
1169 |
possibleDataTypes = new ArrayList<>(headers.length); |
|
1170 |
for (String header : headers) { |
|
1171 |
possibleDataTypes.add(new PossibleDataType()); |
|
1172 |
} |
|
1173 |
locale = CSVStoreParameters.getLocale(getCSVParameters()); |
|
1174 |
if (locale == null) { |
|
1175 |
locale = Locale.getDefault(); |
|
1176 |
} |
|
1177 |
DataTypesManager typeManager = ToolsLocator.getDataTypesManager(); |
|
1178 |
CoercionWithLocale toDouble = (CoercionWithLocale) typeManager.getCoercion(DataTypes.DOUBLE); |
|
1179 |
CoercionWithLocale toFloat = (CoercionWithLocale) typeManager.getCoercion(DataTypes.FLOAT); |
|
1180 |
CoercionWithLocale toDate = (CoercionWithLocale) typeManager.getCoercion(DataTypes.DATE); |
|
1181 |
CoercionWithLocale toInt = (CoercionWithLocale) typeManager.getCoercion(DataTypes.INT); |
|
1182 |
CoercionWithLocale toLong = (CoercionWithLocale) typeManager.getCoercion(DataTypes.LONG); |
|
1183 |
Coercion toGeom = typeManager.getCoercion(DataTypes.GEOMETRY); |
|
1184 |
|
|
1185 |
List<String> row = reader.read(); |
|
1186 |
|
|
1187 |
while (row != null) { |
|
1188 |
for (int i = 0; i < row.size(); i++) { |
|
1189 |
while( possibleDataTypes.size()<row.size() ) { |
|
1190 |
possibleDataTypes.add(new PossibleDataType()); |
|
1191 |
} |
|
1192 |
String rawvalue = row.get(i); |
|
1193 |
PossibleDataType possibleDataType = possibleDataTypes.get(i); |
|
1194 |
if (possibleDataType.possibleDouble) { |
|
1195 |
try { |
|
1196 |
toDouble.coerce(rawvalue, locale); |
|
1197 |
possibleDataType.possibleDouble = true; |
|
1198 |
} catch (Exception ex) { |
|
1199 |
possibleDataType.possibleDouble = false; |
|
1200 |
} |
|
1201 |
} |
|
1202 |
if (possibleDataType.possibleFloat) { |
|
1203 |
try { |
|
1204 |
toFloat.coerce(rawvalue, locale); |
|
1205 |
possibleDataType.possibleFloat = true; |
|
1206 |
} catch (Exception ex) { |
|
1207 |
possibleDataType.possibleFloat = false; |
|
1208 |
} |
|
1209 |
} |
|
1210 |
if (possibleDataType.possibleLong) { |
|
1211 |
possibleDataType.possibleLong = isValidLong(rawvalue); |
|
1212 |
} |
|
1213 |
if (possibleDataType.possibleInt) { |
|
1214 |
possibleDataType.possibleInt = isValidInteger(rawvalue); |
|
1215 |
} |
|
1216 |
if (possibleDataType.possibleDate) { |
|
1217 |
try { |
|
1218 |
toDate.coerce(rawvalue, locale); |
|
1219 |
possibleDataType.possibleDate = true; |
|
1220 |
} catch (Exception ex) { |
|
1221 |
possibleDataType.possibleDate = false; |
|
1222 |
} |
|
1223 |
} |
|
1224 |
if (possibleDataType.possibleURL) { |
|
1225 |
try { |
|
1226 |
new URL((String) rawvalue); |
|
1227 |
possibleDataType.possibleURL = true; |
|
1228 |
} catch (Exception ex) { |
|
1229 |
possibleDataType.possibleURL = false; |
|
1230 |
} |
|
1231 |
} |
|
1232 |
if (possibleDataType.possibleGeometry) { |
|
1233 |
try { |
|
1234 |
toGeom.coerce((String) rawvalue); |
|
1235 |
possibleDataType.possibleGeometry = true; |
|
1236 |
} catch (Exception ex) { |
|
1237 |
possibleDataType.possibleGeometry = false; |
|
1238 |
} |
|
1239 |
} |
|
1240 |
} |
|
1241 |
row = reader.read(); |
|
1242 |
} |
|
1243 |
int n = 0; |
|
1244 |
types = new int[possibleDataTypes.size()]; |
|
1245 |
for (PossibleDataType possibleDataType : possibleDataTypes) { |
|
1246 |
if (possibleDataType.possibleInt) { |
|
1247 |
types[n++] = DataTypes.INT; |
|
1248 |
continue; |
|
1249 |
} |
|
1250 |
if (possibleDataType.possibleLong) { |
|
1251 |
types[n++] = DataTypes.LONG; |
|
1252 |
continue; |
|
1253 |
} |
|
1254 |
if (possibleDataType.possibleFloat) { |
|
1255 |
// Forzamos los float a double para evitar perder precision |
|
1256 |
types[n++] = DataTypes.DOUBLE; |
|
1257 |
continue; |
|
1258 |
} |
|
1259 |
if (possibleDataType.possibleDouble) { |
|
1260 |
types[n++] = DataTypes.DOUBLE; |
|
1261 |
continue; |
|
1262 |
} |
|
1263 |
if (possibleDataType.possibleURL) { |
|
1264 |
types[n++] = DataTypes.URL; |
|
1265 |
continue; |
|
1266 |
} |
|
1267 |
if (possibleDataType.possibleDate) { |
|
1268 |
types[n++] = DataTypes.DATE; |
|
1269 |
continue; |
|
1270 |
} |
|
1271 |
if (possibleDataType.possibleGeometry) { |
|
1272 |
types[n++] = DataTypes.GEOMETRY; |
|
1273 |
continue; |
|
1274 |
} |
|
1275 |
types[n++] = DataTypes.STRING; |
|
1276 |
} |
|
1153 |
AutomaticDetectionOfTypes x = new AutomaticDetectionOfTypes( |
|
1154 |
this.getFullFileName() |
|
1155 |
); |
|
1156 |
types = x.detect( |
|
1157 |
headers.length, |
|
1158 |
reader, |
|
1159 |
CSVStoreParameters.isFirstLineHeader(getCSVParameters()), |
|
1160 |
CSVStoreParameters.getLocale(getCSVParameters()) |
|
1161 |
); |
|
1277 | 1162 |
} catch (Exception ex) { |
1278 | 1163 |
int lineno = 0; |
1279 | 1164 |
if (reader != null) { |
Also available in: Unified diff