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..422f7af747 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 @@ -25,6 +25,7 @@ import java.time.Instant import java.time.LocalDate import java.util.function.Consumer +import org.geotools.data.shapefile.ShapefileDataStore import org.junit.Test import org.locationtech.jts.geom.Coordinate import org.locationtech.jts.geom.Geometry @@ -100,7 +101,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) @@ -114,10 +115,11 @@ class ShapefileInstanceWriterTest { assertTrue(report.isSuccess()) assertTrue(report.getErrors().isEmpty()) + testCpgFileAgainstRelatedSpapefile(file) + return reader.getInstances(); } - /** * Write an instance collection to a Shapefile. */ @@ -184,11 +186,25 @@ class ShapefileInstanceWriterTest { println "Temporary file is $tmpFile" writeInstances(tmpFile.toFile(), schema, instances, configurator) handler.accept(tmpFile.toFile()) + + testCpgFileAgainstRelatedSpapefile(tmpFile.toFile()) } finally { tmpDir.deleteDir() } } + private static testCpgFileAgainstRelatedSpapefile(File shpFile) { + ShapefileDataStore store = new ShapefileDataStore(shpFile.toURL()); + String cpgFilePath = shpFile.getAbsolutePath().replace(ShapefileConstants.SHP_EXTENSION, ShapefileConstants.CPG_EXTENSION) + + def cpgFile = new File(cpgFilePath) + if (cpgFile.exists()) { + assertTrue(store.getCharset().toString().equals(cpgFile.text)) + } else { + println("File not found.") + } + } + //@CompileStatic static void withNewShapefileWithReporterErrors(Schema schema, InstanceCollection instances, Consumer handler, Consumer configurator = null) { @@ -199,6 +215,8 @@ class ShapefileInstanceWriterTest { println "Temporary file is $tmpFile" writeInstancesWithReporterErrors(tmpFile.toFile(), schema, instances, configurator) handler.accept(tmpFile.toFile()) + + testCpgFileAgainstRelatedSpapefile(tmpFile.toFile()) } finally { tmpDir.deleteDir() } @@ -240,6 +258,7 @@ class ShapefileInstanceWriterTest { num++ } } + // 593 instances were loaded assertEquals(593, num) } 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..dd0147951b 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,35 @@ private List writeToFile( return filesWritten; } + /** + * Create the CPG file starting from the Shapefile + * + * @param cpgFilePath Path of the file to be written with just one line of + * the encoding + * @throws IOException exception in any. + */ + public void writeCodePageFile(String cpgFilePath) throws IOException { + File cpgFile = new File(cpgFilePath); + FileWriter fileWriter = new FileWriter(cpgFile); + + ShapefileDataStore store = new ShapefileDataStore( + new File(cpgFilePath.replace(ShapefileConstants.CPG_EXTENSION, + ShapefileConstants.SHP_EXTENSION)).toURL()); + + try { + fileWriter.write(store.getCharset() != null ? store.getCharset().toString() + : getDefaultCharset().toString()); + } catch (IOException e) { + throw new IOException("An error occurred while writing the CPG file: " + cpgFilePath + + " " + e.getMessage()); + } finally { + try { + fileWriter.close(); + } catch (IOException e) { + throw new IOException("An error occurred while trying to close the CPG file: " + + cpgFilePath + " " + e.getMessage()); + } + } + } + }