From de91b2d2bf8418833bd10fe973e90aa098c7bf22 Mon Sep 17 00:00:00 2001 From: Emanuela Epure <67077116+emanuelaepure10@users.noreply.github.com> Date: Thu, 29 Jun 2023 09:55:09 +0200 Subject: [PATCH] feat: extend shapefile writer to create .cpg file Create .cpg file/files when one/more .shp files are created. The .cpg file contain only the encoding used to write the shapefile. --- .../hale/io/shp/ShapefileConstants.java | 6 ++++ .../shp/writer/ShapefileInstanceWriter.java | 34 +++++++++++++++++++ 2 files changed, 40 insertions(+) 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..af3119eecd 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 @@ -16,6 +16,7 @@ package eu.esdihumboldt.hale.io.shp.writer; import java.io.File; +import java.io.FileWriter; import java.io.IOException; import java.io.Serializable; import java.net.URI; @@ -122,6 +123,11 @@ protected IOReport execute(ProgressIndicator progress, IOReporter reporter) setTarget(new MultiLocationOutputSupplier(uris)); } + for (String f : filesWritten) { + String cpgFileName = filePath + "/" + f + ShapefileConstants.CPG_EXTENSION; + writeCodePageFile(cpgFileName); + } + reporter.setSuccess(true); } catch (Exception e) { reporter.error(new IOMessageImpl(e.getMessage(), e)); @@ -472,6 +478,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. @@ -724,4 +732,30 @@ private List writeToFile( return filesWritten; } + /** + * + * @param filePath Path of the file to be written with just one line of the + * encoding + * @return File that has been created filled with the text + * @throws IOException exception in any. + */ + public File writeCodePageFile(String filePath) throws IOException { + File file = new File(filePath); + FileWriter fr = new FileWriter(file); + try { + fr.write(getCharset() != null ? getCharset().toString() + : getDefaultCharset().toString()); + } catch (IOException e) { + throw e; + } finally { + try { + fr.close(); + } catch (IOException e) { + throw new IOException("An error occurred while creating/writing the file: " + + filePath + " " + e.getMessage()); + } + } + return file; + } + }