Revision 47337
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.impl/src/main/java/org/gvsig/fmap/dal/feature/impl/DataTypeDetectorImpl.java | ||
---|---|---|
3 | 3 |
import org.gvsig.fmap.dal.feature.DataTypeDetector; |
4 | 4 |
import java.math.BigDecimal; |
5 | 5 |
import java.net.URL; |
6 |
import java.sql.Date; |
|
7 |
import java.sql.Time; |
|
8 |
import java.sql.Timestamp; |
|
9 |
import java.text.DateFormat; |
|
6 | 10 |
import java.text.DecimalFormat; |
7 | 11 |
import java.text.NumberFormat; |
12 |
import java.text.ParseException; |
|
8 | 13 |
import java.text.ParsePosition; |
14 |
import java.text.SimpleDateFormat; |
|
9 | 15 |
import java.util.Locale; |
10 | 16 |
import org.apache.commons.lang3.StringUtils; |
11 | 17 |
import org.gvsig.fmap.dal.DataTypes; |
... | ... | |
19 | 25 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
20 | 26 |
import org.gvsig.tools.dataTypes.Coercion; |
21 | 27 |
import org.gvsig.tools.dataTypes.CoercionContext; |
28 |
import org.gvsig.tools.dataTypes.CoercionContextLocale; |
|
22 | 29 |
|
23 | 30 |
/** |
24 | 31 |
* |
... | ... | |
93 | 100 |
private PossibleDataType possibleDataType; |
94 | 101 |
private DataTypeDetectedImpl detectedDataType; |
95 | 102 |
// private Coercion toDecimal; |
96 |
private Coercion toDouble; |
|
97 |
private Coercion toFloat; |
|
98 |
private Coercion toDate; |
|
99 |
private Coercion toTime; |
|
100 |
private Coercion toTimestamp; |
|
103 |
// private Coercion toDouble;
|
|
104 |
// private Coercion toFloat;
|
|
105 |
// private Coercion toDate;
|
|
106 |
// private Coercion toTime;
|
|
107 |
// private Coercion toTimestamp;
|
|
101 | 108 |
// private Coercion toInt; |
102 | 109 |
// private Coercion toLong; |
103 | 110 |
private Coercion toGeom; |
... | ... | |
113 | 120 |
locale = Locale.getDefault(); |
114 | 121 |
} |
115 | 122 |
DataTypesManager typeManager = ToolsLocator.getDataTypesManager(); |
123 |
coercionContext = CoercionContextLocale.create(locale); |
|
124 |
|
|
116 | 125 |
// toDecimal = typeManager.getCoercion(DataTypes.DECIMAL); |
117 |
toDouble = typeManager.getCoercion(DataTypes.DOUBLE); |
|
118 |
toFloat = typeManager.getCoercion(DataTypes.FLOAT); |
|
119 |
toDate = typeManager.getCoercion(DataTypes.DATE); |
|
120 |
toTime = typeManager.getCoercion(DataTypes.TIME); |
|
121 |
toTimestamp = typeManager.getCoercion(DataTypes.TIMESTAMP); |
|
126 |
// toDouble = typeManager.getCoercion(DataTypes.DOUBLE);
|
|
127 |
// toFloat = typeManager.getCoercion(DataTypes.FLOAT);
|
|
128 |
// toDate = typeManager.getCoercion(DataTypes.DATE);
|
|
129 |
// toTime = typeManager.getCoercion(DataTypes.TIME);
|
|
130 |
// toTimestamp = typeManager.getCoercion(DataTypes.TIMESTAMP);
|
|
122 | 131 |
// toInt = typeManager.getCoercion(DataTypes.INT); |
123 | 132 |
// toLong = typeManager.getCoercion(DataTypes.LONG); |
124 | 133 |
toGeom = typeManager.getCoercion(DataTypes.GEOMETRY); |
... | ... | |
191 | 200 |
} |
192 | 201 |
if (possibleDataType.possibleDate) { |
193 | 202 |
try { |
194 |
toDate.coerce(rawvalue, coercionContext); |
|
195 |
possibleDataType.possibleDate = true; |
|
203 |
possibleDataType.possibleDate = (toDate(rawvalue) != null); |
|
196 | 204 |
} catch (Exception ex) { |
197 | 205 |
possibleDataType.possibleDate = false; |
198 | 206 |
} |
199 | 207 |
} |
200 | 208 |
if (possibleDataType.possibleTime) { |
201 | 209 |
try { |
202 |
toTime.coerce(rawvalue, coercionContext); |
|
203 |
possibleDataType.possibleTime = true; |
|
210 |
possibleDataType.possibleTime = (toTime(rawvalue) != null); |
|
204 | 211 |
} catch (Exception ex) { |
205 | 212 |
possibleDataType.possibleTime = false; |
206 | 213 |
} |
207 | 214 |
} |
208 | 215 |
if (possibleDataType.possibleTimestamp) { |
209 | 216 |
try { |
210 |
toTimestamp.coerce(rawvalue, coercionContext); |
|
211 |
possibleDataType.possibleTimestamp = true; |
|
217 |
possibleDataType.possibleTimestamp = (toTimestamp(rawvalue) != null); |
|
212 | 218 |
} catch (Exception ex) { |
213 | 219 |
possibleDataType.possibleTimestamp = false; |
214 | 220 |
} |
... | ... | |
382 | 388 |
return null; |
383 | 389 |
} |
384 | 390 |
} |
391 |
|
|
392 |
private Date toDate(String s) { |
|
393 |
DateFormat[] formatters = new DateFormat[]{ |
|
394 |
DateFormat.getDateInstance(DateFormat.SHORT, locale), |
|
395 |
DateFormat.getDateInstance(DateFormat.MEDIUM, locale), |
|
396 |
DateFormat.getDateInstance(DateFormat.LONG, locale), |
|
397 |
new SimpleDateFormat("yyyy-MM-dd") // PostgreSQL format |
|
398 |
}; |
|
399 |
|
|
400 |
for (DateFormat formatter : formatters) { |
|
401 |
formatter.setLenient(false); |
|
402 |
try { |
|
403 |
java.util.Date d = formatter.parse(s); |
|
404 |
if (d != null) { |
|
405 |
return new Date(d.getTime()); |
|
406 |
} |
|
407 |
} catch (ParseException e) { |
|
408 |
// Continue |
|
409 |
} |
|
410 |
} |
|
411 |
return null; |
|
412 |
} |
|
413 |
|
|
414 |
private Time toTime(String s) { |
|
415 |
DateFormat[] formatters = new DateFormat[]{ |
|
416 |
DateFormat.getTimeInstance(DateFormat.MEDIUM, locale), |
|
417 |
DateFormat.getTimeInstance(DateFormat.SHORT, locale), |
|
418 |
DateFormat.getTimeInstance(DateFormat.LONG, locale), |
|
419 |
new SimpleDateFormat("HH:mm:ss"), |
|
420 |
new SimpleDateFormat("HH:mm"), |
|
421 |
|
|
422 |
}; |
|
423 |
|
|
424 |
for (DateFormat formatter : formatters) { |
|
425 |
formatter.setLenient(false); |
|
426 |
try { |
|
427 |
java.util.Date d = formatter.parse(s); |
|
428 |
if (d != null) { |
|
429 |
return new Time(d.getTime()); |
|
430 |
} |
|
431 |
} catch (ParseException e) { |
|
432 |
// Continue |
|
433 |
} |
|
434 |
} |
|
435 |
return null; |
|
436 |
} |
|
437 |
|
|
438 |
private Timestamp toTimestamp(String s) { |
|
439 |
DateFormat[] formatters = new DateFormat[]{ |
|
440 |
DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, locale), |
|
441 |
DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, locale), |
|
442 |
DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG, locale), |
|
443 |
DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, locale), |
|
444 |
DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale), |
|
445 |
DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, locale), |
|
446 |
DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.MEDIUM, locale), |
|
447 |
DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT, locale), |
|
448 |
DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale), |
|
449 |
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), // PostgreSQL timestamp format |
|
450 |
new SimpleDateFormat("yyyy-MM-dd HH:mm") |
|
451 |
|
|
452 |
}; |
|
453 |
|
|
454 |
for (DateFormat formatter : formatters) { |
|
455 |
formatter.setLenient(false); |
|
456 |
try { |
|
457 |
java.util.Date d = formatter.parse(s); |
|
458 |
if (d != null) { |
|
459 |
return new Timestamp(d.getTime()); |
|
460 |
} |
|
461 |
} catch (ParseException e) { |
|
462 |
// Continue |
|
463 |
} |
|
464 |
} |
|
465 |
try { |
|
466 |
return Timestamp.valueOf(s); |
|
467 |
} catch (Exception ex){ |
|
468 |
} |
|
469 |
return null; |
|
470 |
} |
|
385 | 471 |
} |
Also available in: Unified diff