Revision 47235 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

View differences:

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.text.DecimalFormat;
7
import java.text.NumberFormat;
8
import java.text.ParsePosition;
6 9
import java.util.Locale;
7 10
import org.apache.commons.lang3.StringUtils;
8 11
import org.gvsig.fmap.dal.DataTypes;
......
24 27
@SuppressWarnings("UseSpecificCatch")
25 28
public class DataTypeDetectorImpl implements DataTypeDetector {
26 29

  
30
    private Locale locale;
31

  
27 32
    private static class DataTypeDetectedImpl implements DataTypeDetected {
28 33

  
29 34
        private int type;
......
87 92

  
88 93
    private PossibleDataType possibleDataType;
89 94
    private DataTypeDetectedImpl detectedDataType;
90
    private Coercion toDecimal;
95
//    private Coercion toDecimal;
91 96
    private Coercion toDouble;
92 97
    private Coercion toFloat;
93 98
    private Coercion toDate;
......
108 113
            locale = Locale.getDefault();
109 114
        }
110 115
        DataTypesManager typeManager = ToolsLocator.getDataTypesManager();
111
        toDecimal = typeManager.getCoercion(DataTypes.DECIMAL);
116
//        toDecimal = typeManager.getCoercion(DataTypes.DECIMAL);
112 117
        toDouble = typeManager.getCoercion(DataTypes.DOUBLE);
113 118
        toFloat = typeManager.getCoercion(DataTypes.FLOAT);
114 119
        toDate = typeManager.getCoercion(DataTypes.DATE);
......
125 130
        possibleDataType = new PossibleDataType();
126 131
        possibleDataType.possibleGeometryType = Geometry.TYPES.UNKNOWN;
127 132
        possibleDataType.possibleGeometrySubtype = Geometry.SUBTYPES.UNKNOWN;
128
        
133
        this.locale = locale;
129 134
    }
130 135
    
131 136
    @SuppressWarnings("ResultOfObjectAllocationIgnored")
......
143 148
        if( displaySize>detectedDataType.displaySize ) {
144 149
            detectedDataType.displaySize = displaySize;
145 150
        }
146
        if (possibleDataType.possibleDecimal) {
147
            try {
148
                BigDecimal decimal = (BigDecimal) toDecimal.coerce(rawvalue, coercionContext);
149
                possibleDataType.possibleDecimal = true;
150
                if( decimal.scale() > detectedDataType.decimalDigits ) {
151
                    detectedDataType.decimalDigits = decimal.scale();
151
        if( StringUtils.isNotBlank(rawvalue) ) {
152
            if (possibleDataType.possibleDecimal) {
153
                try {
154
                        BigDecimal decimal = this.toDecimal(rawvalue); 
155
                        if( decimal==null ) {
156
                            possibleDataType.possibleDecimal = false;
157
                        } else {
158
                            if( decimal.scale() > detectedDataType.decimalDigits ) {
159
                                detectedDataType.decimalDigits = decimal.scale();
160
                            }
161
                            int integerDigits = decimal.precision() - decimal.scale();
162
                            if( integerDigits>detectedDataType.integerDigits ) {
163
                                detectedDataType.integerDigits = integerDigits;
164
                            }
165
                        }
166
                } catch (Exception ex) {
167
                    possibleDataType.possibleDecimal = false;
152 168
                }
153
                int integerDigits = decimal.precision() - decimal.scale();
154
                if( integerDigits>detectedDataType.integerDigits ) {
155
                    detectedDataType.integerDigits = integerDigits;
169
            }
170
            if (possibleDataType.possibleDouble) {
171
                try {
172
                        Double num = toDouble(rawvalue);
173
                        possibleDataType.possibleDouble = (num!=null);
174
                } catch (Exception ex) {
175
                    possibleDataType.possibleDouble = false;
156 176
                }
157
            } catch (Exception ex) {
158
                possibleDataType.possibleDecimal = false;
159 177
            }
160
        }
161
        if (possibleDataType.possibleDouble) {
162
            try {
163
                toDouble.coerce(rawvalue, coercionContext);
164
                possibleDataType.possibleDouble = true;
165
            } catch (Exception ex) {
166
                possibleDataType.possibleDouble = false;
178
            if (possibleDataType.possibleFloat) {
179
                try {
180
                        Float num = toFloat(rawvalue);
181
                        possibleDataType.possibleFloat = (num!=null);
182
                } catch (Exception ex) {
183
                    possibleDataType.possibleFloat = false;
184
                }
167 185
            }
168
        }
169
        if (possibleDataType.possibleFloat) {
170
            try {
171
                toFloat.coerce(rawvalue, coercionContext);
172
                possibleDataType.possibleFloat = true;
173
            } catch (Exception ex) {
174
                possibleDataType.possibleFloat = false;
186
            if (possibleDataType.possibleLong) {
187
                    possibleDataType.possibleLong = isValidLong(rawvalue);
175 188
            }
176
        }
177
        if (possibleDataType.possibleLong) {
178
            possibleDataType.possibleLong = isValidLong(rawvalue);
179
        }
180
        if (possibleDataType.possibleInt) {
181
            possibleDataType.possibleInt = isValidInteger(rawvalue);
182
        }
183
        if (possibleDataType.possibleDate) {
184
            try {
185
                toDate.coerce(rawvalue, coercionContext);
186
                possibleDataType.possibleDate = true;
187
            } catch (Exception ex) {
188
                possibleDataType.possibleDate = false;
189
            if (possibleDataType.possibleInt) {
190
                possibleDataType.possibleInt = isValidInteger(rawvalue);
189 191
            }
190
        }
191
        if (possibleDataType.possibleTime) {
192
            try {
193
                toTime.coerce(rawvalue, coercionContext);
194
                possibleDataType.possibleTime = true;
195
            } catch (Exception ex) {
196
                possibleDataType.possibleTime = false;
192
            if (possibleDataType.possibleDate) {
193
                try {
194
                    toDate.coerce(rawvalue, coercionContext);
195
                    possibleDataType.possibleDate = true;
196
                } catch (Exception ex) {
197
                    possibleDataType.possibleDate = false;
198
                }
197 199
            }
198
        }
199
        if (possibleDataType.possibleTimestamp) {
200
            try {
201
                toTimestamp.coerce(rawvalue, coercionContext);
202
                possibleDataType.possibleTimestamp = true;
203
            } catch (Exception ex) {
204
                possibleDataType.possibleTimestamp = false;
200
            if (possibleDataType.possibleTime) {
201
                try {
202
                    toTime.coerce(rawvalue, coercionContext);
203
                    possibleDataType.possibleTime = true;
204
                } catch (Exception ex) {
205
                    possibleDataType.possibleTime = false;
206
                }
205 207
            }
206
        }
207
        if (possibleDataType.possibleURL) {
208
            try {
209
                new URL((String) rawvalue);
210
                possibleDataType.possibleURL = true;
211
            } catch (Exception ex) {
212
                possibleDataType.possibleURL = false;
208
            if (possibleDataType.possibleTimestamp) {
209
                try {
210
                    toTimestamp.coerce(rawvalue, coercionContext);
211
                    possibleDataType.possibleTimestamp = true;
212
                } catch (Exception ex) {
213
                    possibleDataType.possibleTimestamp = false;
214
                }
213 215
            }
214
        }
216
            if (possibleDataType.possibleURL) {
217
                try {
218
                    new URL((String) rawvalue);
219
                    possibleDataType.possibleURL = true;
220
                } catch (Exception ex) {
221
                    possibleDataType.possibleURL = false;
222
                }
223
            }
215 224

  
216
        if (possibleDataType.possibleGeometry) {
217
            try {
218
                Geometry geom = (Geometry) toGeom.coerce((String) rawvalue, geometryCoercionContext);
219
                if( geom==null ) {                    
220
                    possibleDataType.possibleGeometry = true;
221
                } else {
222
                    possibleDataType.possibleGeometry = true;
223
                    if( possibleDataType.possibleGeometryType == Geometry.TYPES.UNKNOWN ) {
224
                        possibleDataType.possibleGeometryType = geom.getGeometryType().getType();
225
                    } else if( possibleDataType.possibleGeometryType != geom.getGeometryType().getType() ) {
226
                        // FIXME: habria que calcualar correctamente el geom-type, en funcion del actual y el ultimo.
227
                        possibleDataType.possibleGeometryType = Geometry.TYPES.GEOMETRY;
225
            if (possibleDataType.possibleGeometry) {
226
                try {
227
                    Geometry geom = (Geometry) toGeom.coerce((String) rawvalue, geometryCoercionContext);
228
                    if( geom==null ) {                    
229
                        possibleDataType.possibleGeometry = true;
230
                    } else {
231
                        possibleDataType.possibleGeometry = true;
232
                        if( possibleDataType.possibleGeometryType == Geometry.TYPES.UNKNOWN ) {
233
                            possibleDataType.possibleGeometryType = geom.getGeometryType().getType();
234
                        } else if( possibleDataType.possibleGeometryType != geom.getGeometryType().getType() ) {
235
                            // FIXME: habria que calcualar correctamente el geom-type, en funcion del actual y el ultimo.
236
                            possibleDataType.possibleGeometryType = Geometry.TYPES.GEOMETRY;
237
                        }
238
                        if( possibleDataType.possibleGeometrySubtype == Geometry.SUBTYPES.UNKNOWN ) {
239
                            possibleDataType.possibleGeometrySubtype = geom.getGeometryType().getSubType();
240
                        } else if( possibleDataType.possibleGeometrySubtype != geom.getGeometryType().getSubType()) {
241
                            // FIXME: habria que calcualar correctamente el geom-type, en funcion del actual y el ultimo.
242
    //                        possibleDataType.possibleGeometrySubtype = Geometry.SUBTYPES.GEOM3DM;
243
                            possibleDataType.possibleGeometrySubtype = Geometry.SUBTYPES.GEOM3D; // H2 no soporta M
244
                        }
228 245
                    }
229
                    if( possibleDataType.possibleGeometrySubtype == Geometry.SUBTYPES.UNKNOWN ) {
230
                        possibleDataType.possibleGeometrySubtype = geom.getGeometryType().getSubType();
231
                    } else if( possibleDataType.possibleGeometrySubtype != geom.getGeometryType().getSubType()) {
232
                        // FIXME: habria que calcualar correctamente el geom-type, en funcion del actual y el ultimo.
233
//                        possibleDataType.possibleGeometrySubtype = Geometry.SUBTYPES.GEOM3DM;
234
                        possibleDataType.possibleGeometrySubtype = Geometry.SUBTYPES.GEOM3D; // H2 no soporta M
235
                    }
246
                } catch (Exception ex) {
247
                    possibleDataType.possibleGeometry = false;
236 248
                }
237
            } catch (Exception ex) {
238
                possibleDataType.possibleGeometry = false;
239 249
            }
240 250
        }
241

  
242 251
        if (possibleDataType.possibleInt) {
243 252
            this.detectedDataType.type = DataTypes.INT;
244 253
        } else if (possibleDataType.possibleLong) {
......
316 325
        }
317 326
    }
318 327

  
328
    
329
    private BigDecimal toDecimal(String s) {
330
        try {
331
            if (s.startsWith("+")) {
332
                s = s.substring(1);
333
            }
334
            ParsePosition p = new ParsePosition(0);
335
            DecimalFormat nf = (DecimalFormat) NumberFormat.getInstance(this.locale);
336
            nf.setParseBigDecimal(true);
337
            BigDecimal num = (BigDecimal) nf.parse(s, p);
338
            if (p.getErrorIndex() > 0 || p.getIndex() < s.length()) {
339
                return null;
340
            }
341
            return num;
342
        } catch (Exception ex) {
343
            return null;
344
        }
345
    }
346

  
347
    private Double toDouble(String s) {
348
        try {
349
            if (s.startsWith("+")) {
350
                s = s.substring(1);
351
            }
352
            ParsePosition p = new ParsePosition(0);
353
            DecimalFormat nf = (DecimalFormat) NumberFormat.getInstance(this.locale);
354
            Number n = nf.parse(s, p);
355
            if (p.getErrorIndex() > 0 || p.getIndex() < s.length()) {
356
                return null;
357
            }
358
            double num = n.doubleValue();
359
            return num;
360
        } catch (Exception ex) {
361
            return null;
362
        }
363
    }
364

  
365
    private Float toFloat(String s) {
366
        try {
367
            if (s.startsWith("+")) {
368
                s = s.substring(1);
369
            }
370
            ParsePosition p = new ParsePosition(0);
371
            DecimalFormat nf = (DecimalFormat) NumberFormat.getInstance(this.locale);
372
            Number n = nf.parse(s, p);
373
            if (p.getErrorIndex() > 0 || p.getIndex() < s.length()) {
374
                return null;
375
            }
376
            double num = n.doubleValue();
377
            if( num > Float.MAX_VALUE || num<Float.MIN_VALUE ) {
378
                return null;
379
            }
380
            return (float)num;
381
        } catch (Exception ex) {
382
            return null;
383
        }
384
    }
319 385
}

Also available in: Unified diff