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

History | View | Annotate | Download (8.28 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

    
28
import org.apache.commons.lang3.StringEscapeUtils;
29
import org.cresques.cts.IProjection;
30
import org.gvsig.fmap.dal.DataStoreParameters;
31
import org.gvsig.fmap.dal.FileHelper;
32
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
33
import org.gvsig.fmap.dal.spi.AbstractDataParameters;
34
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
35
import org.gvsig.tools.ToolsLocator;
36
import org.gvsig.tools.dataTypes.DataTypesManager;
37
import org.gvsig.tools.dynobject.DelegatedDynObject;
38
import org.gvsig.tools.dynobject.DynObject;
39
import org.supercsv.prefs.CsvPreference;
40
import org.supercsv.quote.AlwaysQuoteMode;
41
import org.supercsv.quote.NormalQuoteMode;
42
import org.supercsv.quote.QuoteMode;
43

    
44
public class CSVStoreParameters extends AbstractDataParameters implements
45
                DataStoreParameters, FilesystemStoreParameters {
46

    
47
    public static final String PARAMETERS_DEFINITION_NAME = "CSVStoreParameters";
48

    
49
    private static final String FILE = "file";
50
    private static final String PROFILE = "profile";
51
    private static final String QUOTEPOLICY = "quotePolicy";
52
    private static final String QUOTECHAR = "quoteCharacter";
53
    private static final String RECORDSEPARATOR = "recordSeparator";
54
    private static final String DELIMITER = "delimiter";
55
    private static final String COMMENTSTARTMARKER = "commentStartMarker";
56
    
57
    //private static final String ESCAPECHARACTER = "escapeCharacter";
58
    
59
    private static final String HEADER = "header";
60
    private static final String SURROUNDINGSPACESNEEDQUOTES = "surroundingSpacesNeedQuotes";
61
    
62
    //private static final String IGNOREEMPTYLINES = "ignoreEmptyLines";
63
    private static final String CRS = "CRS";
64
    private static final String FIELDTYPES = "fieldtypes";
65
//    private static final String NULLTO = "nullTo";
66
    private static final String CHARSET = "charset"; // Default "UTF-8"
67

    
68
    
69

    
70
        private DelegatedDynObject parameters;
71

    
72
        
73
        public CSVStoreParameters() {
74
                this(PARAMETERS_DEFINITION_NAME);
75
        }
76

    
77
        protected CSVStoreParameters(String parametersDefinitionName) {
78
                this(parametersDefinitionName, CSVStoreProvider.NAME);
79
        }
80

    
81
        public CSVStoreParameters(String parametersDefinitionName, String name) {
82
                super();
83
                this.parameters = (DelegatedDynObject) FileHelper.newParameters(parametersDefinitionName);
84
                this.setDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME, name);
85
        }
86

    
87
        public String getDataStoreName() {
88
                return (String) this.getDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME);
89
        }
90

    
91
        public String getDescription() {
92
                return this.getDynClass().getDescription();
93
        }
94

    
95
        protected DelegatedDynObject getDelegatedDynObject() {
96
                return parameters;
97
        }
98

    
99
        public boolean isValid() {
100
                if (getFileName(this) == null) {
101
                        return false;
102
                }
103
                return true;
104
        }
105

    
106
        public File getFile() {
107
                return (File) this.getDynValue(FILE);
108
        }
109

    
110
        public void setFile(File file) {
111
                this.setDynValue(FILE, file);
112
        }
113
        
114

    
115

    
116
        static CsvPreference getPredefinedCSVPreferences(DynObject dynobj) {
117
                String s = (String) dynobj.getDynValue(PROFILE);
118
                if( "NONE".equalsIgnoreCase(s) ) {
119
                        return null;
120
                }
121
                if( "STANDARD_PREFERENCE".equalsIgnoreCase(s) ) {
122
                        return CsvPreference.STANDARD_PREFERENCE;
123
                }
124
                if( "EXCEL_PREFERENCE".equalsIgnoreCase(s) ) {
125
                        return CsvPreference.EXCEL_PREFERENCE;
126
                }
127
                if( "EXCEL_NORTH_EUROPE_PREFERENCE".equalsIgnoreCase(s) ) {
128
                        return CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE;
129
                }
130
                if( "TAB_PREFERENCE".equalsIgnoreCase(s) ) {
131
                        return CsvPreference.TAB_PREFERENCE;
132
                }
133
                return null ;
134
        }
135
        
136
        static QuoteMode getQuoteMode(DynObject dynobj) {
137
                String s = (String) dynobj.getDynValue(QUOTEPOLICY);
138
                if( "AlwaysQuoteMode".equalsIgnoreCase(s) ) {
139
                        return new AlwaysQuoteMode();
140
                }
141
                if( "NormalQuoteMode".equalsIgnoreCase(s) ) {
142
                        return new NormalQuoteMode();
143
                }
144
                return null;
145
        }
146
        
147
        static IProjection getCRS(DynObject dynobj) {
148
                return (IProjection) dynobj.getDynValue(CRS);
149
        }
150

    
151
        static String getFileName(DynObject dynobj) {
152
                File f = (File) dynobj.getDynValue(FILE);
153
                if( f == null ) {
154
                        return null;
155
                }
156
                return f.getPath();
157
        }
158

    
159
        static File getFile(DynObject dynobj) {
160
                File f = (File) dynobj.getDynValue(FILE);
161
                return f;
162
        }
163

    
164
        static String getRecordSeparator(DynObject dynobj) {
165
                String s = (String) dynobj.getDynValue(RECORDSEPARATOR);
166
                return StringEscapeUtils.unescapeJava(s);
167
        }
168
        
169
        static String getCommentStartMarker(DynObject dynobj) {
170
                String s = (String) dynobj.getDynValue(COMMENTSTARTMARKER);
171
                return StringEscapeUtils.unescapeJava(s);
172
        }
173

    
174
//        static String getEscapeCharacter(DynObject dynobj) {
175
//                String s = (String) dynobj.getDynValue(ESCAPECHARACTER);
176
//                return StringEscapeUtils.unescapeJava(s);
177
//        }
178
//        
179
        static String getQuoteCharacter(DynObject dynobj) {
180
                String s = (String) dynobj.getDynValue(QUOTECHAR);
181
                s = StringEscapeUtils.unescapeJava(s);
182
                if( isEmpty(s) ) {
183
                        return null;
184
                }
185
                return s.substring(0, 1);
186
        }
187
        
188
        static String getDelimiter(DynObject dynobj) {
189
                String s = (String) dynobj.getDynValue(DELIMITER);
190
                s = StringEscapeUtils.unescapeJava(s);
191
                if( isEmpty(s) ) {
192
                        return null;
193
                }
194
                return s.substring(0, 1);
195
        }
196
        
197
        static String getHeader(DynObject dynobj) {
198
                String s = (String) dynobj.getDynValue(HEADER);
199
                s = StringEscapeUtils.unescapeJava(s);
200
                if( isEmpty(s) ) {
201
                        return null;
202
                }
203
                return s;
204
        }
205

    
206
        static String[] getHeaders(DynObject dynobj) {
207
                String s = getHeader(dynobj);
208
                if( isEmpty(s) ) {
209
                        return null;
210
                }
211
                String sep = getDelimiter(dynobj);
212
                if( sep == null ) {
213
                        sep = getDelimiter(s);
214
                        if( sep == null ) {
215
                                // Chungo
216
                                return null;
217
                        }
218
                }
219
                String[] ss = s.split("["+sep+"]");
220
                return ss;
221
        }
222

    
223
        private static String getDelimiter(String line) {
224
                String sep = null;
225
                String seps = ",;:-|@#/+$%&!";
226
                for( int i=0; i<seps.length(); i ++) {
227
                        sep = seps.substring(i, 1);
228
                        if( line.contains(seps.substring(i, 1))) {
229
                                break;
230
                        }
231
                        sep = null;
232
                }
233
                return sep;
234
        }
235
//        static String getNullTo(DynObject dynobj) {
236
//                String s = (String) dynobj.getDynValue(NULLTO);
237
//                return StringEscapeUtils.unescapeJava(s);
238
//        }
239
        
240
        static String getCharset(DynObject dynobj) {
241
                String s = (String) dynobj.getDynValue(CHARSET);
242
                return StringEscapeUtils.unescapeJava(s);
243
        }
244
        
245
        static String[] getPointDimensionNames(DynObject dynobj) {
246
                String s = (String) dynobj.getDynValue("point");
247
                if( isEmpty(s) ) {
248
                        return null;
249
                }
250
                return s.split(",");
251
        }
252
        
253
        static boolean getSurroundingSpacesNeedQuotes(DynObject dynobj) {
254
                Boolean b = (Boolean) dynobj.getDynValue(SURROUNDINGSPACESNEEDQUOTES);
255
                if( b==null ) {
256
                        return false;
257
                }
258
                return b.booleanValue();
259
        }
260
        
261
//        static boolean getIgnoreEmptyLines(DynObject dynobj) {
262
//                Boolean b = (Boolean) dynobj.getDynValue(IGNOREEMPTYLINES);
263
//                return b.booleanValue();
264
//        }
265
        
266
        static int[] getFieldTypes(DynObject dynobj) {
267
                String s = (String) dynobj.getDynValue(FIELDTYPES);
268
                if( isEmpty(s) ) {
269
                        return null;
270
                }
271
                String sep = getDelimiter(s);
272
                if( sep == null ) {
273
                        return null;
274
                }
275
                DataTypesManager dataTypeManager = ToolsLocator.getDataTypesManager();
276
                String fieldTypeNames[] = s.split("["+sep+"]");
277
                int fieldTypes[] = new int[fieldTypeNames.length];
278
                for( int i=0; i<fieldTypeNames.length; i++ ) {
279
                        fieldTypes[i] = dataTypeManager.getType(fieldTypeNames[i].trim());
280
                }
281
                return fieldTypes;
282
        }
283
        
284
        static private boolean isEmpty(String s) {
285
                if( s == null ) {
286
                        return true;
287
                }
288
                if( s.trim().length() == 0 ) {
289
                        return true;
290
                }
291
                return false;
292
        }
293
}