From 29b00780883ab5ebe65425a820b6ca19cc97b3aa Mon Sep 17 00:00:00 2001 From: Jason Wells Date: Thu, 22 Aug 2024 16:02:43 -0700 Subject: [PATCH] Fixes regarding explicitly specify encoding for readers/writers so system default doesn't cause issues --- .../superutilities/export/CustomExporter.java | 71 +++++++++++++++++-- .../superutilities/loadfiles/DatLoadFile.java | 16 +++-- .../loadfiles/DatLoadFileWriter.java | 8 +-- .../loadfiles/SimpleTextFileReader.java | 17 ++--- .../loadfiles/SimpleTextFileWriter.java | 45 ++++++------ 5 files changed, 110 insertions(+), 47 deletions(-) diff --git a/Java/SuperUtilities/src/main/java/com/nuix/superutilities/export/CustomExporter.java b/Java/SuperUtilities/src/main/java/com/nuix/superutilities/export/CustomExporter.java index b475cb2..732058e 100644 --- a/Java/SuperUtilities/src/main/java/com/nuix/superutilities/export/CustomExporter.java +++ b/Java/SuperUtilities/src/main/java/com/nuix/superutilities/export/CustomExporter.java @@ -15,6 +15,8 @@ import org.slf4j.LoggerFactory; import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.util.*; import java.util.function.BiFunction; import java.util.function.Consumer; @@ -147,30 +149,59 @@ public void clearAllDynamicPlaholders() { dynamicPlaceholders.clear(); } + /*** + * Enables export of natives using the provided settings. See {@link PlaceholderResolver#setFromItem(Item)} + * to see what placeholders are inherently supported by fileNameTemplate + * @param fileNameTemplate A filename template that dictates the ultimate export structure. + * @param emailExportSettings Email native export settings as supported by BatchExporter + */ public void exportNatives(String fileNameTemplate, Map emailExportSettings) { exportNatives = true; this.emailExportSettings = emailExportSettings; nativeFileNameTemplate = fileNameTemplate; } + /*** + * Enables export of text files using the provided settings. See {@link PlaceholderResolver#setFromItem(Item)} + * to see what placeholders are inherently supported by fileNameTemplate + * @param fileNameTemplate A filename template that dictates the ultimate export structure. + * @param textExportSettings Text file export settings as supported by BatchExporter + */ public void exportText(String fileNameTemplate, Map textExportSettings) { exportText = true; this.textExportSettings = textExportSettings; textFileNameTemplate = fileNameTemplate; } + /*** + * Enables export of PDF files using the provided settings. See {@link PlaceholderResolver#setFromItem(Item)} + * to see what placeholders are inherently supported by fileNameTemplate + * @param fileNameTemplate A filename template that dictates the ultimate export structure. + * @param pdfExportSettings PDF export settings as supported by BatchExporter + */ public void exportPdfs(String fileNameTemplate, Map pdfExportSettings) { exportPdfs = true; this.pdfExportSettings = pdfExportSettings; pdfFileNameTemplate = fileNameTemplate; } + /*** + * Enables export of TIFF files using the provided settings. See {@link PlaceholderResolver#setFromItem(Item)} + * to see what placeholders are inherently supported by fileNameTemplate + * @param fileNameTemplate A filename template that dictates the ultimate export structure. + * @param tiffExportSettings TIFF export settings as supported by BatchExporter + */ public void exportTiffs(String fileNameTemplate, Map tiffExportSettings) { exportTiffs = true; this.tiffExportSettings = tiffExportSettings; tiffFileNameTemplate = fileNameTemplate; } + /*** + * Enables export of JSON files using the provided settings. See {@link PlaceholderResolver#setFromItem(Item)} + * to see what placeholders are inherently supported by fileNameTemplate + * @param fileNameTemplate A filename template that dictates the ultimate export structure. + */ public void exportJson(String fileNameTemplate) { exportJson = true; jsonFileNameTemplate = fileNameTemplate; @@ -210,7 +241,11 @@ private void logInfo(String format, Object... params) { String message = String.format(format, params); System.out.println(message); if (generalLog != null) { - generalLog.writeLine(message); + try { + generalLog.writeLine(message); + } catch (IOException e) { + throw new RuntimeException(e); + } } else { log.info(message); } @@ -220,7 +255,11 @@ private void logError(String format, Object... params) { String message = String.format(format, params); System.out.println(message); if (errorLog != null) { - errorLog.writeLine(message); + try { + errorLog.writeLine(message); + } catch (IOException e) { + throw new RuntimeException(e); + } } else { log.error(message); } @@ -340,6 +379,7 @@ public void exportItems(Case nuixCase, File exportDirectory, List items) t Map loadfileSettings = new HashMap(); loadfileSettings.put("metadataProfile", exportProfile); + loadfileSettings.put("encoding", StandardCharsets.UTF_8.name()); exporter.addLoadFile("concordance", loadfileSettings); if (exportText) { @@ -430,6 +470,8 @@ public void itemProcessed(ItemEventInfo info) { // Used to resolve naming templates to final path structure PlaceholderResolver resolver = new PlaceholderResolver(); + resolver.setStandardValues(); + resolver.setFromCase(nuixCase); // Tracks old relative path and new relative path so that OPT file can be updated Map tiffRenames = new HashMap(); @@ -474,7 +516,11 @@ public void accept(LinkedHashMap record) { outputHeaders.add("JSONPATH"); } - datWriter.writeValues(outputHeaders); + try { + datWriter.writeValues(outputHeaders); + } catch (IOException e) { + log.error("Error writing to DAT file: " + e.getMessage(), e); + } if (exportXlsx) { worksheet.appendRow(outputHeaders); @@ -485,11 +531,18 @@ public void accept(LinkedHashMap record) { String guid = record.get("GUID"); + if (guid == null || guid.equalsIgnoreCase("null")) { + log.error("'GUID' not found in DAT file? Known headers are:"); + log.error(String.join(", ", DatLoadFile.getHeadersFromRecord(record))); + } + try { Item currentItem = nuixCase.search("guid:" + guid).get(0); resolver.clear(); resolver.setPath("export_directory", exportDirectory.getAbsolutePath()); resolver.setFromItem(currentItem); + resolver.setStandardValues(); + resolver.setFromCase(nuixCase); // Restructure text files if we have them if (exportText) { @@ -574,7 +627,13 @@ public void accept(LinkedHashMap record) { record.remove(header); } - datWriter.writeRecordValues(record); + try { + datWriter.writeRecordValues(record); + } catch (IOException e) { + log.error("Error writing to DAT: " + e.getMessage(), e); + throw new RuntimeException(e); + } + if (exportXlsx) { List recordValues = new ArrayList(record.values()); worksheet.appendRow(recordValues); @@ -646,7 +705,7 @@ public void accept(OptRecord record) { * BatchExporter.setImagingOptions * for a list of settings accepted. */ - public void setImagingOptions(Map settings) { + public void setImagingOptions(Map settings) { this.imagingSettings = settings; } @@ -657,7 +716,7 @@ public void setImagingOptions(Map settings) { * BatchExporter.setStampingOptions * for a list of settings accepted. */ - public void setStampingOptions(Map settings) { + public void setStampingOptions(Map settings) { this.stampingSettings = settings; } diff --git a/Java/SuperUtilities/src/main/java/com/nuix/superutilities/loadfiles/DatLoadFile.java b/Java/SuperUtilities/src/main/java/com/nuix/superutilities/loadfiles/DatLoadFile.java index 2addcaf..5389c1d 100644 --- a/Java/SuperUtilities/src/main/java/com/nuix/superutilities/loadfiles/DatLoadFile.java +++ b/Java/SuperUtilities/src/main/java/com/nuix/superutilities/loadfiles/DatLoadFile.java @@ -87,12 +87,16 @@ public static void transpose(File sourceFile, File destinationFile, Consumer record) { - if(headersWrittern == false) { - datWriter.writeRecordKeys(record); - headersWrittern = true; - } else { - recordModifier.accept(record); - datWriter.writeRecordValues(record); + try { + if(!headersWrittern) { + datWriter.writeRecordKeys(record); + headersWrittern = true; + } else { + recordModifier.accept(record); + datWriter.writeRecordValues(record); + } + } catch (IOException e) { + throw new RuntimeException(e); } } }); diff --git a/Java/SuperUtilities/src/main/java/com/nuix/superutilities/loadfiles/DatLoadFileWriter.java b/Java/SuperUtilities/src/main/java/com/nuix/superutilities/loadfiles/DatLoadFileWriter.java index f3b4ff4..18261c2 100644 --- a/Java/SuperUtilities/src/main/java/com/nuix/superutilities/loadfiles/DatLoadFileWriter.java +++ b/Java/SuperUtilities/src/main/java/com/nuix/superutilities/loadfiles/DatLoadFileWriter.java @@ -10,19 +10,19 @@ public DatLoadFileWriter(File destinationFile) throws IOException { super(destinationFile); } - public void writeDatLine(String[] values) { + public void writeDatLine(String[] values) throws IOException { writeLine(DatLoadFile.toLine(values)); } - public void writeRecordValues(LinkedHashMap record) { + public void writeRecordValues(LinkedHashMap record) throws IOException { writeLine(DatLoadFile.toLine(record)); } - public void writeRecordKeys(LinkedHashMap record) { + public void writeRecordKeys(LinkedHashMap record) throws IOException { writeLine(DatLoadFile.toHeaderLine(record)); } - public void writeValues(List headers) { + public void writeValues(List headers) throws IOException { writeLine(DatLoadFile.toHeaderLine(headers)); } } diff --git a/Java/SuperUtilities/src/main/java/com/nuix/superutilities/loadfiles/SimpleTextFileReader.java b/Java/SuperUtilities/src/main/java/com/nuix/superutilities/loadfiles/SimpleTextFileReader.java index 90354d2..c6c8ee1 100644 --- a/Java/SuperUtilities/src/main/java/com/nuix/superutilities/loadfiles/SimpleTextFileReader.java +++ b/Java/SuperUtilities/src/main/java/com/nuix/superutilities/loadfiles/SimpleTextFileReader.java @@ -4,15 +4,16 @@ import java.io.File; import java.io.FileReader; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.util.function.Consumer; public class SimpleTextFileReader { - public static void withEachLine(File sourceFile, Consumer consumer) throws IOException { - try (BufferedReader br = new BufferedReader(new FileReader(sourceFile))) { - String line; - while ((line = br.readLine()) != null) { - consumer.accept(line); - } - } - } + public static void withEachLine(File sourceFile, Consumer consumer) throws IOException { + try (BufferedReader br = new BufferedReader(new FileReader(sourceFile, StandardCharsets.UTF_8))) { + String line; + while ((line = br.readLine()) != null) { + consumer.accept(line); + } + } + } } diff --git a/Java/SuperUtilities/src/main/java/com/nuix/superutilities/loadfiles/SimpleTextFileWriter.java b/Java/SuperUtilities/src/main/java/com/nuix/superutilities/loadfiles/SimpleTextFileWriter.java index 1440e70..ff2b896 100644 --- a/Java/SuperUtilities/src/main/java/com/nuix/superutilities/loadfiles/SimpleTextFileWriter.java +++ b/Java/SuperUtilities/src/main/java/com/nuix/superutilities/loadfiles/SimpleTextFileWriter.java @@ -4,30 +4,29 @@ import java.io.File; import java.io.FileWriter; import java.io.IOException; -import java.io.PrintWriter; +import java.nio.charset.StandardCharsets; public class SimpleTextFileWriter implements Closeable { - protected File destinationFile; - protected PrintWriter pw; - protected FileWriter fw; - - public SimpleTextFileWriter(File destinationFile) throws IOException { - this.destinationFile = destinationFile; - fw = new FileWriter(destinationFile); - pw = new PrintWriter(fw); - } - - public void writeLine(String line) { - pw.println(line); - } + protected File destinationFile; + protected FileWriter fw; - public File getDestinationFile() { - return destinationFile; - } - - @Override - public void close() throws IOException { - if(pw != null) { pw.close(); } - if(fw != null) { fw.close(); } - } + public SimpleTextFileWriter(File destinationFile) throws IOException { + this.destinationFile = destinationFile; + fw = new FileWriter(destinationFile, StandardCharsets.UTF_8); + } + + public void writeLine(String line) throws IOException { + fw.write(line + "\n"); + } + + public File getDestinationFile() { + return destinationFile; + } + + @Override + public void close() throws IOException { + if (fw != null) { + fw.close(); + } + } }