From 37d0458c0868ccf5ff0adf4cb44bb9b7fb07776a Mon Sep 17 00:00:00 2001 From: Emanuela Epure <67077116+emanuelaepure10@users.noreply.github.com> Date: Thu, 29 Jun 2023 11:01:12 +0200 Subject: [PATCH] Update ShapefileInstanceWriter removing one method --- .../shp/writer/ShapefileInstanceWriter.java | 73 ++++++++----------- 1 file changed, 29 insertions(+), 44 deletions(-) diff --git a/io/plugins/eu.esdihumboldt.hale.io.shp/src/eu/esdihumboldt/hale/io/shp/writer/ShapefileInstanceWriter.java b/io/plugins/eu.esdihumboldt.hale.io.shp/src/eu/esdihumboldt/hale/io/shp/writer/ShapefileInstanceWriter.java index a9321879dc..6cc3bade20 100644 --- a/io/plugins/eu.esdihumboldt.hale.io.shp/src/eu/esdihumboldt/hale/io/shp/writer/ShapefileInstanceWriter.java +++ b/io/plugins/eu.esdihumboldt.hale.io.shp/src/eu/esdihumboldt/hale/io/shp/writer/ShapefileInstanceWriter.java @@ -15,10 +15,14 @@ package eu.esdihumboldt.hale.io.shp.writer; +import java.io.BufferedWriter; import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStreamWriter; import java.io.Serializable; import java.net.URI; +import java.nio.charset.StandardCharsets; import java.nio.file.FileSystems; import java.nio.file.Paths; import java.util.ArrayList; @@ -104,36 +108,31 @@ protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException { progress.begin("Generating Shapefile/CPG", ProgressIndicator.UNKNOWN); InstanceCollection instances = getInstances(); - /** - * @param filesWritten List of file names that were written (without - * suffixes) - * - * @return same list of Files written - * @throws IOException exception in any. - */ - private List writeCodePageFiles(List filesWritten) throws IOException { - for (String filePath : filesWritten) { - writeCodePageFile(filePath); - } - return filesWritten; - } + List filesWritten; + try { + URI location = getTarget().getLocation(); + String filePath = Paths.get(location).getParent().toString(); + + filesWritten = writeInstances(instances, progress, reporter, location); + + if (filesWritten.size() > 1) { + + List uris = new ArrayList<>(); + + for (String f : filesWritten) { + File file = new File(filePath + "/" + f + ShapefileConstants.SHP_EXTENSION); + uris.add(file.toURI()); + + String cpgFileName = filePath + "/" + f + ShapefileConstants.CPG_EXTENSION; + writeCodePageFile(cpgFileName); + file = new File(cpgFileName); + uris.add(file.toURI()); + } - /** - * - * - * @param filePath Path of the file to be written with just one line of the - * encoding - * @throws IOException exception in any. - */ - private void writeCodePageFile(String filePath) throws IOException { - try (BufferedWriter writer = new BufferedWriter( - new OutputStreamWriter(new FileOutputStream(filePath), StandardCharsets.UTF_8))) { - writer.write("UTF-8"); - } catch (IOException e) { - throw new IOException("An error occurred while creating/writing the file: filePath " - + e.getMessage()); + // Reset the target property so that a caller can find out which + // files were created. + setTarget(new MultiLocationOutputSupplier(uris)); } - } reporter.setSuccess(true); } catch (Exception e) { @@ -485,7 +484,7 @@ private Map> createSchema(URI location, * - filename_geometryType.shp if multiple geometries.
* - filename.shp single schema and geom. * - * Create the CPG file starting from the Shapefile names + * Create the CPG file starting from the Shapefile names * * @param location file location. * @param numberOfSchemas number of schemas. @@ -740,23 +739,8 @@ private List writeToFile( return filesWritten; } - - /** - * @param filesWritten List of file names that were written (without - * suffixes) - * - * @return same list of Files written - * @throws IOException exception in any. - */ - private List writeCodePageFiles(List filesWritten) throws IOException { - for (String filePath : filesWritten) { - writeCodePageFile(filePath); - } - return filesWritten; - } /** - * * * @param filePath Path of the file to be written with just one line of the * encoding @@ -766,6 +750,7 @@ private void writeCodePageFile(String filePath) throws IOException { try (BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(filePath), StandardCharsets.UTF_8))) { writer.write("UTF-8"); + writer.close(); } catch (IOException e) { throw new IOException("An error occurred while creating/writing the file: filePath " + e.getMessage());