Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Improve serialization to fix #4877 #5838

Merged
merged 8 commits into from
Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/jmh/java/org/jabref/benchmarks/Benchmarks.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 1 addition & 3 deletions src/main/java/org/jabref/cli/ArgumentProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/gui/ClipBoardManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -152,7 +152,7 @@ public void setContent(String string) {

public void setContent(List<BibEntry> 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);
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/org/jabref/gui/auximport/FromAuxDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/entryeditor/EntryEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ private List<EntryEditorTab> createTabs() {

// Source tab
sourceTab = new SourceTab(databaseContext, undoManager,
entryEditorPreferences.getLatexFieldFormatterPreferences(),
entryEditorPreferences.getFieldWriterPreferences(),
entryEditorPreferences.getImportFormatPreferences(), fileMonitor, dialogService, stateManager, Globals.getKeyPrefs());
entryEditorTabs.add(sourceTab);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
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;

public class EntryEditorPreferences {

private final Map<String, Set<Field>> entryEditorTabList;
private final LatexFieldFormatterPreferences latexFieldFormatterPreferences;
private final FieldWriterPreferences fieldWriterPreferences;
private final ImportFormatPreferences importFormatPreferences;
private final BibtexKeyPatternPreferences bibtexKeyPatternPreferences;
private final List<Field> customTabFieldNames;
Expand All @@ -24,9 +24,9 @@ public class EntryEditorPreferences {
private boolean avoidOverwritingCiteKey;
private final boolean shouldShowLatexCitationsTab;

public EntryEditorPreferences(Map<String, Set<Field>> entryEditorTabList, LatexFieldFormatterPreferences latexFieldFormatterPreferences, ImportFormatPreferences importFormatPreferences, List<Field> customTabFieldNames, boolean shouldShowRecommendationsTab, boolean isMrdlibAccepted, boolean shouldShowLatexCitationsTab, boolean showSourceTabByDefault, BibtexKeyPatternPreferences bibtexKeyPatternPreferences, KeyBindingRepository keyBindings, boolean avoidOverwritingCiteKey) {
public EntryEditorPreferences(Map<String, Set<Field>> entryEditorTabList, FieldWriterPreferences fieldWriterPreferences, ImportFormatPreferences importFormatPreferences, List<Field> 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;
Expand All @@ -42,8 +42,8 @@ public Map<String, Set<Field>> getEntryEditorTabList() {
return entryEditorTabList;
}

public LatexFieldFormatterPreferences getLatexFieldFormatterPreferences() {
return latexFieldFormatterPreferences;
public FieldWriterPreferences getFieldWriterPreferences() {
return fieldWriterPreferences;
}

public ImportFormatPreferences getImportFormatPreferences() {
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/org/jabref/gui/entryeditor/SourceTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<ValidationMessage> sourceIsValid = new SimpleObjectProperty<>();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
18 changes: 10 additions & 8 deletions src/main/java/org/jabref/gui/exporter/SaveDatabaseAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ private void saveWithDifferentEncoding(Path file, boolean selectedOnly, Charset
if (saveWithDifferentEncoding) {
Optional<Charset> 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);
}
}
}
Expand All @@ -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();
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/jabref/gui/importer/NewDatabaseAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/org/jabref/gui/openoffice/OpenOfficePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -435,7 +433,6 @@ private List<URL> findOpenOfficeJars(Path configurationPath) throws IOException
jarURLs.add((jarPath.get().toUri().toURL()));
}
return jarURLs;

}

private OOBibBase createBibBase(List<URL> jarUrls) throws IOException, InvocationTargetException, IllegalAccessException,
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/org/jabref/gui/shared/SharedDatabaseUIManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand Down
5 changes: 2 additions & 3 deletions src/main/java/org/jabref/gui/worker/SendAsEMailAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -72,8 +72,7 @@ private String sendEmail() throws Exception {
List<BibEntry> 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 {
Expand Down
Loading