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

View differences:

AutomaticDetectionOfTypes.java
1 1
package org.gvsig.fmap.dal.store.csv;
2 2

  
3 3
import java.io.IOException;
4
import java.math.BigDecimal;
4 5
import java.net.URL;
5 6
import java.util.ArrayList;
6 7
import java.util.List;
......
22 23

  
23 24
        public List<String> nextRowValues();
24 25
    }
26
    
27
    public interface DetectedValue {
28
        public int getType();
29
        public int getDisplaySize();
30
        public int getPrecision();
31
        public int getScale();
32
    }
33
    
34
    private static class DetectedValueImpl implements DetectedValue {
25 35

  
36
        private int type;
37
        private int displaySize;
38
        private int precision;
39
        private int scale;
40
        
41
        @Override
42
        public int getType() {
43
            return this.type;
44
        }
45

  
46
        @Override
47
        public int getDisplaySize() {
48
            return this.displaySize;
49
        }
50

  
51
        @Override
52
        public int getPrecision() {
53
            return this.precision;
54
        }
55

  
56
        @Override
57
        public int getScale() {
58
            return this.scale;
59
        }
60
        
61
    }
62

  
26 63
    private static class PossibleDataType {
27 64

  
28 65
        public boolean possibleInt = true;
......
50 87
    }
51 88

  
52 89
    @SuppressWarnings({"UseSpecificCatch", "ResultOfObjectAllocationIgnored"})
53
    public int[] detect(int columns, Rows rows, boolean isFirstLineHeader, Locale locale) throws IOException {
90
    public DetectedValue[] detect(int columns, Rows rows, boolean isFirstLineHeader, Locale locale) throws IOException {
54 91
        List<PossibleDataType> possibleDataTypes;
55
        int[] types = null;
92
        DetectedValueImpl[] detectedValues = new DetectedValueImpl [columns];
56 93

  
57 94
        int lineno = 0;
58 95
        try {
......
63 100
            possibleDataTypes = new ArrayList<>(columns);
64 101
            for (int i = 0; i < columns; i++) {
65 102
                possibleDataTypes.add(new PossibleDataType());
103
                detectedValues[i] = new DetectedValueImpl();
66 104
            }
67 105
            if (locale == null) {
68 106
                locale = Locale.getDefault();
......
86 124
                        possibleDataTypes.add(new PossibleDataType());
87 125
                    }
88 126
                    String rawvalue = row.get(i);
127
                    if( rawvalue == null ) {
128
                       continue; 
129
                    }
89 130
                    PossibleDataType possibleDataType = possibleDataTypes.get(i);
131
                    DetectedValueImpl detectedValue = detectedValues[i];
132
                    int displaySize = rawvalue.length();
133
                    if( displaySize>detectedValue.displaySize ) {
134
                        detectedValue.displaySize = displaySize;
135
                    }
90 136
                    if (possibleDataType.possibleDecimal) {
91 137
                        try {
92
                            toDecimal.coerce(rawvalue, coercionContext);
138
                            BigDecimal decimal = (BigDecimal) toDecimal.coerce(rawvalue, coercionContext);
93 139
                            possibleDataType.possibleDecimal = true;
140
                            if( decimal.scale() > detectedValue.scale ) {
141
                                detectedValue.scale = decimal.scale();
142
                            }
143
                            if( decimal.precision()>detectedValue.precision ) {
144
                                detectedValue.precision = decimal.precision();
145
                            }
94 146
                        } catch (Exception ex) {
95 147
                            possibleDataType.possibleDecimal = false;
96 148
                        }
......
145 197
                row = rows.nextRowValues();
146 198
            }
147 199
            int n = 0;
148
            types = new int[possibleDataTypes.size()];
149 200
            for (PossibleDataType possibleDataType : possibleDataTypes) {
150 201
                if (possibleDataType.possibleInt) {
151
                    types[n++] = DataTypes.INT;
202
                    detectedValues[n++].type = DataTypes.INT;
152 203
                    continue;
153 204
                }
154 205
                if (possibleDataType.possibleLong) {
155
                    types[n++] = DataTypes.LONG;
206
                    detectedValues[n++].type = DataTypes.LONG;
156 207
                    continue;
157 208
                }
158 209
                if (possibleDataType.possibleDecimal) {
159 210
                    // Preferimos un Decimal que un Float/Double
160
                    types[n++] = DataTypes.DECIMAL;
211
                    detectedValues[n++].type = DataTypes.DECIMAL;
161 212
                    continue;
162 213
                }
163 214
                if (possibleDataType.possibleFloat) {
164 215
                    // Forzamos los float a double para evitar perder precision
165
                    types[n++] = DataTypes.DOUBLE;
216
                    detectedValues[n++].type = DataTypes.DOUBLE;
166 217
                    continue;
167 218
                }
168 219
                if (possibleDataType.possibleDouble) {
169
                    types[n++] = DataTypes.DOUBLE;
220
                    detectedValues[n++].type = DataTypes.DOUBLE;
170 221
                    continue;
171 222
                }
172 223
                if (possibleDataType.possibleURL) {
173
                    types[n++] = DataTypes.URL;
224
                    detectedValues[n++].type = DataTypes.URL;
174 225
                    continue;
175 226
                }
176 227
                if (possibleDataType.possibleDate) {
177
                    types[n++] = DataTypes.DATE;
228
                    detectedValues[n++].type = DataTypes.DATE;
178 229
                    continue;
179 230
                }
180 231
                if (possibleDataType.possibleGeometry) {
181
                    types[n++] = DataTypes.GEOMETRY;
232
                    detectedValues[n++].type = DataTypes.GEOMETRY;
182 233
                    continue;
183 234
                }
184
                types[n++] = DataTypes.STRING;
235
                detectedValues[n++].type = DataTypes.STRING;
185 236
            }
186 237
        } catch (Exception ex) {
187 238
            throw new RuntimeException("Problems reading file '" + this.getFullFileName() + "' near line " + lineno + ".", ex);
188 239
        }
189
        return types;
240
        return detectedValues;
190 241
    }
191 242

  
192 243
    @SuppressWarnings("UseSpecificCatch")

Also available in: Unified diff