From aca5b04650cbf794299c12b522c24ff68da7c94b Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Tue, 10 Aug 2021 18:31:41 +0200 Subject: [PATCH 1/3] Fix possible NPE in exporter with empty charset And fix issue where calling withEncoding would have no effect if the database ws not utf8 --- .../logic/exporter/TemplateExporter.java | 20 +++-- .../logic/exporter/YamlExporterTest.java | 88 +++++++++++++++++++ 2 files changed, 103 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/jabref/logic/exporter/TemplateExporter.java b/src/main/java/org/jabref/logic/exporter/TemplateExporter.java index 4e3230e509d..d9484d4dae8 100644 --- a/src/main/java/org/jabref/logic/exporter/TemplateExporter.java +++ b/src/main/java/org/jabref/logic/exporter/TemplateExporter.java @@ -8,6 +8,7 @@ import java.io.Reader; import java.net.URL; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.nio.file.Path; import java.util.ArrayList; import java.util.HashMap; @@ -46,7 +47,7 @@ public class TemplateExporter extends Exporter { private final String directory; private final LayoutFormatterPreferences layoutPreferences; private final SavePreferences savePreferences; - private Charset encoding; // If this value is set, it will be used to override the default encoding for the getCurrentBasePanel. + private Charset encodingOverwritten; // If this value is set, it will be used to override the default encoding for the getCurrentBasePanel. private boolean customExport; private BlankLineBehaviour blankLineBehaviour; @@ -145,7 +146,7 @@ public void setCustomExport(boolean custom) { * @param encoding The name of the encoding to use. */ public TemplateExporter withEncoding(Charset encoding) { - this.encoding = encoding; + this.encodingOverwritten = encoding; return this; } @@ -195,11 +196,20 @@ public void export(final BibDatabaseContext databaseContext, final Path file, final Charset encoding, List entries) throws Exception { Objects.requireNonNull(databaseContext); Objects.requireNonNull(entries); + + + Charset encodingToUse = StandardCharsets.UTF_8; + if (encoding != null) { + encodingToUse = encoding; + } else if (this.encodingOverwritten != null) { + encodingToUse = this.encodingOverwritten; + } + if (entries.isEmpty()) { // Do not export if no entries to export -- avoids exports with only template text return; } - try (AtomicFileWriter ps = new AtomicFileWriter(file, encoding)) { + try (AtomicFileWriter ps = new AtomicFileWriter(file, encodingToUse)) { Layout beginLayout = null; // Check if this export filter has bundled name formatters: @@ -218,7 +228,7 @@ public void export(final BibDatabaseContext databaseContext, final Path file, } // Write the header if (beginLayout != null) { - ps.write(beginLayout.doLayout(databaseContext, encoding)); + ps.write(beginLayout.doLayout(databaseContext, encodingToUse)); missingFormatters.addAll(beginLayout.getMissingFormatters()); } @@ -300,7 +310,7 @@ public void export(final BibDatabaseContext databaseContext, final Path file, // Write footer if (endLayout != null) { - ps.write(endLayout.doLayout(databaseContext, this.encoding)); + ps.write(endLayout.doLayout(databaseContext, encodingToUse)); missingFormatters.addAll(endLayout.getMissingFormatters()); } diff --git a/src/test/java/org/jabref/logic/exporter/YamlExporterTest.java b/src/test/java/org/jabref/logic/exporter/YamlExporterTest.java index cb5268cf005..cb4df65d40a 100644 --- a/src/test/java/org/jabref/logic/exporter/YamlExporterTest.java +++ b/src/test/java/org/jabref/logic/exporter/YamlExporterTest.java @@ -105,4 +105,92 @@ public final void formatsContentCorrect(@TempDir Path tempFile) throws Exception assertEquals(expected, Files.readAllLines(file)); } + + @Test + void passesModifiedCharset(@TempDir Path tempFile) throws Exception { + BibEntry entry = new BibEntry(StandardEntryType.Article) + .withCitationKey("test") + .withField(StandardField.AUTHOR, "谷崎 潤一郎") + .withField(StandardField.TITLE, "細雪") + .withField(StandardField.URL, "http://example.com") + .withField(StandardField.DATE, "2020-10-14"); + + + Path file = tempFile.resolve("RandomFileName"); + Files.createFile(file); + yamlExporter.export(databaseContext, file, StandardCharsets.UTF_8, Collections.singletonList(entry)); + + List expected = List.of( + "---", + "references:", + "- id: test", + " type: article", + " author:", + " - literal: \"谷崎 潤一郎\"", + " title: \"細雪\"", + " issued: 2020-10-14", + " url: http://example.com", + "---"); + + assertEquals(expected, Files.readAllLines(file)); + } + + @Test + void passesModifiedCharsetNull(@TempDir Path tempFile) throws Exception { + BibEntry entry = new BibEntry(StandardEntryType.Article) + .withCitationKey("test") + .withField(StandardField.AUTHOR, "谷崎 潤一郎") + .withField(StandardField.TITLE, "細雪") + .withField(StandardField.URL, "http://example.com") + .withField(StandardField.DATE, "2020-10-14"); + + + Path file = tempFile.resolve("RandomFileName"); + Files.createFile(file); + yamlExporter.export(databaseContext, file, null, Collections.singletonList(entry)); + + List expected = List.of( + "---", + "references:", + "- id: test", + " type: article", + " author:", + " - literal: \"谷崎 潤一郎\"", + " title: \"細雪\"", + " issued: 2020-10-14", + " url: http://example.com", + "---"); + + assertEquals(expected, Files.readAllLines(file)); + } + + @Test + void passesModifiedCharsetASCII(@TempDir Path tempFile) throws Exception { + BibEntry entry = new BibEntry(StandardEntryType.Article) + .withCitationKey("test") + .withField(StandardField.AUTHOR, "谷崎 潤一郎") + .withField(StandardField.TITLE, "細雪") + .withField(StandardField.URL, "http://example.com") + .withField(StandardField.DATE, "2020-10-14"); + + + Path file = tempFile.resolve("RandomFileName"); + Files.createFile(file); + yamlExporter.export(databaseContext, file, StandardCharsets.US_ASCII, Collections.singletonList(entry)); + + List expected = List.of( + "---", + "references:", + "- id: test", + " type: article", + " author:", + " - literal: \"?? ???\"", + " title: \"??\"", + " issued: 2020-10-14", + " url: http://example.com", + "---"); + + assertEquals(expected, Files.readAllLines(file)); + + } } From 27b87d0e50d0b5564b86777c00a1773636e90fad Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Sun, 15 Aug 2021 23:07:18 +0200 Subject: [PATCH 2/3] fix checkstyle --- src/main/java/org/jabref/logic/exporter/TemplateExporter.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/org/jabref/logic/exporter/TemplateExporter.java b/src/main/java/org/jabref/logic/exporter/TemplateExporter.java index d9484d4dae8..268b3d53522 100644 --- a/src/main/java/org/jabref/logic/exporter/TemplateExporter.java +++ b/src/main/java/org/jabref/logic/exporter/TemplateExporter.java @@ -197,7 +197,6 @@ public void export(final BibDatabaseContext databaseContext, final Path file, Objects.requireNonNull(databaseContext); Objects.requireNonNull(entries); - Charset encodingToUse = StandardCharsets.UTF_8; if (encoding != null) { encodingToUse = encoding; From d9c60cd9479edef94b227cea22819f8d7c9738e5 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Sun, 15 Aug 2021 23:11:46 +0200 Subject: [PATCH 3/3] checkstyle --- src/test/java/org/jabref/logic/exporter/YamlExporterTest.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/test/java/org/jabref/logic/exporter/YamlExporterTest.java b/src/test/java/org/jabref/logic/exporter/YamlExporterTest.java index cb4df65d40a..84b0d9349f2 100644 --- a/src/test/java/org/jabref/logic/exporter/YamlExporterTest.java +++ b/src/test/java/org/jabref/logic/exporter/YamlExporterTest.java @@ -115,7 +115,6 @@ void passesModifiedCharset(@TempDir Path tempFile) throws Exception { .withField(StandardField.URL, "http://example.com") .withField(StandardField.DATE, "2020-10-14"); - Path file = tempFile.resolve("RandomFileName"); Files.createFile(file); yamlExporter.export(databaseContext, file, StandardCharsets.UTF_8, Collections.singletonList(entry)); @@ -144,7 +143,6 @@ void passesModifiedCharsetNull(@TempDir Path tempFile) throws Exception { .withField(StandardField.URL, "http://example.com") .withField(StandardField.DATE, "2020-10-14"); - Path file = tempFile.resolve("RandomFileName"); Files.createFile(file); yamlExporter.export(databaseContext, file, null, Collections.singletonList(entry)); @@ -173,7 +171,6 @@ void passesModifiedCharsetASCII(@TempDir Path tempFile) throws Exception { .withField(StandardField.URL, "http://example.com") .withField(StandardField.DATE, "2020-10-14"); - Path file = tempFile.resolve("RandomFileName"); Files.createFile(file); yamlExporter.export(databaseContext, file, StandardCharsets.US_ASCII, Collections.singletonList(entry));