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