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 @ 41876

History | View | Annotate | Download (14.2 KB)

1
/**
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
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * 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
 * MA 02110-1301, USA.
20
 *
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
import java.util.Locale;
28
import org.apache.commons.lang3.BooleanUtils;
29

    
30
import org.apache.commons.lang3.StringEscapeUtils;
31
import org.apache.commons.lang3.StringUtils;
32
import org.cresques.cts.IProjection;
33
import org.gvsig.fmap.dal.FileHelper;
34
import org.gvsig.fmap.dal.OpenDataStoreParameters;
35
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.fmap.geom.DataTypes;
39
import org.gvsig.tools.ToolsLocator;
40
import org.gvsig.tools.dataTypes.DataTypesManager;
41
import org.gvsig.tools.dynobject.DelegatedDynObject;
42
import org.gvsig.tools.dynobject.DynObject;
43
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
45
import org.supercsv.prefs.CsvPreference;
46
import org.supercsv.quote.AlwaysQuoteMode;
47
import org.supercsv.quote.NormalQuoteMode;
48
import org.supercsv.quote.QuoteMode;
49

    
50
public class CSVStoreParameters extends AbstractDataParameters implements
51
        OpenDataStoreParameters, FilesystemStoreParameters {
52

    
53
    private static final Logger logger = LoggerFactory.getLogger(CSVStoreParameters.class);
54

    
55
    public static final String PARAMETERS_DEFINITION_NAME = "CSVStoreParameters";
56

    
57
    private static final String FILE = "file";
58
    private static final String IGNOREERRORS = "ignoreErrors";
59
    private static final String PROFILE = "profile";
60
    private static final String QUOTEPOLICY = "quotePolicy";
61
    private static final String QUOTECHAR = "quoteCharacter";
62
    private static final String RECORDSEPARATOR = "recordSeparator";
63
    private static final String DELIMITER = "delimiter";
64
    private static final String COMMENTSTARTMARKER = "commentStartMarker";
65
    private static final String AUTOMATICTYPESDETECTION = "automaticTypesDetection";
66

    
67
    //private static final String ESCAPECHARACTER = "escapeCharacter";
68
    private static final String HEADER = "header";
69
    private static final String SURROUNDINGSPACESNEEDQUOTES = "surroundingSpacesNeedQuotes";
70

    
71
    //private static final String IGNOREEMPTYLINES = "ignoreEmptyLines";
72
    private static final String CRS = "CRS";
73
    private static final String FIELDTYPES = "fieldtypes";
74
//    private static final String NULLTO = "nullTo";
75
    private static final String CHARSET = "charset"; // Default "UTF-8"
76
    private static final String LOCALE = "locale";
77

    
78
    private DelegatedDynObject parameters;
79

    
80
    public CSVStoreParameters() {
81
        this(PARAMETERS_DEFINITION_NAME);
82
    }
83

    
84
    protected CSVStoreParameters(String parametersDefinitionName) {
85
        this(parametersDefinitionName, CSVStoreProvider.NAME);
86
    }
87

    
88
    public CSVStoreParameters(String parametersDefinitionName, String name) {
89
        super();
90
        this.parameters = (DelegatedDynObject) FileHelper.newParameters(parametersDefinitionName);
91
        this.setDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME, name);
92
    }
93

    
94
    public String getDataStoreName() {
95
        return (String) this.getDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME);
96
    }
97

    
98
    public String getDescription() {
99
        return this.getDynClass().getDescription();
100
    }
101

    
102
    protected DelegatedDynObject getDelegatedDynObject() {
103
        return parameters;
104
    }
105

    
106
    public boolean isValid() {
107
        if ( getFileName(this) == null ) {
108
            return false;
109
        }
110
        return true;
111
    }
112

    
113
    public File getFile() {
114
        return (File) this.getDynValue(FILE);
115
    }
116

    
117
    public void setFile(File file) {
118
        this.setDynValue(FILE, file);
119
    }
120

    
121
    public static CsvPreference getPredefinedCSVPreferences(DynObject dynobj) {
122
        String s = (String) dynobj.getDynValue(PROFILE);
123
        if ( "NONE".equalsIgnoreCase(s) ) {
124
            return null;
125
        }
126
        if ( "STANDARD_PREFERENCE".equalsIgnoreCase(s) ) {
127
            return CsvPreference.STANDARD_PREFERENCE;
128
        }
129
        if ( "EXCEL_PREFERENCE".equalsIgnoreCase(s) ) {
130
            return CsvPreference.EXCEL_PREFERENCE;
131
        }
132
        if ( "EXCEL_NORTH_EUROPE_PREFERENCE".equalsIgnoreCase(s) ) {
133
            return CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE;
134
        }
135
        if ( "TAB_PREFERENCE".equalsIgnoreCase(s) ) {
136
            return CsvPreference.TAB_PREFERENCE;
137
        }
138
        return null;
139
    }
140

    
141
    public static QuoteMode getQuoteMode(DynObject dynobj) {
142
        String s = (String) dynobj.getDynValue(QUOTEPOLICY);
143
        if ( "AlwaysQuoteMode".equalsIgnoreCase(s) ) {
144
            return new AlwaysQuoteMode();
145
        }
146
        if ( "NormalQuoteMode".equalsIgnoreCase(s) ) {
147
            return new NormalQuoteMode();
148
        }
149
        return null;
150
    }
151

    
152
    static IProjection getCRS(DynObject dynobj) {
153
        return (IProjection) dynobj.getDynValue(CRS);
154
    }
155

    
156
    static String getFileName(DynObject dynobj) {
157
        File f = (File) dynobj.getDynValue(FILE);
158
        if ( f == null ) {
159
            return null;
160
        }
161
        return f.getPath();
162
    }
163

    
164
    static File getFile(DynObject dynobj) {
165
        File f = (File) dynobj.getDynValue(FILE);
166
        return f;
167
    }
168

    
169
    public static String getRecordSeparator(DynObject dynobj) {
170
        String s = (String) dynobj.getDynValue(RECORDSEPARATOR);
171
        return StringEscapeUtils.unescapeJava(s);
172
    }
173

    
174
    static Locale getLocale(DynObject dynobj) {
175
        try {
176
            String s = (String) dynobj.getDynValue(LOCALE);
177
            if ( s.trim().length() == 0 ) {
178
                return null;
179
            }
180
            if ( "DEFAULT".equalsIgnoreCase(s.trim()) ) {
181
                return Locale.getDefault();
182
            }
183
            Locale locale = null;
184
            // locale = Locale.forLanguageTag(s); // Since java 1.7
185
            String[] ss = s.split("-");
186
            switch (ss.length) {
187
            case 1:
188
                locale = new Locale(ss[0]);
189
                break;
190
            case 2:
191
                locale = new Locale(ss[0], ss[1]);
192
                break;
193
            case 3:
194
            default:
195
                locale = new Locale(ss[0], ss[1], ss[2]);
196
                break;
197
            }
198
            return locale;
199
        } catch (Exception ex) {
200
            logger.warn("Can't get locale from CSV parameters.", ex);
201
            return null;
202
        }
203
    }
204

    
205
    public static String getCommentStartMarker(DynObject dynobj) {
206
        String s = (String) dynobj.getDynValue(COMMENTSTARTMARKER);
207
        return StringEscapeUtils.unescapeJava(s);
208
    }
209

    
210
    public static String getQuoteCharacter(DynObject dynobj) {
211
        String s = (String) dynobj.getDynValue(QUOTECHAR);
212
        s = StringEscapeUtils.unescapeJava(s);
213
        if ( StringUtils.isBlank(s) ) {
214
            return null;
215
        }
216
        return s.substring(0, 1);
217
    }
218

    
219
    public static String getDelimiter(DynObject dynobj) {
220
        String s = (String) dynobj.getDynValue(DELIMITER);
221
        s = StringEscapeUtils.unescapeJava(s);
222
        if ( StringUtils.isBlank(s) ) {
223
            return null;
224
        }
225
        return s.substring(0, 1);
226
    }
227

    
228
    static String getHeader(DynObject dynobj) {
229
        String s = (String) dynobj.getDynValue(HEADER);
230
        s = StringEscapeUtils.unescapeJava(s);
231
        if ( StringUtils.isBlank(s) ) {
232
            return null;
233
        }
234
        return s;
235
    }
236

    
237
    static String[] getHeaders(DynObject dynobj) {
238
        String s = getHeader(dynobj);
239
        if ( StringUtils.isBlank(s) ) {
240
            return null;
241
        }
242
        String sep = getDelimiter(dynobj);
243
        if ( sep == null ) {
244
            sep = getDelimiter(s);
245
            if ( sep == null ) {
246
                // Chungo
247
                return null;
248
            }
249
        }
250
        String[] ss = s.split("[" + sep + "]");
251
        return ss;
252
    }
253

    
254
    static String getDelimiter(String line) {
255
        String sep = null;
256
        // Cuidado con los ":", los he puesto al final a proposito
257
        // ya que podian estar en la cadena para separar el size
258
        // de cada tipo.
259
        String seps = ",;-|@#/+$%&!:";
260
        for ( int i = 0; i < seps.length(); i++ ) {
261
            sep = seps.substring(i, 1);
262
            if ( line.contains(seps.substring(i, 1)) ) {
263
                break;
264
            }
265
            sep = null;
266
        }
267
        return sep;
268
    }
269

    
270
    static String getCharset(DynObject dynobj) {
271
        String s = (String) dynobj.getDynValue(CHARSET);
272
        return StringEscapeUtils.unescapeJava(s);
273
    }
274

    
275
    static String[] getPointDimensionNames(DynObject dynobj) {
276
        String s = (String) dynobj.getDynValue("point");
277
        if ( StringUtils.isBlank(s) ) {
278
            return null;
279
        }
280
        return s.split(",");
281
    }
282

    
283
    public static boolean getSurroundingSpacesNeedQuotes(DynObject dynobj) {
284
        Boolean b = (Boolean) dynobj.getDynValue(SURROUNDINGSPACESNEEDQUOTES);
285
        return BooleanUtils.isTrue(b);
286
    }
287

    
288
    static boolean getAutomaticTypesDetection(DynObject dynobj) {
289
        Boolean b = (Boolean) dynobj.getDynValue(AUTOMATICTYPESDETECTION);
290
        return BooleanUtils.isTrue(b);
291
    }
292

    
293
    static boolean getIgnoreErrors(DynObject dynobj) {
294
        Boolean b = (Boolean) dynobj.getDynValue(IGNOREERRORS);
295
        return BooleanUtils.isTrue(b);
296
    }
297

    
298
//    static int[] getFieldTypes(DynObject dynobj) {
299
//        String s = (String) dynobj.getDynValue(FIELDTYPES);
300
//        if ( StringUtils.isBlank(s) ) {
301
//            return null;
302
//        }
303
//        String sep = getDelimiter(s);
304
//        if ( sep == null ) {
305
//            return null;
306
//        }
307
//        DataTypesManager dataTypeManager = ToolsLocator.getDataTypesManager();
308
//        String fieldTypeNames[] = s.split("[" + sep + "]");
309
//        int fieldTypes[] = new int[fieldTypeNames.length];
310
//        for ( int i = 0; i < fieldTypeNames.length; i++ ) {
311
//            s = fieldTypeNames[i].trim();
312
//            if ( s.contains(":") ) {
313
//                s = s.split(":")[0];
314
//            }
315
//            fieldTypes[i] = dataTypeManager.getType(s);
316
//        }
317
//        return fieldTypes;
318
//    }
319
//
320
//    static int[] getFieldSizes(DynObject dynobj) {
321
//        String s = (String) dynobj.getDynValue(FIELDTYPES);
322
//        if ( StringUtils.isBlank(s) ) {
323
//            return null;
324
//        }
325
//        String sep = getDelimiter(s);
326
//        if ( sep == null ) {
327
//            return null;
328
//        }
329
//        DataTypesManager dataTypeManager = ToolsLocator.getDataTypesManager();
330
//        String fieldTypeNames[] = s.split("[" + sep + "]");
331
//        int fieldSizes[] = new int[fieldTypeNames.length];
332
//        for ( int i = 0; i < fieldTypeNames.length; i++ ) {
333
//            String fieldtypeDef = fieldTypeNames[i].trim();
334
//            if ( fieldtypeDef.contains(":") ) {
335
//                try {
336
//                    String[] parts = fieldtypeDef.split(":");
337
//                    int fieldType = dataTypeManager.getType(parts[0]);
338
//                    if( fieldType == DataTypes.GEOMETRY ) {
339
//                        fieldSizes[i] = 1;
340
//                    } else {
341
//                        s = parts[1];
342
//                        fieldSizes[i] = Integer.parseInt(s);
343
//                    }
344
//                } catch (Exception ex) {
345
//                    logger.warn("Can't get size of field " + i + " (" + fieldtypeDef + ").", ex);
346
//                }
347
//            } else {
348
//                fieldSizes[i] = 0;
349
//            }
350
//        }
351
//        return fieldSizes;
352
//    }
353

    
354
    static String getRawFieldTypes(DynObject dynobj) {
355
        String s = (String) dynobj.getDynValue(FIELDTYPES);
356
        if ( StringUtils.isBlank(s) ) {
357
            return null;
358
        }
359
        return s.trim();
360
    }
361

    
362
    static int getSkipLines(DynObject dynobj) {
363
        Integer n = (Integer) dynobj.getDynValue("skipLines");
364
        if ( n == null ) {
365
            return 0;
366
        }
367
        return new Integer(n);
368
    }
369

    
370
    static String getRawFieldsDefinition(DynObject dynobj) {
371
        String s = (String) dynobj.getDynValue("fieldsDefinition");
372
        if ( StringUtils.isBlank(s) ) {
373
            return null;
374
        }
375
        return s.trim();
376
    }
377

    
378
    public static class FieldDefinition {
379

    
380
        private int start;
381
        private int end;
382

    
383
        public FieldDefinition(String def) {
384
            def = def.trim();
385
            String[] ss = def.split(":");
386
            this.start = Integer.parseInt(ss[0])-1;
387
            if ( ss.length < 2 ) {
388
                this.end = -1;
389
            } else {
390
                this.end = Integer.parseInt(ss[1])-1;
391
            }
392
        }
393

    
394
        public int getStart() {
395
            return this.start;
396
        }
397

    
398
        public int getEnd() {
399
            return this.end;
400
        }
401

    
402
        public boolean getToEndOfLine() {
403
            return this.end == -1;
404
        }
405
    }
406

    
407
    public static FieldDefinition[] getFieldsDefinition(DynObject dynobj) {
408
        String definition = getRawFieldsDefinition(dynobj);
409
        if ( definition == null ) {
410
            return null;
411
        }
412
        int i=0;
413
        try {
414
            String[] defs = StringUtils.split(definition);
415
            FieldDefinition[] fieldsDefinition = new FieldDefinition[defs.length];
416
            for ( i = 0; i < defs.length; i++ ) {
417
                fieldsDefinition[i] = new FieldDefinition(defs[i]);
418
            }
419
            return fieldsDefinition;
420
        } catch (Exception ex) {
421
            throw  new IllegalArgumentException("Can't recognize the format field definition '"+definition+"' ("+i+").");
422
        }
423
    }
424

    
425
}