Revision 41069 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/CSVStoreProvider.java
CSVStoreProvider.java | ||
---|---|---|
33 | 33 |
import java.util.HashMap; |
34 | 34 |
import java.util.Iterator; |
35 | 35 |
import java.util.List; |
36 |
import java.util.Locale; |
|
36 | 37 |
|
37 | 38 |
import org.apache.commons.io.FilenameUtils; |
38 | 39 |
import org.cresques.cts.IProjection; |
... | ... | |
76 | 77 |
import org.gvsig.tools.dataTypes.CoercionException; |
77 | 78 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
78 | 79 |
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion; |
80 |
import org.gvsig.tools.dataTypes.DataTypesManager.CoercionWithLocale; |
|
79 | 81 |
import org.gvsig.tools.dispose.DisposableIterator; |
80 | 82 |
import org.gvsig.tools.dynobject.DynObject; |
81 | 83 |
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException; |
... | ... | |
147 | 149 |
} |
148 | 150 |
|
149 | 151 |
public boolean allowWrite() { |
150 |
return false;
|
|
152 |
return true;
|
|
151 | 153 |
} |
152 | 154 |
|
153 | 155 |
private String getFullFileName() { |
... | ... | |
208 | 210 |
private Coercion convert = null; |
209 | 211 |
private int errorcounts =0; |
210 | 212 |
private Throwable lasterror = null; |
213 |
private Locale locale = null; |
|
211 | 214 |
|
212 | 215 |
public void initialize(File file, FeatureType ftype, CsvPreference csvpreferences) { |
213 | 216 |
this.file = file; |
214 | 217 |
this.ftype = ftype; |
215 | 218 |
this.csvpreferences = csvpreferences; |
219 |
this.locale = CSVStoreParameters.getLocale(getCSVParameters()); |
|
216 | 220 |
if( csvpreferences == null ) { |
217 | 221 |
this.csvpreferences = CsvPreference.STANDARD_PREFERENCE; |
218 | 222 |
} |
... | ... | |
247 | 251 |
FeatureAttributeDescriptor descriptor = descriptors[i]; |
248 | 252 |
if( descriptor.getEvaluator()== null ) { |
249 | 253 |
String name = descriptor.getName(); |
250 |
String type = descriptor.getDataTypeName(); |
|
251 |
header[n++] = name + "__" + type; |
|
254 |
String typeName = descriptor.getDataTypeName(); |
|
255 |
if( descriptor.getDataType().getType() == DataTypes.STRING ) { |
|
256 |
header[n++] = name + "__" + typeName + "__" + descriptor.getSize(); |
|
257 |
} else { |
|
258 |
header[n++] = name + "__" + typeName; |
|
259 |
} |
|
252 | 260 |
} |
253 | 261 |
} |
254 | 262 |
try { |
... | ... | |
281 | 289 |
Object value = feature.get(i); |
282 | 290 |
try { |
283 | 291 |
n++; |
284 |
values[n] = (String) this.convert.coerce(value); |
|
292 |
if( this.convert!=null && this.convert instanceof CoercionWithLocale ) { |
|
293 |
values[n] = (String) ((CoercionWithLocale)this.convert).coerce(value,this.locale); |
|
294 |
} else { |
|
295 |
values[n] = (String) this.convert.coerce(value); |
|
296 |
} |
|
285 | 297 |
} catch (CoercionException e) { |
286 | 298 |
try { |
287 | 299 |
values[n] = value.toString(); |
... | ... | |
352 | 364 |
File file = (File) resource.get(); |
353 | 365 |
|
354 | 366 |
Writer writer = new Writer(); |
355 |
writer.initialize(file,getStoreServices().getDefaultFeatureType(),null);
|
|
367 |
writer.initialize(file,getStoreServices().getDefaultFeatureType(),getCSVPreferences());
|
|
356 | 368 |
features = |
357 | 369 |
getStoreServices().getFeatureStore() |
358 | 370 |
.getFeatureSet(); |
... | ... | |
585 | 597 |
DataTypesManager dataTypesManager = ToolsLocator.getDataTypesManager(); |
586 | 598 |
|
587 | 599 |
int[] types = new int[headers.length]; |
600 |
int[] sizes = new int[headers.length]; |
|
588 | 601 |
// |
589 | 602 |
// Calculamos cuales pueden ser los tipos de datos |
590 | 603 |
// |
... | ... | |
592 | 605 |
// Por defecto todos string |
593 | 606 |
for( int i=0; i<types.length ; i++) { |
594 | 607 |
types[i] = DataTypes.STRING; |
608 |
sizes[i] = 0; |
|
595 | 609 |
} |
596 | 610 |
|
597 | 611 |
// Luego asuminos los tipos pasados por parametro, que se supone |
... | ... | |
604 | 618 |
|
605 | 619 |
// Luego probamos con lo que diga las cabezeras del CVS, sobreescribiendo |
606 | 620 |
// los tipos anteriores en caso de definirse en la cabezara. |
621 |
// El formato de la cabecera seria: |
|
622 |
// name[:typename[:size]] |
|
623 |
// name[__typename[__size]] |
|
624 |
// |
|
607 | 625 |
for( int i=0; i<types.length; i++) { |
608 | 626 |
String s = headers[i]; |
609 | 627 |
String typename = null; |
628 |
String[] ss = null; |
|
610 | 629 |
if( s.contains(":") ) { |
611 |
String[] ss = s.split(":"); |
|
630 |
ss = s.split(":"); |
|
631 |
} else if( s.contains("__") ) { |
|
632 |
ss = s.split("__"); |
|
633 |
} |
|
634 |
if( ss!=null ) { |
|
612 | 635 |
headers[i] = ss[0]; |
613 | 636 |
typename = ss[1]; |
614 | 637 |
types[i] = dataTypesManager.getType(typename); |
615 |
} else if( s.contains("__") ) { |
|
616 |
String[] ss = s.split("__"); |
|
617 |
headers[i] = ss[0]; |
|
618 |
typename = ss[1]; |
|
619 |
types[i] = dataTypesManager.getType(typename); |
|
638 |
if( types[i]==DataTypes.INVALID ) { |
|
639 |
types[i] = DataTypes.STRING; |
|
640 |
logger.info("Type '"+typename+"' not valid for attribute '"+s+"' in CSV file '"+this.getFullFileName()+"'."); |
|
641 |
} |
|
642 |
if( types[i]== DataTypes.STRING && ss.length>2 ) { |
|
643 |
try { |
|
644 |
sizes[i] = Integer.parseInt(ss[2]); |
|
645 |
} catch(Exception ex) { |
|
646 |
logger.warn("Ignore incorrect field size for field "+i+" ("+s+") in CSV header of '"+getFullFileName()+"'.",ex); |
|
647 |
} |
|
648 |
} |
|
620 | 649 |
} |
621 |
if( types[i]==DataTypes.INVALID ) { |
|
622 |
types[i] = DataTypes.STRING; |
|
623 |
logger.info("Type '"+typename+"' not valid for attribute '"+headers[i]+"' in CSV file '"+this.getFullFileName()+"'."); |
|
624 |
} |
|
625 | 650 |
} |
626 | 651 |
|
627 | 652 |
// Y por ultimo hacemos caso a lo que se haya especificado en los parametros |
... | ... | |
641 | 666 |
int fieldType = types[i]; |
642 | 667 |
String fieldName = headers[i]; |
643 | 668 |
EditableFeatureAttributeDescriptor fad = fType.add(fieldName, fieldType); |
644 |
if( param_sizes!=null && param_sizes[i]>0 ) { |
|
669 |
if( param_sizes!=null && i<param_sizes.length && param_sizes[i]>0 ) {
|
|
645 | 670 |
fad.setSize(param_sizes[i]); |
671 |
} else { |
|
672 |
fad.setSize(sizes[i]); |
|
646 | 673 |
} |
647 | 674 |
if( fieldType == DataTypes.GEOMETRY && fType.getDefaultGeometryAttributeName() == null) { |
648 | 675 |
fType.setDefaultGeometryAttributeName(fieldName); |
... | ... | |
764 | 791 |
if( ftype.getDefaultGeometryAttributeName() != null) { |
765 | 792 |
this.need_calculate_envelope = true; |
766 | 793 |
} |
794 |
|
|
795 |
Locale locale = CSVStoreParameters.getLocale(getCSVParameters()); |
|
767 | 796 |
taskStatus.message("_loading"); |
768 | 797 |
int count = 0; |
769 | 798 |
|
... | ... | |
776 | 805 |
for (int i = 0; i < row.size(); i++) { |
777 | 806 |
Object rawvalue = row.get(i); |
778 | 807 |
try { |
779 |
Object value = coercion[i].coerce(rawvalue); |
|
808 |
Object value = null; |
|
809 |
if( locale != null && coercion[i] instanceof CoercionWithLocale ) { |
|
810 |
value = ((CoercionWithLocale)(coercion[i])).coerce(rawvalue, locale); |
|
811 |
} else { |
|
812 |
value = coercion[i].coerce(rawvalue); |
|
813 |
} |
|
780 | 814 |
feature.set(i, value); |
781 | 815 |
if (sizes[i] >= 0 && value != null) { |
782 | 816 |
int x = ((String) value).length(); |
Also available in: Unified diff