Revision 47638 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
CSVStoreParameters.java | ||
---|---|---|
39 | 39 |
import org.gvsig.fmap.dal.feature.FeatureType; |
40 | 40 |
import org.gvsig.fmap.dal.feature.OpenFeatureStoreParameters; |
41 | 41 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
42 |
import org.gvsig.fmap.dal.store.gml.GMLFeatureTypeLoader; |
|
43 |
import org.gvsig.fmap.dal.store.simplereader.SimpleReaderFeatureTypeLoader; |
|
42 | 44 |
import org.gvsig.fmap.dal.store.simplereader.SimpleReaderStoreParameters; |
43 | 45 |
import org.gvsig.fmap.geom.Geometry; |
44 | 46 |
import org.gvsig.tools.dynobject.DelegatedDynObject; |
... | ... | |
59 | 61 |
|
60 | 62 |
public static final String PARAMETERS_DEFINITION_NAME = "CSVStoreParameters"; |
61 | 63 |
|
62 |
private static final String FILE = "file"; |
|
63 |
private static final String IGNOREERRORS = "ignoreErrors"; |
|
64 |
// private static final String FILE = "file";
|
|
65 |
// private static final String IGNOREERRORS = "ignoreErrors";
|
|
64 | 66 |
private static final String PROFILE = "profile"; |
65 | 67 |
private static final String QUOTEPOLICY = "quotePolicy"; |
66 | 68 |
private static final String QUOTECHAR = "quoteCharacter"; |
67 | 69 |
private static final String RECORDSEPARATOR = "recordSeparator"; |
68 | 70 |
private static final String DELIMITER = "delimiter"; |
69 | 71 |
private static final String COMMENTSTARTMARKER = "commentStartMarker"; |
70 |
private static final String AUTOMATICTYPESDETECTION = "automaticTypesDetection"; |
|
72 |
// private static final String AUTOMATICTYPESDETECTION = "automaticTypesDetection";
|
|
71 | 73 |
|
72 | 74 |
private static final String ESCAPECHARACTER = "escapeCharacter"; |
73 | 75 |
public static final String FIRST_LINE_HEADER = "firstLineHeader"; |
74 |
public static final String HEADER = "header"; |
|
76 |
// public static final String HEADER = "header";
|
|
75 | 77 |
private static final String SURROUNDINGSPACESNEEDQUOTES = "surroundingSpacesNeedQuotes"; |
76 | 78 |
|
77 | 79 |
//private static final String IGNOREEMPTYLINES = "ignoreEmptyLines"; |
78 |
private static final String CRS = CRS_PARAMTER_NAME; |
|
79 |
private static final String FIELDTYPES = "fieldtypes"; |
|
80 |
// private static final String NULLTO = "nullTo"; |
|
81 |
private static final String CHARSET = "charset"; // Default "UTF-8" |
|
82 |
private static final String LOCALE = "locale"; |
|
80 |
// private static final String CRS = CRS_PARAMTER_NAME; |
|
81 |
// private static final String FIELDTYPES = "fieldtypes"; |
|
82 |
// private static final String CHARSET = "charset"; // Default "UTF-8" |
|
83 |
// private static final String LOCALE = "locale"; |
|
83 | 84 |
private static final String POINT_COLUMN_NAME = "pointColumnName"; |
84 |
// private static final String LIMIT = "limit"; |
|
85 | 85 |
private static final String INCLUDE_METADATA_IN_HEADER = "includeMetadataInHeader"; |
86 | 86 |
private static final String GEOMETRY_COLUMN = "geometry_column"; |
87 | 87 |
private static final String GEOMETRY_TYPE = "GeometryType"; |
88 | 88 |
private static final String GEOMETRY_SUBTYPE = "GeometrySubtype"; |
89 | 89 |
private static final String GEOMETRY_FORMAT = "GeometryFormat"; |
90 | 90 |
|
91 |
private DelegatedDynObject parameters; |
|
92 |
private FeatureType featureType; |
|
93 |
private boolean defaultValueOfAutomaticTypesDetection = true; |
|
94 |
|
|
95 | 91 |
public CSVStoreParameters() { |
96 | 92 |
this(PARAMETERS_DEFINITION_NAME); |
97 | 93 |
} |
... | ... | |
104 | 100 |
if( this.featureType==null ) { |
105 | 101 |
try { |
106 | 102 |
EditableFeatureType ftype = DALLocator.getDataManager().createFeatureType(); |
107 |
boolean all_fields_declare_type = CSVUtils.loadFeatureType(this, ftype, false, null); |
|
103 |
SimpleReaderFeatureTypeLoader featureTypeLoader = new CSVFeatureTypeLoader(this); |
|
104 |
featureTypeLoader.loadFeatureType(ftype, false, null); |
|
105 |
boolean all_fields_declare_type = featureTypeLoader.isAllFieldsDeclareType(); |
|
108 | 106 |
defaultValueOfAutomaticTypesDetection = !all_fields_declare_type; |
109 | 107 |
this.featureType = ftype; |
110 | 108 |
} catch (Exception ex) { |
... | ... | |
115 | 113 |
return this.featureType; |
116 | 114 |
} |
117 | 115 |
|
118 |
@Override |
|
119 |
protected DelegatedDynObject getDelegatedDynObject() { |
|
120 |
return parameters; |
|
121 |
} |
|
116 |
// @Override |
|
117 |
// protected DelegatedDynObject getDelegatedDynObject() { |
|
118 |
// return parameters; |
|
119 |
// } |
|
120 |
// |
|
121 |
// @Override |
|
122 |
// public void setDynValue(String name, Object value) { |
|
123 |
// super.setDynValue(name, value); |
|
124 |
// this.featureType = null; |
|
125 |
// } |
|
122 | 126 |
|
123 | 127 |
@Override |
124 |
public void setDynValue(String name, Object value) { |
|
125 |
super.setDynValue(name, value); |
|
126 |
this.featureType = null; |
|
127 |
} |
|
128 |
|
|
129 |
@Override |
|
130 | 128 |
public void validate() throws ValidateDataParametersException { |
131 | 129 |
File f = this.getFile(); |
132 | 130 |
if( f!=null ) { |
... | ... | |
176 | 174 |
super.validate(); |
177 | 175 |
} |
178 | 176 |
|
179 |
@Override |
|
180 |
public boolean isValid() { |
|
181 |
if ( getFileName(this) == null ) { |
|
182 |
return false; |
|
183 |
} |
|
184 |
return true; |
|
185 |
} |
|
186 |
|
|
187 |
@Override |
|
188 |
public File getFile() { |
|
189 |
return (File) this.getDynValue(FILE); |
|
190 |
} |
|
191 |
|
|
192 |
@Override |
|
193 |
public void setFile(File file) { |
|
194 |
this.setDynValue(FILE, file); |
|
195 |
} |
|
196 |
|
|
197 | 177 |
public static CsvPreference getPredefinedCSVPreferences(DynObject dynobj) { |
198 | 178 |
String s = (String) dynobj.getDynValue(PROFILE); |
199 | 179 |
if ( "NONE".equalsIgnoreCase(s) ) { |
... | ... | |
237 | 217 |
return f.getPath(); |
238 | 218 |
} |
239 | 219 |
|
240 |
public static File getFile(DynObject dynobj) { |
|
241 |
File f = (File) dynobj.getDynValue(FILE); |
|
242 |
return f; |
|
243 |
} |
|
244 |
|
|
245 | 220 |
public static String getRecordSeparator(DynObject dynobj) { |
246 | 221 |
String s = (String) dynobj.getDynValue(RECORDSEPARATOR); |
247 | 222 |
return StringEscapeUtils.unescapeJava(s); |
... | ... | |
283 | 258 |
} |
284 | 259 |
return StringUtils.equalsIgnoreCase("DEFAULT",s.trim()); |
285 | 260 |
} |
286 |
|
|
287 |
public static Locale getLocale(DynObject dynobj) { |
|
288 |
try { |
|
289 |
String s = (String) dynobj.getDynValue(LOCALE); |
|
290 |
if ( StringUtils.isBlank(s) ) { |
|
291 |
return null; |
|
292 |
} |
|
293 |
if (StringUtils.equalsIgnoreCase("DEFAULT",s.trim())) { |
|
294 |
return Locale.getDefault(); |
|
295 |
} |
|
296 |
Locale locale; |
|
297 |
// locale = Locale.forLanguageTag(s); // Since java 1.7 |
|
298 |
String[] ss = s.split("-"); |
|
299 |
switch (ss.length) { |
|
300 |
case 1: |
|
301 |
locale = new Locale(ss[0]); |
|
302 |
break; |
|
303 |
case 2: |
|
304 |
locale = new Locale(ss[0], ss[1]); |
|
305 |
break; |
|
306 |
case 3: |
|
307 |
default: |
|
308 |
locale = new Locale(ss[0], ss[1], ss[2]); |
|
309 |
break; |
|
310 |
} |
|
311 |
return locale; |
|
312 |
} catch (Exception ex) { |
|
313 |
LOGGER.warn("Can't get locale from CSV parameters.", ex); |
|
314 |
return null; |
|
315 |
} |
|
316 |
} |
|
317 |
|
|
318 | 261 |
public static String getCommentStartMarker(DynObject dynobj) { |
319 | 262 |
String s = (String) dynobj.getDynValue(COMMENTSTARTMARKER); |
320 | 263 |
return StringEscapeUtils.unescapeJava(s); |
... | ... | |
343 | 286 |
return s.substring(0, 1); |
344 | 287 |
} |
345 | 288 |
|
346 |
public static String getHeader(DynObject dynobj) { |
|
347 |
String s = (String) dynobj.getDynValue(HEADER); |
|
348 |
s = StringEscapeUtils.unescapeJava(s); |
|
349 |
if ( StringUtils.isBlank(s) ) { |
|
350 |
return null; |
|
351 |
} |
|
352 |
return s; |
|
353 |
} |
|
354 |
|
|
355 |
public static String[] getHeaders(DynObject dynobj) { |
|
356 |
String s = getHeader(dynobj); |
|
357 |
if ( StringUtils.isBlank(s) ) { |
|
358 |
return null; |
|
359 |
} |
|
360 |
String sep = getDelimiter(dynobj); |
|
361 |
if ( sep == null ) { |
|
362 |
sep = getDelimiter(s); |
|
363 |
if ( sep == null ) { |
|
364 |
// Chungo |
|
365 |
return null; |
|
366 |
} |
|
367 |
} |
|
368 |
String[] ss = s.split("[" + sep + "]"); |
|
369 |
return ss; |
|
370 |
} |
|
371 |
|
|
372 |
public static String getDelimiter(String line) { |
|
373 |
if( StringUtils.isBlank(line) ) { |
|
374 |
return null; |
|
375 |
} |
|
376 |
String sep = null; |
|
377 |
// Cuidado con los ":", los he puesto al final a proposito |
|
378 |
// ya que podian estar en la cadena para separar el size |
|
379 |
// de cada tipo. |
|
380 |
String seps = ",;-|@#/+$%&!:"; |
|
381 |
for ( int i = 0; i < seps.length(); i++ ) { |
|
382 |
sep = seps.substring(i, 1); |
|
383 |
if ( line.contains(seps.substring(i, 1)) ) { |
|
384 |
break; |
|
385 |
} |
|
386 |
sep = null; |
|
387 |
} |
|
388 |
return sep; |
|
389 |
} |
|
390 |
|
|
391 |
public static String getCharset(DynObject dynobj) { |
|
392 |
String s = (String) dynobj.getDynValue(CHARSET); |
|
393 |
return StringEscapeUtils.unescapeJava(s); |
|
394 |
} |
|
395 |
|
|
396 | 289 |
public static String[] getPointDimensionNames(DynObject dynobj) { |
397 | 290 |
String s = (String) dynobj.getDynValue("point"); |
398 | 291 |
if ( StringUtils.isBlank(s) ) { |
... | ... | |
411 | 304 |
return BooleanUtils.isTrue(b); |
412 | 305 |
} |
413 | 306 |
|
414 |
public static boolean getAutomaticTypesDetection(DynObject dynobj) { |
|
415 |
Boolean b = (Boolean) dynobj.getDynValue(AUTOMATICTYPESDETECTION); |
|
416 |
return BooleanUtils.isTrue(b); |
|
417 |
} |
|
418 |
|
|
419 |
public static boolean getIgnoreErrors(DynObject dynobj) { |
|
420 |
Boolean b = (Boolean) dynobj.getDynValue(IGNOREERRORS); |
|
421 |
return BooleanUtils.isTrue(b); |
|
422 |
} |
|
423 |
|
|
424 | 307 |
public static boolean isFirstLineHeader(DynObject dynobj) { |
425 | 308 |
Boolean b = (Boolean) dynobj.getDynValue(FIRST_LINE_HEADER); |
426 | 309 |
return BooleanUtils.isTrue(b); |
427 | 310 |
} |
428 | 311 |
|
429 |
// static int[] getFieldTypes(DynObject dynobj) { |
|
430 |
// String s = (String) dynobj.getDynValue(FIELDTYPES); |
|
431 |
// if ( StringUtils.isBlank(s) ) { |
|
432 |
// return null; |
|
433 |
// } |
|
434 |
// String sep = getDelimiter(s); |
|
435 |
// if ( sep == null ) { |
|
436 |
// return null; |
|
437 |
// } |
|
438 |
// DataTypesManager dataTypeManager = ToolsLocator.getDataTypesManager(); |
|
439 |
// String fieldTypeNames[] = s.split("[" + sep + "]"); |
|
440 |
// int fieldTypes[] = new int[fieldTypeNames.length]; |
|
441 |
// for ( int i = 0; i < fieldTypeNames.length; i++ ) { |
|
442 |
// s = fieldTypeNames[i].trim(); |
|
443 |
// if ( s.contains(":") ) { |
|
444 |
// s = s.split(":")[0]; |
|
445 |
// } |
|
446 |
// fieldTypes[i] = dataTypeManager.getType(s); |
|
447 |
// } |
|
448 |
// return fieldTypes; |
|
449 |
// } |
|
450 |
// |
|
451 |
// static int[] getFieldSizes(DynObject dynobj) { |
|
452 |
// String s = (String) dynobj.getDynValue(FIELDTYPES); |
|
453 |
// if ( StringUtils.isBlank(s) ) { |
|
454 |
// return null; |
|
455 |
// } |
|
456 |
// String sep = getDelimiter(s); |
|
457 |
// if ( sep == null ) { |
|
458 |
// return null; |
|
459 |
// } |
|
460 |
// DataTypesManager dataTypeManager = ToolsLocator.getDataTypesManager(); |
|
461 |
// String fieldTypeNames[] = s.split("[" + sep + "]"); |
|
462 |
// int fieldSizes[] = new int[fieldTypeNames.length]; |
|
463 |
// for ( int i = 0; i < fieldTypeNames.length; i++ ) { |
|
464 |
// String fieldtypeDef = fieldTypeNames[i].trim(); |
|
465 |
// if ( fieldtypeDef.contains(":") ) { |
|
466 |
// try { |
|
467 |
// String[] parts = fieldtypeDef.split(":"); |
|
468 |
// int fieldType = dataTypeManager.getType(parts[0]); |
|
469 |
// if( fieldType == DataTypes.GEOMETRY ) { |
|
470 |
// fieldSizes[i] = 1; |
|
471 |
// } else { |
|
472 |
// s = parts[1]; |
|
473 |
// fieldSizes[i] = Integer.parseInt(s); |
|
474 |
// } |
|
475 |
// } catch (Exception ex) { |
|
476 |
// logger.warn("Can't get size of field " + i + " (" + fieldtypeDef + ").", ex); |
|
477 |
// } |
|
478 |
// } else { |
|
479 |
// fieldSizes[i] = 0; |
|
480 |
// } |
|
481 |
// } |
|
482 |
// return fieldSizes; |
|
483 |
// } |
|
484 |
|
|
485 |
public static String getRawFieldTypes(DynObject dynobj) { |
|
486 |
String s = (String) dynobj.getDynValue(FIELDTYPES); |
|
487 |
if ( StringUtils.isBlank(s) ) { |
|
488 |
return null; |
|
489 |
} |
|
490 |
return s.trim(); |
|
491 |
} |
|
492 |
|
|
493 |
// public static int getSkipLines(DynObject dynobj) { |
|
494 |
// Integer n = (Integer) dynobj.getDynValue("skipLines"); |
|
495 |
// if ( n == null ) { |
|
496 |
// return 0; |
|
497 |
// } |
|
498 |
// return n; |
|
499 |
// } |
|
500 |
// |
|
501 |
// public static int getLimit(DynObject dynobj) { |
|
502 |
// Integer n = (Integer) dynobj.getDynValue(LIMIT); |
|
503 |
// if ( n == null ) { |
|
504 |
// return -1; |
|
505 |
// } |
|
506 |
// return n; |
|
507 |
// } |
|
508 |
// |
|
509 |
public static String getRawFieldsDefinition(DynObject dynobj) { |
|
510 |
String s = (String) dynobj.getDynValue("fieldsDefinition"); |
|
511 |
if ( StringUtils.isBlank(s) ) { |
|
512 |
return null; |
|
513 |
} |
|
514 |
return s.trim(); |
|
515 |
} |
|
516 |
|
|
517 |
public static class FieldDefinition { |
|
518 |
|
|
519 |
private final int start; |
|
520 |
private final int end; |
|
521 |
|
|
522 |
public FieldDefinition(String def) { |
|
523 |
def = def.trim(); |
|
524 |
String[] ss = def.split(":"); |
|
525 |
this.start = Integer.parseInt(ss[0]); |
|
526 |
if ( ss.length < 2 ) { |
|
527 |
this.end = -1; |
|
528 |
} else { |
|
529 |
this.end = Integer.parseInt(ss[1]); |
|
530 |
} |
|
531 |
} |
|
532 |
|
|
533 |
public int getStart() { |
|
534 |
return this.start; |
|
535 |
} |
|
536 |
|
|
537 |
public int getEnd() { |
|
538 |
return this.end; |
|
539 |
} |
|
540 |
|
|
541 |
public boolean getToEndOfLine() { |
|
542 |
return this.end == -1; |
|
543 |
} |
|
544 |
} |
|
545 |
|
|
546 |
public static FieldDefinition[] getFieldsDefinition(DynObject dynobj) { |
|
547 |
String definition = getRawFieldsDefinition(dynobj); |
|
548 |
if ( definition == null ) { |
|
549 |
return null; |
|
550 |
} |
|
551 |
int i=0; |
|
552 |
try { |
|
553 |
String[] defs = StringUtils.split(definition); |
|
554 |
FieldDefinition[] fieldsDefinition = new FieldDefinition[defs.length]; |
|
555 |
for ( i = 0; i < defs.length; i++ ) { |
|
556 |
fieldsDefinition[i] = new FieldDefinition(defs[i]); |
|
557 |
} |
|
558 |
return fieldsDefinition; |
|
559 |
} catch (Exception ex) { |
|
560 |
throw new IllegalArgumentException("Can't recognize the format field definition '"+definition+"' ("+i+")."); |
|
561 |
} |
|
562 |
} |
|
563 | 312 |
|
564 | 313 |
} |
Also available in: Unified diff