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 @ 43983
History | View | Annotate | Download (14.8 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.feature.OpenFeatureStoreParameters; |
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.tools.dynobject.DelegatedDynObject; |
39 |
import org.gvsig.tools.dynobject.DynObject; |
40 |
import org.slf4j.Logger; |
41 |
import org.slf4j.LoggerFactory; |
42 |
import org.supercsv.prefs.CsvPreference; |
43 |
import org.supercsv.quote.AlwaysQuoteMode; |
44 |
import org.supercsv.quote.NormalQuoteMode; |
45 |
import org.supercsv.quote.QuoteMode; |
46 |
|
47 |
public class CSVStoreParameters extends AbstractDataParameters implements |
48 |
OpenFeatureStoreParameters, FilesystemStoreParameters { |
49 |
|
50 |
private static final Logger logger = LoggerFactory.getLogger(CSVStoreParameters.class); |
51 |
|
52 |
public static final String PARAMETERS_DEFINITION_NAME = "CSVStoreParameters"; |
53 |
|
54 |
private static final String FILE = "file"; |
55 |
private static final String IGNOREERRORS = "ignoreErrors"; |
56 |
private static final String PROFILE = "profile"; |
57 |
private static final String QUOTEPOLICY = "quotePolicy"; |
58 |
private static final String QUOTECHAR = "quoteCharacter"; |
59 |
private static final String RECORDSEPARATOR = "recordSeparator"; |
60 |
private static final String DELIMITER = "delimiter"; |
61 |
private static final String COMMENTSTARTMARKER = "commentStartMarker"; |
62 |
private static final String AUTOMATICTYPESDETECTION = "automaticTypesDetection"; |
63 |
|
64 |
private static final String ESCAPECHARACTER = "escapeCharacter"; |
65 |
private static final String FIRST_LINE_HEADER = "firstLineHeader"; |
66 |
private static final String HEADER = "header"; |
67 |
private static final String SURROUNDINGSPACESNEEDQUOTES = "surroundingSpacesNeedQuotes"; |
68 |
|
69 |
//private static final String IGNOREEMPTYLINES = "ignoreEmptyLines";
|
70 |
private static final String CRS = "CRS"; |
71 |
private static final String FIELDTYPES = "fieldtypes"; |
72 |
// private static final String NULLTO = "nullTo";
|
73 |
private static final String CHARSET = "charset"; // Default "UTF-8" |
74 |
private static final String LOCALE = "locale"; |
75 |
private static final String POINT_COLUMN_NAME = "pointColumnName"; |
76 |
private static final String LIMIT = "limit"; |
77 |
private static final String GEOEMTRY_COLUMN = "geometry_column"; |
78 |
|
79 |
private DelegatedDynObject parameters;
|
80 |
|
81 |
public CSVStoreParameters() {
|
82 |
this(PARAMETERS_DEFINITION_NAME);
|
83 |
} |
84 |
|
85 |
protected CSVStoreParameters(String parametersDefinitionName) { |
86 |
this(parametersDefinitionName, CSVStoreProvider.NAME);
|
87 |
} |
88 |
|
89 |
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 |
|
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 |
public static CsvPreference getPredefinedCSVPreferences(DynObject dynobj) { |
115 |
String s = (String) dynobj.getDynValue(PROFILE); |
116 |
if ( "NONE".equalsIgnoreCase(s) ) { |
117 |
return null; |
118 |
} |
119 |
if ( "STANDARD_PREFERENCE".equalsIgnoreCase(s) ) { |
120 |
return CsvPreference.STANDARD_PREFERENCE;
|
121 |
} |
122 |
if ( "EXCEL_PREFERENCE".equalsIgnoreCase(s) ) { |
123 |
return CsvPreference.EXCEL_PREFERENCE;
|
124 |
} |
125 |
if ( "EXCEL_NORTH_EUROPE_PREFERENCE".equalsIgnoreCase(s) ) { |
126 |
return CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE;
|
127 |
} |
128 |
if ( "TAB_PREFERENCE".equalsIgnoreCase(s) ) { |
129 |
return CsvPreference.TAB_PREFERENCE;
|
130 |
} |
131 |
return null; |
132 |
} |
133 |
|
134 |
public static QuoteMode getQuoteMode(DynObject dynobj) { |
135 |
String s = (String) dynobj.getDynValue(QUOTEPOLICY); |
136 |
if ( "AlwaysQuoteMode".equalsIgnoreCase(s) ) { |
137 |
return new AlwaysQuoteMode(); |
138 |
} |
139 |
if ( "NormalQuoteMode".equalsIgnoreCase(s) ) { |
140 |
return new NormalQuoteMode(); |
141 |
} |
142 |
return null; |
143 |
} |
144 |
|
145 |
static IProjection getCRS(DynObject dynobj) {
|
146 |
return (IProjection) dynobj.getDynValue(CRS);
|
147 |
} |
148 |
|
149 |
static String getFileName(DynObject dynobj) { |
150 |
File f = (File) dynobj.getDynValue(FILE); |
151 |
if ( f == null ) { |
152 |
return null; |
153 |
} |
154 |
return f.getPath();
|
155 |
} |
156 |
|
157 |
static File getFile(DynObject dynobj) { |
158 |
File f = (File) dynobj.getDynValue(FILE); |
159 |
return f;
|
160 |
} |
161 |
|
162 |
public static String getRecordSeparator(DynObject dynobj) { |
163 |
String s = (String) dynobj.getDynValue(RECORDSEPARATOR); |
164 |
return StringEscapeUtils.unescapeJava(s);
|
165 |
} |
166 |
|
167 |
public static String getGeometryColumn(DynObject dynobj) { |
168 |
String s = (String) dynobj.getDynValue(GEOEMTRY_COLUMN); |
169 |
return s;
|
170 |
} |
171 |
|
172 |
static Locale getLocale(DynObject dynobj) { |
173 |
try {
|
174 |
String s = (String) dynobj.getDynValue(LOCALE); |
175 |
if ( s.trim().length() == 0 ) { |
176 |
return null; |
177 |
} |
178 |
if ( "DEFAULT".equalsIgnoreCase(s.trim()) ) { |
179 |
return Locale.getDefault(); |
180 |
} |
181 |
Locale locale = null; |
182 |
// locale = Locale.forLanguageTag(s); // Since java 1.7
|
183 |
String[] ss = s.split("-"); |
184 |
switch (ss.length) {
|
185 |
case 1: |
186 |
locale = new Locale(ss[0]); |
187 |
break;
|
188 |
case 2: |
189 |
locale = new Locale(ss[0], ss[1]); |
190 |
break;
|
191 |
case 3: |
192 |
default:
|
193 |
locale = new Locale(ss[0], ss[1], ss[2]); |
194 |
break;
|
195 |
} |
196 |
return locale;
|
197 |
} catch (Exception ex) { |
198 |
logger.warn("Can't get locale from CSV parameters.", ex);
|
199 |
return null; |
200 |
} |
201 |
} |
202 |
|
203 |
public static String getCommentStartMarker(DynObject dynobj) { |
204 |
String s = (String) dynobj.getDynValue(COMMENTSTARTMARKER); |
205 |
return StringEscapeUtils.unescapeJava(s);
|
206 |
} |
207 |
|
208 |
public static String getPointColumnName(DynObject dynobj) { |
209 |
String s = (String) dynobj.getDynValue(POINT_COLUMN_NAME); |
210 |
return s;
|
211 |
} |
212 |
|
213 |
public static String getQuoteCharacter(DynObject dynobj) { |
214 |
String s = (String) dynobj.getDynValue(QUOTECHAR); |
215 |
s = StringEscapeUtils.unescapeJava(s); |
216 |
if ( StringUtils.isBlank(s) ) {
|
217 |
return null; |
218 |
} |
219 |
return s.substring(0, 1); |
220 |
} |
221 |
|
222 |
public static String getDelimiter(DynObject dynobj) { |
223 |
String s = (String) dynobj.getDynValue(DELIMITER); |
224 |
s = StringEscapeUtils.unescapeJava(s); |
225 |
if ( StringUtils.isBlank(s) ) {
|
226 |
return null; |
227 |
} |
228 |
return s.substring(0, 1); |
229 |
} |
230 |
|
231 |
static String getHeader(DynObject dynobj) { |
232 |
String s = (String) dynobj.getDynValue(HEADER); |
233 |
s = StringEscapeUtils.unescapeJava(s); |
234 |
if ( StringUtils.isBlank(s) ) {
|
235 |
return null; |
236 |
} |
237 |
return s;
|
238 |
} |
239 |
|
240 |
static String[] getHeaders(DynObject dynobj) { |
241 |
String s = getHeader(dynobj);
|
242 |
if ( StringUtils.isBlank(s) ) {
|
243 |
return null; |
244 |
} |
245 |
String sep = getDelimiter(dynobj);
|
246 |
if ( sep == null ) { |
247 |
sep = getDelimiter(s); |
248 |
if ( sep == null ) { |
249 |
// Chungo
|
250 |
return null; |
251 |
} |
252 |
} |
253 |
String[] ss = s.split("[" + sep + "]"); |
254 |
return ss;
|
255 |
} |
256 |
|
257 |
static String getDelimiter(String line) { |
258 |
if( StringUtils.isBlank(line) ) {
|
259 |
return null; |
260 |
} |
261 |
String sep = null; |
262 |
// Cuidado con los ":", los he puesto al final a proposito
|
263 |
// ya que podian estar en la cadena para separar el size
|
264 |
// de cada tipo.
|
265 |
String seps = ",;-|@#/+$%&!:"; |
266 |
for ( int i = 0; i < seps.length(); i++ ) { |
267 |
sep = seps.substring(i, 1);
|
268 |
if ( line.contains(seps.substring(i, 1)) ) { |
269 |
break;
|
270 |
} |
271 |
sep = null;
|
272 |
} |
273 |
return sep;
|
274 |
} |
275 |
|
276 |
static String getCharset(DynObject dynobj) { |
277 |
String s = (String) dynobj.getDynValue(CHARSET); |
278 |
return StringEscapeUtils.unescapeJava(s);
|
279 |
} |
280 |
|
281 |
static String[] getPointDimensionNames(DynObject dynobj) { |
282 |
String s = (String) dynobj.getDynValue("point"); |
283 |
if ( StringUtils.isBlank(s) ) {
|
284 |
return null; |
285 |
} |
286 |
return s.split(","); |
287 |
} |
288 |
|
289 |
public static boolean getSurroundingSpacesNeedQuotes(DynObject dynobj) { |
290 |
Boolean b = (Boolean) dynobj.getDynValue(SURROUNDINGSPACESNEEDQUOTES); |
291 |
return BooleanUtils.isTrue(b);
|
292 |
} |
293 |
|
294 |
static boolean getAutomaticTypesDetection(DynObject dynobj) { |
295 |
Boolean b = (Boolean) dynobj.getDynValue(AUTOMATICTYPESDETECTION); |
296 |
return BooleanUtils.isTrue(b);
|
297 |
} |
298 |
|
299 |
static boolean getIgnoreErrors(DynObject dynobj) { |
300 |
Boolean b = (Boolean) dynobj.getDynValue(IGNOREERRORS); |
301 |
return BooleanUtils.isTrue(b);
|
302 |
} |
303 |
|
304 |
static boolean isFirstLineHeader(DynObject dynobj) { |
305 |
Boolean b = (Boolean) dynobj.getDynValue(FIRST_LINE_HEADER); |
306 |
return BooleanUtils.isTrue(b);
|
307 |
} |
308 |
|
309 |
// static int[] getFieldTypes(DynObject dynobj) {
|
310 |
// String s = (String) dynobj.getDynValue(FIELDTYPES);
|
311 |
// if ( StringUtils.isBlank(s) ) {
|
312 |
// return null;
|
313 |
// }
|
314 |
// String sep = getDelimiter(s);
|
315 |
// if ( sep == null ) {
|
316 |
// return null;
|
317 |
// }
|
318 |
// DataTypesManager dataTypeManager = ToolsLocator.getDataTypesManager();
|
319 |
// String fieldTypeNames[] = s.split("[" + sep + "]");
|
320 |
// int fieldTypes[] = new int[fieldTypeNames.length];
|
321 |
// for ( int i = 0; i < fieldTypeNames.length; i++ ) {
|
322 |
// s = fieldTypeNames[i].trim();
|
323 |
// if ( s.contains(":") ) {
|
324 |
// s = s.split(":")[0];
|
325 |
// }
|
326 |
// fieldTypes[i] = dataTypeManager.getType(s);
|
327 |
// }
|
328 |
// return fieldTypes;
|
329 |
// }
|
330 |
//
|
331 |
// static int[] getFieldSizes(DynObject dynobj) {
|
332 |
// String s = (String) dynobj.getDynValue(FIELDTYPES);
|
333 |
// if ( StringUtils.isBlank(s) ) {
|
334 |
// return null;
|
335 |
// }
|
336 |
// String sep = getDelimiter(s);
|
337 |
// if ( sep == null ) {
|
338 |
// return null;
|
339 |
// }
|
340 |
// DataTypesManager dataTypeManager = ToolsLocator.getDataTypesManager();
|
341 |
// String fieldTypeNames[] = s.split("[" + sep + "]");
|
342 |
// int fieldSizes[] = new int[fieldTypeNames.length];
|
343 |
// for ( int i = 0; i < fieldTypeNames.length; i++ ) {
|
344 |
// String fieldtypeDef = fieldTypeNames[i].trim();
|
345 |
// if ( fieldtypeDef.contains(":") ) {
|
346 |
// try {
|
347 |
// String[] parts = fieldtypeDef.split(":");
|
348 |
// int fieldType = dataTypeManager.getType(parts[0]);
|
349 |
// if( fieldType == DataTypes.GEOMETRY ) {
|
350 |
// fieldSizes[i] = 1;
|
351 |
// } else {
|
352 |
// s = parts[1];
|
353 |
// fieldSizes[i] = Integer.parseInt(s);
|
354 |
// }
|
355 |
// } catch (Exception ex) {
|
356 |
// logger.warn("Can't get size of field " + i + " (" + fieldtypeDef + ").", ex);
|
357 |
// }
|
358 |
// } else {
|
359 |
// fieldSizes[i] = 0;
|
360 |
// }
|
361 |
// }
|
362 |
// return fieldSizes;
|
363 |
// }
|
364 |
|
365 |
static String getRawFieldTypes(DynObject dynobj) { |
366 |
String s = (String) dynobj.getDynValue(FIELDTYPES); |
367 |
if ( StringUtils.isBlank(s) ) {
|
368 |
return null; |
369 |
} |
370 |
return s.trim();
|
371 |
} |
372 |
|
373 |
static int getSkipLines(DynObject dynobj) { |
374 |
Integer n = (Integer) dynobj.getDynValue("skipLines"); |
375 |
if ( n == null ) { |
376 |
return 0; |
377 |
} |
378 |
return n;
|
379 |
} |
380 |
|
381 |
static int getLimit(DynObject dynobj) { |
382 |
Integer n = (Integer) dynobj.getDynValue(LIMIT); |
383 |
if ( n == null ) { |
384 |
return -1; |
385 |
} |
386 |
return n;
|
387 |
} |
388 |
|
389 |
static String getRawFieldsDefinition(DynObject dynobj) { |
390 |
String s = (String) dynobj.getDynValue("fieldsDefinition"); |
391 |
if ( StringUtils.isBlank(s) ) {
|
392 |
return null; |
393 |
} |
394 |
return s.trim();
|
395 |
} |
396 |
|
397 |
public static class FieldDefinition { |
398 |
|
399 |
private int start; |
400 |
private int end; |
401 |
|
402 |
public FieldDefinition(String def) { |
403 |
def = def.trim(); |
404 |
String[] ss = def.split(":"); |
405 |
this.start = Integer.parseInt(ss[0]); |
406 |
if ( ss.length < 2 ) { |
407 |
this.end = -1; |
408 |
} else {
|
409 |
this.end = Integer.parseInt(ss[1]); |
410 |
} |
411 |
} |
412 |
|
413 |
public int getStart() { |
414 |
return this.start; |
415 |
} |
416 |
|
417 |
public int getEnd() { |
418 |
return this.end; |
419 |
} |
420 |
|
421 |
public boolean getToEndOfLine() { |
422 |
return this.end == -1; |
423 |
} |
424 |
} |
425 |
|
426 |
public static FieldDefinition[] getFieldsDefinition(DynObject dynobj) { |
427 |
String definition = getRawFieldsDefinition(dynobj);
|
428 |
if ( definition == null ) { |
429 |
return null; |
430 |
} |
431 |
int i=0; |
432 |
try {
|
433 |
String[] defs = StringUtils.split(definition); |
434 |
FieldDefinition[] fieldsDefinition = new FieldDefinition[defs.length]; |
435 |
for ( i = 0; i < defs.length; i++ ) { |
436 |
fieldsDefinition[i] = new FieldDefinition(defs[i]);
|
437 |
} |
438 |
return fieldsDefinition;
|
439 |
} catch (Exception ex) { |
440 |
throw new IllegalArgumentException("Can't recognize the format field definition '"+definition+"' ("+i+")."); |
441 |
} |
442 |
} |
443 |
|
444 |
} |