Revision 44309 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 | ||
---|---|---|
37 | 37 |
import java.util.Iterator; |
38 | 38 |
import java.util.List; |
39 | 39 |
import java.util.Locale; |
40 |
import java.util.logging.Level; |
|
40 | 41 |
|
41 | 42 |
import org.apache.commons.io.FilenameUtils; |
42 | 43 |
import org.apache.commons.io.IOUtils; |
... | ... | |
49 | 50 |
import org.gvsig.fmap.dal.DataStoreNotification; |
50 | 51 |
import org.gvsig.fmap.dal.DataTypes; |
51 | 52 |
import org.gvsig.fmap.dal.FileHelper; |
53 |
import org.gvsig.fmap.dal.exception.CloseException; |
|
52 | 54 |
import org.gvsig.fmap.dal.exception.DataException; |
53 | 55 |
import org.gvsig.fmap.dal.exception.InitializeException; |
54 | 56 |
import org.gvsig.fmap.dal.exception.OpenException; |
... | ... | |
108 | 110 |
import org.supercsv.io.CsvListWriter; |
109 | 111 |
import org.supercsv.prefs.CsvPreference; |
110 | 112 |
|
113 |
@SuppressWarnings("UseSpecificCatch") |
|
111 | 114 |
public class CSVStoreProvider extends AbstractMemoryStoreProvider implements |
112 | 115 |
ResourceConsumer { |
113 | 116 |
|
... | ... | |
123 | 126 |
private long counterNewsOIDs = 0; |
124 | 127 |
private Envelope envelope; |
125 | 128 |
private boolean need_calculate_envelope = false; |
126 |
private SimpleTaskStatus taskStatus; |
|
129 |
private final SimpleTaskStatus taskStatus; |
|
130 |
private final CSVFeatureWriter writer; |
|
127 | 131 |
|
128 | 132 |
public CSVStoreProvider(CSVStoreParameters parameters, |
129 | 133 |
DataStoreProviderServices storeServices) throws InitializeException { |
... | ... | |
132 | 136 |
storeServices, |
133 | 137 |
FileHelper.newMetadataContainer(METADATA_DEFINITION_NAME) |
134 | 138 |
); |
135 |
|
|
139 |
this.writer = new CSVFeatureWriter(); |
|
136 | 140 |
TaskStatusManager manager = ToolsLocator.getTaskStatusManager(); |
137 | 141 |
this.taskStatus = manager.createDefaultSimpleTaskStatus("CSV"); |
138 | 142 |
|
... | ... | |
145 | 149 |
); |
146 | 150 |
|
147 | 151 |
resource.addConsumer(this); |
152 |
|
|
148 | 153 |
initializeFeatureTypes(); |
149 | 154 |
} |
150 | 155 |
|
... | ... | |
152 | 157 |
return (CSVStoreParameters) this.getParameters(); |
153 | 158 |
} |
154 | 159 |
|
160 |
@Override |
|
155 | 161 |
public String getProviderName() { |
156 | 162 |
return NAME; |
157 | 163 |
} |
158 | 164 |
|
165 |
@Override |
|
159 | 166 |
public boolean allowWrite() { |
160 |
return false;
|
|
167 |
return true;
|
|
161 | 168 |
} |
162 | 169 |
|
163 | 170 |
private String getFullFileName() { |
164 | 171 |
// Usar solo para mostrar mensajes en el logger. |
165 |
String s = "(unknow)";
|
|
172 |
String s; |
|
166 | 173 |
try { |
167 | 174 |
s = getCSVParameters().getFile().getAbsolutePath(); |
168 | 175 |
} catch (Exception e2) { |
... | ... | |
171 | 178 |
return s; |
172 | 179 |
} |
173 | 180 |
|
181 |
@Override |
|
174 | 182 |
public void open() throws OpenException { |
175 | 183 |
if (this.data != null) { |
176 | 184 |
return; |
... | ... | |
189 | 197 |
} |
190 | 198 |
} |
191 | 199 |
|
200 |
@Override |
|
192 | 201 |
public DataServerExplorer getExplorer() throws ReadException { |
193 | 202 |
DataManager manager = DALLocator.getDataManager(); |
194 | 203 |
FilesystemServerExplorerParameters params; |
... | ... | |
205 | 214 |
|
206 | 215 |
} |
207 | 216 |
|
208 |
class Writer { |
|
209 |
|
|
210 |
private Envelope envelope = null; |
|
211 |
private boolean calculate_envelope = false; |
|
212 |
private CsvListWriter listWriter = null; |
|
213 |
private CsvPreference csvpreferences = null; |
|
214 |
private FileWriter fwriter = null; |
|
215 |
private FeatureType ftype; |
|
216 |
private File file; |
|
217 |
private String[] values; |
|
218 |
private FeatureAttributeDescriptor[] descriptors; |
|
219 |
private Coercion convert = null; |
|
220 |
private int errorcounts = 0; |
|
221 |
private Throwable lasterror = null; |
|
222 |
private Locale locale = null; |
|
223 |
|
|
224 |
public void initialize(File file, FeatureType ftype, CsvPreference csvpreferences) { |
|
225 |
this.file = file; |
|
226 |
this.ftype = ftype; |
|
227 |
this.csvpreferences = csvpreferences; |
|
228 |
this.locale = CSVStoreParameters.getLocale(getCSVParameters()); |
|
229 |
if (csvpreferences == null) { |
|
230 |
this.csvpreferences = CsvPreference.STANDARD_PREFERENCE; |
|
231 |
} |
|
232 |
if (ftype.getDefaultGeometryAttributeName() != null) { |
|
233 |
this.calculate_envelope = true; |
|
234 |
} |
|
235 |
this.descriptors = this.ftype.getAttributeDescriptors(); |
|
236 |
this.convert = ToolsLocator.getDataTypesManager().getCoercion(org.gvsig.tools.dataTypes.DataTypes.STRING); |
|
237 |
this.errorcounts = 0; |
|
238 |
} |
|
239 |
|
|
240 |
public void begin() { |
|
241 |
try { |
|
242 |
this.fwriter = new FileWriter(file); |
|
243 |
} catch (IOException e) { |
|
244 |
logger.warn("Can't open file for write (" + file.getAbsolutePath() + ").", e); |
|
245 |
throw new RuntimeException(e); |
|
246 |
} |
|
247 |
this.listWriter = new CsvListWriter(this.fwriter, this.csvpreferences); |
|
248 |
int n = 0; |
|
249 |
for (int i = 0; i < descriptors.length; i++) { |
|
250 |
FeatureAttributeDescriptor descriptor = descriptors[i]; |
|
251 |
if (descriptor.getEvaluator() == null) { |
|
252 |
n++; |
|
253 |
} |
|
254 |
} |
|
255 |
|
|
256 |
String[] header = new String[n]; |
|
257 |
this.values = new String[n]; |
|
258 |
n = 0; |
|
259 |
for (int i = 0; i < descriptors.length; i++) { |
|
260 |
FeatureAttributeDescriptor descriptor = descriptors[i]; |
|
261 |
if (descriptor.getEvaluator() == null) { |
|
262 |
String name = descriptor.getName(); |
|
263 |
String typeName = descriptor.getDataTypeName(); |
|
264 |
if (descriptor.getDataType().getType() == DataTypes.STRING) { |
|
265 |
header[n++] = name + "__" + typeName + "__" + descriptor.getSize(); |
|
266 |
} else { |
|
267 |
header[n++] = name + "__" + typeName; |
|
268 |
} |
|
269 |
} |
|
270 |
} |
|
271 |
try { |
|
272 |
listWriter.writeHeader(header); |
|
273 |
} catch (Exception e) { |
|
274 |
logger.warn("Can't write header '" + header.toString() + "' file for write (" + file.getAbsolutePath() + ").", e); |
|
275 |
throw new RuntimeException(e); |
|
276 |
} |
|
277 |
} |
|
278 |
|
|
279 |
public void add(FeatureProvider feature) { |
|
280 |
if (this.calculate_envelope) { |
|
281 |
Geometry geom = feature.getDefaultGeometry(); |
|
282 |
if (geom != null) { |
|
283 |
if (envelope == null) { |
|
284 |
try { |
|
285 |
envelope = (Envelope) geom.getEnvelope().clone(); |
|
286 |
} catch (CloneNotSupportedException e) { |
|
287 |
logger.warn("Este error no deberia pasar, siempre se puede hacer un clone de un envelope.", e); |
|
288 |
} |
|
289 |
} else { |
|
290 |
envelope.add(geom.getEnvelope()); |
|
291 |
} |
|
292 |
} |
|
293 |
} |
|
294 |
int n = 0; |
|
295 |
for (int i = 0; i < descriptors.length; i++) { |
|
296 |
FeatureAttributeDescriptor descriptor = descriptors[i]; |
|
297 |
if (descriptor.getEvaluator() == null) { |
|
298 |
Object value = feature.get(i); |
|
299 |
try { |
|
300 |
n++; |
|
301 |
if (this.convert != null && this.convert instanceof CoercionWithLocale) { |
|
302 |
values[n] = (String) ((CoercionWithLocale) this.convert).coerce(value, this.locale); |
|
303 |
} else { |
|
304 |
values[n] = (String) this.convert.coerce(value); |
|
305 |
} |
|
306 |
} catch (CoercionException e) { |
|
307 |
try { |
|
308 |
values[n] = value.toString(); |
|
309 |
} catch (Exception ex) { |
|
310 |
values[n] = ""; |
|
311 |
} |
|
312 |
if (errorcounts++ <= 10) { |
|
313 |
this.lasterror = e; |
|
314 |
logger.warn("Can't convert value of field " + i + " to string in CVS file '" + getFullFileName() + "'.", e); |
|
315 |
if (errorcounts == 10) { |
|
316 |
logger.warn("Too many error writing CVS file '" + getFullFileName() + "', don't output more."); |
|
317 |
} |
|
318 |
} |
|
319 |
} |
|
320 |
} |
|
321 |
} |
|
322 |
try { |
|
323 |
this.listWriter.writeHeader(values); |
|
324 |
} catch (IOException e) { |
|
325 |
if (errorcounts++ <= 10) { |
|
326 |
this.lasterror = e; |
|
327 |
logger.warn("Can't write values to CVS file '" + getFullFileName() + "'.", e); |
|
328 |
if (errorcounts == 10) { |
|
329 |
logger.warn("Too many error writing CVS file '" + getFullFileName() + "', don't output more."); |
|
330 |
} |
|
331 |
} |
|
332 |
} |
|
333 |
|
|
334 |
} |
|
335 |
|
|
336 |
public void end() throws PerformEditingException { |
|
337 |
if (this.errorcounts > 0) { |
|
338 |
throw new PerformEditingException(this.file.getAbsolutePath(), lasterror); |
|
339 |
} |
|
340 |
if (listWriter != null) { |
|
341 |
try { |
|
342 |
listWriter.close(); |
|
343 |
} catch (Exception ex) { |
|
344 |
// Ignore error |
|
345 |
} |
|
346 |
listWriter = null; |
|
347 |
} |
|
348 |
if (fwriter != null) { |
|
349 |
try { |
|
350 |
fwriter.close(); |
|
351 |
} catch (Exception ex) { |
|
352 |
// Ignore error |
|
353 |
} |
|
354 |
fwriter = null; |
|
355 |
} |
|
356 |
} |
|
357 |
|
|
358 |
public Envelope getEnvelope() { |
|
359 |
return this.envelope; |
|
360 |
} |
|
361 |
} |
|
362 |
|
|
217 |
@Override |
|
363 | 218 |
public void performChanges(Iterator deleteds, Iterator inserteds, Iterator updateds, Iterator originalFeatureTypesUpdated) throws PerformEditingException { |
364 | 219 |
|
365 | 220 |
try { |
366 | 221 |
this.taskStatus.add(); |
367 | 222 |
taskStatus.message("_preparing"); |
223 |
this.getResource().closeRequest(); |
|
368 | 224 |
getResource().execute(new ResourceAction() { |
225 |
@Override |
|
369 | 226 |
public Object run() throws Exception { |
370 | 227 |
FeatureSet features = null; |
371 | 228 |
DisposableIterator it = null; |
372 | 229 |
try { |
373 | 230 |
File file = (File) resource.get(); |
374 | 231 |
|
375 |
Writer writer = new Writer(); |
|
376 |
writer.initialize(file, getStoreServices().getDefaultFeatureType(), getCSVPreferences()); |
|
377 | 232 |
features |
378 | 233 |
= getStoreServices().getFeatureStore() |
379 |
.getFeatureSet(); |
|
234 |
.getFeatureSet();
|
|
380 | 235 |
List<FeatureProvider> newdata = new ArrayList<FeatureProvider>(); |
381 |
writer.begin(); |
|
236 |
FeatureType ftype = getStoreServices().getDefaultFeatureType(); |
|
237 |
writer.initialize(getCSVParameters(), file, ftype, getCSVPreferences()); |
|
238 |
writer.beginAppend(); |
|
382 | 239 |
it = features.fastIterator(); |
383 | 240 |
taskStatus.setRangeOfValues(0, 0); |
384 | 241 |
long counter = 0; |
... | ... | |
410 | 267 |
return null; |
411 | 268 |
} |
412 | 269 |
|
413 |
private CsvPreference getCSVPreferences() { |
|
414 |
CSVReader reader = new CSVReader(getCSVParameters()); |
|
415 |
return reader.getCSVPreferences(); |
|
416 |
} |
|
417 |
|
|
418 | 270 |
}); |
419 | 271 |
this.taskStatus.terminate(); |
420 | 272 |
} catch (Exception e) { |
... | ... | |
425 | 277 |
} |
426 | 278 |
} |
427 | 279 |
|
280 |
private CsvPreference getCSVPreferences() { |
|
281 |
CSVReader reader = new CSVReader(getCSVParameters()); |
|
282 |
return reader.getCSVPreferences(); |
|
283 |
} |
|
284 |
|
|
285 |
@Override |
|
428 | 286 |
public boolean closeResourceRequested(ResourceProvider resource) { |
429 | 287 |
return true; |
430 | 288 |
} |
431 | 289 |
|
290 |
@Override |
|
432 | 291 |
public int getOIDType() { |
433 | 292 |
return DataTypes.LONG; |
434 | 293 |
} |
435 | 294 |
|
295 |
@Override |
|
436 | 296 |
public boolean supportsAppendMode() { |
437 |
return false;
|
|
297 |
return true;
|
|
438 | 298 |
} |
439 | 299 |
|
440 |
public void append(FeatureProvider featureProvider) { |
|
441 |
throw new UnsupportedOperationException(); |
|
300 |
@Override |
|
301 |
public void append(final FeatureProvider featureProvider) { |
|
302 |
//todo |
|
303 |
getResource().execute(new ResourceAction() { |
|
304 |
@Override |
|
305 |
public Object run() throws Exception { |
|
306 |
//writer.append(getStoreServices().createFeature(featureProvider)); |
|
307 |
writer.add(featureProvider); |
|
308 |
return null; |
|
309 |
} |
|
310 |
}); |
|
442 | 311 |
} |
443 | 312 |
|
444 |
public void beginAppend() { |
|
445 |
throw new UnsupportedOperationException(); |
|
313 |
@Override |
|
314 |
public void beginAppend() throws DataException { |
|
315 |
this.close(); |
|
316 |
getResource().execute(new ResourceAction() { |
|
317 |
public Object run() throws Exception { |
|
318 |
writer.initialize( |
|
319 |
getCSVParameters(), |
|
320 |
getCSVParameters().getFile(), |
|
321 |
getFeatureStore().getDefaultFeatureType(), |
|
322 |
null |
|
323 |
); |
|
324 |
writer.beginAppend(); |
|
325 |
return null; |
|
326 |
} |
|
327 |
}); |
|
328 |
|
|
446 | 329 |
} |
447 | 330 |
|
331 |
@Override |
|
448 | 332 |
public void endAppend() { |
449 |
throw new UnsupportedOperationException(); |
|
333 |
try { |
|
334 |
getResource().execute(new ResourceAction() { |
|
335 |
public Object run() throws Exception { |
|
336 |
writer.end(); |
|
337 |
resource.notifyChanges(); //resourcesNotifyChanges(); |
|
338 |
counterNewsOIDs = -1; |
|
339 |
return null; |
|
340 |
} |
|
341 |
}); |
|
342 |
writer.end(); |
|
343 |
} catch (PerformEditingException ex) { |
|
344 |
logger.warn("Not been able to end append '" + this.getFullName() + "'.", ex); |
|
345 |
} |
|
450 | 346 |
} |
451 | 347 |
|
452 | 348 |
public void saveToState(PersistentState state) throws PersistenceException { |
... | ... | |
457 | 353 |
throw new NotYetImplemented(); |
458 | 354 |
} |
459 | 355 |
|
356 |
@Override |
|
460 | 357 |
public Object createNewOID() { |
461 |
return new Long(counterNewsOIDs++);
|
|
358 |
return counterNewsOIDs++;
|
|
462 | 359 |
} |
463 | 360 |
|
464 | 361 |
protected void initializeFeatureTypes() throws InitializeException { |
... | ... | |
469 | 366 |
} |
470 | 367 |
} |
471 | 368 |
|
369 |
@Override |
|
472 | 370 |
public Envelope getEnvelope() throws DataException { |
473 | 371 |
this.open(); |
474 | 372 |
if (this.envelope != null) { |
... | ... | |
484 | 382 |
try { |
485 | 383 |
this.envelope = GeometryLocator.getGeometryManager().createEnvelope(fad.getGeomType().getSubType()); |
486 | 384 |
fs.accept(new Visitor() { |
385 |
@Override |
|
487 | 386 |
public void visit(Object obj) throws VisitCanceledException, BaseException { |
488 | 387 |
Feature f = (Feature) obj; |
489 | 388 |
Geometry geom = f.getDefaultGeometry(); |
... | ... | |
501 | 400 |
return this.envelope; |
502 | 401 |
} |
503 | 402 |
|
403 |
@Override |
|
504 | 404 |
public Object getDynValue(String name) throws DynFieldNotFoundException { |
505 | 405 |
if (DataStore.METADATA_ENVELOPE.equalsIgnoreCase(name)) { |
506 | 406 |
try { |
... | ... | |
519 | 419 |
return super.getDynValue(name); |
520 | 420 |
} |
521 | 421 |
|
422 |
@Override |
|
522 | 423 |
public void resourceChanged(ResourceProvider resource) { |
523 | 424 |
this.getStoreServices().notifyChange( |
524 | 425 |
DataStoreNotification.RESOURCE_CHANGED, |
525 | 426 |
resource); |
526 | 427 |
} |
527 | 428 |
|
429 |
@Override |
|
528 | 430 |
public Object getSourceId() { |
529 | 431 |
return this.getCSVParameters().getFile(); |
530 | 432 |
} |
531 | 433 |
|
434 |
@Override |
|
532 | 435 |
public String getName() { |
533 | 436 |
String name = this.getCSVParameters().getFile().getName(); |
534 | 437 |
return FilenameUtils.getBaseName(name); |
535 | 438 |
} |
536 | 439 |
|
440 |
@Override |
|
537 | 441 |
public String getFullName() { |
538 | 442 |
return this.getCSVParameters().getFile().getAbsolutePath(); |
539 | 443 |
} |
540 | 444 |
|
445 |
@Override |
|
541 | 446 |
public ResourceProvider getResource() { |
542 | 447 |
return resource; |
543 | 448 |
} |
... | ... | |
549 | 454 |
return s.trim().length() == 0; |
550 | 455 |
} |
551 | 456 |
|
457 |
private void init(CSVStoreParameters parameters, DataStoreProviderServices storeServices) { |
|
458 |
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
|
459 |
} |
|
460 |
|
|
552 | 461 |
private class FieldTypeParser { |
553 | 462 |
|
554 | 463 |
public String name = null; |
... | ... | |
589 | 498 |
// name[__typename[__size[__notnull|null]]] |
590 | 499 |
// |
591 | 500 |
public boolean parse(String value) { |
592 |
String typename = null; |
|
501 |
String theTypename = null;
|
|
593 | 502 |
String[] ss = null; |
594 | 503 |
if (value.contains(":")) { |
595 | 504 |
ss = value.split(":"); |
... | ... | |
615 | 524 |
this.type = this.getType(this.typename); |
616 | 525 |
if (this.type == DataTypes.INVALID) { |
617 | 526 |
this.type = DataTypes.STRING; |
618 |
logger.info("Type '" + typename + "' not valid for attribute '" + value + "' in CSV file '" + getFullFileName() + "'."); |
|
527 |
logger.info("Type '" + theTypename + "' not valid for attribute '" + value + "' in CSV file '" + getFullFileName() + "'.");
|
|
619 | 528 |
} |
620 | 529 |
} |
621 | 530 |
if (!StringUtils.isBlank(ss[2])) { |
... | ... | |
651 | 560 |
this.type = this.getType(this.typename); |
652 | 561 |
if (this.type == DataTypes.INVALID) { |
653 | 562 |
this.type = DataTypes.STRING; |
654 |
logger.info("Type '" + typename + "' not valid for attribute '" + value + "' in CSV file '" + getFullFileName() + "'."); |
|
563 |
logger.info("Type '" + theTypename + "' not valid for attribute '" + value + "' in CSV file '" + getFullFileName() + "'.");
|
|
655 | 564 |
} |
656 | 565 |
} |
657 | 566 |
case 1: |
... | ... | |
736 | 645 |
if (pointDimensionNames != null) { |
737 | 646 |
PointAttributeEmulator emulator = new PointAttributeEmulator(pointDimensionNames); |
738 | 647 |
EditableFeatureAttributeDescriptor attr = fType.add( |
739 |
CSVStoreParameters.getPointColumnName(this.getParameters()),
|
|
648 |
CSVStoreParameters.getPointColumnName(this.getParameters()), |
|
740 | 649 |
DataTypes.GEOMETRY, emulator |
741 | 650 |
); |
742 | 651 |
GeometryManager geommgr = GeometryLocator.getGeometryManager(); |
... | ... | |
750 | 659 |
logger.warn("Can't set geometry type for the calculated field in CSV file '" + getFullFileName() + "'.", e); |
751 | 660 |
} |
752 | 661 |
} |
753 |
|
|
662 |
|
|
754 | 663 |
String geometry_column = CSVStoreParameters.getGeometryColumn(this.getParameters()); |
755 |
if( !StringUtils.isEmpty(geometry_column) ) {
|
|
664 |
if (!StringUtils.isEmpty(geometry_column)) {
|
|
756 | 665 |
EditableFeatureAttributeDescriptor attr = (EditableFeatureAttributeDescriptor) fType.get(geometry_column); |
757 |
if( attr!=null && attr.getType()!=DataTypes.GEOMETRY ) {
|
|
666 |
if (attr != null && attr.getType() != DataTypes.GEOMETRY) {
|
|
758 | 667 |
attr.setDataType(DataTypes.GEOMETRY); |
759 | 668 |
GeometryManager geommgr = GeometryLocator.getGeometryManager(); |
760 | 669 |
GeometryType gt; |
... | ... | |
800 | 709 |
this.dataType = datatypeManager.get(DataTypes.GEOMETRY); |
801 | 710 |
} |
802 | 711 |
|
712 |
@Override |
|
803 | 713 |
public Object get(Feature feature) { |
804 | 714 |
try { |
805 | 715 |
Object valueX = feature.get(this.fieldNames[XNAME]); |
... | ... | |
820 | 730 |
} |
821 | 731 |
} |
822 | 732 |
|
823 |
double x = ((Double) valueX).doubleValue();
|
|
824 |
double y = ((Double) valueY).doubleValue();
|
|
733 |
double x = ((Double) valueX); |
|
734 |
double y = ((Double) valueY); |
|
825 | 735 |
Point point = geommgr.createPoint(x, y, Geometry.SUBTYPES.GEOM3D); |
826 | 736 |
if (this.fieldNames.length > 2) { |
827 |
double z = ((Double) valueZ).doubleValue();
|
|
737 |
double z = ((Double) valueZ); |
|
828 | 738 |
point.setCoordinateAt(2, z); |
829 | 739 |
} |
830 | 740 |
return point; |
... | ... | |
854 | 764 |
} |
855 | 765 |
} |
856 | 766 |
|
767 |
@Override |
|
857 | 768 |
public boolean allowSetting() { |
858 | 769 |
return true; |
859 | 770 |
} |
860 | 771 |
|
772 |
@Override |
|
861 | 773 |
public String[] getRequiredFieldNames() { |
862 | 774 |
return this.fieldNames; |
863 | 775 |
} |
... | ... | |
885 | 797 |
this.toDouble = ToolsLocator.getDataTypesManager().getCoercion(DataTypes.DOUBLE); |
886 | 798 |
} |
887 | 799 |
|
800 |
@Override |
|
888 | 801 |
public Object evaluate(EvaluatorData data) throws EvaluatorException { |
889 | 802 |
try { |
890 | 803 |
double x = ((Double) toDouble.coerce(data.getDataValue(xname))).doubleValue(); |
... | ... | |
904 | 817 |
} |
905 | 818 |
} |
906 | 819 |
|
820 |
@Override |
|
907 | 821 |
public String getName() { |
908 | 822 |
return "ToPointEvaluaror"; |
909 | 823 |
} |
... | ... | |
941 | 855 |
private InputStreamReader openFile(File f, String charsetName) throws FileNotFoundException { |
942 | 856 |
Charset charset = Charset.defaultCharset(); |
943 | 857 |
FileInputStream fis = new FileInputStream(f); |
944 |
if( !StringUtils.isEmpty(charsetName) ) {
|
|
945 |
if( Charset.isSupported(charsetName) ) {
|
|
858 |
if (!StringUtils.isEmpty(charsetName)) {
|
|
859 |
if (Charset.isSupported(charsetName)) {
|
|
946 | 860 |
try { |
947 | 861 |
charset = Charset.forName(charsetName); |
948 |
} catch(Throwable th) { |
|
949 |
logger.warn("Can't use charset '"+charsetName+"' for read csv '"+this.getFullFileName()+"'.", th);
|
|
862 |
} catch (Throwable th) {
|
|
863 |
logger.warn("Can't use charset '" + charsetName + "' for read csv '" + this.getFullFileName() + "'.", th);
|
|
950 | 864 |
} |
951 | 865 |
} else { |
952 |
logger.warn("charset '"+charsetName+"' not supported for read csv '"+this.getFullFileName()+"'.");
|
|
866 |
logger.warn("charset '" + charsetName + "' not supported for read csv '" + this.getFullFileName() + "'.");
|
|
953 | 867 |
} |
954 | 868 |
} |
955 | 869 |
InputStreamReader isr = new InputStreamReader(fis, charset); |
956 | 870 |
return isr; |
957 | 871 |
} |
958 |
|
|
872 |
|
|
959 | 873 |
private void loadFeatures() { |
960 | 874 |
InputStreamReader in = null; |
961 | 875 |
SimpleReader reader = null; |
... | ... | |
966 | 880 |
boolean ignore_errors = CSVStoreParameters.getIgnoreErrors(getCSVParameters()); |
967 | 881 |
|
968 | 882 |
in = openFile( |
969 |
this.getCSVParameters().getFile(),
|
|
970 |
CSVStoreParameters.getCharset(this.getCSVParameters()) |
|
883 |
this.getCSVParameters().getFile(),
|
|
884 |
CSVStoreParameters.getCharset(this.getCSVParameters())
|
|
971 | 885 |
); |
972 | 886 |
|
973 | 887 |
reader = getSimpleReader(in); |
... | ... | |
982 | 896 |
} else { |
983 | 897 |
String msg = "Can't retrieve header from csv file '" |
984 | 898 |
+ this.getCSVParameters().getFile() |
985 |
.getAbsolutePath() |
|
899 |
.getAbsolutePath()
|
|
986 | 900 |
+ "' and not specified in the parameters."; |
987 | 901 |
logger.warn(msg); |
988 | 902 |
throw new RuntimeException(msg); |
... | ... | |
998 | 912 |
} |
999 | 913 |
|
1000 | 914 |
int[] detectedTypes = automaticDetectionOfTypes(headers); |
1001 |
if( detectedTypes!=null && detectedTypes.length>headers.length ) {
|
|
915 |
if (detectedTypes != null && detectedTypes.length > headers.length) {
|
|
1002 | 916 |
// Se han detectado mas columnas que las que hay en la cabezera, |
1003 | 917 |
// a?adimos mas columnas a la cabezera. |
1004 | 918 |
String[] headers2 = new String[detectedTypes.length]; |
1005 |
for( int i=0; i<headers2.length; i++ ) {
|
|
1006 |
if( i<headers.length ) {
|
|
919 |
for (int i = 0; i < headers2.length; i++) {
|
|
920 |
if (i < headers.length) {
|
|
1007 | 921 |
headers2[i] = headers[i]; |
1008 | 922 |
} else { |
1009 | 923 |
headers2[i] = getFixedHeader(i); |
... | ... | |
1011 | 925 |
} |
1012 | 926 |
headers = headers2; |
1013 | 927 |
} |
1014 |
for( int i=0; i<headers.length; i++ ) {
|
|
1015 |
if( StringUtils.isEmpty(headers[i]) ) {
|
|
928 |
for (int i = 0; i < headers.length; i++) {
|
|
929 |
if (StringUtils.isEmpty(headers[i])) {
|
|
1016 | 930 |
headers[i] = getFixedHeader(i); |
1017 | 931 |
} |
1018 | 932 |
} |
... | ... | |
1029 | 943 |
sizes[i] = -1; |
1030 | 944 |
FeatureAttributeDescriptor ad = ftype.getAttributeDescriptor(i); |
1031 | 945 |
coercion[i] = ad.getDataType().getCoercion(); |
1032 |
switch (ad.getDataType().getType() ) { |
|
1033 |
case DataTypes.INT: |
|
1034 |
sizes[i] = 10; |
|
1035 |
break; |
|
1036 |
case DataTypes.LONG: |
|
1037 |
sizes[i] = 20; |
|
1038 |
break; |
|
1039 |
case DataTypes.STRING: |
|
1040 |
if (ad.getSize() == 0) { |
|
1041 |
// Es un string y no tiene un size asignado. |
|
1042 |
// Lo ponemos a cero para calcularlo. |
|
946 |
switch (ad.getDataType().getType()) { |
|
947 |
case DataTypes.INT: |
|
948 |
sizes[i] = 10; |
|
949 |
break; |
|
950 |
case DataTypes.LONG: |
|
951 |
sizes[i] = 20; |
|
952 |
break; |
|
953 |
case DataTypes.STRING: |
|
954 |
if (ad.getSize() == 0) { |
|
955 |
// Es un string y no tiene un size asignado. |
|
956 |
// Lo ponemos a cero para calcularlo. |
|
957 |
sizes[i] = 0; |
|
958 |
} |
|
959 |
break; |
|
960 |
case DataTypes.URL: |
|
961 |
case DataTypes.URI: |
|
962 |
case DataTypes.FILE: |
|
1043 | 963 |
sizes[i] = 0; |
1044 |
} |
|
1045 |
break; |
|
1046 |
case DataTypes.URL: |
|
1047 |
case DataTypes.URI: |
|
1048 |
case DataTypes.FILE: |
|
1049 |
sizes[i] = 0; |
|
1050 | 964 |
} |
1051 | 965 |
} |
1052 | 966 |
if (ftype.getDefaultGeometryAttributeName() != null) { |
... | ... | |
1079 | 993 |
value = coercion[i].coerce(rawvalue); |
1080 | 994 |
} |
1081 | 995 |
feature.set(i, value); |
1082 |
if (sizes[i] >= 0 &&
|
|
1083 |
(value instanceof String || value instanceof URL ||
|
|
1084 |
value instanceof URI || value instanceof File) ) {
|
|
996 |
if (sizes[i] >= 0 |
|
997 |
&& (value instanceof String || value instanceof URL
|
|
998 |
|| value instanceof URI || value instanceof File)) {
|
|
1085 | 999 |
int x = value.toString().length(); |
1086 | 1000 |
if (sizes[i] < x) { |
1087 | 1001 |
sizes[i] = x; |
... | ... | |
1100 | 1014 |
} |
1101 | 1015 |
} |
1102 | 1016 |
this.addFeatureProvider(feature); |
1103 |
if( limit>0 ) {
|
|
1104 |
if( limit < this.data.size() ) {
|
|
1017 |
if (limit > 0) {
|
|
1018 |
if (limit < this.data.size()) {
|
|
1105 | 1019 |
break; |
1106 | 1020 |
} |
1107 | 1021 |
} |
... | ... | |
1165 | 1079 |
this.getFullFileName() |
1166 | 1080 |
); |
1167 | 1081 |
types = x.detect( |
1168 |
headers.length,
|
|
1169 |
reader,
|
|
1170 |
CSVStoreParameters.isFirstLineHeader(getCSVParameters()),
|
|
1082 |
headers.length, |
|
1083 |
reader, |
|
1084 |
CSVStoreParameters.isFirstLineHeader(getCSVParameters()), |
|
1171 | 1085 |
CSVStoreParameters.getLocale(getCSVParameters()) |
1172 | 1086 |
); |
1173 | 1087 |
} catch (Exception ex) { |
Also available in: Unified diff