From d7b38bd8a6f2eaa65df52b1ce827a8419502ce1c Mon Sep 17 00:00:00 2001 From: Tobias Diez Date: Fri, 17 Jan 2020 20:18:34 +0100 Subject: [PATCH 1/8] Always remember which encoding was used for saving --- .../gui/exporter/SaveDatabaseAction.java | 18 ++++++++++-------- .../org/jabref/logic/importer/Importer.java | 3 +-- .../model/database/BibDatabaseContext.java | 6 +++--- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/jabref/gui/exporter/SaveDatabaseAction.java b/src/main/java/org/jabref/gui/exporter/SaveDatabaseAction.java index f131795a608..0a8f5b3ce28 100644 --- a/src/main/java/org/jabref/gui/exporter/SaveDatabaseAction.java +++ b/src/main/java/org/jabref/gui/exporter/SaveDatabaseAction.java @@ -114,10 +114,10 @@ private void saveWithDifferentEncoding(Path file, boolean selectedOnly, Charset if (saveWithDifferentEncoding) { Optional newEncoding = frame.getDialogService().showChoiceDialogAndWait(Localization.lang("Save library"), Localization.lang("Select new encoding"), Localization.lang("Save library"), encoding, Encodings.getCharsets()); if (newEncoding.isPresent()) { - saveDatabase(file, selectedOnly, newEncoding.get(), saveType); - // Make sure to remember which encoding we used. panel.getBibDatabaseContext().getMetaData().setEncoding(newEncoding.get(), ChangePropagation.DO_NOT_POST_EVENT); + + saveDatabase(file, selectedOnly, newEncoding.get(), saveType); } } } @@ -126,13 +126,15 @@ private boolean doSave() { panel.setSaving(true); Path targetPath = panel.getBibDatabaseContext().getDatabasePath().get(); try { + Charset encoding = panel.getBibDatabaseContext() + .getMetaData() + .getEncoding() + .orElse(prefs.getDefaultEncoding()); + // Make sure to remember which encoding we used. + panel.getBibDatabaseContext().getMetaData().setEncoding(encoding, ChangePropagation.DO_NOT_POST_EVENT); + // Save the database - boolean success = saveDatabase(targetPath, false, - panel.getBibDatabaseContext() - .getMetaData() - .getEncoding() - .orElse(prefs.getDefaultEncoding()), - SavePreferences.DatabaseSaveType.ALL); + boolean success = saveDatabase(targetPath, false, encoding, SavePreferences.DatabaseSaveType.ALL); if (success) { panel.getUndoManager().markUnchanged(); diff --git a/src/main/java/org/jabref/logic/importer/Importer.java b/src/main/java/org/jabref/logic/importer/Importer.java index 597a821ba2d..e3f7a84a51f 100644 --- a/src/main/java/org/jabref/logic/importer/Importer.java +++ b/src/main/java/org/jabref/logic/importer/Importer.java @@ -104,8 +104,7 @@ public ParserResult importDatabase(Path filePath, Charset encoding) throws IOExc */ public ParserResult importDatabase(String data) throws IOException { try (StringReader stringReader = new StringReader(data); BufferedReader bufferedReader = new BufferedReader(stringReader)) { - ParserResult parserResult = importDatabase(bufferedReader); - return parserResult; + return importDatabase(bufferedReader); } } diff --git a/src/main/java/org/jabref/model/database/BibDatabaseContext.java b/src/main/java/org/jabref/model/database/BibDatabaseContext.java index 4e5a46e3dd1..d46411100e1 100644 --- a/src/main/java/org/jabref/model/database/BibDatabaseContext.java +++ b/src/main/java/org/jabref/model/database/BibDatabaseContext.java @@ -89,10 +89,10 @@ public BibDatabaseContext(BibDatabase database, MetaData metaData, Path file) { public BibDatabaseMode getMode() { Optional mode = metaData.getMode(); - if (!mode.isPresent()) { - BibDatabaseMode inferredMode = BibDatabaseModeDetection.inferMode(database); + if (mode.isEmpty()) { BibDatabaseMode newMode = BibDatabaseMode.BIBTEX; - if ((defaults.mode == BibDatabaseMode.BIBLATEX) || (inferredMode == BibDatabaseMode.BIBLATEX)) { + if ((defaults.mode == BibDatabaseMode.BIBLATEX) + || (BibDatabaseModeDetection.inferMode(database) == BibDatabaseMode.BIBLATEX)) { newMode = BibDatabaseMode.BIBLATEX; } this.setMode(newMode); From 785187350c5c45851651e1892dab956e8b14ccfa Mon Sep 17 00:00:00 2001 From: Tobias Diez Date: Fri, 17 Jan 2020 20:46:07 +0100 Subject: [PATCH 2/8] Already infer mode upon loading --- .../org/jabref/benchmarks/Benchmarks.java | 4 +- .../org/jabref/cli/ArgumentProcessor.java | 4 +- .../jabref/gui/auximport/FromAuxDialog.java | 4 +- .../gui/importer/NewDatabaseAction.java | 3 +- .../gui/openoffice/OpenOfficePanel.java | 5 +- .../gui/shared/SharedDatabaseUIManager.java | 10 ++-- .../org/jabref/logic/importer/Importer.java | 6 +++ src/main/java/org/jabref/model/Defaults.java | 17 ------- .../model/database/BibDatabaseContext.java | 47 ++++--------------- .../database/BibDatabaseModeDetection.java | 5 +- .../logic/cleanup/CleanupWorkerTest.java | 3 +- .../logic/cleanup/MoveFilesCleanupTest.java | 3 +- .../logic/cleanup/RenamePdfCleanupTest.java | 3 +- .../exporter/BibtexDatabaseWriterTest.java | 35 +++++--------- .../logic/integrity/IntegrityCheckTest.java | 7 ++- .../shared/SynchronizationTestSimulator.java | 6 +-- .../jabref/model/BibDatabaseContextTest.java | 4 +- 17 files changed, 46 insertions(+), 120 deletions(-) delete mode 100644 src/main/java/org/jabref/model/Defaults.java diff --git a/src/jmh/java/org/jabref/benchmarks/Benchmarks.java b/src/jmh/java/org/jabref/benchmarks/Benchmarks.java index d0a0704c5f6..a4d12e698ce 100644 --- a/src/jmh/java/org/jabref/benchmarks/Benchmarks.java +++ b/src/jmh/java/org/jabref/benchmarks/Benchmarks.java @@ -16,7 +16,6 @@ import org.jabref.logic.layout.format.HTMLChars; import org.jabref.logic.layout.format.LatexToUnicodeFormatter; import org.jabref.logic.search.SearchQuery; -import org.jabref.model.Defaults; import org.jabref.model.database.BibDatabase; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.database.BibDatabaseMode; @@ -76,8 +75,7 @@ public void init() throws Exception { private StringWriter getOutputWriter() throws IOException { StringWriter outputWriter = new StringWriter(); BibtexDatabaseWriter databaseWriter = new BibtexDatabaseWriter(outputWriter, mock(SavePreferences.class), new BibEntryTypesManager()); - databaseWriter.savePartOfDatabase( - new BibDatabaseContext(database, new MetaData(), new Defaults()), database.getEntries()); + databaseWriter.savePartOfDatabase(new BibDatabaseContext(database, new MetaData()), database.getEntries()); return outputWriter; } diff --git a/src/main/java/org/jabref/cli/ArgumentProcessor.java b/src/main/java/org/jabref/cli/ArgumentProcessor.java index a67f4a857cf..c9d01d6784d 100644 --- a/src/main/java/org/jabref/cli/ArgumentProcessor.java +++ b/src/main/java/org/jabref/cli/ArgumentProcessor.java @@ -44,7 +44,6 @@ import org.jabref.logic.shared.prefs.SharedDatabasePreferences; import org.jabref.logic.util.OS; import org.jabref.logic.xmp.XmpPreferences; -import org.jabref.model.Defaults; import org.jabref.model.database.BibDatabase; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.database.BibDatabaseMode; @@ -392,8 +391,7 @@ private void saveDatabase(BibDatabase newBase, String subName) { SavePreferences prefs = Globals.prefs.loadForSaveFromPreferences(); AtomicFileWriter fileWriter = new AtomicFileWriter(Paths.get(subName), prefs.getEncoding()); BibDatabaseWriter databaseWriter = new BibtexDatabaseWriter(fileWriter, prefs, Globals.entryTypesManager); - Defaults defaults = new Defaults(Globals.prefs.getDefaultBibDatabaseMode()); - databaseWriter.saveDatabase(new BibDatabaseContext(newBase, defaults)); + databaseWriter.saveDatabase(new BibDatabaseContext(newBase)); // Show just a warning message if encoding did not work for all characters: if (fileWriter.hasEncodingProblems()) { diff --git a/src/main/java/org/jabref/gui/auximport/FromAuxDialog.java b/src/main/java/org/jabref/gui/auximport/FromAuxDialog.java index 59e13b5c41e..d1043c6981d 100644 --- a/src/main/java/org/jabref/gui/auximport/FromAuxDialog.java +++ b/src/main/java/org/jabref/gui/auximport/FromAuxDialog.java @@ -20,7 +20,6 @@ import org.jabref.logic.auxparser.DefaultAuxParser; import org.jabref.logic.l10n.Localization; import org.jabref.logic.util.StandardFileType; -import org.jabref.model.Defaults; import org.jabref.model.auxparser.AuxParser; import org.jabref.model.auxparser.AuxParserResult; import org.jabref.model.database.BibDatabase; @@ -59,8 +58,7 @@ public FromAuxDialog(JabRefFrame frame) { generateButton.defaultButtonProperty().bind(generateButton.disableProperty().not()); setResultConverter(button -> { if (button == generateButtonType) { - Defaults defaults = new Defaults(Globals.prefs.getDefaultBibDatabaseMode()); - BasePanel bp = new BasePanel(frame, BasePanelPreferences.from(Globals.prefs), new BibDatabaseContext(auxParserResult.getGeneratedBibDatabase(), defaults), ExternalFileTypes.getInstance()); + BasePanel bp = new BasePanel(frame, BasePanelPreferences.from(Globals.prefs), new BibDatabaseContext(auxParserResult.getGeneratedBibDatabase()), ExternalFileTypes.getInstance()); frame.addTab(bp, true); } return null; diff --git a/src/main/java/org/jabref/gui/importer/NewDatabaseAction.java b/src/main/java/org/jabref/gui/importer/NewDatabaseAction.java index 9b0cf743395..553361ae451 100644 --- a/src/main/java/org/jabref/gui/importer/NewDatabaseAction.java +++ b/src/main/java/org/jabref/gui/importer/NewDatabaseAction.java @@ -2,7 +2,6 @@ import org.jabref.gui.JabRefFrame; import org.jabref.gui.actions.SimpleCommand; -import org.jabref.model.Defaults; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.database.BibDatabaseMode; @@ -21,7 +20,7 @@ public NewDatabaseAction(JabRefFrame jabRefFrame, BibDatabaseMode mode) { @Override public void execute() { - BibDatabaseContext bibDatabaseContext = new BibDatabaseContext(new Defaults(BibDatabaseMode.BIBTEX)); + BibDatabaseContext bibDatabaseContext = new BibDatabaseContext(); bibDatabaseContext.setMode(mode); jabRefFrame.addTab(bibDatabaseContext, true); } diff --git a/src/main/java/org/jabref/gui/openoffice/OpenOfficePanel.java b/src/main/java/org/jabref/gui/openoffice/OpenOfficePanel.java index e790ce3c4db..e235e401967 100644 --- a/src/main/java/org/jabref/gui/openoffice/OpenOfficePanel.java +++ b/src/main/java/org/jabref/gui/openoffice/OpenOfficePanel.java @@ -48,7 +48,6 @@ import org.jabref.logic.openoffice.StyleLoader; import org.jabref.logic.openoffice.UndefinedParagraphFormatException; import org.jabref.logic.util.io.FileUtil; -import org.jabref.model.Defaults; import org.jabref.model.database.BibDatabase; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.BibEntry; @@ -295,8 +294,7 @@ private void exportEntries() { } - Defaults defaults = new Defaults(jabRefPreferences.getDefaultBibDatabaseMode()); - BibDatabaseContext databaseContext = new BibDatabaseContext(newDatabase, defaults); + BibDatabaseContext databaseContext = new BibDatabaseContext(newDatabase); this.frame.addTab(databaseContext, true); } catch (BibEntryNotFoundException ex) { @@ -435,7 +433,6 @@ private List findOpenOfficeJars(Path configurationPath) throws IOException jarURLs.add((jarPath.get().toUri().toURL())); } return jarURLs; - } private OOBibBase createBibBase(List jarUrls) throws IOException, InvocationTargetException, IllegalAccessException, diff --git a/src/main/java/org/jabref/gui/shared/SharedDatabaseUIManager.java b/src/main/java/org/jabref/gui/shared/SharedDatabaseUIManager.java index b1ab7b06dd3..9931096eb66 100644 --- a/src/main/java/org/jabref/gui/shared/SharedDatabaseUIManager.java +++ b/src/main/java/org/jabref/gui/shared/SharedDatabaseUIManager.java @@ -28,9 +28,7 @@ import org.jabref.logic.shared.exception.InvalidDBMSConnectionPropertiesException; import org.jabref.logic.shared.exception.NotASharedDatabaseException; import org.jabref.logic.shared.prefs.SharedDatabasePreferences; -import org.jabref.model.Defaults; import org.jabref.model.database.BibDatabaseContext; -import org.jabref.model.database.BibDatabaseMode; import org.jabref.model.database.shared.DatabaseNotSupportedException; import org.jabref.model.database.shared.DatabaseSynchronizer; import org.jabref.model.entry.BibEntry; @@ -144,8 +142,8 @@ public void listen(SharedEntriesNotPresentEvent event) { public BasePanel openNewSharedDatabaseTab(DBMSConnectionProperties dbmsConnectionProperties) throws SQLException, DatabaseNotSupportedException, InvalidDBMSConnectionPropertiesException { - BibDatabaseMode selectedMode = Globals.prefs.getDefaultBibDatabaseMode(); - BibDatabaseContext bibDatabaseContext = new BibDatabaseContext(new Defaults(selectedMode)); + BibDatabaseContext bibDatabaseContext = new BibDatabaseContext(); + bibDatabaseContext.setMode(Globals.prefs.getDefaultBibDatabaseMode()); DBMSSynchronizer synchronizer = new DBMSSynchronizer(bibDatabaseContext, Globals.prefs.getKeywordDelimiter(), Globals.prefs.getKeyPattern(), Globals.getFileUpdateMonitor()); bibDatabaseContext.convertToSharedDatabase(synchronizer); @@ -169,8 +167,8 @@ public void openSharedDatabaseFromParserResult(ParserResult parserResult) String sharedDatabaseID = sharedDatabaseIDOptional.get(); DBMSConnectionProperties dbmsConnectionProperties = new DBMSConnectionProperties(new SharedDatabasePreferences(sharedDatabaseID)); - BibDatabaseMode selectedMode = Globals.prefs.getDefaultBibDatabaseMode(); - BibDatabaseContext bibDatabaseContext = new BibDatabaseContext(new Defaults(selectedMode)); + BibDatabaseContext bibDatabaseContext = new BibDatabaseContext(); + bibDatabaseContext.setMode(Globals.prefs.getDefaultBibDatabaseMode()); DBMSSynchronizer synchronizer = new DBMSSynchronizer(bibDatabaseContext, Globals.prefs.getKeywordDelimiter(), Globals.prefs.getKeyPattern(), Globals.getFileUpdateMonitor()); bibDatabaseContext.convertToSharedDatabase(synchronizer); diff --git a/src/main/java/org/jabref/logic/importer/Importer.java b/src/main/java/org/jabref/logic/importer/Importer.java index e3f7a84a51f..54822aeeab5 100644 --- a/src/main/java/org/jabref/logic/importer/Importer.java +++ b/src/main/java/org/jabref/logic/importer/Importer.java @@ -12,6 +12,7 @@ import java.util.Objects; import org.jabref.logic.util.FileType; +import org.jabref.model.database.BibDatabaseModeDetection; /** * Role of an importer for JabRef. @@ -88,6 +89,11 @@ public ParserResult importDatabase(Path filePath, Charset encoding) throws IOExc ParserResult parserResult = importDatabase(bufferedReader); parserResult.getMetaData().setEncoding(encoding); parserResult.setFile(filePath.toFile()); + + // Make sure the mode is always set + if (parserResult.getMetaData().getMode().isEmpty()) { + parserResult.getMetaData().setMode(BibDatabaseModeDetection.inferMode(parserResult.getDatabase())); + } return parserResult; } } diff --git a/src/main/java/org/jabref/model/Defaults.java b/src/main/java/org/jabref/model/Defaults.java deleted file mode 100644 index 1adb9c76952..00000000000 --- a/src/main/java/org/jabref/model/Defaults.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.jabref.model; - -import org.jabref.model.database.BibDatabaseMode; - -public class Defaults { - - public final BibDatabaseMode mode; - - public Defaults() { - this.mode = BibDatabaseMode.BIBTEX; - } - - public Defaults(BibDatabaseMode mode) { - this.mode = mode; - } - -} diff --git a/src/main/java/org/jabref/model/database/BibDatabaseContext.java b/src/main/java/org/jabref/model/database/BibDatabaseContext.java index d46411100e1..9cff25928fc 100644 --- a/src/main/java/org/jabref/model/database/BibDatabaseContext.java +++ b/src/main/java/org/jabref/model/database/BibDatabaseContext.java @@ -10,7 +10,6 @@ import java.util.Optional; import java.util.stream.Collectors; -import org.jabref.model.Defaults; import org.jabref.model.database.event.CoarseChangeFilter; import org.jabref.model.database.shared.DatabaseLocation; import org.jabref.model.database.shared.DatabaseSynchronizer; @@ -27,7 +26,6 @@ public class BibDatabaseContext { private final BibDatabase database; - private final Defaults defaults; private MetaData metaData; /** @@ -40,36 +38,26 @@ public class BibDatabaseContext { private DatabaseLocation location; public BibDatabaseContext() { - this(new Defaults()); - } - - public BibDatabaseContext(Defaults defaults) { - this(new BibDatabase(), defaults); + this(new BibDatabase()); } public BibDatabaseContext(BibDatabase database) { - this(database, new Defaults()); - } - - public BibDatabaseContext(BibDatabase database, Defaults defaults) { - this(database, new MetaData(), defaults); + this(database, new MetaData()); } - public BibDatabaseContext(BibDatabase database, MetaData metaData, Defaults defaults) { - this.defaults = Objects.requireNonNull(defaults); + public BibDatabaseContext(BibDatabase database, MetaData metaData) { this.database = Objects.requireNonNull(database); this.metaData = Objects.requireNonNull(metaData); this.location = DatabaseLocation.LOCAL; this.file = Optional.empty(); } - public BibDatabaseContext(BibDatabase database, MetaData metaData) { - this(database, metaData, new Defaults()); + public BibDatabaseContext(BibDatabase database, MetaData metaData, Path file) { + this(database, metaData, file, DatabaseLocation.LOCAL); } - public BibDatabaseContext(BibDatabase database, MetaData metaData, Path file, Defaults defaults, - DatabaseLocation location) { - this(database, metaData, defaults); + public BibDatabaseContext(BibDatabase database, MetaData metaData, Path file, DatabaseLocation location) { + this(database, metaData); Objects.requireNonNull(location); this.file = Optional.ofNullable(file); @@ -78,27 +66,8 @@ public BibDatabaseContext(BibDatabase database, MetaData metaData, Path file, De } } - public BibDatabaseContext(BibDatabase database, MetaData metaData, Path file, Defaults defaults) { - this(database, metaData, file, defaults, DatabaseLocation.LOCAL); - } - - public BibDatabaseContext(BibDatabase database, MetaData metaData, Path file) { - this(database, metaData, file, new Defaults()); - } - public BibDatabaseMode getMode() { - Optional mode = metaData.getMode(); - - if (mode.isEmpty()) { - BibDatabaseMode newMode = BibDatabaseMode.BIBTEX; - if ((defaults.mode == BibDatabaseMode.BIBLATEX) - || (BibDatabaseModeDetection.inferMode(database) == BibDatabaseMode.BIBLATEX)) { - newMode = BibDatabaseMode.BIBLATEX; - } - this.setMode(newMode); - return newMode; - } - return mode.get(); + return metaData.getMode().orElse(BibDatabaseMode.BIBLATEX); } public void setMode(BibDatabaseMode bibDatabaseMode) { diff --git a/src/main/java/org/jabref/model/database/BibDatabaseModeDetection.java b/src/main/java/org/jabref/model/database/BibDatabaseModeDetection.java index 69472948e0a..fb1a3688a01 100644 --- a/src/main/java/org/jabref/model/database/BibDatabaseModeDetection.java +++ b/src/main/java/org/jabref/model/database/BibDatabaseModeDetection.java @@ -17,8 +17,7 @@ private BibDatabaseModeDetection() { * Only standard BibTex and Biblatex entry types are considered in the decision process. * * 1. Check if any of the entries is a type exclusive to Biblatex - * 2. Check if any exclusive Biblatex fields are present - * 3. Otherwise return BibTex + * 2. Otherwise return BibTex * * @param database a BibDatabase database * @return the inferred database type @@ -26,11 +25,9 @@ private BibDatabaseModeDetection() { public static BibDatabaseMode inferMode(BibDatabase database) { final Stream entryTypes = database.getEntries().stream().map(BibEntry::getType); - // type-based check if (entryTypes.anyMatch(EntryTypeFactory::isExclusiveBiblatex)) { return BibDatabaseMode.BIBLATEX; } else { - // field-based check return BibDatabaseMode.BIBTEX; } } diff --git a/src/test/java/org/jabref/logic/cleanup/CleanupWorkerTest.java b/src/test/java/org/jabref/logic/cleanup/CleanupWorkerTest.java index 009cce0cb2d..0e719bb2b3e 100644 --- a/src/test/java/org/jabref/logic/cleanup/CleanupWorkerTest.java +++ b/src/test/java/org/jabref/logic/cleanup/CleanupWorkerTest.java @@ -20,7 +20,6 @@ import org.jabref.logic.layout.LayoutFormatterPreferences; import org.jabref.logic.protectedterms.ProtectedTermsLoader; import org.jabref.logic.protectedterms.ProtectedTermsPreferences; -import org.jabref.model.Defaults; import org.jabref.model.FieldChange; import org.jabref.model.cleanup.FieldFormatterCleanup; import org.jabref.model.cleanup.FieldFormatterCleanups; @@ -59,7 +58,7 @@ void setUp(@TempDir Path bibFolder) throws IOException { MetaData metaData = new MetaData(); metaData.setDefaultFileDirectory(pdfFolder.getAbsolutePath()); - BibDatabaseContext context = new BibDatabaseContext(new BibDatabase(), metaData, new Defaults()); + BibDatabaseContext context = new BibDatabaseContext(new BibDatabase(), metaData); Files.createFile(bibFolder.resolve("test.bib")); context.setDatabaseFile(bibFolder.resolve("test.bib").toFile()); diff --git a/src/test/java/org/jabref/logic/cleanup/MoveFilesCleanupTest.java b/src/test/java/org/jabref/logic/cleanup/MoveFilesCleanupTest.java index 727da151fe0..f17c694528a 100644 --- a/src/test/java/org/jabref/logic/cleanup/MoveFilesCleanupTest.java +++ b/src/test/java/org/jabref/logic/cleanup/MoveFilesCleanupTest.java @@ -6,7 +6,6 @@ import java.util.Arrays; import java.util.Optional; -import org.jabref.model.Defaults; import org.jabref.model.database.BibDatabase; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.BibEntry; @@ -48,7 +47,7 @@ void setUp(@TempDir Path bibFolder) throws IOException { MetaData metaData = new MetaData(); metaData.setDefaultFileDirectory(defaultFileFolder.toAbsolutePath().toString()); - BibDatabaseContext databaseContext = new BibDatabaseContext(new BibDatabase(), metaData, new Defaults()); + BibDatabaseContext databaseContext = new BibDatabaseContext(new BibDatabase(), metaData); Files.createFile(bibFolder.resolve("test.bib")); databaseContext.setDatabaseFile(bibFolder.resolve("test.bib")); diff --git a/src/test/java/org/jabref/logic/cleanup/RenamePdfCleanupTest.java b/src/test/java/org/jabref/logic/cleanup/RenamePdfCleanupTest.java index 8196a580a43..ad01d555be2 100644 --- a/src/test/java/org/jabref/logic/cleanup/RenamePdfCleanupTest.java +++ b/src/test/java/org/jabref/logic/cleanup/RenamePdfCleanupTest.java @@ -6,7 +6,6 @@ import java.util.Arrays; import java.util.Optional; -import org.jabref.model.Defaults; import org.jabref.model.database.BibDatabase; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.BibEntry; @@ -35,7 +34,7 @@ class RenamePdfCleanupTest { void setUp(@TempDir Path testFolder) { Path path = testFolder.resolve("test.bib"); MetaData metaData = new MetaData(); - BibDatabaseContext context = new BibDatabaseContext(new BibDatabase(), metaData, new Defaults()); + BibDatabaseContext context = new BibDatabaseContext(new BibDatabase(), metaData); context.setDatabaseFile(path); entry = new BibEntry(); diff --git a/src/test/java/org/jabref/logic/exporter/BibtexDatabaseWriterTest.java b/src/test/java/org/jabref/logic/exporter/BibtexDatabaseWriterTest.java index 1d779dd50c0..381e120eb86 100644 --- a/src/test/java/org/jabref/logic/exporter/BibtexDatabaseWriterTest.java +++ b/src/test/java/org/jabref/logic/exporter/BibtexDatabaseWriterTest.java @@ -19,7 +19,6 @@ import org.jabref.logic.importer.ParserResult; import org.jabref.logic.importer.fileformat.BibtexParser; import org.jabref.logic.util.OS; -import org.jabref.model.Defaults; import org.jabref.model.bibtexkeypattern.AbstractBibtexKeyPattern; import org.jabref.model.bibtexkeypattern.DatabaseBibtexKeyPattern; import org.jabref.model.bibtexkeypattern.GlobalBibtexKeyPattern; @@ -81,7 +80,7 @@ void setUp() { database = new BibDatabase(); metaData = new MetaData(); - bibtexContext = new BibDatabaseContext(database, metaData, new Defaults(BibDatabaseMode.BIBTEX)); + bibtexContext = new BibDatabaseContext(database, metaData); importFormatPreferences = mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS); } @@ -288,7 +287,7 @@ void writeEntryWithCustomizedTypeAlsoWritesTypeDeclaration() throws Exception { + "@Comment{jabref-entrytype: customizedtype: req[author;date;title] opt[month;publisher;year]}" + OS.NEWLINE, stringWriter.toString()); } - + @Test void writeCustomizedTypesInAlphabeticalOrder() throws Exception { EntryType customizedType = new UnknownEntryType("customizedType"); @@ -329,8 +328,7 @@ void roundtripWithArticleMonths() throws Exception { when(preferences.getEncoding()).thenReturn(encoding); when(preferences.isSaveInOriginalOrder()).thenReturn(true); - BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData(), - new Defaults(BibDatabaseMode.BIBTEX)); + BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData()); databaseWriter.savePartOfDatabase(context, result.getDatabase().getEntries()); assertEquals(Files.readString(testBibtexFile, encoding), stringWriter.toString()); @@ -344,8 +342,7 @@ void roundtripWithComplexBib() throws Exception { when(preferences.getEncoding()).thenReturn(encoding); when(preferences.isSaveInOriginalOrder()).thenReturn(true); - BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData(), - new Defaults(BibDatabaseMode.BIBTEX)); + BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData()); databaseWriter.savePartOfDatabase(context, result.getDatabase().getEntries()); assertEquals(Files.readString(testBibtexFile, encoding), stringWriter.toString()); @@ -359,8 +356,7 @@ void roundtripWithUserComment() throws Exception { when(preferences.getEncoding()).thenReturn(encoding); when(preferences.isSaveInOriginalOrder()).thenReturn(true); - BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData(), - new Defaults(BibDatabaseMode.BIBTEX)); + BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData()); databaseWriter.savePartOfDatabase(context, result.getDatabase().getEntries()); assertEquals(Files.readString(testBibtexFile, encoding), stringWriter.toString()); @@ -377,8 +373,7 @@ void roundtripWithUserCommentAndEntryChange() throws Exception { when(preferences.getEncoding()).thenReturn(encoding); when(preferences.isSaveInOriginalOrder()).thenReturn(true); - BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData(), - new Defaults(BibDatabaseMode.BIBTEX)); + BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData()); databaseWriter.savePartOfDatabase(context, result.getDatabase().getEntries()); assertEquals(Files.readString(Paths.get("src/test/resources/testbib/bibWithUserCommentAndEntryChange.bib"), encoding), stringWriter.toString()); @@ -397,8 +392,7 @@ void roundtripWithUserCommentBeforeStringAndChange() throws Exception { when(preferences.getEncoding()).thenReturn(encoding); when(preferences.isSaveInOriginalOrder()).thenReturn(true); - BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData(), - new Defaults(BibDatabaseMode.BIBTEX)); + BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData()); databaseWriter.savePartOfDatabase(context, result.getDatabase().getEntries()); @@ -413,8 +407,7 @@ void roundtripWithUnknownMetaData() throws Exception { when(preferences.getEncoding()).thenReturn(encoding); when(preferences.isSaveInOriginalOrder()).thenReturn(true); - BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData(), - new Defaults(BibDatabaseMode.BIBTEX)); + BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData()); databaseWriter.savePartOfDatabase(context, result.getDatabase().getEntries()); assertEquals(Files.readString(testBibtexFile, encoding), stringWriter.toString()); @@ -664,8 +657,7 @@ void roundtripWithContentSelectorsAndUmlauts() throws Exception { when(preferences.getEncoding()).thenReturn(encoding); when(preferences.isSaveInOriginalOrder()).thenReturn(true); - BibDatabaseContext context = new BibDatabaseContext(firstParse.getDatabase(), firstParse.getMetaData(), - new Defaults(BibDatabaseMode.BIBTEX)); + BibDatabaseContext context = new BibDatabaseContext(firstParse.getDatabase(), firstParse.getMetaData()); databaseWriter.savePartOfDatabase(context, firstParse.getDatabase().getEntries()); @@ -692,8 +684,7 @@ public void saveAlsoSavesSecondModification() throws Exception { // modify entry entry.setField(StandardField.AUTHOR, "BlaBla"); - BibDatabaseContext context = new BibDatabaseContext(firstParse.getDatabase(), firstParse.getMetaData(), - new Defaults(BibDatabaseMode.BIBTEX)); + BibDatabaseContext context = new BibDatabaseContext(firstParse.getDatabase(), firstParse.getMetaData()); databaseWriter.savePartOfDatabase(context, firstParse.getDatabase().getEntries()); @@ -742,8 +733,7 @@ public void saveReturnsToOriginalEntryWhenEntryIsFlaggedUnchanged() throws Excep // write entry stringWriter = new StringWriter(); databaseWriter = new BibtexDatabaseWriter(stringWriter, preferences, entryTypesManager); - BibDatabaseContext context = new BibDatabaseContext(firstParse.getDatabase(), firstParse.getMetaData(), - new Defaults(BibDatabaseMode.BIBTEX)); + BibDatabaseContext context = new BibDatabaseContext(firstParse.getDatabase(), firstParse.getMetaData()); databaseWriter.savePartOfDatabase(context, firstParse.getDatabase().getEntries()); assertEquals(bibtexEntry + OS.NEWLINE + @@ -770,8 +760,7 @@ public void saveReturnsToOriginalEntryWhenEntryIsFlaggedUnchangedEvenInThePrecen // modify entry entry.setField(StandardField.AUTHOR, "BlaBla"); - BibDatabaseContext context = new BibDatabaseContext(firstParse.getDatabase(), firstParse.getMetaData(), - new Defaults(BibDatabaseMode.BIBTEX)); + BibDatabaseContext context = new BibDatabaseContext(firstParse.getDatabase(), firstParse.getMetaData()); databaseWriter.savePartOfDatabase(context, firstParse.getDatabase().getEntries()); diff --git a/src/test/java/org/jabref/logic/integrity/IntegrityCheckTest.java b/src/test/java/org/jabref/logic/integrity/IntegrityCheckTest.java index fbfb3f556c9..3ff92b7abb3 100644 --- a/src/test/java/org/jabref/logic/integrity/IntegrityCheckTest.java +++ b/src/test/java/org/jabref/logic/integrity/IntegrityCheckTest.java @@ -12,7 +12,6 @@ import org.jabref.logic.bibtexkeypattern.BibtexKeyPatternPreferences; import org.jabref.logic.journals.Abbreviation; import org.jabref.logic.journals.JournalAbbreviationRepository; -import org.jabref.model.Defaults; import org.jabref.model.bibtexkeypattern.GlobalBibtexKeyPattern; import org.jabref.model.database.BibDatabase; import org.jabref.model.database.BibDatabaseContext; @@ -343,7 +342,7 @@ void testEntryIsUnchangedAfterChecks() { BibDatabase bibDatabase = new BibDatabase(); bibDatabase.insertEntry(entry); - BibDatabaseContext context = new BibDatabaseContext(bibDatabase, new Defaults()); + BibDatabaseContext context = new BibDatabaseContext(bibDatabase); new IntegrityCheck(context, mock(FilePreferences.class), @@ -367,7 +366,7 @@ private BibDatabaseContext createContext(Field field, String value, EntryType ty entry.setType(type); BibDatabase bibDatabase = new BibDatabase(); bibDatabase.insertEntry(entry); - return new BibDatabaseContext(bibDatabase, new Defaults()); + return new BibDatabaseContext(bibDatabase); } private BibDatabaseContext createContext(Field field, String value, MetaData metaData) { @@ -375,7 +374,7 @@ private BibDatabaseContext createContext(Field field, String value, MetaData met entry.setField(field, value); BibDatabase bibDatabase = new BibDatabase(); bibDatabase.insertEntry(entry); - return new BibDatabaseContext(bibDatabase, metaData, new Defaults()); + return new BibDatabaseContext(bibDatabase, metaData); } private BibDatabaseContext createContext(Field field, String value) { diff --git a/src/test/java/org/jabref/logic/shared/SynchronizationTestSimulator.java b/src/test/java/org/jabref/logic/shared/SynchronizationTestSimulator.java index 6bda1fcb941..934b8762ed8 100644 --- a/src/test/java/org/jabref/logic/shared/SynchronizationTestSimulator.java +++ b/src/test/java/org/jabref/logic/shared/SynchronizationTestSimulator.java @@ -3,10 +3,8 @@ import java.sql.SQLException; import java.util.List; -import org.jabref.model.Defaults; import org.jabref.model.bibtexkeypattern.GlobalBibtexKeyPattern; import org.jabref.model.database.BibDatabaseContext; -import org.jabref.model.database.BibDatabaseMode; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.field.StandardField; import org.jabref.model.entry.field.UnknownField; @@ -49,12 +47,12 @@ public void setup() throws Exception { DBMSConnection dbmsConnection = TestConnector.getTestDBMSConnection(TestManager.getDBMSTypeTestParameter()); TestManager.clearTables(dbmsConnection); - clientContextA = new BibDatabaseContext(new Defaults(BibDatabaseMode.BIBTEX)); + clientContextA = new BibDatabaseContext(); DBMSSynchronizer synchronizerA = new DBMSSynchronizer(clientContextA, ',', pattern, new DummyFileUpdateMonitor()); clientContextA.convertToSharedDatabase(synchronizerA); clientContextA.getDBMSSynchronizer().openSharedDatabase(dbmsConnection); - clientContextB = new BibDatabaseContext(new Defaults(BibDatabaseMode.BIBTEX)); + clientContextB = new BibDatabaseContext(); DBMSSynchronizer synchronizerB = new DBMSSynchronizer(clientContextB, ',', pattern, new DummyFileUpdateMonitor()); clientContextB.convertToSharedDatabase(synchronizerB); // use a second connection, because this is another client (typically on another machine) diff --git a/src/test/java/org/jabref/model/BibDatabaseContextTest.java b/src/test/java/org/jabref/model/BibDatabaseContextTest.java index 09434f50c67..288efe5152c 100644 --- a/src/test/java/org/jabref/model/BibDatabaseContextTest.java +++ b/src/test/java/org/jabref/model/BibDatabaseContextTest.java @@ -14,7 +14,7 @@ public class BibDatabaseContextTest { @Test public void testTypeBasedOnDefaultBibtex() { - BibDatabaseContext bibDatabaseContext = new BibDatabaseContext(new BibDatabase(), new MetaData(), new Defaults(BibDatabaseMode.BIBTEX)); + BibDatabaseContext bibDatabaseContext = new BibDatabaseContext(new BibDatabase(), new MetaData()); assertEquals(BibDatabaseMode.BIBTEX, bibDatabaseContext.getMode()); bibDatabaseContext.setMode(BibDatabaseMode.BIBLATEX); @@ -23,7 +23,7 @@ public void testTypeBasedOnDefaultBibtex() { @Test public void testTypeBasedOnDefaultBiblatex() { - BibDatabaseContext bibDatabaseContext = new BibDatabaseContext(new BibDatabase(), new MetaData(), new Defaults(BibDatabaseMode.BIBLATEX)); + BibDatabaseContext bibDatabaseContext = new BibDatabaseContext(new BibDatabase(), new MetaData()); assertEquals(BibDatabaseMode.BIBLATEX, bibDatabaseContext.getMode()); bibDatabaseContext.setMode(BibDatabaseMode.BIBTEX); From fdfad7c8fc26da5f27b7560707670c1be7f7ccf5 Mon Sep 17 00:00:00 2001 From: Tobias Diez Date: Fri, 17 Jan 2020 22:07:51 +0100 Subject: [PATCH 3/8] Don't format field values upon writing --- .../java/org/jabref/gui/ClipBoardManager.java | 4 +- .../jabref/gui/entryeditor/EntryEditor.java | 2 +- .../entryeditor/EntryEditorPreferences.java | 12 +-- .../org/jabref/gui/entryeditor/SourceTab.java | 20 ++--- .../jabref/gui/worker/SendAsEMailAction.java | 5 +- .../jabref/logic/bibtex/BibEntryWriter.java | 23 +++--- ...exFieldFormatter.java => FieldWriter.java} | 36 +++------ ...ences.java => FieldWriterPreferences.java} | 8 +- .../logic/exporter/BibDatabaseWriter.java | 15 ++++ .../logic/exporter/BibtexDatabaseWriter.java | 11 ++- .../logic/exporter/SavePreferences.java | 22 +++--- .../NormalizeNewlinesFormatter.java | 47 ++++++++++++ .../jabref/logic/shared/DBMSSynchronizer.java | 16 ++-- .../jabref/preferences/JabRefPreferences.java | 74 +++++++++---------- .../jabref/gui/entryeditor/SourceTabTest.java | 4 +- .../gui/exporter/SaveDatabaseActionTest.java | 13 ++-- .../logic/bibtex/BibEntryWriterTest.java | 22 +----- ...matterTests.java => FieldWriterTests.java} | 62 ++++++++-------- .../exporter/BibtexDatabaseWriterTest.java | 19 ++++- 19 files changed, 230 insertions(+), 185 deletions(-) rename src/main/java/org/jabref/logic/bibtex/{LatexFieldFormatter.java => FieldWriter.java} (87%) rename src/main/java/org/jabref/logic/bibtex/{LatexFieldFormatterPreferences.java => FieldWriterPreferences.java} (78%) create mode 100644 src/main/java/org/jabref/logic/formatter/bibtexfields/NormalizeNewlinesFormatter.java rename src/test/java/org/jabref/logic/bibtex/{LatexFieldFormatterTests.java => FieldWriterTests.java} (50%) diff --git a/src/main/java/org/jabref/gui/ClipBoardManager.java b/src/main/java/org/jabref/gui/ClipBoardManager.java index 80f66951882..ba680353434 100644 --- a/src/main/java/org/jabref/gui/ClipBoardManager.java +++ b/src/main/java/org/jabref/gui/ClipBoardManager.java @@ -21,7 +21,7 @@ import org.jabref.Globals; import org.jabref.logic.bibtex.BibEntryWriter; -import org.jabref.logic.bibtex.LatexFieldFormatter; +import org.jabref.logic.bibtex.FieldWriter; import org.jabref.logic.importer.FetcherException; import org.jabref.logic.importer.ImportException; import org.jabref.logic.importer.ImportFormatReader; @@ -152,7 +152,7 @@ public void setContent(String string) { public void setContent(List entries) throws IOException { final ClipboardContent content = new ClipboardContent(); - BibEntryWriter writer = new BibEntryWriter(new LatexFieldFormatter(Globals.prefs.getLatexFieldFormatterPreferences()), Globals.entryTypesManager); + BibEntryWriter writer = new BibEntryWriter(new FieldWriter(Globals.prefs.getFieldWriterPreferences()), Globals.entryTypesManager); String serializedEntries = writer.serializeAll(entries, BibDatabaseMode.BIBTEX); content.put(DragAndDropDataFormats.ENTRIES, serializedEntries); content.putString(serializedEntries); diff --git a/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java b/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java index 2cda30e38e5..18fe1f0fd00 100644 --- a/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java +++ b/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java @@ -252,7 +252,7 @@ private List createTabs() { // Source tab sourceTab = new SourceTab(databaseContext, undoManager, - entryEditorPreferences.getLatexFieldFormatterPreferences(), + entryEditorPreferences.getFieldWriterPreferences(), entryEditorPreferences.getImportFormatPreferences(), fileMonitor, dialogService, stateManager, Globals.getKeyPrefs()); entryEditorTabs.add(sourceTab); diff --git a/src/main/java/org/jabref/gui/entryeditor/EntryEditorPreferences.java b/src/main/java/org/jabref/gui/entryeditor/EntryEditorPreferences.java index b102e04d9ed..26c4b48ef2a 100644 --- a/src/main/java/org/jabref/gui/entryeditor/EntryEditorPreferences.java +++ b/src/main/java/org/jabref/gui/entryeditor/EntryEditorPreferences.java @@ -5,7 +5,7 @@ import java.util.Set; import org.jabref.gui.keyboard.KeyBindingRepository; -import org.jabref.logic.bibtex.LatexFieldFormatterPreferences; +import org.jabref.logic.bibtex.FieldWriterPreferences; import org.jabref.logic.bibtexkeypattern.BibtexKeyPatternPreferences; import org.jabref.logic.importer.ImportFormatPreferences; import org.jabref.model.entry.field.Field; @@ -13,7 +13,7 @@ public class EntryEditorPreferences { private final Map> entryEditorTabList; - private final LatexFieldFormatterPreferences latexFieldFormatterPreferences; + private final FieldWriterPreferences fieldWriterPreferences; private final ImportFormatPreferences importFormatPreferences; private final BibtexKeyPatternPreferences bibtexKeyPatternPreferences; private final List customTabFieldNames; @@ -24,9 +24,9 @@ public class EntryEditorPreferences { private boolean avoidOverwritingCiteKey; private final boolean shouldShowLatexCitationsTab; - public EntryEditorPreferences(Map> entryEditorTabList, LatexFieldFormatterPreferences latexFieldFormatterPreferences, ImportFormatPreferences importFormatPreferences, List customTabFieldNames, boolean shouldShowRecommendationsTab, boolean isMrdlibAccepted, boolean shouldShowLatexCitationsTab, boolean showSourceTabByDefault, BibtexKeyPatternPreferences bibtexKeyPatternPreferences, KeyBindingRepository keyBindings, boolean avoidOverwritingCiteKey) { + public EntryEditorPreferences(Map> entryEditorTabList, FieldWriterPreferences fieldWriterPreferences, ImportFormatPreferences importFormatPreferences, List customTabFieldNames, boolean shouldShowRecommendationsTab, boolean isMrdlibAccepted, boolean shouldShowLatexCitationsTab, boolean showSourceTabByDefault, BibtexKeyPatternPreferences bibtexKeyPatternPreferences, KeyBindingRepository keyBindings, boolean avoidOverwritingCiteKey) { this.entryEditorTabList = entryEditorTabList; - this.latexFieldFormatterPreferences = latexFieldFormatterPreferences; + this.fieldWriterPreferences = fieldWriterPreferences; this.importFormatPreferences = importFormatPreferences; this.customTabFieldNames = customTabFieldNames; this.shouldShowRecommendationsTab = shouldShowRecommendationsTab; @@ -42,8 +42,8 @@ public Map> getEntryEditorTabList() { return entryEditorTabList; } - public LatexFieldFormatterPreferences getLatexFieldFormatterPreferences() { - return latexFieldFormatterPreferences; + public FieldWriterPreferences getFieldWriterPreferences() { + return fieldWriterPreferences; } public ImportFormatPreferences getImportFormatPreferences() { diff --git a/src/main/java/org/jabref/gui/entryeditor/SourceTab.java b/src/main/java/org/jabref/gui/entryeditor/SourceTab.java index ba563be84f4..8ac69be215b 100644 --- a/src/main/java/org/jabref/gui/entryeditor/SourceTab.java +++ b/src/main/java/org/jabref/gui/entryeditor/SourceTab.java @@ -33,9 +33,9 @@ import org.jabref.gui.undo.UndoableFieldChange; import org.jabref.gui.util.DefaultTaskExecutor; import org.jabref.logic.bibtex.BibEntryWriter; +import org.jabref.logic.bibtex.FieldWriter; +import org.jabref.logic.bibtex.FieldWriterPreferences; import org.jabref.logic.bibtex.InvalidFieldValueException; -import org.jabref.logic.bibtex.LatexFieldFormatter; -import org.jabref.logic.bibtex.LatexFieldFormatterPreferences; import org.jabref.logic.importer.ImportFormatPreferences; import org.jabref.logic.importer.ParserResult; import org.jabref.logic.importer.fileformat.BibtexParser; @@ -59,7 +59,7 @@ public class SourceTab extends EntryEditorTab { private static final Logger LOGGER = LoggerFactory.getLogger(SourceTab.class); - private final LatexFieldFormatterPreferences fieldFormatterPreferences; + private final FieldWriterPreferences fieldWriterPreferences; private final BibDatabaseMode mode; private final UndoManager undoManager; private final ObjectProperty sourceIsValid = new SimpleObjectProperty<>(); @@ -100,13 +100,13 @@ public void execute() { } } - public SourceTab(BibDatabaseContext bibDatabaseContext, CountingUndoManager undoManager, LatexFieldFormatterPreferences fieldFormatterPreferences, ImportFormatPreferences importFormatPreferences, FileUpdateMonitor fileMonitor, DialogService dialogService, StateManager stateManager, KeyBindingRepository keyBindingRepository) { + public SourceTab(BibDatabaseContext bibDatabaseContext, CountingUndoManager undoManager, FieldWriterPreferences fieldWriterPreferences, ImportFormatPreferences importFormatPreferences, FileUpdateMonitor fileMonitor, DialogService dialogService, StateManager stateManager, KeyBindingRepository keyBindingRepository) { this.mode = bibDatabaseContext.getMode(); this.setText(Localization.lang("%0 source", mode.getFormattedName())); this.setTooltip(new Tooltip(Localization.lang("Show/edit %0 source", mode.getFormattedName()))); this.setGraphic(IconTheme.JabRefIcons.SOURCE.getGraphicNode()); this.undoManager = undoManager; - this.fieldFormatterPreferences = fieldFormatterPreferences; + this.fieldWriterPreferences = fieldWriterPreferences; this.importFormatPreferences = importFormatPreferences; this.fileMonitor = fileMonitor; this.dialogService = dialogService; @@ -137,10 +137,10 @@ private void highlightSearchPattern() { } } - private static String getSourceString(BibEntry entry, BibDatabaseMode type, LatexFieldFormatterPreferences fieldFormatterPreferences) throws IOException { + private static String getSourceString(BibEntry entry, BibDatabaseMode type, FieldWriterPreferences fieldWriterPreferences) throws IOException { StringWriter stringWriter = new StringWriter(200); - LatexFieldFormatter formatter = LatexFieldFormatter.buildIgnoreHashes(fieldFormatterPreferences); - new BibEntryWriter(formatter, Globals.entryTypesManager).writeWithoutPrependedNewlines(entry, stringWriter, type); + FieldWriter fieldWriter = FieldWriter.buildIgnoreHashes(fieldWriterPreferences); + new BibEntryWriter(fieldWriter, Globals.entryTypesManager).writeWithoutPrependedNewlines(entry, stringWriter, type); return stringWriter.getBuffer().toString(); } @@ -220,7 +220,7 @@ private void updateCodeArea() { DefaultTaskExecutor.runAndWaitInJavaFXThread(() -> { codeArea.clear(); try { - codeArea.appendText(getSourceString(currentEntry, mode, fieldFormatterPreferences)); + codeArea.appendText(getSourceString(currentEntry, mode, fieldWriterPreferences)); highlightSearchPattern(); } catch (IOException ex) { codeArea.setEditable(false); @@ -300,7 +300,7 @@ private void storeSource(BibEntry outOfFocusEntry, String text) { String newValue = field.getValue(); if (!Objects.equals(oldValue, newValue)) { // Test if the field is legally set. - new LatexFieldFormatter(fieldFormatterPreferences).format(newValue, fieldName); + new FieldWriter(fieldWriterPreferences).write(fieldName, newValue); compound.addEdit(new UndoableFieldChange(outOfFocusEntry, fieldName, oldValue, newValue)); outOfFocusEntry.setField(fieldName, newValue); diff --git a/src/main/java/org/jabref/gui/worker/SendAsEMailAction.java b/src/main/java/org/jabref/gui/worker/SendAsEMailAction.java index 7a40174f225..24877673394 100644 --- a/src/main/java/org/jabref/gui/worker/SendAsEMailAction.java +++ b/src/main/java/org/jabref/gui/worker/SendAsEMailAction.java @@ -15,7 +15,7 @@ import org.jabref.gui.desktop.JabRefDesktop; import org.jabref.gui.util.BackgroundTask; import org.jabref.logic.bibtex.BibEntryWriter; -import org.jabref.logic.bibtex.LatexFieldFormatter; +import org.jabref.logic.bibtex.FieldWriter; import org.jabref.logic.l10n.Localization; import org.jabref.logic.util.io.FileUtil; import org.jabref.model.entry.BibEntry; @@ -72,8 +72,7 @@ private String sendEmail() throws Exception { List bes = panel.getSelectedEntries(); // write the entries using sw, which is used later to form the email content - BibEntryWriter bibtexEntryWriter = new BibEntryWriter( - new LatexFieldFormatter(Globals.prefs.getLatexFieldFormatterPreferences()), Globals.entryTypesManager); + BibEntryWriter bibtexEntryWriter = new BibEntryWriter(new FieldWriter(Globals.prefs.getFieldWriterPreferences()), Globals.entryTypesManager); for (BibEntry entry : bes) { try { diff --git a/src/main/java/org/jabref/logic/bibtex/BibEntryWriter.java b/src/main/java/org/jabref/logic/bibtex/BibEntryWriter.java index 2f1bce49674..a5be765f4fe 100644 --- a/src/main/java/org/jabref/logic/bibtex/BibEntryWriter.java +++ b/src/main/java/org/jabref/logic/bibtex/BibEntryWriter.java @@ -26,11 +26,11 @@ public class BibEntryWriter { - private final LatexFieldFormatter fieldFormatter; private final BibEntryTypesManager entryTypesManager; + private final FieldWriter fieldWriter; - public BibEntryWriter(LatexFieldFormatter fieldFormatter, BibEntryTypesManager entryTypesManager) { - this.fieldFormatter = fieldFormatter; + public BibEntryWriter(FieldWriter fieldWriter, BibEntryTypesManager entryTypesManager) { + this.fieldWriter = fieldWriter; this.entryTypesManager = entryTypesManager; } @@ -144,22 +144,21 @@ private void writeKeyField(BibEntry entry, Writer out) throws IOException { * * @param entry the entry to write * @param out the target of the write - * @param name The field name + * @param field the field * @throws IOException In case of an IO error */ - private void writeField(BibEntry entry, Writer out, Field name, int indentation) throws IOException { - Optional field = entry.getField(name); + private void writeField(BibEntry entry, Writer out, Field field, int indentation) throws IOException { + Optional value = entry.getField(field); // only write field if is is not empty // field.ifPresent does not work as an IOException may be thrown - if (field.isPresent() && !field.get().trim().isEmpty()) { - out.write(" " + getFormattedFieldName(name, indentation)); - + if (value.isPresent() && !value.get().trim().isEmpty()) { + out.write(" " + getFormattedFieldName(field, indentation)); try { - out.write(fieldFormatter.format(field.get(), name)); - out.write(',' + OS.NEWLINE); + out.write(fieldWriter.write(field, value.get())); } catch (InvalidFieldValueException ex) { - throw new IOException("Error in field '" + name + "': " + ex.getMessage(), ex); + throw new IOException("Error in field '" + field + " of entry " + entry.getCiteKeyOptional().orElse("") + "': " + ex.getMessage(), ex); } + out.write(',' + OS.NEWLINE); } } diff --git a/src/main/java/org/jabref/logic/bibtex/LatexFieldFormatter.java b/src/main/java/org/jabref/logic/bibtex/FieldWriter.java similarity index 87% rename from src/main/java/org/jabref/logic/bibtex/LatexFieldFormatter.java rename to src/main/java/org/jabref/logic/bibtex/FieldWriter.java index 01aa8fe8b6d..732d3a3941e 100644 --- a/src/main/java/org/jabref/logic/bibtex/LatexFieldFormatter.java +++ b/src/main/java/org/jabref/logic/bibtex/FieldWriter.java @@ -7,35 +7,33 @@ import org.jabref.model.strings.StringUtil; /** - * Currently the only implementation of org.jabref.exporter.FieldFormatter - *

* Obeys following settings: * * JabRefPreferences.RESOLVE_STRINGS_ALL_FIELDS * * JabRefPreferences.DO_NOT_RESOLVE_STRINGS_FOR * * JabRefPreferences.WRITEFIELD_WRAPFIELD */ -public class LatexFieldFormatter { +public class FieldWriter { private static final char FIELD_START = '{'; private static final char FIELD_END = '}'; private final boolean neverFailOnHashes; - private final LatexFieldFormatterPreferences prefs; + private final FieldWriterPreferences prefs; private final FieldContentParser parser; private StringBuilder stringBuilder; - public LatexFieldFormatter(LatexFieldFormatterPreferences prefs) { + public FieldWriter(FieldWriterPreferences prefs) { this(true, prefs); } - private LatexFieldFormatter(boolean neverFailOnHashes, LatexFieldFormatterPreferences prefs) { + private FieldWriter(boolean neverFailOnHashes, FieldWriterPreferences prefs) { this.neverFailOnHashes = neverFailOnHashes; this.prefs = prefs; parser = new FieldContentParser(prefs.getFieldContentParserPreferences()); } - public static LatexFieldFormatter buildIgnoreHashes(LatexFieldFormatterPreferences prefs) { - return new LatexFieldFormatter(true, prefs); + public static FieldWriter buildIgnoreHashes(FieldWriterPreferences prefs) { + return new FieldWriter(true, prefs); } private static void checkBraces(String text) throws InvalidFieldValueException { @@ -73,34 +71,22 @@ private static void checkBraces(String text) throws InvalidFieldValueException { /** * Formats the content of a field. * - * @param content the content of the field - * @param field the name of the field - used to trigger different serializations, e.g., turning off resolution for some strings + * @param field the name of the field - used to trigger different serializations, e.g., turning off resolution for some strings + * @param content the content of the field * @return a formatted string suitable for output * @throws InvalidFieldValueException if s is not a correct bibtex string, e.g., because of improperly balanced braces or using # not paired */ - public String format(String content, Field field) throws InvalidFieldValueException { + public String write(Field field, String content) throws InvalidFieldValueException { if (content == null) { return FIELD_START + String.valueOf(FIELD_END); } - String result = content; - - // normalize newlines - boolean shouldNormalizeNewlines = !result.contains(OS.NEWLINE) && result.contains("\n"); - if (shouldNormalizeNewlines) { - // if we don't have real new lines, but pseudo newlines, we replace them - // On Win 8.1, this is always true for multiline fields - result = result.replace("\n", OS.NEWLINE); - } - // If the field is non-standard, we will just append braces, wrap and write. if (!shouldResolveStrings(field)) { - return formatWithoutResolvingStrings(result, field); + return formatWithoutResolvingStrings(content, field); } - // Trim whitespace - result = result.trim(); - return formatAndResolveStrings(result, field); + return formatAndResolveStrings(content, field); } /** diff --git a/src/main/java/org/jabref/logic/bibtex/LatexFieldFormatterPreferences.java b/src/main/java/org/jabref/logic/bibtex/FieldWriterPreferences.java similarity index 78% rename from src/main/java/org/jabref/logic/bibtex/LatexFieldFormatterPreferences.java rename to src/main/java/org/jabref/logic/bibtex/FieldWriterPreferences.java index 6c071adc049..0776160d6d2 100644 --- a/src/main/java/org/jabref/logic/bibtex/LatexFieldFormatterPreferences.java +++ b/src/main/java/org/jabref/logic/bibtex/FieldWriterPreferences.java @@ -5,21 +5,21 @@ import org.jabref.model.entry.field.Field; -public class LatexFieldFormatterPreferences { +public class FieldWriterPreferences { private final boolean resolveStringsAllFields; private final List doNotResolveStringsFor; private final int lineLength = 65; // Constant private final FieldContentParserPreferences fieldContentParserPreferences; - public LatexFieldFormatterPreferences(boolean resolveStringsAllFields, List doNotResolveStringsFor, - FieldContentParserPreferences fieldContentParserPreferences) { + public FieldWriterPreferences(boolean resolveStringsAllFields, List doNotResolveStringsFor, + FieldContentParserPreferences fieldContentParserPreferences) { this.resolveStringsAllFields = resolveStringsAllFields; this.doNotResolveStringsFor = doNotResolveStringsFor; this.fieldContentParserPreferences = fieldContentParserPreferences; } - public LatexFieldFormatterPreferences() { + public FieldWriterPreferences() { // This constructor is only to allow an empty constructor in SavePreferences this(true, Collections.emptyList(), new FieldContentParserPreferences()); } diff --git a/src/main/java/org/jabref/logic/exporter/BibDatabaseWriter.java b/src/main/java/org/jabref/logic/exporter/BibDatabaseWriter.java index d5c238b09cd..67beb678b17 100644 --- a/src/main/java/org/jabref/logic/exporter/BibDatabaseWriter.java +++ b/src/main/java/org/jabref/logic/exporter/BibDatabaseWriter.java @@ -17,6 +17,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; +import java.util.stream.Stream; import org.jabref.logic.bibtex.comparator.BibtexStringComparator; import org.jabref.logic.bibtex.comparator.CrossRefEntryComparator; @@ -24,8 +25,11 @@ import org.jabref.logic.bibtex.comparator.FieldComparatorStack; import org.jabref.logic.bibtex.comparator.IdComparator; import org.jabref.logic.bibtexkeypattern.BibtexKeyGenerator; +import org.jabref.logic.formatter.bibtexfields.NormalizeNewlinesFormatter; +import org.jabref.logic.formatter.bibtexfields.TrimWhitespaceFormatter; import org.jabref.model.FieldChange; import org.jabref.model.bibtexkeypattern.GlobalBibtexKeyPattern; +import org.jabref.model.cleanup.FieldFormatterCleanup; import org.jabref.model.cleanup.FieldFormatterCleanups; import org.jabref.model.database.BibDatabase; import org.jabref.model.database.BibDatabaseContext; @@ -64,6 +68,17 @@ private static List applySaveActions(List toChange, MetaD } }); + // Run a couple of standard cleanups + List preSaveCleanups = + Stream.of(new TrimWhitespaceFormatter(), new NormalizeNewlinesFormatter()) + .map(formatter -> new FieldFormatterCleanup(InternalField.INTERNAL_ALL_FIELD, formatter)) + .collect(Collectors.toList()); + for (FieldFormatterCleanup formatter : preSaveCleanups) { + for (BibEntry entry : toChange) { + changes.addAll(formatter.cleanup(entry)); + } + } + return changes; } diff --git a/src/main/java/org/jabref/logic/exporter/BibtexDatabaseWriter.java b/src/main/java/org/jabref/logic/exporter/BibtexDatabaseWriter.java index 38670443eeb..e120cf44327 100644 --- a/src/main/java/org/jabref/logic/exporter/BibtexDatabaseWriter.java +++ b/src/main/java/org/jabref/logic/exporter/BibtexDatabaseWriter.java @@ -6,8 +6,8 @@ import java.util.Map; import org.jabref.logic.bibtex.BibEntryWriter; +import org.jabref.logic.bibtex.FieldWriter; import org.jabref.logic.bibtex.InvalidFieldValueException; -import org.jabref.logic.bibtex.LatexFieldFormatter; import org.jabref.logic.util.OS; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.database.BibDatabaseMode; @@ -85,9 +85,9 @@ protected void writeString(BibtexString bibtexString, boolean isFirstString, int writer.write("{}"); } else { try { - String formatted = new LatexFieldFormatter(preferences.getLatexFieldFormatterPreferences()) - .format(bibtexString.getContent(), - InternalField.BIBTEX_STRING); + String formatted = new FieldWriter(preferences.getFieldWriterPreferences()) + .write(InternalField.BIBTEX_STRING, bibtexString.getContent() + ); writer.write(formatted); } catch (InvalidFieldValueException ex) { throw new IOException(ex); @@ -129,8 +129,7 @@ protected void writeDatabaseID(String sharedDatabaseID) throws IOException { @Override protected void writeEntry(BibEntry entry, BibDatabaseMode mode) throws IOException { - BibEntryWriter bibtexEntryWriter = new BibEntryWriter( - new LatexFieldFormatter(preferences.getLatexFieldFormatterPreferences()), entryTypesManager); + BibEntryWriter bibtexEntryWriter = new BibEntryWriter(new FieldWriter(preferences.getFieldWriterPreferences()), entryTypesManager); bibtexEntryWriter.write(entry, writer, mode, preferences.isReformatFile()); } } diff --git a/src/main/java/org/jabref/logic/exporter/SavePreferences.java b/src/main/java/org/jabref/logic/exporter/SavePreferences.java index d3258a26156..ea99c4d2915 100644 --- a/src/main/java/org/jabref/logic/exporter/SavePreferences.java +++ b/src/main/java/org/jabref/logic/exporter/SavePreferences.java @@ -2,7 +2,7 @@ import java.nio.charset.Charset; -import org.jabref.logic.bibtex.LatexFieldFormatterPreferences; +import org.jabref.logic.bibtex.FieldWriterPreferences; import org.jabref.logic.bibtexkeypattern.BibtexKeyPatternPreferences; import org.jabref.model.bibtexkeypattern.GlobalBibtexKeyPattern; import org.jabref.model.metadata.SaveOrderConfig; @@ -19,14 +19,14 @@ public class SavePreferences { private final boolean makeBackup; private final DatabaseSaveType saveType; private final boolean takeMetadataSaveOrderInAccount; - private final LatexFieldFormatterPreferences latexFieldFormatterPreferences; + private final FieldWriterPreferences fieldWriterPreferences; private final GlobalBibtexKeyPattern globalCiteKeyPattern; private final Boolean generateBibtexKeysBeforeSaving; private final BibtexKeyPatternPreferences bibtexKeyPatternPreferences; public SavePreferences(Boolean saveInOriginalOrder, SaveOrderConfig saveOrder, Charset encoding, Boolean makeBackup, DatabaseSaveType saveType, Boolean takeMetadataSaveOrderInAccount, Boolean reformatFile, - LatexFieldFormatterPreferences latexFieldFormatterPreferences, GlobalBibtexKeyPattern globalCiteKeyPattern, + FieldWriterPreferences fieldWriterPreferences, GlobalBibtexKeyPattern globalCiteKeyPattern, Boolean generateBibtexKeysBeforeSaving, BibtexKeyPatternPreferences bibtexKeyPatternPreferences) { this.saveInOriginalOrder = saveInOriginalOrder; this.saveOrder = saveOrder; @@ -35,7 +35,7 @@ public SavePreferences(Boolean saveInOriginalOrder, SaveOrderConfig saveOrder, C this.saveType = saveType; this.takeMetadataSaveOrderInAccount = takeMetadataSaveOrderInAccount; this.reformatFile = reformatFile; - this.latexFieldFormatterPreferences = latexFieldFormatterPreferences; + this.fieldWriterPreferences = fieldWriterPreferences; this.globalCiteKeyPattern = globalCiteKeyPattern; this.generateBibtexKeysBeforeSaving = generateBibtexKeysBeforeSaving; this.bibtexKeyPatternPreferences = bibtexKeyPatternPreferences; @@ -55,7 +55,7 @@ public boolean isSaveInOriginalOrder() { public SavePreferences withSaveInOriginalOrder(Boolean newSaveInOriginalOrder) { return new SavePreferences(newSaveInOriginalOrder, this.saveOrder, this.encoding, this.makeBackup, this.saveType, - this.takeMetadataSaveOrderInAccount, this.reformatFile, this.latexFieldFormatterPreferences, + this.takeMetadataSaveOrderInAccount, this.reformatFile, this.fieldWriterPreferences, this.globalCiteKeyPattern, this.generateBibtexKeysBeforeSaving, this.bibtexKeyPatternPreferences); } @@ -65,7 +65,7 @@ public boolean makeBackup() { public SavePreferences withMakeBackup(Boolean newMakeBackup) { return new SavePreferences(this.saveInOriginalOrder, this.saveOrder, this.encoding, newMakeBackup, this.saveType, - this.takeMetadataSaveOrderInAccount, this.reformatFile, this.latexFieldFormatterPreferences, + this.takeMetadataSaveOrderInAccount, this.reformatFile, this.fieldWriterPreferences, this.globalCiteKeyPattern, this.generateBibtexKeysBeforeSaving, this.bibtexKeyPatternPreferences); } @@ -75,7 +75,7 @@ public Charset getEncoding() { public SavePreferences withEncoding(Charset newEncoding) { return new SavePreferences(this.saveInOriginalOrder, this.saveOrder, newEncoding, this.makeBackup, this.saveType, - this.takeMetadataSaveOrderInAccount, this.reformatFile, this.latexFieldFormatterPreferences, + this.takeMetadataSaveOrderInAccount, this.reformatFile, this.fieldWriterPreferences, this.globalCiteKeyPattern, this.generateBibtexKeysBeforeSaving, this.bibtexKeyPatternPreferences); } @@ -85,7 +85,7 @@ public DatabaseSaveType getSaveType() { public SavePreferences withSaveType(DatabaseSaveType newSaveType) { return new SavePreferences(this.saveInOriginalOrder, this.saveOrder, this.encoding, this.makeBackup, newSaveType, - this.takeMetadataSaveOrderInAccount, this.reformatFile, this.latexFieldFormatterPreferences, + this.takeMetadataSaveOrderInAccount, this.reformatFile, this.fieldWriterPreferences, this.globalCiteKeyPattern, this.generateBibtexKeysBeforeSaving, this.bibtexKeyPatternPreferences); } @@ -95,7 +95,7 @@ public Boolean isReformatFile() { public SavePreferences withReformatFile(boolean newReformatFile) { return new SavePreferences(this.saveInOriginalOrder, this.saveOrder, this.encoding, this.makeBackup, - this.saveType, this.takeMetadataSaveOrderInAccount, newReformatFile, this.latexFieldFormatterPreferences, + this.saveType, this.takeMetadataSaveOrderInAccount, newReformatFile, this.fieldWriterPreferences, this.globalCiteKeyPattern, this.generateBibtexKeysBeforeSaving, this.bibtexKeyPatternPreferences); } @@ -103,8 +103,8 @@ public Charset getEncodingOrDefault() { return encoding == null ? Charset.defaultCharset() : encoding; } - public LatexFieldFormatterPreferences getLatexFieldFormatterPreferences() { - return latexFieldFormatterPreferences; + public FieldWriterPreferences getFieldWriterPreferences() { + return fieldWriterPreferences; } public GlobalBibtexKeyPattern getGlobalCiteKeyPattern() { diff --git a/src/main/java/org/jabref/logic/formatter/bibtexfields/NormalizeNewlinesFormatter.java b/src/main/java/org/jabref/logic/formatter/bibtexfields/NormalizeNewlinesFormatter.java new file mode 100644 index 00000000000..343e3178af1 --- /dev/null +++ b/src/main/java/org/jabref/logic/formatter/bibtexfields/NormalizeNewlinesFormatter.java @@ -0,0 +1,47 @@ +package org.jabref.logic.formatter.bibtexfields; + +import java.util.Objects; + +import org.jabref.logic.l10n.Localization; +import org.jabref.logic.util.OS; +import org.jabref.model.cleanup.Formatter; + +/** + * Trim all whitespace characters(defined in java) in the string. + */ +public class NormalizeNewlinesFormatter extends Formatter { + + @Override + public String getName() { + return Localization.lang("Normalize newline characters"); + } + + @Override + public String getKey() { + return "normalize_newlines"; + } + + @Override + public String format(String value) { + Objects.requireNonNull(value); + + boolean shouldNormalizeNewlines = !value.contains(OS.NEWLINE) && value.contains("\n"); + if (shouldNormalizeNewlines) { + // if we don't have real new lines, but pseudo newlines, we replace them + // On Win 8.1, this is always true for multiline fields + return value.replace("\n", OS.NEWLINE); + } else { + return value; + } + } + + @Override + public String getDescription() { + return Localization.lang("Normalizes all newline characters in the field content."); + } + + @Override + public String getExampleInput() { + return "\r\n InCDMA\n "; + } +} diff --git a/src/main/java/org/jabref/logic/shared/DBMSSynchronizer.java b/src/main/java/org/jabref/logic/shared/DBMSSynchronizer.java index ed535797f62..72b9fc069f2 100644 --- a/src/main/java/org/jabref/logic/shared/DBMSSynchronizer.java +++ b/src/main/java/org/jabref/logic/shared/DBMSSynchronizer.java @@ -73,7 +73,7 @@ public DBMSSynchronizer(BibDatabaseContext bibDatabaseContext, Character keyword /** * Listening method. Inserts a new {@link BibEntry} into shared database. * - * @param event {@link EntryAddedEvent} object + * @param event {@link EntriesAddedEvent} object */ @Subscribe public void listen(EntriesAddedEvent event) { @@ -299,15 +299,15 @@ public void applyMetaData() { return; } for (BibEntry bibEntry : bibDatabase.getEntries()) { - // synchronize only if changes were present - if (!BibDatabaseWriter.applySaveActions(bibEntry, metaData).isEmpty()) { - try { + try { + // synchronize only if changes were present + if (!BibDatabaseWriter.applySaveActions(bibEntry, metaData).isEmpty()) { dbmsProcessor.updateEntry(bibEntry); - } catch (OfflineLockException exception) { - eventBus.post(new UpdateRefusedEvent(bibDatabaseContext, exception.getLocalBibEntry(), exception.getSharedBibEntry())); - } catch (SQLException e) { - LOGGER.error("SQL Error: ", e); } + } catch (OfflineLockException exception) { + eventBus.post(new UpdateRefusedEvent(bibDatabaseContext, exception.getLocalBibEntry(), exception.getSharedBibEntry())); + } catch (SQLException e) { + LOGGER.error("SQL Error: ", e); } } } diff --git a/src/main/java/org/jabref/preferences/JabRefPreferences.java b/src/main/java/org/jabref/preferences/JabRefPreferences.java index 86b00512825..c62345a312d 100644 --- a/src/main/java/org/jabref/preferences/JabRefPreferences.java +++ b/src/main/java/org/jabref/preferences/JabRefPreferences.java @@ -55,7 +55,7 @@ import org.jabref.gui.specialfields.SpecialFieldsPreferences; import org.jabref.gui.util.ThemeLoader; import org.jabref.logic.bibtex.FieldContentParserPreferences; -import org.jabref.logic.bibtex.LatexFieldFormatterPreferences; +import org.jabref.logic.bibtex.FieldWriterPreferences; import org.jabref.logic.bibtexkeypattern.BibtexKeyPatternPreferences; import org.jabref.logic.citationstyle.CitationStyle; import org.jabref.logic.citationstyle.CitationStylePreviewLayout; @@ -868,16 +868,16 @@ private static void insertDefaultCleanupPreset(Map storage) { @Override public EntryEditorPreferences getEntryEditorPreferences() { return new EntryEditorPreferences(getEntryEditorTabList(), - getLatexFieldFormatterPreferences(), - getImportFormatPreferences(), - getCustomTabFieldNames(), - getBoolean(SHOW_RECOMMENDATIONS), - getBoolean(ACCEPT_RECOMMENDATIONS), - getBoolean(SHOW_LATEX_CITATIONS), - getBoolean(DEFAULT_SHOW_SOURCE), - getBibtexKeyPatternPreferences(), - Globals.getKeyPrefs(), - getBoolean(AVOID_OVERWRITING_KEY)); + getFieldWriterPreferences(), + getImportFormatPreferences(), + getCustomTabFieldNames(), + getBoolean(SHOW_RECOMMENDATIONS), + getBoolean(ACCEPT_RECOMMENDATIONS), + getBoolean(SHOW_LATEX_CITATIONS), + getBoolean(DEFAULT_SHOW_SOURCE), + getBibtexKeyPatternPreferences(), + Globals.getKeyPrefs(), + getBoolean(AVOID_OVERWRITING_KEY)); } public Map getSidePanePreferredPositions() { @@ -1423,18 +1423,18 @@ public FilePreferences getFilePreferences() { fieldDirectories, getBoolean(JabRefPreferences.BIB_LOC_AS_PRIMARY_DIR), get(IMPORT_FILENAMEPATTERN), - get(IMPORT_FILEDIRPATTERN)); + get(IMPORT_FILEDIRPATTERN)); } @Override public UpdateFieldPreferences getUpdateFieldPreferences() { return new UpdateFieldPreferences(getBoolean(USE_OWNER), getBoolean(OVERWRITE_OWNER), get(DEFAULT_OWNER), getBoolean(USE_TIME_STAMP), getBoolean(OVERWRITE_TIME_STAMP), FieldFactory.parseField(get(TIME_STAMP_FIELD)), - get(TIME_STAMP_FORMAT)); + get(TIME_STAMP_FORMAT)); } - public LatexFieldFormatterPreferences getLatexFieldFormatterPreferences() { - return new LatexFieldFormatterPreferences( + public FieldWriterPreferences getFieldWriterPreferences() { + return new FieldWriterPreferences( getBoolean(RESOLVE_STRINGS_ALL_FIELDS), getStringList(DO_NOT_RESOLVE_STRINGS_FOR).stream().map(FieldFactory::parseField).collect(Collectors.toList()), getFieldContentParserPreferences()); @@ -1469,32 +1469,32 @@ public SavePreferences loadForExportFromPreferences() { } } return new SavePreferences( - saveInOriginalOrder, - saveOrder, - this.getDefaultEncoding(), - this.getBoolean(JabRefPreferences.BACKUP), - SavePreferences.DatabaseSaveType.ALL, - false, - this.getBoolean(JabRefPreferences.REFORMAT_FILE_ON_SAVE_AND_EXPORT), - this.getLatexFieldFormatterPreferences(), - this.getKeyPattern(), - getBoolean(JabRefPreferences.GENERATE_KEYS_BEFORE_SAVING), - getBibtexKeyPatternPreferences()); + saveInOriginalOrder, + saveOrder, + this.getDefaultEncoding(), + this.getBoolean(JabRefPreferences.BACKUP), + SavePreferences.DatabaseSaveType.ALL, + false, + this.getBoolean(JabRefPreferences.REFORMAT_FILE_ON_SAVE_AND_EXPORT), + this.getFieldWriterPreferences(), + this.getKeyPattern(), + getBoolean(JabRefPreferences.GENERATE_KEYS_BEFORE_SAVING), + getBibtexKeyPatternPreferences()); } public SavePreferences loadForSaveFromPreferences() { return new SavePreferences( - false, - null, - this.getDefaultEncoding(), - this.getBoolean(JabRefPreferences.BACKUP), - SavePreferences.DatabaseSaveType.ALL, - true, - this.getBoolean(JabRefPreferences.REFORMAT_FILE_ON_SAVE_AND_EXPORT), - this.getLatexFieldFormatterPreferences(), - this.getKeyPattern(), - getBoolean(JabRefPreferences.GENERATE_KEYS_BEFORE_SAVING), - getBibtexKeyPatternPreferences()); + false, + null, + this.getDefaultEncoding(), + this.getBoolean(JabRefPreferences.BACKUP), + SavePreferences.DatabaseSaveType.ALL, + true, + this.getBoolean(JabRefPreferences.REFORMAT_FILE_ON_SAVE_AND_EXPORT), + this.getFieldWriterPreferences(), + this.getKeyPattern(), + getBoolean(JabRefPreferences.GENERATE_KEYS_BEFORE_SAVING), + getBibtexKeyPatternPreferences()); } public ExporterFactory getExporterFactory(JournalAbbreviationLoader abbreviationLoader) { diff --git a/src/test/java/org/jabref/gui/entryeditor/SourceTabTest.java b/src/test/java/org/jabref/gui/entryeditor/SourceTabTest.java index 320b2f37c34..da779de6c24 100644 --- a/src/test/java/org/jabref/gui/entryeditor/SourceTabTest.java +++ b/src/test/java/org/jabref/gui/entryeditor/SourceTabTest.java @@ -14,7 +14,7 @@ import org.jabref.gui.undo.CountingUndoManager; import org.jabref.gui.util.OptionalObjectProperty; import org.jabref.logic.bibtex.FieldContentParserPreferences; -import org.jabref.logic.bibtex.LatexFieldFormatterPreferences; +import org.jabref.logic.bibtex.FieldWriterPreferences; import org.jabref.logic.importer.ImportFormatPreferences; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.BibEntry; @@ -53,7 +53,7 @@ public void onStart(Stage stage) { when(importFormatPreferences.getFieldContentParserPreferences()) .thenReturn(mock(FieldContentParserPreferences.class)); - sourceTab = new SourceTab(new BibDatabaseContext(), new CountingUndoManager(), new LatexFieldFormatterPreferences(), importFormatPreferences, new DummyFileUpdateMonitor(), mock(DialogService.class), stateManager, keyBindingRepository); + sourceTab = new SourceTab(new BibDatabaseContext(), new CountingUndoManager(), new FieldWriterPreferences(), importFormatPreferences, new DummyFileUpdateMonitor(), mock(DialogService.class), stateManager, keyBindingRepository); pane = new TabPane( new Tab("main area", area), new Tab("other tab", new Label("some text")), diff --git a/src/test/java/org/jabref/gui/exporter/SaveDatabaseActionTest.java b/src/test/java/org/jabref/gui/exporter/SaveDatabaseActionTest.java index 285f06c5611..eef64da0e42 100644 --- a/src/test/java/org/jabref/gui/exporter/SaveDatabaseActionTest.java +++ b/src/test/java/org/jabref/gui/exporter/SaveDatabaseActionTest.java @@ -2,6 +2,7 @@ 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.Collections; @@ -15,7 +16,7 @@ import org.jabref.gui.undo.CountingUndoManager; import org.jabref.gui.util.FileDialogConfiguration; import org.jabref.logic.bibtex.FieldContentParserPreferences; -import org.jabref.logic.bibtex.LatexFieldFormatterPreferences; +import org.jabref.logic.bibtex.FieldWriterPreferences; import org.jabref.logic.exporter.SavePreferences; import org.jabref.model.bibtexkeypattern.GlobalBibtexKeyPattern; import org.jabref.model.database.BibDatabase; @@ -110,8 +111,8 @@ private SaveDatabaseAction createSaveDatabaseActionForBibDatabase(BibDatabase da file = Files.createTempFile("JabRef", ".bib"); file.toFile().deleteOnExit(); - LatexFieldFormatterPreferences latexFieldFormatterPreferences = mock(LatexFieldFormatterPreferences.class); - when(latexFieldFormatterPreferences.getFieldContentParserPreferences()).thenReturn(mock(FieldContentParserPreferences.class)); + FieldWriterPreferences fieldWriterPreferences = mock(FieldWriterPreferences.class); + when(fieldWriterPreferences.getFieldContentParserPreferences()).thenReturn(mock(FieldContentParserPreferences.class)); SavePreferences savePreferences = mock(SavePreferences.class); // In case a "thenReturn" is modified, the whole mock has to be recreated dbContext = mock(BibDatabaseContext.class); @@ -119,8 +120,8 @@ private SaveDatabaseAction createSaveDatabaseActionForBibDatabase(BibDatabase da MetaData metaData = mock(MetaData.class); when(savePreferences.withEncoding(any(Charset.class))).thenReturn(savePreferences); when(savePreferences.withSaveType(any(SavePreferences.DatabaseSaveType.class))).thenReturn(savePreferences); - when(savePreferences.getEncoding()).thenReturn(Charset.forName("UTF-8")); - when(savePreferences.getLatexFieldFormatterPreferences()).thenReturn(latexFieldFormatterPreferences); + when(savePreferences.getEncoding()).thenReturn(StandardCharsets.UTF_8); + when(savePreferences.getFieldWriterPreferences()).thenReturn(fieldWriterPreferences); GlobalBibtexKeyPattern emptyGlobalBibtexKeyPattern = GlobalBibtexKeyPattern.fromPattern(""); when(savePreferences.getGlobalCiteKeyPattern()).thenReturn(emptyGlobalBibtexKeyPattern); when(metaData.getCiteKeyPattern(any(GlobalBibtexKeyPattern.class))).thenReturn(emptyGlobalBibtexKeyPattern); @@ -130,7 +131,7 @@ private SaveDatabaseAction createSaveDatabaseActionForBibDatabase(BibDatabase da when(dbContext.getMetaData()).thenReturn(metaData); when(dbContext.getEntries()).thenReturn(database.getEntries()); when(preferences.getBoolean(JabRefPreferences.LOCAL_AUTO_SAVE)).thenReturn(false); - when(preferences.getDefaultEncoding()).thenReturn(Charset.forName("UTF-8")); + when(preferences.getDefaultEncoding()).thenReturn(StandardCharsets.UTF_8); when(preferences.getFieldContentParserPreferences()).thenReturn(mock(FieldContentParserPreferences.class)); when(preferences.loadForSaveFromPreferences()).thenReturn(savePreferences); when(basePanel.frame()).thenReturn(jabRefFrame); diff --git a/src/test/java/org/jabref/logic/bibtex/BibEntryWriterTest.java b/src/test/java/org/jabref/logic/bibtex/BibEntryWriterTest.java index 6c58f5c8684..19411d8254c 100644 --- a/src/test/java/org/jabref/logic/bibtex/BibEntryWriterTest.java +++ b/src/test/java/org/jabref/logic/bibtex/BibEntryWriterTest.java @@ -39,8 +39,8 @@ class BibEntryWriterTest { @BeforeEach void setUpWriter() { importFormatPreferences = mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS); - LatexFieldFormatterPreferences latexFieldFormatterPreferences = mock(LatexFieldFormatterPreferences.class, Answers.RETURNS_DEEP_STUBS); - writer = new BibEntryWriter(new LatexFieldFormatter(latexFieldFormatterPreferences), new BibEntryTypesManager()); + FieldWriterPreferences fieldWriterPreferences = mock(FieldWriterPreferences.class, Answers.RETURNS_DEEP_STUBS); + writer = new BibEntryWriter(new FieldWriter(fieldWriterPreferences), new BibEntryTypesManager()); } @Test @@ -515,24 +515,6 @@ void doNotWriteEmptyFields() throws IOException { assertEquals(expected, actual); } - @Test - void trimFieldContents() throws IOException { - StringWriter stringWriter = new StringWriter(); - - BibEntry entry = new BibEntry(StandardEntryType.Article); - entry.setField(StandardField.NOTE, " some note \t"); - - writer.write(entry, stringWriter, BibDatabaseMode.BIBTEX); - - String actual = stringWriter.toString(); - - String expected = OS.NEWLINE + "@Article{," + OS.NEWLINE + - " note = {some note}," + OS.NEWLINE + - "}" + OS.NEWLINE; - - assertEquals(expected, actual); - } - @Test void writeThrowsErrorIfFieldContainsUnbalancedBraces() { StringWriter stringWriter = new StringWriter(); diff --git a/src/test/java/org/jabref/logic/bibtex/LatexFieldFormatterTests.java b/src/test/java/org/jabref/logic/bibtex/FieldWriterTests.java similarity index 50% rename from src/test/java/org/jabref/logic/bibtex/LatexFieldFormatterTests.java rename to src/test/java/org/jabref/logic/bibtex/FieldWriterTests.java index 58927a846f3..e0ac0138bf7 100644 --- a/src/test/java/org/jabref/logic/bibtex/LatexFieldFormatterTests.java +++ b/src/test/java/org/jabref/logic/bibtex/FieldWriterTests.java @@ -14,17 +14,17 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.mock; -public class LatexFieldFormatterTests { +class FieldWriterTests { - private LatexFieldFormatter formatter; + private FieldWriter writer; @BeforeEach - public void setUp() { - this.formatter = new LatexFieldFormatter(mock(LatexFieldFormatterPreferences.class, Answers.RETURNS_DEEP_STUBS)); + void setUp() { + this.writer = new FieldWriter(mock(FieldWriterPreferences.class, Answers.RETURNS_DEEP_STUBS)); } @Test - public void normalizeNewlineInAbstractField() throws Exception { + void normalizeNewlineInAbstractField() throws Exception { String text = "lorem" + OS.NEWLINE + " ipsum lorem ipsum\nlorem ipsum \rlorem ipsum\r\ntest"; String expected = "{" + "lorem" + OS.NEWLINE + " ipsum lorem ipsum" + OS.NEWLINE @@ -32,103 +32,103 @@ public void normalizeNewlineInAbstractField() throws Exception { + OS.NEWLINE + "lorem ipsum" + OS.NEWLINE + "test" + "}"; - String result = formatter.format(text, StandardField.ABSTRACT); + String result = writer.write(StandardField.ABSTRACT, text); assertEquals(expected, result); } @Test - public void newlineAtEndOfAbstractFieldIsDeleted() throws Exception { + void newlineAtEndOfAbstractFieldIsDeleted() throws Exception { String text = "lorem ipsum lorem ipsum" + OS.NEWLINE + "lorem ipsum lorem ipsum"; - String result = formatter.format(text + OS.NEWLINE, StandardField.ABSTRACT); + String result = writer.write(StandardField.ABSTRACT, text + OS.NEWLINE); String expected = "{" + text + "}"; assertEquals(expected, result); } @Test - public void preserveNewlineInAbstractField() throws Exception { + void preserveNewlineInAbstractField() throws Exception { String text = "lorem ipsum lorem ipsum" + OS.NEWLINE + "lorem ipsum lorem ipsum"; - String result = formatter.format(text, StandardField.ABSTRACT); + String result = writer.write(StandardField.ABSTRACT, text); String expected = "{" + text + "}"; assertEquals(expected, result); } @Test - public void preserveMultipleNewlinesInAbstractField() throws Exception { + void preserveMultipleNewlinesInAbstractField() throws Exception { String text = "lorem ipsum lorem ipsum" + OS.NEWLINE + OS.NEWLINE + "lorem ipsum lorem ipsum"; - String result = formatter.format(text, StandardField.ABSTRACT); + String result = writer.write(StandardField.ABSTRACT, text); String expected = "{" + text + "}"; assertEquals(expected, result); } @Test - public void preserveNewlineInReviewField() throws Exception { + void preserveNewlineInReviewField() throws Exception { String text = "lorem ipsum lorem ipsum" + OS.NEWLINE + "lorem ipsum lorem ipsum"; - String result = formatter.format(text, StandardField.REVIEW); + String result = writer.write(StandardField.REVIEW, text); String expected = "{" + text + "}"; assertEquals(expected, result); } @Test - public void removeWhitespaceFromNonMultiLineFields() throws Exception { + void removeWhitespaceFromNonMultiLineFields() throws Exception { String original = "I\nshould\nnot\ninclude\nadditional\nwhitespaces \nor\n\ttabs."; String expected = "{I should not include additional whitespaces or tabs.}"; - String title = formatter.format(original, StandardField.TITLE); - String any = formatter.format(original, new UnknownField("anyotherfield")); + String title = writer.write(StandardField.TITLE, original); + String any = writer.write(new UnknownField("anyotherfield"), original); assertEquals(expected, title); assertEquals(expected, any); } @Test - public void reportUnbalancedBracing() throws Exception { + void reportUnbalancedBracing() throws Exception { String unbalanced = "{"; - assertThrows(InvalidFieldValueException.class, () -> formatter.format(unbalanced, new UnknownField("anyfield"))); + assertThrows(InvalidFieldValueException.class, () -> writer.write(new UnknownField("anyfield"), unbalanced)); } @Test - public void reportUnbalancedBracingWithEscapedBraces() throws Exception { + void reportUnbalancedBracingWithEscapedBraces() throws Exception { String unbalanced = "{\\}"; - assertThrows(InvalidFieldValueException.class, () -> formatter.format(unbalanced, new UnknownField("anyfield"))); + assertThrows(InvalidFieldValueException.class, () -> writer.write(new UnknownField("anyfield"), unbalanced)); } @Test - public void tolerateBalancedBrace() throws Exception { + void tolerateBalancedBrace() throws Exception { String text = "Incorporating evolutionary {Measures into Conservation Prioritization}"; - assertEquals("{" + text + "}", formatter.format(text, new UnknownField("anyfield"))); + assertEquals("{" + text + "}", writer.write(new UnknownField("anyfield"), text)); } @Test - public void tolerateEscapeCharacters() throws Exception { + void tolerateEscapeCharacters() throws Exception { String text = "Incorporating {\\O}evolutionary {Measures into Conservation Prioritization}"; - assertEquals("{" + text + "}", formatter.format(text, new UnknownField("anyfield"))); + assertEquals("{" + text + "}", writer.write(new UnknownField("anyfield"), text)); } @Test - public void hashEnclosedWordsGetRealStringsInMonthField() throws Exception { + void hashEnclosedWordsGetRealStringsInMonthField() throws Exception { String text = "#jan# - #feb#"; - assertEquals("jan #{ - } # feb", formatter.format(text, StandardField.MONTH)); + assertEquals("jan #{ - } # feb", writer.write(StandardField.MONTH, text)); } @Test - public void hashEnclosedWordsGetRealStringsInMonthFieldBecauseMonthIsStandardField() throws Exception { - LatexFieldFormatterPreferences latexFieldFormatterPreferences = new LatexFieldFormatterPreferences( + void hashEnclosedWordsGetRealStringsInMonthFieldBecauseMonthIsStandardField() throws Exception { + FieldWriterPreferences fieldWriterPreferences = new FieldWriterPreferences( false, Collections.emptyList(), new FieldContentParserPreferences()); - LatexFieldFormatter formatter = new LatexFieldFormatter(latexFieldFormatterPreferences); + FieldWriter formatter = new FieldWriter(fieldWriterPreferences); String text = "#jan# - #feb#"; - assertEquals("jan #{ - } # feb", formatter.format(text, StandardField.MONTH)); + assertEquals("jan #{ - } # feb", formatter.write(StandardField.MONTH, text)); } } diff --git a/src/test/java/org/jabref/logic/exporter/BibtexDatabaseWriterTest.java b/src/test/java/org/jabref/logic/exporter/BibtexDatabaseWriterTest.java index 381e120eb86..e15ca5d07be 100644 --- a/src/test/java/org/jabref/logic/exporter/BibtexDatabaseWriterTest.java +++ b/src/test/java/org/jabref/logic/exporter/BibtexDatabaseWriterTest.java @@ -1,5 +1,6 @@ package org.jabref.logic.exporter; +import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; import java.nio.charset.Charset; @@ -277,7 +278,7 @@ void writeEntryWithCustomizedTypeAlsoWritesTypeDeclaration() throws Exception { BibEntry entry = new BibEntry(customizedType); database.insertEntry(entry); - databaseWriter.savePartOfDatabase(bibtexContext, Collections.singletonList(entry)); + databaseWriter.saveDatabase(bibtexContext); assertEquals( OS.NEWLINE + @@ -648,6 +649,22 @@ void writeEntriesInOriginalOrderWhenNoSaveOrderConfigIsSetInMetadata() throws Ex , stringWriter.toString()); } + @Test + void trimFieldContents() throws IOException { + StringWriter stringWriter = new StringWriter(); + + BibEntry entry = new BibEntry(StandardEntryType.Article); + entry.setField(StandardField.NOTE, " some note \t"); + database.insertEntry(entry); + + databaseWriter.savePartOfDatabase(bibtexContext, database.getEntries()); + + String expected = OS.NEWLINE + "@Article{," + OS.NEWLINE + + " note = {some note}," + OS.NEWLINE + + "}" + OS.NEWLINE; + assertEquals(expected, stringWriter.toString()); + } + @Test void roundtripWithContentSelectorsAndUmlauts() throws Exception { String fileContent = "% Encoding: UTF-8" + OS.NEWLINE + OS.NEWLINE + "@Comment{jabref-meta: selector_journal:Test {\\\\\"U}mlaut;}" + OS.NEWLINE; From 79f62b7ed326bb7b8205246c4b74d48928762635 Mon Sep 17 00:00:00 2001 From: Tobias Diez Date: Fri, 17 Jan 2020 22:17:08 +0100 Subject: [PATCH 4/8] Fix tests --- .../jabref/logic/bibtex/FieldWriterTests.java | 10 --- .../exporter/BibtexDatabaseWriterTest.java | 72 +++++++++++-------- 2 files changed, 42 insertions(+), 40 deletions(-) diff --git a/src/test/java/org/jabref/logic/bibtex/FieldWriterTests.java b/src/test/java/org/jabref/logic/bibtex/FieldWriterTests.java index e0ac0138bf7..2dfdca2edc0 100644 --- a/src/test/java/org/jabref/logic/bibtex/FieldWriterTests.java +++ b/src/test/java/org/jabref/logic/bibtex/FieldWriterTests.java @@ -37,16 +37,6 @@ void normalizeNewlineInAbstractField() throws Exception { assertEquals(expected, result); } - @Test - void newlineAtEndOfAbstractFieldIsDeleted() throws Exception { - String text = "lorem ipsum lorem ipsum" + OS.NEWLINE + "lorem ipsum lorem ipsum"; - - String result = writer.write(StandardField.ABSTRACT, text + OS.NEWLINE); - String expected = "{" + text + "}"; - - assertEquals(expected, result); - } - @Test void preserveNewlineInAbstractField() throws Exception { String text = "lorem ipsum lorem ipsum" + OS.NEWLINE + "lorem ipsum lorem ipsum"; diff --git a/src/test/java/org/jabref/logic/exporter/BibtexDatabaseWriterTest.java b/src/test/java/org/jabref/logic/exporter/BibtexDatabaseWriterTest.java index e15ca5d07be..12682482a1d 100644 --- a/src/test/java/org/jabref/logic/exporter/BibtexDatabaseWriterTest.java +++ b/src/test/java/org/jabref/logic/exporter/BibtexDatabaseWriterTest.java @@ -132,10 +132,11 @@ void writeEntry() throws Exception { databaseWriter.savePartOfDatabase(bibtexContext, Collections.singletonList(entry)); - assertEquals(OS.NEWLINE + - "@Article{," + OS.NEWLINE + "}" + OS.NEWLINE + OS.NEWLINE - + "@Comment{jabref-meta: databaseType:bibtex;}" - + OS.NEWLINE, stringWriter.toString()); + assertEquals( + OS.NEWLINE + + "@Article{," + OS.NEWLINE + "}" + + OS.NEWLINE, + stringWriter.toString()); } @Test @@ -147,11 +148,11 @@ void writeEncodingAndEntry() throws Exception { databaseWriter.savePartOfDatabase(bibtexContext, Collections.singletonList(entry)); - assertEquals("% Encoding: US-ASCII" + OS.NEWLINE + OS.NEWLINE + - "@Article{," + OS.NEWLINE + "}" - + OS.NEWLINE + OS.NEWLINE - + "@Comment{jabref-meta: databaseType:bibtex;}" - + OS.NEWLINE, stringWriter.toString()); + assertEquals( + "% Encoding: US-ASCII" + OS.NEWLINE + OS.NEWLINE + + "@Article{," + OS.NEWLINE + "}" + + OS.NEWLINE, + stringWriter.toString()); } @Test @@ -277,6 +278,7 @@ void writeEntryWithCustomizedTypeAlsoWritesTypeDeclaration() throws Exception { entryTypesManager.addCustomOrModifiedType(customizedBibType, BibDatabaseMode.BIBTEX); BibEntry entry = new BibEntry(customizedType); database.insertEntry(entry); + bibtexContext.setMode(BibDatabaseMode.BIBTEX); databaseWriter.saveDatabase(bibtexContext); @@ -307,6 +309,7 @@ void writeCustomizedTypesInAlphabeticalOrder() throws Exception { BibEntry otherEntry = new BibEntry(otherCustomizedType); database.insertEntry(otherEntry); database.insertEntry(entry); + bibtexContext.setMode(BibDatabaseMode.BIBTEX); databaseWriter.savePartOfDatabase(bibtexContext, Arrays.asList(entry, otherEntry)); @@ -425,8 +428,7 @@ void writeSavedSerializationOfEntryIfUnchanged() throws Exception { databaseWriter.savePartOfDatabase(bibtexContext, Collections.singletonList(entry)); - assertEquals("presaved serialization" + OS.NEWLINE + "@Comment{jabref-meta: databaseType:bibtex;}" - + OS.NEWLINE, stringWriter.toString()); + assertEquals("presaved serialization", stringWriter.toString()); } @Test @@ -441,10 +443,9 @@ void reformatEntryIfAskedToDoSo() throws Exception { when(preferences.isReformatFile()).thenReturn(true); databaseWriter.savePartOfDatabase(bibtexContext, Collections.singletonList(entry)); - assertEquals(OS.NEWLINE + - "@Article{," + OS.NEWLINE + " author = {Mr. author}," + OS.NEWLINE + "}" - + OS.NEWLINE + OS.NEWLINE - + "@Comment{jabref-meta: databaseType:bibtex;}" + assertEquals( + OS.NEWLINE + + "@Article{," + OS.NEWLINE + " author = {Mr. author}," + OS.NEWLINE + "}" + OS.NEWLINE, stringWriter.toString()); } @@ -598,8 +599,6 @@ void writeEntriesSorted() throws Exception { " author = {B}," + OS.NEWLINE + " year = {2000}," + OS.NEWLINE + "}" + OS.NEWLINE + OS.NEWLINE + - "@Comment{jabref-meta: databaseType:bibtex;}" - + OS.NEWLINE + OS.NEWLINE + "@Comment{jabref-meta: saveOrderConfig:specified;author;false;year;true;abstract;false;}" + OS.NEWLINE, stringWriter.toString()); @@ -643,26 +642,40 @@ void writeEntriesInOriginalOrderWhenNoSaveOrderConfigIsSetInMetadata() throws Ex " author = {A}," + OS.NEWLINE + " year = {2000}," + OS.NEWLINE + "}" - + OS.NEWLINE + OS.NEWLINE + - "@Comment{jabref-meta: databaseType:bibtex;}" + OS.NEWLINE , stringWriter.toString()); } @Test void trimFieldContents() throws IOException { - StringWriter stringWriter = new StringWriter(); - BibEntry entry = new BibEntry(StandardEntryType.Article); entry.setField(StandardField.NOTE, " some note \t"); database.insertEntry(entry); - databaseWriter.savePartOfDatabase(bibtexContext, database.getEntries()); + databaseWriter.saveDatabase(bibtexContext); - String expected = OS.NEWLINE + "@Article{," + OS.NEWLINE + - " note = {some note}," + OS.NEWLINE + - "}" + OS.NEWLINE; - assertEquals(expected, stringWriter.toString()); + assertEquals( + OS.NEWLINE + "@Article{," + OS.NEWLINE + + " note = {some note}," + OS.NEWLINE + + "}" + OS.NEWLINE, + stringWriter.toString()); + } + + @Test + void newlineAtEndOfAbstractFieldIsDeleted() throws Exception { + String text = "lorem ipsum lorem ipsum" + OS.NEWLINE + "lorem ipsum lorem ipsum"; + + BibEntry entry = new BibEntry(StandardEntryType.Article); + entry.setField(StandardField.ABSTRACT, text + OS.NEWLINE); + database.insertEntry(entry); + + databaseWriter.saveDatabase(bibtexContext); + + assertEquals( + OS.NEWLINE + "@Article{," + OS.NEWLINE + + " abstract = {" + text + "}," + OS.NEWLINE + + "}" + OS.NEWLINE, + stringWriter.toString()); } @Test @@ -702,6 +715,7 @@ public void saveAlsoSavesSecondModification() throws Exception { entry.setField(StandardField.AUTHOR, "BlaBla"); BibDatabaseContext context = new BibDatabaseContext(firstParse.getDatabase(), firstParse.getMetaData()); + context.setMode(BibDatabaseMode.BIBTEX); databaseWriter.savePartOfDatabase(context, firstParse.getDatabase().getEntries()); @@ -753,8 +767,7 @@ public void saveReturnsToOriginalEntryWhenEntryIsFlaggedUnchanged() throws Excep BibDatabaseContext context = new BibDatabaseContext(firstParse.getDatabase(), firstParse.getMetaData()); databaseWriter.savePartOfDatabase(context, firstParse.getDatabase().getEntries()); - assertEquals(bibtexEntry + OS.NEWLINE + - "@Comment{jabref-meta: databaseType:bibtex;}" + OS.NEWLINE, stringWriter.toString()); + assertEquals(bibtexEntry, stringWriter.toString()); } @Test @@ -792,7 +805,6 @@ public void saveReturnsToOriginalEntryWhenEntryIsFlaggedUnchangedEvenInThePrecen databaseWriter.savePartOfDatabase(context, firstParse.getDatabase().getEntries()); // returns tu original entry, not to the last saved one - assertEquals(bibtexEntry + OS.NEWLINE + - "@Comment{jabref-meta: databaseType:bibtex;}" + OS.NEWLINE, stringWriter.toString()); + assertEquals(bibtexEntry, stringWriter.toString()); } } From 3cc259a2437af98ee06b5092b9275651cb3dc484 Mon Sep 17 00:00:00 2001 From: Tobias Diez Date: Fri, 17 Jan 2020 22:35:09 +0100 Subject: [PATCH 5/8] Fix tests --- src/main/resources/l10n/JabRef_en.properties | 4 ++-- .../logic/integrity/IntegrityCheckTest.java | 4 +++- .../jabref/model/BibDatabaseContextTest.java | 20 +++++-------------- 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/src/main/resources/l10n/JabRef_en.properties b/src/main/resources/l10n/JabRef_en.properties index dbfe46deb75..b22a25ac6c8 100644 --- a/src/main/resources/l10n/JabRef_en.properties +++ b/src/main/resources/l10n/JabRef_en.properties @@ -2094,10 +2094,10 @@ Special\ field\ type\ %0\ is\ unknown.\ Using\ normal\ column\ type.=Special fie insert\ entries=insert entries In\ JabRef=In JabRef On\ disk=On disk - Select\ all\ changes\ on\ the\ left=Select all changes on the left Select\ all\ changes\ on\ the\ right=Select all changes on the right - Dismiss=Dismiss Mark\ all\ changes\ as\ accepted=Mark all changes as accepted Unmark\ all\ changes=Unmark all changes +Normalize=newline characters=Normalize newline characters +Normalizes=all newline characters in the field content.=Normalizes all newline characters in the field content. diff --git a/src/test/java/org/jabref/logic/integrity/IntegrityCheckTest.java b/src/test/java/org/jabref/logic/integrity/IntegrityCheckTest.java index 3ff92b7abb3..e166869806d 100644 --- a/src/test/java/org/jabref/logic/integrity/IntegrityCheckTest.java +++ b/src/test/java/org/jabref/logic/integrity/IntegrityCheckTest.java @@ -378,7 +378,9 @@ private BibDatabaseContext createContext(Field field, String value, MetaData met } private BibDatabaseContext createContext(Field field, String value) { - return createContext(field, value, new MetaData()); + MetaData metaData = new MetaData(); + metaData.setMode(BibDatabaseMode.BIBTEX); + return createContext(field, value, metaData); } private void assertWrong(BibDatabaseContext context) { diff --git a/src/test/java/org/jabref/model/BibDatabaseContextTest.java b/src/test/java/org/jabref/model/BibDatabaseContextTest.java index 288efe5152c..e34d459f907 100644 --- a/src/test/java/org/jabref/model/BibDatabaseContextTest.java +++ b/src/test/java/org/jabref/model/BibDatabaseContextTest.java @@ -11,18 +11,18 @@ import static org.junit.jupiter.api.Assertions.assertEquals; -public class BibDatabaseContextTest { +class BibDatabaseContextTest { @Test - public void testTypeBasedOnDefaultBibtex() { + void testTypeBasedOnDefaultBibtex() { BibDatabaseContext bibDatabaseContext = new BibDatabaseContext(new BibDatabase(), new MetaData()); - assertEquals(BibDatabaseMode.BIBTEX, bibDatabaseContext.getMode()); + assertEquals(BibDatabaseMode.BIBLATEX, bibDatabaseContext.getMode()); bibDatabaseContext.setMode(BibDatabaseMode.BIBLATEX); assertEquals(BibDatabaseMode.BIBLATEX, bibDatabaseContext.getMode()); } @Test - public void testTypeBasedOnDefaultBiblatex() { + void testTypeBasedOnDefaultBiblatex() { BibDatabaseContext bibDatabaseContext = new BibDatabaseContext(new BibDatabase(), new MetaData()); assertEquals(BibDatabaseMode.BIBLATEX, bibDatabaseContext.getMode()); @@ -31,17 +31,7 @@ public void testTypeBasedOnDefaultBiblatex() { } @Test - public void testTypeBasedOnInferredModeBibTeX() { - BibDatabase db = new BibDatabase(); - BibEntry e1 = new BibEntry(); - db.insertEntry(e1); - - BibDatabaseContext bibDatabaseContext = new BibDatabaseContext(db); - assertEquals(BibDatabaseMode.BIBTEX, bibDatabaseContext.getMode()); - } - - @Test - public void testTypeBasedOnInferredModeBiblatex() { + void testTypeBasedOnInferredModeBiblatex() { BibDatabase db = new BibDatabase(); BibEntry e1 = new BibEntry(IEEETranEntryType.Electronic); db.insertEntry(e1); From 06edaa74f61f2de7dbd53a59390cd1cedf01f133 Mon Sep 17 00:00:00 2001 From: Tobias Diez Date: Fri, 17 Jan 2020 22:46:41 +0100 Subject: [PATCH 6/8] Fix localization and add changelog --- CHANGELOG.md | 6 +++--- src/main/resources/l10n/JabRef_en.properties | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8489723adc4..9185ff1bb36 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,11 +15,11 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `# ### Fixed -- We fixed an issue where the Medline fetcher was only working when JabRef was running from source [#5645](https://github.com/JabRef/jabref/issues/5645) -- We fixed some visual issues in the dark theme [#5764](https://github.com/JabRef/jabref/pull/5764) [#5753](https://github.com/JabRef/jabref/issues/5753) +- We fixed an issue where the Medline fetcher was only working when JabRef was running from source. [#5645](https://github.com/JabRef/jabref/issues/5645) +- We fixed some visual issues in the dark theme. [#5764](https://github.com/JabRef/jabref/pull/5764) [#5753](https://github.com/JabRef/jabref/issues/5753) - We fixed an issue where non-default previews didn't handle unicode characters. [#5779](https://github.com/JabRef/jabref/issues/5779) - We fixed an issue where the ampersand character wasn't rendering correctly on previews.[#3840](https://github.com/JabRef/jabref/issues/3840) - +- We fixed an issue where an erroneous "The library has been modified by another program" message was shown when saving. [#4877](https://github.com/JabRef/jabref/issues/4877) ### Removed diff --git a/src/main/resources/l10n/JabRef_en.properties b/src/main/resources/l10n/JabRef_en.properties index b22a25ac6c8..800628cb28a 100644 --- a/src/main/resources/l10n/JabRef_en.properties +++ b/src/main/resources/l10n/JabRef_en.properties @@ -2099,5 +2099,5 @@ Select\ all\ changes\ on\ the\ right=Select all changes on the right Dismiss=Dismiss Mark\ all\ changes\ as\ accepted=Mark all changes as accepted Unmark\ all\ changes=Unmark all changes -Normalize=newline characters=Normalize newline characters -Normalizes=all newline characters in the field content.=Normalizes all newline characters in the field content. +Normalize\ newline\ characters=Normalize newline characters +Normalizes\ all\ newline\ characters\ in\ the\ field\ content.=Normalizes all newline characters in the field content. From ab4f0db12eda9809697f2eda899c5665554e20df Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Fri, 17 Jan 2020 22:56:40 +0100 Subject: [PATCH 7/8] Add missing space --- .../formatter/bibtexfields/NormalizeNewlinesFormatter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/jabref/logic/formatter/bibtexfields/NormalizeNewlinesFormatter.java b/src/main/java/org/jabref/logic/formatter/bibtexfields/NormalizeNewlinesFormatter.java index 343e3178af1..37ced90bc91 100644 --- a/src/main/java/org/jabref/logic/formatter/bibtexfields/NormalizeNewlinesFormatter.java +++ b/src/main/java/org/jabref/logic/formatter/bibtexfields/NormalizeNewlinesFormatter.java @@ -7,7 +7,7 @@ import org.jabref.model.cleanup.Formatter; /** - * Trim all whitespace characters(defined in java) in the string. + * Trim all whitespace characters (defined in java) in the string. */ public class NormalizeNewlinesFormatter extends Formatter { From c301d27317a803db3e80356a651937ca8a334dd7 Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Fri, 17 Jan 2020 23:01:37 +0100 Subject: [PATCH 8/8] Fix test method name --- src/test/java/org/jabref/model/BibDatabaseContextTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/jabref/model/BibDatabaseContextTest.java b/src/test/java/org/jabref/model/BibDatabaseContextTest.java index e34d459f907..1be4f0eb7da 100644 --- a/src/test/java/org/jabref/model/BibDatabaseContextTest.java +++ b/src/test/java/org/jabref/model/BibDatabaseContextTest.java @@ -13,7 +13,7 @@ class BibDatabaseContextTest { @Test - void testTypeBasedOnDefaultBibtex() { + void testTypeBasedOnDefaultBiblatex() { BibDatabaseContext bibDatabaseContext = new BibDatabaseContext(new BibDatabase(), new MetaData()); assertEquals(BibDatabaseMode.BIBLATEX, bibDatabaseContext.getMode()); @@ -22,7 +22,7 @@ void testTypeBasedOnDefaultBibtex() { } @Test - void testTypeBasedOnDefaultBiblatex() { + void testTypeBasedOnDefaultBibtex() { BibDatabaseContext bibDatabaseContext = new BibDatabaseContext(new BibDatabase(), new MetaData()); assertEquals(BibDatabaseMode.BIBLATEX, bibDatabaseContext.getMode());