Skip to content

Commit

Permalink
Fixes regarding explicitly specify encoding for readers/writers so sy…
Browse files Browse the repository at this point in the history
…stem default doesn't cause issues
  • Loading branch information
JuicyDragon committed Aug 22, 2024
1 parent 6ee45fc commit 29b0078
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 <b>fileNameTemplate</b>
* @param fileNameTemplate A filename template that dictates the ultimate export structure.
* @param emailExportSettings Email native export settings as supported by <b>BatchExporter</b>
*/
public void exportNatives(String fileNameTemplate, Map<String, Object> 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 <b>fileNameTemplate</b>
* @param fileNameTemplate A filename template that dictates the ultimate export structure.
* @param textExportSettings Text file export settings as supported by <b>BatchExporter</b>
*/
public void exportText(String fileNameTemplate, Map<String, Object> 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 <b>fileNameTemplate</b>
* @param fileNameTemplate A filename template that dictates the ultimate export structure.
* @param pdfExportSettings PDF export settings as supported by <b>BatchExporter</b>
*/
public void exportPdfs(String fileNameTemplate, Map<String, Object> 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 <b>fileNameTemplate</b>
* @param fileNameTemplate A filename template that dictates the ultimate export structure.
* @param tiffExportSettings TIFF export settings as supported by <b>BatchExporter</b>
*/
public void exportTiffs(String fileNameTemplate, Map<String, Object> 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 <b>fileNameTemplate</b>
* @param fileNameTemplate A filename template that dictates the ultimate export structure.
*/
public void exportJson(String fileNameTemplate) {
exportJson = true;
jsonFileNameTemplate = fileNameTemplate;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -340,6 +379,7 @@ public void exportItems(Case nuixCase, File exportDirectory, List<Item> items) t

Map<String, Object> loadfileSettings = new HashMap<String, Object>();
loadfileSettings.put("metadataProfile", exportProfile);
loadfileSettings.put("encoding", StandardCharsets.UTF_8.name());
exporter.addLoadFile("concordance", loadfileSettings);

if (exportText) {
Expand Down Expand Up @@ -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<String, String> tiffRenames = new HashMap<String, String>();
Expand Down Expand Up @@ -474,7 +516,11 @@ public void accept(LinkedHashMap<String, String> 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);
Expand All @@ -485,11 +531,18 @@ public void accept(LinkedHashMap<String, String> 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) {
Expand Down Expand Up @@ -574,7 +627,13 @@ public void accept(LinkedHashMap<String, String> 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<Object> recordValues = new ArrayList<Object>(record.values());
worksheet.appendRow(recordValues);
Expand Down Expand Up @@ -646,7 +705,7 @@ public void accept(OptRecord record) {
* <a href="https://download.nuix.com/releases/desktop/stable/docs/en/scripting/api/nuix/ImagingConfigurable.html#setImagingOptions-java.util.Map-">BatchExporter.setImagingOptions</a>
* for a list of settings accepted.
*/
public void setImagingOptions(Map<?, ?> settings) {
public void setImagingOptions(Map<String, Object> settings) {
this.imagingSettings = settings;
}

Expand All @@ -657,7 +716,7 @@ public void setImagingOptions(Map<?, ?> settings) {
* <a href="https://download.nuix.com/releases/desktop/stable/docs/en/scripting/api/nuix/StampingConfigurable.html#setStampingOptions-java.util.Map-">BatchExporter.setStampingOptions</a>
* for a list of settings accepted.
*/
public void setStampingOptions(Map<?, ?> settings) {
public void setStampingOptions(Map<String, Object> settings) {
this.stampingSettings = settings;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,16 @@ public static void transpose(File sourceFile, File destinationFile, Consumer<Lin
boolean headersWrittern = false;
@Override
public void accept(LinkedHashMap<String, String> 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);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String,String> record) {
public void writeRecordValues(LinkedHashMap<String,String> record) throws IOException {
writeLine(DatLoadFile.toLine(record));
}

public void writeRecordKeys(LinkedHashMap<String,String> record) {
public void writeRecordKeys(LinkedHashMap<String,String> record) throws IOException {
writeLine(DatLoadFile.toHeaderLine(record));
}

public void writeValues(List<String> headers) {
public void writeValues(List<String> headers) throws IOException {
writeLine(DatLoadFile.toHeaderLine(headers));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<String> consumer) throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader(sourceFile, StandardCharsets.UTF_8))) {
String line;
while ((line = br.readLine()) != null) {
consumer.accept(line);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}

0 comments on commit 29b0078

Please sign in to comment.