diff --git a/io/plugins/eu.esdihumboldt.hale.io.shp/src/eu/esdihumboldt/hale/io/shp/ShapefileConstants.java b/io/plugins/eu.esdihumboldt.hale.io.shp/src/eu/esdihumboldt/hale/io/shp/ShapefileConstants.java index 18f7701281..bffeb96ab1 100644 --- a/io/plugins/eu.esdihumboldt.hale.io.shp/src/eu/esdihumboldt/hale/io/shp/ShapefileConstants.java +++ b/io/plugins/eu.esdihumboldt.hale.io.shp/src/eu/esdihumboldt/hale/io/shp/ShapefileConstants.java @@ -81,6 +81,12 @@ public interface ShapefileConstants { * Constant for the shape file extension. */ public static final String SHP_EXTENSION = ".shp"; + + /** + * Constant for the CPG file extension. + */ + public static final String CPG_EXTENSION = ".cpg"; + /** * Constant for underscore. */ 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 ad6e86c954..a9321879dc 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 @@ -102,31 +102,44 @@ public boolean isCancelable() { @Override protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException { - progress.begin("Generating Shapefile", ProgressIndicator.UNKNOWN); + progress.begin("Generating Shapefile/CPG", ProgressIndicator.UNKNOWN); InstanceCollection instances = getInstances(); - 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 = filesWritten.stream().map(f -> { - File file = new File(filePath + "/" + f + ShapefileConstants.SHP_EXTENSION); - return file.toURI(); - }).collect(Collectors.toList()); + /** + * @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; + } - // Reset the target property so that a caller can find out which - // files were created. - setTarget(new MultiLocationOutputSupplier(uris)); + /** + * + * + * @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()); } + } reporter.setSuccess(true); } catch (Exception e) { reporter.error(new IOMessageImpl(e.getMessage(), e)); reporter.setSuccess(false); - reporter.setSummary("Saving instances to Shapefile failed."); + reporter.setSummary("Saving instances to Shapefile/CPG failed."); } finally { progress.end(); } @@ -472,6 +485,8 @@ 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 + * * @param location file location. * @param numberOfSchemas number of schemas. * @param schemaEntry current schema in process. @@ -507,6 +522,8 @@ else if (numberOfGeometries > 1) { File file; try { file = new File(filenameWithType); + writeCodePageFile(filenameWithType.replace(ShapefileConstants.SHP_EXTENSION, + ShapefileConstants.CPG_EXTENSION)); } catch (Exception e) { throw new IllegalArgumentException("Only files are supported as data source", e); } @@ -723,5 +740,36 @@ 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 + * @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()); + } + } }