From 955ceea944e4aa28b4730700f2751f33b7daa6f5 Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Thu, 13 Jan 2022 21:31:23 +0100 Subject: [PATCH 01/28] Pin encoding to UTF-8 - no more unused variables "encoding" --- CHANGELOG.md | 4 ++ build.gradle | 3 +- src/main/java/module-info.java | 1 + .../org/jabref/cli/ArgumentProcessor.java | 41 +++++------- src/main/java/org/jabref/gui/JabRefFrame.java | 2 +- src/main/java/org/jabref/gui/JabRefGUI.java | 1 - .../org/jabref/gui/collab/ChangeScanner.java | 2 +- .../jabref/gui/entryeditor/EntryEditor.java | 6 +- .../jabref/gui/exporter/ExportCommand.java | 4 -- .../gui/exporter/ExportToClipboardAction.java | 22 +------ .../gui/exporter/SaveDatabaseAction.java | 5 +- .../exporter/WriteMetadataToPdfAction.java | 13 ++-- .../gui/fieldeditors/LinkedFileViewModel.java | 4 +- .../org/jabref/gui/importer/ImportAction.java | 2 +- .../importer/actions/OpenDatabaseAction.java | 1 - .../gui/integrity/IntegrityCheckAction.java | 1 - .../general/GeneralPropertiesViewModel.java | 3 +- .../gui/openoffice/OpenOfficePanel.java | 3 +- .../gui/preferences/general/GeneralTab.fxml | 2 - .../general/GeneralTabViewModel.java | 2 - .../autosaveandbackup/BackupManager.java | 3 +- .../jabref/logic/crawler/StudyRepository.java | 8 +-- .../logic/exporter/BibTeXMLExporter.java | 3 +- .../exporter/EmbeddedBibFilePdfExporter.java | 11 ++-- .../org/jabref/logic/exporter/Exporter.java | 14 ++--- .../logic/exporter/ExporterFactory.java | 3 +- .../jabref/logic/exporter/MSBibExporter.java | 3 +- .../jabref/logic/exporter/ModsExporter.java | 3 +- .../OpenDocumentSpreadsheetCreator.java | 3 +- .../exporter/OpenOfficeDocumentCreator.java | 3 +- .../logic/exporter/TemplateExporter.java | 18 +----- .../jabref/logic/exporter/XmpExporter.java | 13 ++-- .../jabref/logic/exporter/XmpPdfExporter.java | 4 +- .../ExternalFilesContentImporter.java | 5 +- .../logic/importer/ImportFormatReader.java | 6 +- .../org/jabref/logic/importer/Importer.java | 63 +++++++++++++------ .../jabref/logic/importer/OpenDatabase.java | 6 +- .../jabref/logic/importer/WebFetchers.java | 4 +- .../importer/fileformat/BibtexImporter.java | 26 ++++---- .../importer/fileformat/BibtexParser.java | 13 ++-- .../fileformat/PdfContentImporter.java | 3 +- .../PdfEmbeddedBibFileImporter.java | 3 +- .../fileformat/PdfGrobidImporter.java | 5 +- .../fileformat/PdfMergeMetadataImporter.java | 11 ++-- .../PdfVerbatimBibTextImporter.java | 3 +- .../importer/fileformat/PdfXmpImporter.java | 5 +- .../logic/integrity/IntegrityCheck.java | 5 +- .../logic/openoffice/style/OOBibStyle.java | 10 +-- .../logic/openoffice/style/StyleLoader.java | 10 +-- .../texparser/TexBibEntriesResolver.java | 2 +- .../preferences/GeneralPreferences.java | 19 +----- .../jabref/preferences/JabRefPreferences.java | 5 +- .../org/jabref/JabRefPreferencesTest.java | 34 ---------- .../exporter/ExportToClipboardActionTest.java | 5 +- .../gui/exporter/SaveDatabaseActionTest.java | 2 - .../jabref/logic/bibtex/BibEntryAssert.java | 2 +- .../org/jabref/logic/crawler/CrawlerTest.java | 1 - .../StudyDatabaseToFetcherConverterTest.java | 1 - .../logic/crawler/StudyRepositoryTest.java | 1 - .../exporter/BibTeXMLExporterTestFiles.java | 6 +- .../exporter/BibtexDatabaseWriterTest.java | 55 +++------------- .../logic/exporter/CsvExportFormatTest.java | 12 ++-- .../logic/exporter/DocBook5ExporterTest.java | 2 +- .../logic/exporter/DocbookExporterTest.java | 4 +- .../EmbeddedBibFilePdfExporterTest.java | 8 +-- .../jabref/logic/exporter/ExporterTest.java | 6 +- .../logic/exporter/HtmlExportFormatTest.java | 2 +- .../exporter/MSBibExportFormatTestFiles.java | 4 +- .../logic/exporter/ModsExportFormatTest.java | 6 +- .../exporter/ModsExportFormatTestFiles.java | 12 ++-- .../logic/exporter/MsBibExportFormatTest.java | 4 +- .../OpenOfficeDocumentCreatorTest.java | 2 +- .../logic/exporter/XmpExporterTest.java | 12 ++-- .../logic/exporter/XmpPdfExporterTest.java | 8 +-- .../logic/exporter/YamlExporterTest.java | 12 ++-- .../importer/DatabaseFileLookupTest.java | 3 +- .../ImportFormatReaderIntegrationTest.java | 2 - .../ImportFormatReaderTestParameterless.java | 1 - .../logic/importer/OpenDatabaseTest.java | 20 +++--- .../logic/importer/WebFetchersTest.java | 3 +- .../fileformat/BiblioscapeImporterTest.java | 3 +- .../fileformat/BibtexImporterTest.java | 14 ++--- .../importer/fileformat/CffImporterTest.java | 19 +++--- .../fileformat/CopacImporterTest.java | 3 +- .../fileformat/EndnoteImporterTest.java | 10 ++- .../fileformat/ImporterTestEngine.java | 9 ++- .../importer/fileformat/IsiImporterTest.java | 17 +++-- .../fileformat/MedlinePlainImporterTest.java | 12 ++-- .../fileformat/MsBibImporterTest.java | 7 +-- .../importer/fileformat/OvidImporterTest.java | 9 ++- .../fileformat/PdfContentImporterTest.java | 7 +-- .../PdfEmbeddedBibFileImporterTest.java | 5 +- .../fileformat/PdfGrobidImporterTest.java | 7 +-- .../PdfMergeMetadataImporterTest.java | 4 +- .../PdfVerbatimBibTextImporterTest.java | 6 +- .../fileformat/PdfXmpImporterTest.java | 6 +- .../importer/fileformat/RISImporterTest.java | 2 +- .../logic/integrity/IntegrityCheckTest.java | 3 - .../openoffice/style/OOBibStyleTest.java | 3 +- .../openoffice/style/StyleLoaderTest.java | 32 +++++----- .../logic/texparser/LatexParserTest.java | 1 - .../texparser/TexBibEntriesResolverTest.java | 1 - 102 files changed, 305 insertions(+), 520 deletions(-) delete mode 100644 src/test/java/org/jabref/JabRefPreferencesTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 7009cc52038..350a5526106 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,9 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve - We moved the search box in preview preferences closer to the available citation styles list. [#8370](https://github.com/JabRef/jabref/pull/8370) - Changing the preference to show the preview panel as a separate tab now has effect without restarting JabRef. [#8370](https://github.com/JabRef/jabref/pull/8370) - We enabled switching themes in JabRef without the need to restart JabRef. [#7335](https://github.com/JabRef/jabref/pull/7335) +- When exporting entries, the encoding is always UTF-8 +- When embedding BibTeX data into a PDF, the encoding is always UTF-8 +- JabRef automatically detects the encoding of a `.bib` file and does not need the `% Encoding:` header anymore. [#8417](https://github.com/JabRef/jabref/issues/8417) ### Fixed @@ -30,6 +33,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve - We fixed an issue where someone could add a duplicate field in the customize entry type dialog. [#8194](https://github.com/JabRef/jabref/issues/8194) - We fixed a typo in the library properties tab: "String constants". There, one can configure [BibTeX string constants](https://docs.jabref.org/advanced/strings). - We fixed an issue when writing a non-UTF-8 encoded file: The header is written again. [#8417](https://github.com/JabRef/jabref/issues/8417) +- We fixed an issue when reading non-UTF-8 encoded. [#8417](https://github.com/JabRef/jabref/issues/8417) ### Removed diff --git a/build.gradle b/build.gradle index b180819ca9f..4eca689392f 100644 --- a/build.gradle +++ b/build.gradle @@ -1,9 +1,7 @@ -import groovy.json.JsonSlurper import org.gradle.internal.os.OperatingSystem import org.jabref.build.JournalAbbreviationConverter import org.jabref.build.xjc.XjcPlugin import org.jabref.build.xjc.XjcTask - // to update the gradle wrapper, execute // ./gradlew wrapper --gradle-version=6.0 --distribution-type=bin @@ -122,6 +120,7 @@ dependencies { implementation 'com.h2database:h2-mvstore:2.0.206' implementation group: 'org.apache.tika', name: 'tika-core', version: '2.2.1' + implementation 'com.ibm.icu:icu4j-charset:70.1' // required for reading write-protected PDFs - see https://github.com/JabRef/jabref/pull/942#issuecomment-209252635 implementation 'org.bouncycastle:bcprov-jdk15on:1.70' diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index 0fc2e70e7e0..50515d2e4b3 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -91,6 +91,7 @@ requires org.antlr.antlr4.runtime; requires flowless; requires org.apache.tika.core; + requires com.ibm.icu; requires flexmark; requires flexmark.ext.gfm.strikethrough; diff --git a/src/main/java/org/jabref/cli/ArgumentProcessor.java b/src/main/java/org/jabref/cli/ArgumentProcessor.java index adbf9be7f42..74dff0305b9 100644 --- a/src/main/java/org/jabref/cli/ArgumentProcessor.java +++ b/src/main/java/org/jabref/cli/ArgumentProcessor.java @@ -1,7 +1,7 @@ package org.jabref.cli; import java.io.IOException; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; @@ -236,7 +236,6 @@ private List processArguments() { if (!loaded.isEmpty()) { writeMetadatatoPdf(loaded, cli.getWriteMetadatatoPdf(), - preferencesService.getGeneralPreferences().getDefaultEncoding(), preferencesService.getXmpPreferences(), preferencesService.getFilePreferences(), preferencesService.getGeneralPreferences().getDefaultBibDatabaseMode(), @@ -271,7 +270,7 @@ private List processArguments() { return loaded; } - private void writeMetadatatoPdf(List loaded, String filesAndCitekeys, Charset encoding, XmpPreferences xmpPreferences, FilePreferences filePreferences, BibDatabaseMode databaseMode, BibEntryTypesManager entryTypesManager, FieldWriterPreferences fieldWriterPreferences, boolean writeXMP, boolean embeddBibfile) { + private void writeMetadatatoPdf(List loaded, String filesAndCitekeys, XmpPreferences xmpPreferences, FilePreferences filePreferences, BibDatabaseMode databaseMode, BibEntryTypesManager entryTypesManager, FieldWriterPreferences fieldWriterPreferences, boolean writeXMP, boolean embeddBibfile) { if (loaded.isEmpty()) { LOGGER.error("The write xmp option depends on a valid import option."); return; @@ -285,7 +284,7 @@ private void writeMetadatatoPdf(List loaded, String filesAndCiteke if ("all".equals(filesAndCitekeys)) { for (BibEntry entry : dataBase.getEntries()) { - writeMetadatatoPDFsOfEntry(databaseContext, entry.getCitationKey().orElse(""), entry, encoding, filePreferences, xmpPdfExporter, embeddedBibFilePdfExporter, writeXMP, embeddBibfile); + writeMetadatatoPDFsOfEntry(databaseContext, entry.getCitationKey().orElse(""), entry, filePreferences, xmpPdfExporter, embeddedBibFilePdfExporter, writeXMP, embeddBibfile); } return; } @@ -300,22 +299,22 @@ private void writeMetadatatoPdf(List loaded, String filesAndCiteke } } - writeMetadatatoPdfByCitekey(databaseContext, dataBase, citeKeys, encoding, filePreferences, xmpPdfExporter, embeddedBibFilePdfExporter, writeXMP, embeddBibfile); - writeMetadatatoPdfByFileNames(databaseContext, dataBase, pdfs, encoding, filePreferences, xmpPdfExporter, embeddedBibFilePdfExporter, writeXMP, embeddBibfile); + writeMetadatatoPdfByCitekey(databaseContext, dataBase, citeKeys, filePreferences, xmpPdfExporter, embeddedBibFilePdfExporter, writeXMP, embeddBibfile); + writeMetadatatoPdfByFileNames(databaseContext, dataBase, pdfs, filePreferences, xmpPdfExporter, embeddedBibFilePdfExporter, writeXMP, embeddBibfile); } - private void writeMetadatatoPDFsOfEntry(BibDatabaseContext databaseContext, String citeKey, BibEntry entry, Charset encoding, FilePreferences filePreferences, XmpPdfExporter xmpPdfExporter, EmbeddedBibFilePdfExporter embeddedBibFilePdfExporter, boolean writeXMP, boolean embeddBibfile) { + private void writeMetadatatoPDFsOfEntry(BibDatabaseContext databaseContext, String citeKey, BibEntry entry, FilePreferences filePreferences, XmpPdfExporter xmpPdfExporter, EmbeddedBibFilePdfExporter embeddedBibFilePdfExporter, boolean writeXMP, boolean embeddBibfile) { try { if (writeXMP) { - if (xmpPdfExporter.exportToAllFilesOfEntry(databaseContext, encoding, filePreferences, entry, List.of(entry))) { + if (xmpPdfExporter.exportToAllFilesOfEntry(databaseContext, filePreferences, entry, List.of(entry))) { System.out.printf("Successfully written XMP metadata on at least one linked file of %s%n", citeKey); } else { System.err.printf("Cannot write XMP metadata on any linked files of %s. Make sure there is at least one linked file and the path is correct.%n", citeKey); } } if (embeddBibfile) { - if (embeddedBibFilePdfExporter.exportToAllFilesOfEntry(databaseContext, encoding, filePreferences, entry, List.of(entry))) { + if (embeddedBibFilePdfExporter.exportToAllFilesOfEntry(databaseContext, filePreferences, entry, List.of(entry))) { System.out.printf("Successfully embedded metadata on at least one linked file of %s%n", citeKey); } else { System.out.printf("Cannot embedd metadata on any linked files of %s. Make sure there is at least one linked file and the path is correct.%n", citeKey); @@ -326,7 +325,7 @@ private void writeMetadatatoPDFsOfEntry(BibDatabaseContext databaseContext, Stri } } - private void writeMetadatatoPdfByCitekey(BibDatabaseContext databaseContext, BibDatabase dataBase, Vector citeKeys, Charset encoding, FilePreferences filePreferences, XmpPdfExporter xmpPdfExporter, EmbeddedBibFilePdfExporter embeddedBibFilePdfExporter, boolean writeXMP, boolean embeddBibfile) { + private void writeMetadatatoPdfByCitekey(BibDatabaseContext databaseContext, BibDatabase dataBase, Vector citeKeys, FilePreferences filePreferences, XmpPdfExporter xmpPdfExporter, EmbeddedBibFilePdfExporter embeddedBibFilePdfExporter, boolean writeXMP, boolean embeddBibfile) { for (String citeKey : citeKeys) { List bibEntryList = dataBase.getEntriesByCitationKey(citeKey); if (bibEntryList.isEmpty()) { @@ -334,12 +333,12 @@ private void writeMetadatatoPdfByCitekey(BibDatabaseContext databaseContext, Bib continue; } for (BibEntry entry : bibEntryList) { - writeMetadatatoPDFsOfEntry(databaseContext, citeKey, entry, encoding, filePreferences, xmpPdfExporter, embeddedBibFilePdfExporter, writeXMP, embeddBibfile); + writeMetadatatoPDFsOfEntry(databaseContext, citeKey, entry, filePreferences, xmpPdfExporter, embeddedBibFilePdfExporter, writeXMP, embeddBibfile); } } } - private void writeMetadatatoPdfByFileNames(BibDatabaseContext databaseContext, BibDatabase dataBase, Vector fileNames, Charset encoding, FilePreferences filePreferences, XmpPdfExporter xmpPdfExporter, EmbeddedBibFilePdfExporter embeddedBibFilePdfExporter, boolean writeXMP, boolean embeddBibfile) { + private void writeMetadatatoPdfByFileNames(BibDatabaseContext databaseContext, BibDatabase dataBase, Vector fileNames, FilePreferences filePreferences, XmpPdfExporter xmpPdfExporter, EmbeddedBibFilePdfExporter embeddedBibFilePdfExporter, boolean writeXMP, boolean embeddBibfile) { for (String fileName : fileNames) { Path filePath = Path.of(fileName); if (!filePath.isAbsolute()) { @@ -348,14 +347,14 @@ private void writeMetadatatoPdfByFileNames(BibDatabaseContext databaseContext, B if (Files.exists(filePath)) { try { if (writeXMP) { - if (xmpPdfExporter.exportToFileByPath(databaseContext, dataBase, encoding, filePreferences, filePath)) { + if (xmpPdfExporter.exportToFileByPath(databaseContext, dataBase, filePreferences, filePath)) { System.out.printf("Successfully written XMP metadata of at least one entry to %s%n", fileName); } else { System.out.printf("File %s is not linked to any entry in database.%n", fileName); } } if (embeddBibfile) { - if (embeddedBibFilePdfExporter.exportToFileByPath(databaseContext, dataBase, encoding, filePreferences, filePath)) { + if (embeddedBibFilePdfExporter.exportToFileByPath(databaseContext, dataBase, filePreferences, filePath)) { System.out.printf("Successfully embedded XMP metadata of at least one entry to %s%n", fileName); } else { System.out.printf("File %s is not linked to any entry in database.%n", fileName); @@ -410,9 +409,7 @@ private boolean exportMatches(List loaded) { // We have an TemplateExporter instance: try { System.out.println(Localization.lang("Exporting") + ": " + data[1]); - exporter.get().export(databaseContext, Path.of(data[1]), - databaseContext.getMetaData().getEncoding().orElse(preferencesService.getGeneralPreferences().getDefaultEncoding()), - matches); + exporter.get().export(databaseContext, Path.of(data[1]), matches); } catch (Exception ex) { System.err.println(Localization.lang("Could not export file") + " '" + data[1] + "': " + Throwables.getStackTraceAsString(ex)); @@ -455,7 +452,6 @@ private List importAndOpenFiles() { try { pr = OpenDatabase.loadDatabase( Path.of(aLeftOver), - preferencesService.getGeneralPreferences(), preferencesService.getImportFormatPreferences(), Globals.getFileUpdateMonitor()); } catch (IOException ex) { @@ -530,7 +526,7 @@ private void saveDatabase(BibDatabase newBase, String subName) { System.out.println(Localization.lang("Saving") + ": " + subName); GeneralPreferences generalPreferences = preferencesService.getGeneralPreferences(); SavePreferences savePreferences = preferencesService.getSavePreferences(); - AtomicFileWriter fileWriter = new AtomicFileWriter(Path.of(subName), generalPreferences.getDefaultEncoding()); + AtomicFileWriter fileWriter = new AtomicFileWriter(Path.of(subName), StandardCharsets.UTF_8); BibWriter bibWriter = new BibWriter(fileWriter, OS.NEWLINE); BibDatabaseWriter databaseWriter = new BibtexDatabaseWriter(bibWriter, generalPreferences, savePreferences, Globals.entryTypesManager); databaseWriter.saveDatabase(new BibDatabaseContext(newBase)); @@ -539,9 +535,8 @@ private void saveDatabase(BibDatabase newBase, String subName) { if (fileWriter.hasEncodingProblems()) { System.err.println(Localization.lang("Warning") + ": " + Localization.lang( - "The chosen encoding '%0' could not encode the following characters:", - generalPreferences.getDefaultEncoding().displayName()) - + " " + fileWriter.getEncodingProblems()); + "UTF-8 could not be used to encode the following characters:" + + " " + fileWriter.getEncodingProblems())); } } catch (IOException ex) { System.err.println(Localization.lang("Could not save file.") + "\n" + ex.getLocalizedMessage()); @@ -581,8 +576,6 @@ private void exportFile(List loaded, String[] data) { // We have an exporter: try { exporter.get().export(pr.getDatabaseContext(), Path.of(data[0]), - pr.getDatabaseContext().getMetaData().getEncoding() - .orElse(preferencesService.getGeneralPreferences().getDefaultEncoding()), pr.getDatabaseContext().getDatabase().getEntries()); } catch (Exception ex) { System.err.println(Localization.lang("Could not export file") + " '" + data[0] + "': " diff --git a/src/main/java/org/jabref/gui/JabRefFrame.java b/src/main/java/org/jabref/gui/JabRefFrame.java index d0f6b9a27bf..d351cc6c5ec 100644 --- a/src/main/java/org/jabref/gui/JabRefFrame.java +++ b/src/main/java/org/jabref/gui/JabRefFrame.java @@ -836,7 +836,7 @@ private MenuBar createMenu() { new SeparatorMenuItem(), - factory.createMenuItem(StandardActions.WRITE_METADATA_TO_PDF, new WriteMetadataToPdfAction(stateManager, prefs.getGeneralPreferences().getDefaultBibDatabaseMode(), Globals.entryTypesManager, prefs.getFieldWriterPreferences(), dialogService, taskExecutor, prefs.getFilePreferences(), prefs.getXmpPreferences(), prefs.getGeneralPreferences().getDefaultEncoding())), + factory.createMenuItem(StandardActions.WRITE_METADATA_TO_PDF, new WriteMetadataToPdfAction(stateManager, prefs.getGeneralPreferences().getDefaultBibDatabaseMode(), Globals.entryTypesManager, prefs.getFieldWriterPreferences(), dialogService, taskExecutor, prefs.getFilePreferences(), prefs.getXmpPreferences())), factory.createMenuItem(StandardActions.COPY_LINKED_FILES, new CopyFilesAction(dialogService, prefs, stateManager)), new SeparatorMenuItem(), diff --git a/src/main/java/org/jabref/gui/JabRefGUI.java b/src/main/java/org/jabref/gui/JabRefGUI.java index b2ad32cde07..aeb20bdbd92 100644 --- a/src/main/java/org/jabref/gui/JabRefGUI.java +++ b/src/main/java/org/jabref/gui/JabRefGUI.java @@ -279,7 +279,6 @@ private void openLastEditedDatabases() { try { parsedDatabase = OpenDatabase.loadDatabase( dbFile, - preferencesService.getGeneralPreferences(), preferencesService.getImportFormatPreferences(), Globals.getFileUpdateMonitor()); } catch (IOException ex) { diff --git a/src/main/java/org/jabref/gui/collab/ChangeScanner.java b/src/main/java/org/jabref/gui/collab/ChangeScanner.java index cf76d3b7c41..f19f406e18f 100644 --- a/src/main/java/org/jabref/gui/collab/ChangeScanner.java +++ b/src/main/java/org/jabref/gui/collab/ChangeScanner.java @@ -55,7 +55,7 @@ public List scanForChanges() { // Important: apply all post-load actions ImportFormatPreferences importFormatPreferences = preferencesService.getImportFormatPreferences(); GeneralPreferences generalPreferences = preferencesService.getGeneralPreferences(); - ParserResult result = OpenDatabase.loadDatabase(database.getDatabasePath().get(), generalPreferences, importFormatPreferences, new DummyFileUpdateMonitor()); + ParserResult result = OpenDatabase.loadDatabase(database.getDatabasePath().get(), importFormatPreferences, new DummyFileUpdateMonitor()); BibDatabaseContext databaseOnDisk = result.getDatabaseContext(); // Start looking at changes. diff --git a/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java b/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java index 1b8cd4b4846..187328a02cd 100644 --- a/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java +++ b/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java @@ -370,8 +370,7 @@ private void setupToolBar() { preferencesService.getImporterPreferences(), preferencesService.getImportFormatPreferences(), preferencesService.getFilePreferences(), - databaseContext, - preferencesService.getGeneralPreferences().getDefaultEncoding()); + databaseContext); for (EntryBasedFetcher fetcher : entryBasedFetchers) { MenuItem fetcherMenuItem = new MenuItem(fetcher.getName()); if (fetcher instanceof PdfMergeMetadataImporter.EntryBasedFetcherWrapper) { @@ -383,8 +382,7 @@ private void setupToolBar() { preferencesService.getImporterPreferences(), preferencesService.getImportFormatPreferences(), preferencesService.getFilePreferences(), - databaseContext, - preferencesService.getGeneralPreferences().getDefaultEncoding()); + databaseContext); fetchAndMerge(pdfMergeMetadataImporter); }); } else { diff --git a/src/main/java/org/jabref/gui/exporter/ExportCommand.java b/src/main/java/org/jabref/gui/exporter/ExportCommand.java index 4511c7107ac..9d49e62652e 100644 --- a/src/main/java/org/jabref/gui/exporter/ExportCommand.java +++ b/src/main/java/org/jabref/gui/exporter/ExportCommand.java @@ -118,10 +118,6 @@ private void export(Path file, FileChooser.ExtensionFilter selectedExtensionFilt .wrap(() -> { format.export(stateManager.getActiveDatabase().get(), file, - stateManager.getActiveDatabase().get() - .getMetaData() - .getEncoding() - .orElse(preferences.getGeneralPreferences().getDefaultEncoding()), finEntries); return null; // can not use BackgroundTask.wrap(Runnable) because Runnable.run() can't throw Exceptions }) diff --git a/src/main/java/org/jabref/gui/exporter/ExportToClipboardAction.java b/src/main/java/org/jabref/gui/exporter/ExportToClipboardAction.java index 80247e2c335..3407c25c153 100644 --- a/src/main/java/org/jabref/gui/exporter/ExportToClipboardAction.java +++ b/src/main/java/org/jabref/gui/exporter/ExportToClipboardAction.java @@ -1,14 +1,11 @@ package org.jabref.gui.exporter; -import java.io.BufferedReader; import java.io.IOException; -import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.Comparator; import java.util.List; -import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; @@ -26,7 +23,6 @@ import org.jabref.logic.exporter.ExporterFactory; import org.jabref.logic.l10n.Localization; import org.jabref.logic.util.FileType; -import org.jabref.logic.util.OS; import org.jabref.logic.util.StandardFileType; import org.jabref.model.entry.BibEntry; import org.jabref.preferences.PreferencesService; @@ -124,14 +120,10 @@ private ExportResult exportToClipboard(Exporter exporter) throws Exception { // Write to file: exporter.export(stateManager.getActiveDatabase().get(), tmp, - stateManager.getActiveDatabase().get() - .getMetaData() - .getEncoding() - .orElse(preferences.getGeneralPreferences().getDefaultEncoding()), entries); // Read the file and put the contents on the clipboard: - return new ExportResult(readFileToString(tmp), exporter.getFileType()); + return new ExportResult(Files.readString(tmp), exporter.getFileType()); } finally { // Clean up: if ((tmp != null) && Files.exists(tmp)) { @@ -160,18 +152,6 @@ private void setContentToClipboard(ExportResult result) { dialogService.notify(Localization.lang("Entries exported to clipboard") + ": " + entries.size()); } - private String readFileToString(Path tmp) throws IOException { - Charset defaultEncoding = Objects.requireNonNull(preferences.getGeneralPreferences().getDefaultEncoding()); - try (BufferedReader reader = Files.newBufferedReader(tmp, stateManager.getActiveDatabase() - .map(db -> db.getMetaData() - .getEncoding() - .orElse(defaultEncoding)) - .orElse(defaultEncoding))) { - return reader.lines().collect(Collectors.joining(OS.NEWLINE)); - } - } - private record ExportResult(String content, FileType fileType) { - } } diff --git a/src/main/java/org/jabref/gui/exporter/SaveDatabaseAction.java b/src/main/java/org/jabref/gui/exporter/SaveDatabaseAction.java index c6bdf64e531..cc831776f6d 100644 --- a/src/main/java/org/jabref/gui/exporter/SaveDatabaseAction.java +++ b/src/main/java/org/jabref/gui/exporter/SaveDatabaseAction.java @@ -2,6 +2,7 @@ import java.io.IOException; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.nio.charset.UnsupportedCharsetException; import java.nio.file.Files; import java.nio.file.Path; @@ -92,7 +93,7 @@ public boolean saveAs(Path file) { public void saveSelectedAsPlain() { askForSavePath().ifPresent(path -> { try { - saveDatabase(path, true, preferences.getGeneralPreferences().getDefaultEncoding(), SavePreferences.DatabaseSaveType.PLAIN_BIBTEX); + saveDatabase(path, true, StandardCharsets.UTF_8, SavePreferences.DatabaseSaveType.PLAIN_BIBTEX); frame.getFileHistory().newFile(path); dialogService.notify(Localization.lang("Saved selected to '%0'.", path.toString())); } catch (SaveException ex) { @@ -203,7 +204,7 @@ private boolean save(Path targetPath, SaveDatabaseMode mode) { Charset encoding = libraryTab.getBibDatabaseContext() .getMetaData() .getEncoding() - .orElse(preferences.getGeneralPreferences().getDefaultEncoding()); + .orElse(StandardCharsets.UTF_8); // Make sure to remember which encoding we used. libraryTab.getBibDatabaseContext().getMetaData().setEncoding(encoding, ChangePropagation.DO_NOT_POST_EVENT); diff --git a/src/main/java/org/jabref/gui/exporter/WriteMetadataToPdfAction.java b/src/main/java/org/jabref/gui/exporter/WriteMetadataToPdfAction.java index 2e72706e0ba..4043dde2b46 100644 --- a/src/main/java/org/jabref/gui/exporter/WriteMetadataToPdfAction.java +++ b/src/main/java/org/jabref/gui/exporter/WriteMetadataToPdfAction.java @@ -1,6 +1,5 @@ package org.jabref.gui.exporter; -import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.util.Collection; @@ -50,7 +49,6 @@ public class WriteMetadataToPdfAction extends SimpleCommand { private final FilePreferences filePreferences; private final XmpPreferences xmpPreferences; private final EmbeddedBibFilePdfExporter embeddedBibExporter; - private final Charset encoding; private OptionsDialog optionsDialog; @@ -62,13 +60,12 @@ public class WriteMetadataToPdfAction extends SimpleCommand { private int entriesChanged; private int errors; - public WriteMetadataToPdfAction(StateManager stateManager, BibDatabaseMode databaseMode, BibEntryTypesManager entryTypesManager, FieldWriterPreferences fieldWriterPreferences, DialogService dialogService, TaskExecutor taskExecutor, FilePreferences filePreferences, XmpPreferences xmpPreferences, Charset encoding) { + public WriteMetadataToPdfAction(StateManager stateManager, BibDatabaseMode databaseMode, BibEntryTypesManager entryTypesManager, FieldWriterPreferences fieldWriterPreferences, DialogService dialogService, TaskExecutor taskExecutor, FilePreferences filePreferences, XmpPreferences xmpPreferences) { this.stateManager = stateManager; this.dialogService = dialogService; this.taskExecutor = taskExecutor; this.filePreferences = filePreferences; this.xmpPreferences = xmpPreferences; - this.encoding = encoding; this.embeddedBibExporter = new EmbeddedBibFilePdfExporter(databaseMode, entryTypesManager, fieldWriterPreferences); this.executable.bind(needsDatabase(stateManager)); @@ -191,12 +188,12 @@ private void writeMetadata() { String.valueOf(entriesChanged), String.valueOf(skipped), String.valueOf(errors))); } + /** + * This writes both XMP data and embeddeds the .bib file + */ private void writeMetadataToFile(Path file, BibEntry entry, BibDatabaseContext databaseContext, BibDatabase database) throws Exception { - // XMP XmpUtilWriter.writeXmp(file, entry, database, xmpPreferences); - - // Embedded Bib File - embeddedBibExporter.exportToFileByPath(databaseContext, database, encoding, filePreferences, file); + embeddedBibExporter.exportToFileByPath(databaseContext, database, filePreferences, file); } class OptionsDialog extends FXDialog { diff --git a/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java b/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java index 50f27f6bb5e..63523c76d9b 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java +++ b/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java @@ -428,7 +428,7 @@ public void writeMetadataToPdf() { XmpUtilWriter.writeXmp(file.get(), entry, databaseContext.getDatabase(), preferences.getXmpPreferences()); EmbeddedBibFilePdfExporter embeddedBibExporter = new EmbeddedBibFilePdfExporter(preferences.getGeneralPreferences().getDefaultBibDatabaseMode(), Globals.entryTypesManager, preferences.getFieldWriterPreferences()); - embeddedBibExporter.exportToFileByPath(databaseContext, databaseContext.getDatabase(), preferences.getGeneralPreferences().getDefaultEncoding(), preferences.getFilePreferences(), file.get()); + embeddedBibExporter.exportToFileByPath(databaseContext, databaseContext.getDatabase(), preferences.getFilePreferences(), file.get()); } catch (IOException | TransformerException ex) { // TODO: Print error message // Localization.lang("Error while writing") + " '" + file.toString() + "': " + ex; @@ -584,7 +584,7 @@ public void parsePdfMetadataAndShowMergeDialog() { private Supplier wrapImporterToSupplier(Importer importer, Path filePath) { return () -> { try { - ParserResult parserResult = importer.importDatabase(filePath, preferences.getGeneralPreferences().getDefaultEncoding()); + ParserResult parserResult = importer.importDatabase(filePath); if (parserResult.isInvalid() || parserResult.isEmpty() || !parserResult.getDatabase().hasEntries()) { return null; } diff --git a/src/main/java/org/jabref/gui/importer/ImportAction.java b/src/main/java/org/jabref/gui/importer/ImportAction.java index a4eeea9e99a..aa63222685e 100644 --- a/src/main/java/org/jabref/gui/importer/ImportAction.java +++ b/src/main/java/org/jabref/gui/importer/ImportAction.java @@ -132,7 +132,7 @@ private List doImport(List files) frame.getDialogService().notify(Localization.lang("Importing in %0 format", importer.get().getName()) + "..."); }); // Specific importer: - ParserResult pr = importer.get().importDatabase(filename, prefs.getGeneralPreferences().getDefaultEncoding()); + ParserResult pr = importer.get().importDatabase(filename); imports.add(new ImportFormatReader.UnknownFormatImport(importer.get().getName(), pr)); } } catch (ImportException | IOException e) { diff --git a/src/main/java/org/jabref/gui/importer/actions/OpenDatabaseAction.java b/src/main/java/org/jabref/gui/importer/actions/OpenDatabaseAction.java index c79bc929950..6cc24a8f005 100644 --- a/src/main/java/org/jabref/gui/importer/actions/OpenDatabaseAction.java +++ b/src/main/java/org/jabref/gui/importer/actions/OpenDatabaseAction.java @@ -198,7 +198,6 @@ private ParserResult loadDatabase(Path file) throws Exception { ParserResult result; try { result = OpenDatabase.loadDatabase(fileToLoad, - preferencesService.getGeneralPreferences(), preferencesService.getImportFormatPreferences(), Globals.getFileUpdateMonitor()); } catch (IOException e) { diff --git a/src/main/java/org/jabref/gui/integrity/IntegrityCheckAction.java b/src/main/java/org/jabref/gui/integrity/IntegrityCheckAction.java index e0bda0f8d33..809a2020c78 100644 --- a/src/main/java/org/jabref/gui/integrity/IntegrityCheckAction.java +++ b/src/main/java/org/jabref/gui/integrity/IntegrityCheckAction.java @@ -42,7 +42,6 @@ public void execute() { IntegrityCheck check = new IntegrityCheck(database, Globals.prefs.getFilePreferences(), Globals.prefs.getCitationKeyPatternPreferences(), - Globals.prefs.getGeneralPreferences().getDefaultEncoding(), Globals.journalAbbreviationRepository, Globals.prefs.getEntryEditorPreferences().shouldAllowIntegerEditionBibtex()); diff --git a/src/main/java/org/jabref/gui/libraryproperties/general/GeneralPropertiesViewModel.java b/src/main/java/org/jabref/gui/libraryproperties/general/GeneralPropertiesViewModel.java index 9fbdf98c565..65dc1d0b9ef 100644 --- a/src/main/java/org/jabref/gui/libraryproperties/general/GeneralPropertiesViewModel.java +++ b/src/main/java/org/jabref/gui/libraryproperties/general/GeneralPropertiesViewModel.java @@ -1,6 +1,7 @@ package org.jabref.gui.libraryproperties.general; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.nio.file.Path; import javax.swing.undo.UndoManager; @@ -62,7 +63,7 @@ public void setValues() { boolean isShared = (databaseContext.getLocation() == DatabaseLocation.SHARED); encodingDisableProperty.setValue(isShared); // the encoding of shared database is always UTF-8 - selectedEncodingProperty.setValue(metaData.getEncoding().orElse(preferencesService.getGeneralPreferences().getDefaultEncoding())); + selectedEncodingProperty.setValue(metaData.getEncoding().orElse(StandardCharsets.UTF_8)); selectedDatabaseModeProperty.setValue(metaData.getMode().orElse(BibDatabaseMode.BIBLATEX)); generalFileDirectoryProperty.setValue(metaData.getDefaultFileDirectory().orElse("").trim()); userSpecificFileDirectoryProperty.setValue(metaData.getUserFileDirectory(preferencesService.getFilePreferences().getUser()).orElse("").trim()); diff --git a/src/main/java/org/jabref/gui/openoffice/OpenOfficePanel.java b/src/main/java/org/jabref/gui/openoffice/OpenOfficePanel.java index 681e16ad424..662a4aac69e 100644 --- a/src/main/java/org/jabref/gui/openoffice/OpenOfficePanel.java +++ b/src/main/java/org/jabref/gui/openoffice/OpenOfficePanel.java @@ -142,8 +142,7 @@ public OpenOfficePanel(PreferencesService preferencesService, update.setMaxWidth(Double.MAX_VALUE); loader = new StyleLoader(ooPrefs, - preferencesService.getLayoutFormatterPreferences(Globals.journalAbbreviationRepository), - preferencesService.getGeneralPreferences().getDefaultEncoding()); + preferencesService.getLayoutFormatterPreferences(Globals.journalAbbreviationRepository)); initPanel(); } diff --git a/src/main/java/org/jabref/gui/preferences/general/GeneralTab.fxml b/src/main/java/org/jabref/gui/preferences/general/GeneralTab.fxml index 0fe621f75c2..73a010b459a 100644 --- a/src/main/java/org/jabref/gui/preferences/general/GeneralTab.fxml +++ b/src/main/java/org/jabref/gui/preferences/general/GeneralTab.fxml @@ -27,8 +27,6 @@