Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix possible NPE in exporter with empty charset #7979

Merged
merged 3 commits into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/main/java/org/jabref/logic/exporter/TemplateExporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -195,11 +196,19 @@ public void export(final BibDatabaseContext databaseContext, final Path file,
final Charset encoding, List<BibEntry> 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:
Expand All @@ -218,7 +227,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());
}

Expand Down Expand Up @@ -300,7 +309,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());
}

Expand Down
85 changes: 85 additions & 0 deletions src/test/java/org/jabref/logic/exporter/YamlExporterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,89 @@ 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<String> 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<String> 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<String> expected = List.of(
"---",
"references:",
"- id: test",
" type: article",
" author:",
" - literal: \"?? ???\"",
" title: \"??\"",
" issued: 2020-10-14",
" url: http://example.com",
"---");

assertEquals(expected, Files.readAllLines(file));

}
}