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..fdcbb9c525 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..49baef328e 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; @@ -102,20 +106,27 @@ 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; + List codepagefilesWritten; try { URI location = getTarget().getLocation(); String filePath = Paths.get(location).getParent().toString(); filesWritten = writeInstances(instances, progress, reporter, location); + codepagefilesWritten = writeCodePageFiles(filesWritten); - if (filesWritten.size() > 1) { - List uris = filesWritten.stream().map(f -> { + if (filesWritten.size() > 1 && codepagefilesWritten.size() > 1) { + + List uris = new ArrayList<>(); + + for (String f : filesWritten) { File file = new File(filePath + "/" + f + ShapefileConstants.SHP_EXTENSION); - return file.toURI(); - }).collect(Collectors.toList()); + uris.add(file.toURI()); + file = new File(filePath + "/" + f + ShapefileConstants.CPG_EXTENSION); + uris.add(file.toURI()); + } // Reset the target property so that a caller can find out which // files were created. @@ -126,7 +137,7 @@ protected IOReport execute(ProgressIndicator progress, IOReporter reporter) } 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 +483,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 +520,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); } @@ -724,4 +739,35 @@ 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()); + } + } + }