From 58b31f15505ba0f4b730b9126461ffb80ad7907e 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. Create test for creating .cpg file --- .../io/shp/ShapefileInstanceWriterTest.groovy | 35 ++++++++++++++++++- .../hale/io/shp/ShapefileConstants.java | 6 ++++ .../shp/writer/ShapefileInstanceWriter.java | 31 ++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/io/plugins/eu.esdihumboldt.hale.io.shp.test/src/eu/esdihumboldt/hale/io/shp/ShapefileInstanceWriterTest.groovy b/io/plugins/eu.esdihumboldt.hale.io.shp.test/src/eu/esdihumboldt/hale/io/shp/ShapefileInstanceWriterTest.groovy index 9cfc8b9a8d..b5a0323e3e 100644 --- a/io/plugins/eu.esdihumboldt.hale.io.shp.test/src/eu/esdihumboldt/hale/io/shp/ShapefileInstanceWriterTest.groovy +++ b/io/plugins/eu.esdihumboldt.hale.io.shp.test/src/eu/esdihumboldt/hale/io/shp/ShapefileInstanceWriterTest.groovy @@ -100,7 +100,7 @@ class ShapefileInstanceWriterTest { String filenameOnly = Paths.get(location).getFileName().toString(); filenameOnly = filenameOnly.substring(0, filenameOnly.lastIndexOf(".")); - String filename = filePath + "/" + filenameOnly + "_" + additionalName + ".shp"; + String filename = filePath + "/" + filenameOnly + "_" + additionalName + ShapefileConstants.SHP_EXTENSION; file = new File(filename) Schema schema = loadSchema(file) @@ -117,6 +117,19 @@ class ShapefileInstanceWriterTest { return reader.getInstances(); } + @Test + public void whenReadWithBufferedReader_thenCorrect() + throws IOException { + String expected_value = "UTF-8"; + String file ="src/test/resources/fileTest.txt"; + + BufferedReader reader = new BufferedReader(new FileReader(file)); + String currentLine = reader.readLine(); + reader.close(); + + assertEquals(expected_value, currentLine); + } + /** * Write an instance collection to a Shapefile. @@ -1443,4 +1456,24 @@ class ShapefileInstanceWriterTest { assertEquals(2, num) } } + + @Test + void testCPGFileCreation() { + def tmpDir = Files.createTempDirectory("CPGFileTest") + def tmpFile = Files.createTempFile(tmpDir, 'new', '.cpg') + + try { + ShapefileInstanceWriter writer = new ShapefileInstanceWriter(); + writer.writeCodePageFile(tmpFile.toFile().getAbsolutePath()); + assertTrue(tmpFile.toFile().exists()); + + String expected_value = writer.getCharset().toString(); + BufferedReader bufferedReader = new BufferedReader(new FileReader(tmpFile.toFile().getAbsolutePath())); + String currentLine = bufferedReader.readLine(); + bufferedReader.close(); + assertEquals(expected_value, currentLine); + } finally { + tmpDir.deleteDir() + } + } } 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..17a5f48ac4 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)); @@ -724,4 +730,29 @@ private List writeToFile( return filesWritten; } + /** + * + * @param filePath Path of the file to be written with just one line of the + * encoding + * @throws IOException exception in any. + */ + public void writeCodePageFile(String filePath) throws IOException { + File file = new File(filePath); + FileWriter fileWriter = new FileWriter(file); + try { + fileWriter.write(getCharset() != null ? getCharset().toString() + : getDefaultCharset().toString()); + } catch (IOException e) { + throw new IOException("An error occurred while creating/writing the CPG file: " + + filePath + " " + e.getMessage()); + } finally { + try { + fileWriter.close(); + } catch (IOException e) { + throw new IOException("An error occurred while trying to close the CPG file: " + + filePath + " " + e.getMessage()); + } + } + } + }