Statistics
| Revision:

svn-gvsig-desktop / 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 / CSVStoreParameters.java @ 43283

History | View | Annotate | Download (15 KB)

1 40846 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 41617 jjdelcerro
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 40846 jjdelcerro
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 41617 jjdelcerro
 * MA 02110-1301, USA.
20 40846 jjdelcerro
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.dal.store.csv;
25
26
import java.io.File;
27 41069 jjdelcerro
import java.util.Locale;
28 41617 jjdelcerro
import org.apache.commons.lang3.BooleanUtils;
29 40846 jjdelcerro
30
import org.apache.commons.lang3.StringEscapeUtils;
31 41617 jjdelcerro
import org.apache.commons.lang3.StringUtils;
32 40846 jjdelcerro
import org.cresques.cts.IProjection;
33
import org.gvsig.fmap.dal.FileHelper;
34 43088 jjdelcerro
import org.gvsig.fmap.dal.feature.OpenFeatureStoreParameters;
35 40846 jjdelcerro
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
36
import org.gvsig.fmap.dal.spi.AbstractDataParameters;
37
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
38
import org.gvsig.tools.dynobject.DelegatedDynObject;
39
import org.gvsig.tools.dynobject.DynObject;
40 41059 jjdelcerro
import org.slf4j.Logger;
41
import org.slf4j.LoggerFactory;
42 40878 jjdelcerro
import org.supercsv.prefs.CsvPreference;
43 41006 jjdelcerro
import org.supercsv.quote.AlwaysQuoteMode;
44
import org.supercsv.quote.NormalQuoteMode;
45
import org.supercsv.quote.QuoteMode;
46 40846 jjdelcerro
47
public class CSVStoreParameters extends AbstractDataParameters implements
48 43088 jjdelcerro
        OpenFeatureStoreParameters, FilesystemStoreParameters {
49 40846 jjdelcerro
50 41617 jjdelcerro
    private static final Logger logger = LoggerFactory.getLogger(CSVStoreParameters.class);
51
52 40846 jjdelcerro
    public static final String PARAMETERS_DEFINITION_NAME = "CSVStoreParameters";
53
54
    private static final String FILE = "file";
55 41059 jjdelcerro
    private static final String IGNOREERRORS = "ignoreErrors";
56 40846 jjdelcerro
    private static final String PROFILE = "profile";
57 41006 jjdelcerro
    private static final String QUOTEPOLICY = "quotePolicy";
58
    private static final String QUOTECHAR = "quoteCharacter";
59 40846 jjdelcerro
    private static final String RECORDSEPARATOR = "recordSeparator";
60
    private static final String DELIMITER = "delimiter";
61
    private static final String COMMENTSTARTMARKER = "commentStartMarker";
62 41062 jjdelcerro
    private static final String AUTOMATICTYPESDETECTION = "automaticTypesDetection";
63 41617 jjdelcerro
64 42775 jjdelcerro
    private static final String ESCAPECHARACTER = "escapeCharacter";
65
    private static final String FIRST_LINE_HEADER = "firstLineHeader";
66 40846 jjdelcerro
    private static final String HEADER = "header";
67 40878 jjdelcerro
    private static final String SURROUNDINGSPACESNEEDQUOTES = "surroundingSpacesNeedQuotes";
68 41617 jjdelcerro
69 40878 jjdelcerro
    //private static final String IGNOREEMPTYLINES = "ignoreEmptyLines";
70 40846 jjdelcerro
    private static final String CRS = "CRS";
71
    private static final String FIELDTYPES = "fieldtypes";
72 40878 jjdelcerro
//    private static final String NULLTO = "nullTo";
73 40846 jjdelcerro
    private static final String CHARSET = "charset"; // Default "UTF-8"
74 41617 jjdelcerro
    private static final String LOCALE = "locale";
75 42775 jjdelcerro
    private static final String POINT_COLUMN_NAME = "pointColumnName";
76
    private static final String LIMIT = "limit";
77 43283 jjdelcerro
    private static final String GEOEMTRY_COLUMN = "geometry_column";
78 40846 jjdelcerro
79 41617 jjdelcerro
    private DelegatedDynObject parameters;
80 40846 jjdelcerro
81 41617 jjdelcerro
    public CSVStoreParameters() {
82
        this(PARAMETERS_DEFINITION_NAME);
83
    }
84 40846 jjdelcerro
85 41617 jjdelcerro
    protected CSVStoreParameters(String parametersDefinitionName) {
86
        this(parametersDefinitionName, CSVStoreProvider.NAME);
87
    }
88 40846 jjdelcerro
89 41617 jjdelcerro
    public CSVStoreParameters(String parametersDefinitionName, String name) {
90
        super();
91
        this.parameters = (DelegatedDynObject) FileHelper.newParameters(parametersDefinitionName);
92
        this.setDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME, name);
93
    }
94 40846 jjdelcerro
95 41617 jjdelcerro
    public String getDataStoreName() {
96
        return (String) this.getDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME);
97
    }
98 40846 jjdelcerro
99 41617 jjdelcerro
    public String getDescription() {
100
        return this.getDynClass().getDescription();
101
    }
102 40846 jjdelcerro
103 41617 jjdelcerro
    protected DelegatedDynObject getDelegatedDynObject() {
104
        return parameters;
105
    }
106 40846 jjdelcerro
107 41617 jjdelcerro
    public boolean isValid() {
108
        if ( getFileName(this) == null ) {
109
            return false;
110
        }
111
        return true;
112
    }
113 40846 jjdelcerro
114 41617 jjdelcerro
    public File getFile() {
115
        return (File) this.getDynValue(FILE);
116
    }
117 40846 jjdelcerro
118 41617 jjdelcerro
    public void setFile(File file) {
119
        this.setDynValue(FILE, file);
120
    }
121 40846 jjdelcerro
122 41617 jjdelcerro
    public static CsvPreference getPredefinedCSVPreferences(DynObject dynobj) {
123
        String s = (String) dynobj.getDynValue(PROFILE);
124
        if ( "NONE".equalsIgnoreCase(s) ) {
125
            return null;
126
        }
127
        if ( "STANDARD_PREFERENCE".equalsIgnoreCase(s) ) {
128
            return CsvPreference.STANDARD_PREFERENCE;
129
        }
130
        if ( "EXCEL_PREFERENCE".equalsIgnoreCase(s) ) {
131
            return CsvPreference.EXCEL_PREFERENCE;
132
        }
133
        if ( "EXCEL_NORTH_EUROPE_PREFERENCE".equalsIgnoreCase(s) ) {
134
            return CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE;
135
        }
136
        if ( "TAB_PREFERENCE".equalsIgnoreCase(s) ) {
137
            return CsvPreference.TAB_PREFERENCE;
138
        }
139
        return null;
140
    }
141 40846 jjdelcerro
142 41617 jjdelcerro
    public static QuoteMode getQuoteMode(DynObject dynobj) {
143
        String s = (String) dynobj.getDynValue(QUOTEPOLICY);
144
        if ( "AlwaysQuoteMode".equalsIgnoreCase(s) ) {
145
            return new AlwaysQuoteMode();
146
        }
147
        if ( "NormalQuoteMode".equalsIgnoreCase(s) ) {
148
            return new NormalQuoteMode();
149
        }
150
        return null;
151
    }
152 40878 jjdelcerro
153 41617 jjdelcerro
    static IProjection getCRS(DynObject dynobj) {
154
        return (IProjection) dynobj.getDynValue(CRS);
155
    }
156 40846 jjdelcerro
157 41617 jjdelcerro
    static String getFileName(DynObject dynobj) {
158
        File f = (File) dynobj.getDynValue(FILE);
159
        if ( f == null ) {
160
            return null;
161
        }
162
        return f.getPath();
163
    }
164 40846 jjdelcerro
165 41617 jjdelcerro
    static File getFile(DynObject dynobj) {
166
        File f = (File) dynobj.getDynValue(FILE);
167
        return f;
168
    }
169 40846 jjdelcerro
170 41617 jjdelcerro
    public static String getRecordSeparator(DynObject dynobj) {
171
        String s = (String) dynobj.getDynValue(RECORDSEPARATOR);
172
        return StringEscapeUtils.unescapeJava(s);
173
    }
174 40846 jjdelcerro
175 43283 jjdelcerro
    public static String getGeometryColumn(DynObject dynobj) {
176
        String s = (String) dynobj.getDynValue(GEOEMTRY_COLUMN);
177
        return s;
178
    }
179
180 41617 jjdelcerro
    static Locale getLocale(DynObject dynobj) {
181
        try {
182
            String s = (String) dynobj.getDynValue(LOCALE);
183
            if ( s.trim().length() == 0 ) {
184
                return null;
185
            }
186
            if ( "DEFAULT".equalsIgnoreCase(s.trim()) ) {
187
                return Locale.getDefault();
188
            }
189
            Locale locale = null;
190
            // locale = Locale.forLanguageTag(s); // Since java 1.7
191
            String[] ss = s.split("-");
192
            switch (ss.length) {
193
            case 1:
194
                locale = new Locale(ss[0]);
195
                break;
196
            case 2:
197
                locale = new Locale(ss[0], ss[1]);
198
                break;
199
            case 3:
200
            default:
201
                locale = new Locale(ss[0], ss[1], ss[2]);
202
                break;
203
            }
204
            return locale;
205
        } catch (Exception ex) {
206
            logger.warn("Can't get locale from CSV parameters.", ex);
207
            return null;
208
        }
209
    }
210 40886 jjdelcerro
211 41617 jjdelcerro
    public static String getCommentStartMarker(DynObject dynobj) {
212
        String s = (String) dynobj.getDynValue(COMMENTSTARTMARKER);
213
        return StringEscapeUtils.unescapeJava(s);
214
    }
215 42775 jjdelcerro
216
    public static String getPointColumnName(DynObject dynobj) {
217
        String s = (String) dynobj.getDynValue(POINT_COLUMN_NAME);
218
        return s;
219
    }
220 40886 jjdelcerro
221 41617 jjdelcerro
    public static String getQuoteCharacter(DynObject dynobj) {
222
        String s = (String) dynobj.getDynValue(QUOTECHAR);
223
        s = StringEscapeUtils.unescapeJava(s);
224
        if ( StringUtils.isBlank(s) ) {
225
            return null;
226
        }
227
        return s.substring(0, 1);
228
    }
229
230
    public static String getDelimiter(DynObject dynobj) {
231
        String s = (String) dynobj.getDynValue(DELIMITER);
232
        s = StringEscapeUtils.unescapeJava(s);
233
        if ( StringUtils.isBlank(s) ) {
234
            return null;
235
        }
236
        return s.substring(0, 1);
237
    }
238
239
    static String getHeader(DynObject dynobj) {
240
        String s = (String) dynobj.getDynValue(HEADER);
241
        s = StringEscapeUtils.unescapeJava(s);
242
        if ( StringUtils.isBlank(s) ) {
243
            return null;
244
        }
245
        return s;
246
    }
247
248
    static String[] getHeaders(DynObject dynobj) {
249
        String s = getHeader(dynobj);
250
        if ( StringUtils.isBlank(s) ) {
251
            return null;
252
        }
253
        String sep = getDelimiter(dynobj);
254
        if ( sep == null ) {
255
            sep = getDelimiter(s);
256
            if ( sep == null ) {
257
                // Chungo
258
                return null;
259
            }
260
        }
261
        String[] ss = s.split("[" + sep + "]");
262
        return ss;
263
    }
264
265 41876 jjdelcerro
    static String getDelimiter(String line) {
266 41901 jjdelcerro
        if( StringUtils.isBlank(line) ) {
267
            return null;
268
        }
269 41617 jjdelcerro
        String sep = null;
270 41876 jjdelcerro
        // Cuidado con los ":", los he puesto al final a proposito
271 41617 jjdelcerro
        // ya que podian estar en la cadena para separar el size
272 41876 jjdelcerro
        // de cada tipo.
273 41617 jjdelcerro
        String seps = ",;-|@#/+$%&!:";
274
        for ( int i = 0; i < seps.length(); i++ ) {
275
            sep = seps.substring(i, 1);
276
            if ( line.contains(seps.substring(i, 1)) ) {
277
                break;
278
            }
279
            sep = null;
280
        }
281
        return sep;
282
    }
283
284
    static String getCharset(DynObject dynobj) {
285
        String s = (String) dynobj.getDynValue(CHARSET);
286
        return StringEscapeUtils.unescapeJava(s);
287
    }
288
289
    static String[] getPointDimensionNames(DynObject dynobj) {
290
        String s = (String) dynobj.getDynValue("point");
291
        if ( StringUtils.isBlank(s) ) {
292
            return null;
293
        }
294
        return s.split(",");
295
    }
296
297
    public static boolean getSurroundingSpacesNeedQuotes(DynObject dynobj) {
298
        Boolean b = (Boolean) dynobj.getDynValue(SURROUNDINGSPACESNEEDQUOTES);
299
        return BooleanUtils.isTrue(b);
300
    }
301
302
    static boolean getAutomaticTypesDetection(DynObject dynobj) {
303
        Boolean b = (Boolean) dynobj.getDynValue(AUTOMATICTYPESDETECTION);
304
        return BooleanUtils.isTrue(b);
305
    }
306
307
    static boolean getIgnoreErrors(DynObject dynobj) {
308
        Boolean b = (Boolean) dynobj.getDynValue(IGNOREERRORS);
309
        return BooleanUtils.isTrue(b);
310
    }
311
312 42775 jjdelcerro
    static boolean isFirstLineHeader(DynObject dynobj) {
313
        Boolean b = (Boolean) dynobj.getDynValue(FIRST_LINE_HEADER);
314
        return BooleanUtils.isTrue(b);
315
    }
316
317 41876 jjdelcerro
//    static int[] getFieldTypes(DynObject dynobj) {
318
//        String s = (String) dynobj.getDynValue(FIELDTYPES);
319
//        if ( StringUtils.isBlank(s) ) {
320
//            return null;
321
//        }
322
//        String sep = getDelimiter(s);
323
//        if ( sep == null ) {
324
//            return null;
325
//        }
326
//        DataTypesManager dataTypeManager = ToolsLocator.getDataTypesManager();
327
//        String fieldTypeNames[] = s.split("[" + sep + "]");
328
//        int fieldTypes[] = new int[fieldTypeNames.length];
329
//        for ( int i = 0; i < fieldTypeNames.length; i++ ) {
330
//            s = fieldTypeNames[i].trim();
331
//            if ( s.contains(":") ) {
332
//                s = s.split(":")[0];
333
//            }
334
//            fieldTypes[i] = dataTypeManager.getType(s);
335
//        }
336
//        return fieldTypes;
337
//    }
338
//
339
//    static int[] getFieldSizes(DynObject dynobj) {
340
//        String s = (String) dynobj.getDynValue(FIELDTYPES);
341
//        if ( StringUtils.isBlank(s) ) {
342
//            return null;
343
//        }
344
//        String sep = getDelimiter(s);
345
//        if ( sep == null ) {
346
//            return null;
347
//        }
348
//        DataTypesManager dataTypeManager = ToolsLocator.getDataTypesManager();
349
//        String fieldTypeNames[] = s.split("[" + sep + "]");
350
//        int fieldSizes[] = new int[fieldTypeNames.length];
351
//        for ( int i = 0; i < fieldTypeNames.length; i++ ) {
352
//            String fieldtypeDef = fieldTypeNames[i].trim();
353
//            if ( fieldtypeDef.contains(":") ) {
354
//                try {
355
//                    String[] parts = fieldtypeDef.split(":");
356
//                    int fieldType = dataTypeManager.getType(parts[0]);
357
//                    if( fieldType == DataTypes.GEOMETRY ) {
358
//                        fieldSizes[i] = 1;
359
//                    } else {
360
//                        s = parts[1];
361
//                        fieldSizes[i] = Integer.parseInt(s);
362
//                    }
363
//                } catch (Exception ex) {
364
//                    logger.warn("Can't get size of field " + i + " (" + fieldtypeDef + ").", ex);
365
//                }
366
//            } else {
367
//                fieldSizes[i] = 0;
368
//            }
369
//        }
370
//        return fieldSizes;
371
//    }
372 41617 jjdelcerro
373 41876 jjdelcerro
    static String getRawFieldTypes(DynObject dynobj) {
374 41617 jjdelcerro
        String s = (String) dynobj.getDynValue(FIELDTYPES);
375
        if ( StringUtils.isBlank(s) ) {
376
            return null;
377
        }
378 41876 jjdelcerro
        return s.trim();
379 41617 jjdelcerro
    }
380
381
    static int getSkipLines(DynObject dynobj) {
382
        Integer n = (Integer) dynobj.getDynValue("skipLines");
383
        if ( n == null ) {
384
            return 0;
385
        }
386 42775 jjdelcerro
        return n;
387 41617 jjdelcerro
    }
388
389 42775 jjdelcerro
    static int getLimit(DynObject dynobj) {
390
        Integer n = (Integer) dynobj.getDynValue(LIMIT);
391
        if ( n == null ) {
392
            return -1;
393
        }
394
        return n;
395
    }
396
397 41617 jjdelcerro
    static String getRawFieldsDefinition(DynObject dynobj) {
398
        String s = (String) dynobj.getDynValue("fieldsDefinition");
399
        if ( StringUtils.isBlank(s) ) {
400
            return null;
401
        }
402
        return s.trim();
403
    }
404
405
    public static class FieldDefinition {
406
407
        private int start;
408
        private int end;
409
410
        public FieldDefinition(String def) {
411
            def = def.trim();
412
            String[] ss = def.split(":");
413 42775 jjdelcerro
            this.start = Integer.parseInt(ss[0]);
414 41617 jjdelcerro
            if ( ss.length < 2 ) {
415
                this.end = -1;
416
            } else {
417 42775 jjdelcerro
                this.end = Integer.parseInt(ss[1]);
418 41617 jjdelcerro
            }
419
        }
420
421
        public int getStart() {
422
            return this.start;
423
        }
424
425
        public int getEnd() {
426
            return this.end;
427
        }
428
429
        public boolean getToEndOfLine() {
430
            return this.end == -1;
431
        }
432
    }
433
434
    public static FieldDefinition[] getFieldsDefinition(DynObject dynobj) {
435
        String definition = getRawFieldsDefinition(dynobj);
436
        if ( definition == null ) {
437
            return null;
438
        }
439
        int i=0;
440
        try {
441
            String[] defs = StringUtils.split(definition);
442
            FieldDefinition[] fieldsDefinition = new FieldDefinition[defs.length];
443
            for ( i = 0; i < defs.length; i++ ) {
444
                fieldsDefinition[i] = new FieldDefinition(defs[i]);
445
            }
446
            return fieldsDefinition;
447
        } catch (Exception ex) {
448
            throw  new IllegalArgumentException("Can't recognize the format field definition '"+definition+"' ("+i+").");
449
        }
450
    }
451
452 40846 jjdelcerro
}