Skip to content

Commit

Permalink
Fix exception when opening preferences (#10294)
Browse files Browse the repository at this point in the history
* Introduce SelfContainedSaveConfiguration

* Fix checkstyle

* Fix NPE
  • Loading branch information
koppor authored Sep 3, 2023
1 parent 0c7f16c commit 1e0bbea
Show file tree
Hide file tree
Showing 17 changed files with 94 additions and 59 deletions.
4 changes: 2 additions & 2 deletions src/jmh/java/org/jabref/benchmarks/Benchmarks.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.jabref.logic.citationkeypattern.CitationKeyPatternPreferences;
import org.jabref.logic.exporter.BibWriter;
import org.jabref.logic.exporter.BibtexDatabaseWriter;
import org.jabref.logic.exporter.SaveConfiguration;
import org.jabref.logic.exporter.SelfContainedSaveConfiguration;
import org.jabref.logic.formatter.bibtexfields.HtmlToLatexFormatter;
import org.jabref.logic.importer.ParserResult;
import org.jabref.logic.importer.fileformat.BibtexParser;
Expand Down Expand Up @@ -82,7 +82,7 @@ private StringWriter getOutputWriter() throws IOException {
BibWriter bibWriter = new BibWriter(outputWriter, OS.NEWLINE);
BibtexDatabaseWriter databaseWriter = new BibtexDatabaseWriter(
bibWriter,
mock(SaveConfiguration.class),
mock(SelfContainedSaveConfiguration.class),
mock(FieldPreferences.class),
mock(CitationKeyPatternPreferences.class),
new BibEntryTypesManager());
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/cli/ArgumentProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.jabref.logic.exporter.EmbeddedBibFilePdfExporter;
import org.jabref.logic.exporter.Exporter;
import org.jabref.logic.exporter.ExporterFactory;
import org.jabref.logic.exporter.SaveConfiguration;
import org.jabref.logic.exporter.SelfContainedSaveConfiguration;
import org.jabref.logic.exporter.XmpPdfExporter;
import org.jabref.logic.importer.FetcherException;
import org.jabref.logic.importer.ImportException;
Expand Down Expand Up @@ -598,7 +598,7 @@ private void saveDatabase(BibDatabase newBase, String subName) {
System.out.println(Localization.lang("Saving") + ": " + subName);
try (AtomicFileWriter fileWriter = new AtomicFileWriter(Path.of(subName), StandardCharsets.UTF_8)) {
BibWriter bibWriter = new BibWriter(fileWriter, OS.NEWLINE);
SaveConfiguration saveConfiguration = new SaveConfiguration()
SelfContainedSaveConfiguration saveConfiguration = (SelfContainedSaveConfiguration) new SelfContainedSaveConfiguration()
.withReformatOnSave(preferencesService.getLibraryPreferences().shouldAlwaysReformatOnSave());
BibDatabaseWriter databaseWriter = new BibtexDatabaseWriter(
bibWriter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.jabref.logic.exporter.AtomicFileWriter;
import org.jabref.logic.exporter.BibWriter;
import org.jabref.logic.exporter.BibtexDatabaseWriter;
import org.jabref.logic.exporter.SaveConfiguration;
import org.jabref.logic.exporter.SelfContainedSaveConfiguration;
import org.jabref.logic.util.BackupFileType;
import org.jabref.logic.util.CoarseChangeFilter;
import org.jabref.logic.util.io.BackupFileUtil;
Expand Down Expand Up @@ -250,7 +250,7 @@ void performBackup(Path backupPath) {
}
})
.orElse(SaveOrder.getDefaultSaveOrder());
SaveConfiguration saveConfiguration = new SaveConfiguration()
SelfContainedSaveConfiguration saveConfiguration = (SelfContainedSaveConfiguration) new SelfContainedSaveConfiguration()
.withMakeBackup(false)
.withSaveOrder(saveOrder)
.withReformatOnSave(preferences.getLibraryPreferences().shouldAlwaysReformatOnSave());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public ExporterViewModel saveExporter() {
layoutFile.get(),
extension.get(),
preferences.getLayoutFormatterPreferences(),
preferences.getExportConfiguration().getSaveOrder());
preferences.getSelfContainedExportConfiguration().getSelfContainedSaveOrder());
format.setCustomExport(true);
return new ExporterViewModel(format);
}
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/org/jabref/gui/exporter/SaveDatabaseAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
import org.jabref.logic.exporter.BibDatabaseWriter;
import org.jabref.logic.exporter.BibWriter;
import org.jabref.logic.exporter.BibtexDatabaseWriter;
import org.jabref.logic.exporter.SaveConfiguration;
import org.jabref.logic.exporter.SaveException;
import org.jabref.logic.exporter.SelfContainedSaveConfiguration;
import org.jabref.logic.l10n.Encodings;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.shared.DatabaseLocation;
Expand Down Expand Up @@ -256,11 +256,8 @@ private boolean save(Path targetPath, SaveDatabaseMode mode) {

private boolean saveDatabase(Path file, boolean selectedOnly, Charset encoding, BibDatabaseWriter.SaveType saveType, SelfContainedSaveOrder saveOrder) throws SaveException {
// if this code is adapted, please also adapt org.jabref.logic.autosaveandbackup.BackupManager.performBackup

SaveConfiguration saveConfiguration = new SaveConfiguration()
.withSaveType(saveType)
.withSaveOrder(saveOrder)
.withReformatOnSave(preferences.getLibraryPreferences().shouldAlwaysReformatOnSave());
SelfContainedSaveConfiguration saveConfiguration
= new SelfContainedSaveConfiguration(saveOrder, false, saveType, preferences.getLibraryPreferences().shouldAlwaysReformatOnSave());
BibDatabaseContext bibDatabaseContext = libraryTab.getBibDatabaseContext();
synchronized (bibDatabaseContext) {
try (AtomicFileWriter fileWriter = new AtomicFileWriter(file, encoding, saveConfiguration.shouldMakeBackup())) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/logic/crawler/StudyRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import org.jabref.logic.exporter.AtomicFileWriter;
import org.jabref.logic.exporter.BibWriter;
import org.jabref.logic.exporter.BibtexDatabaseWriter;
import org.jabref.logic.exporter.SaveConfiguration;
import org.jabref.logic.exporter.SaveException;
import org.jabref.logic.exporter.SelfContainedSaveConfiguration;
import org.jabref.logic.git.SlrGitHandler;
import org.jabref.logic.importer.OpenDatabase;
import org.jabref.logic.importer.SearchBasedFetcher;
Expand Down Expand Up @@ -427,7 +427,7 @@ private void generateCiteKeys(BibDatabaseContext existingEntries, BibDatabase ta

private void writeResultToFile(Path pathToFile, BibDatabaseContext context) throws SaveException {
try (AtomicFileWriter fileWriter = new AtomicFileWriter(pathToFile, StandardCharsets.UTF_8)) {
SaveConfiguration saveConfiguration = new SaveConfiguration()
SelfContainedSaveConfiguration saveConfiguration = (SelfContainedSaveConfiguration) new SelfContainedSaveConfiguration()
.withSaveOrder(context.getMetaData().getSaveOrder().map(so -> SelfContainedSaveOrder.of(so)).orElse(SaveOrder.getDefaultSaveOrder()))
.withReformatOnSave(preferencesService.getLibraryPreferences().shouldAlwaysReformatOnSave());
BibWriter bibWriter = new BibWriter(fileWriter, OS.NEWLINE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ public enum SaveType { WITH_JABREF_META_DATA, PLAIN_BIBTEX }

private static final Pattern REFERENCE_PATTERN = Pattern.compile("(#[A-Za-z]+#)"); // Used to detect string references in strings
protected final BibWriter bibWriter;
protected final SaveConfiguration saveConfiguration;
protected final SelfContainedSaveConfiguration saveConfiguration;
protected final CitationKeyPatternPreferences keyPatternPreferences;
protected final List<FieldChange> saveActionsFieldChanges = new ArrayList<>();
protected final BibEntryTypesManager entryTypesManager;

public BibDatabaseWriter(BibWriter bibWriter,
SaveConfiguration saveConfiguration,
SelfContainedSaveConfiguration saveConfiguration,
CitationKeyPatternPreferences keyPatternPreferences,
BibEntryTypesManager entryTypesManager) {
this.bibWriter = Objects.requireNonNull(bibWriter);
Expand Down Expand Up @@ -181,7 +181,7 @@ public void savePartOfDatabase(BibDatabaseContext bibDatabaseContext, List<BibEn
writeStrings(bibDatabaseContext.getDatabase());

// Write database entries.
List<BibEntry> sortedEntries = getSortedEntries(entries, saveConfiguration.getSaveOrder());
List<BibEntry> sortedEntries = getSortedEntries(entries, saveConfiguration.getSelfContainedSaveOrder());
List<FieldChange> saveActionChanges = applySaveActions(sortedEntries, bibDatabaseContext.getMetaData());
saveActionsFieldChanges.addAll(saveActionChanges);
if (keyPatternPreferences.shouldGenerateCiteKeysBeforeSaving()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class BibtexDatabaseWriter extends BibDatabaseWriter {
private final FieldPreferences fieldPreferences;

public BibtexDatabaseWriter(BibWriter bibWriter,
SaveConfiguration saveConfiguration,
SelfContainedSaveConfiguration saveConfiguration,
FieldPreferences fieldPreferences,
CitationKeyPatternPreferences citationKeyPatternPreferences,
BibEntryTypesManager entryTypesManager) {
Expand All @@ -49,7 +49,7 @@ public BibtexDatabaseWriter(BibWriter bibWriter,

public BibtexDatabaseWriter(Writer writer,
String newline,
SaveConfiguration saveConfiguration,
SelfContainedSaveConfiguration saveConfiguration,
FieldPreferences fieldPreferences,
CitationKeyPatternPreferences citationKeyPatternPreferences,
BibEntryTypesManager entryTypesManager) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static ExporterFactory create(PreferencesService preferencesService,

List<TemplateExporter> customFormats = preferencesService.getExportPreferences().getCustomExporters();
LayoutFormatterPreferences layoutPreferences = preferencesService.getLayoutFormatterPreferences();
SelfContainedSaveOrder saveOrder = SelfContainedSaveOrder.of(preferencesService.getExportConfiguration().getSaveOrder());
SelfContainedSaveOrder saveOrder = SelfContainedSaveOrder.of(preferencesService.getSelfContainedExportConfiguration().getSaveOrder());
XmpPreferences xmpPreferences = preferencesService.getXmpPreferences();
FieldPreferences fieldPreferences = preferencesService.getFieldPreferences();
BibDatabaseMode bibDatabaseMode = preferencesService.getLibraryPreferences().getDefaultBibDatabaseMode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@

import org.jabref.gui.autosaveandbackup.BackupManager;
import org.jabref.model.metadata.SaveOrder;
import org.jabref.model.metadata.SelfContainedSaveOrder;

public class SaveConfiguration {

// Encoding written at the top of the .bib file.
public static final String ENCODING_PREFIX = "Encoding: ";

private boolean reformatFile;
private SelfContainedSaveOrder saveOrder;
private SaveOrder saveOrder;
private boolean makeBackup;
private BibDatabaseWriter.SaveType saveType;

public SaveConfiguration(SelfContainedSaveOrder saveOrder,
public SaveConfiguration(SaveOrder saveOrder,
Boolean makeBackup,
BibDatabaseWriter.SaveType saveType,
Boolean reformatFile) {
Expand All @@ -31,11 +30,11 @@ public SaveConfiguration() {
false);
}

public SelfContainedSaveOrder getSaveOrder() {
public SaveOrder getSaveOrder() {
return saveOrder;
}

public SaveConfiguration withSaveOrder(SelfContainedSaveOrder newSaveOrder) {
public SaveConfiguration withSaveOrder(SaveOrder newSaveOrder) {
this.saveOrder = newSaveOrder;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.jabref.logic.exporter;

import org.jabref.model.metadata.SaveOrder;
import org.jabref.model.metadata.SelfContainedSaveOrder;

/**
* This is a {@link SaveOrder} where the contained saveConfiguration is a {@link org.jabref.model.metadata.SelfContainedSaveOrder}
*/
public class SelfContainedSaveConfiguration extends SaveConfiguration {
public SelfContainedSaveConfiguration() {
super();
}

public SelfContainedSaveConfiguration(
SelfContainedSaveOrder saveOrder,
Boolean makeBackup,
BibDatabaseWriter.SaveType saveType,
Boolean reformatFile) {
super(saveOrder, makeBackup, saveType, reformatFile);
}

public SelfContainedSaveOrder getSelfContainedSaveOrder() {
return (SelfContainedSaveOrder) getSaveOrder();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* With this class, the user of an instance can directly sort things. Without looking up anything in the preferences or in the UI.
*
* To avoid confusion at the caller, we offer ORIGINAL and SPECIFIED only. Not TABLE.
*/
public class SelfContainedSaveOrder extends SaveOrder {

public static final Logger LOGGER = LoggerFactory.getLogger(SelfContainedSaveOrder.class);

public SelfContainedSaveOrder(OrderType orderType, List<SortCriterion> sortCriteria) {
super(orderType, sortCriteria);
if (orderType == OrderType.TABLE) {
throw new IllegalArgumentException("TABLE requires external lookup.");
LOGGER.debug("TABLE with sort criteria {}", sortCriteria);
throw new IllegalArgumentException("TABLE might require external lookup.");
}
}

Expand All @@ -22,6 +31,11 @@ public static SelfContainedSaveOrder of(SaveOrder saveOrder) {
if (saveOrder instanceof SelfContainedSaveOrder order) {
return order;
}
if ((saveOrder.getOrderType() == OrderType.TABLE) && (!saveOrder.getSortCriteria().isEmpty())) {
// We map from TABLE to SPECIFIED to have the users of this class just to `switch` between
// ORIGINAL and SPECIFIED
return new SelfContainedSaveOrder(OrderType.SPECIFIED, saveOrder.getSortCriteria());
}
return new SelfContainedSaveOrder(saveOrder.getOrderType(), saveOrder.getSortCriteria());
}
}
31 changes: 18 additions & 13 deletions src/main/java/org/jabref/preferences/JabRefPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@
import org.jabref.logic.citationstyle.CitationStyle;
import org.jabref.logic.citationstyle.CitationStylePreviewLayout;
import org.jabref.logic.cleanup.FieldFormatterCleanups;
import org.jabref.logic.exporter.BibDatabaseWriter;
import org.jabref.logic.exporter.MetaDataSerializer;
import org.jabref.logic.exporter.SaveConfiguration;
import org.jabref.logic.exporter.SelfContainedSaveConfiguration;
import org.jabref.logic.exporter.TemplateExporter;
import org.jabref.logic.importer.ImportFormatPreferences;
import org.jabref.logic.importer.ImporterPreferences;
Expand Down Expand Up @@ -2256,29 +2257,33 @@ private void storeExportSaveOrder(SaveOrder saveOrder) {
putBoolean(EXPORT_TERTIARY_SORT_DESCENDING, saveOrder.getSortCriteria().get(2).descending);
}

public SaveOrder getTableSaveOrder() {
/**
* For the export configuration, generates the SelfContainedSaveOrder having the reference to TABLE resolved.
*/
public SelfContainedSaveOrder getSelfContainedTableSaveOrder() {
List<MainTableColumnModel> sortOrder = mainTableColumnPreferences.getColumnSortOrder();
return new SaveOrder(
SaveOrder.OrderType.TABLE,
return new SelfContainedSaveOrder(
SaveOrder.OrderType.SPECIFIED,
sortOrder.stream().flatMap(model -> model.getSortCriteria().stream()).toList());
}

@Override
public SaveConfiguration getExportConfiguration() {
SaveOrder saveOrder = switch (getExportSaveOrder().getOrderType()) {
case TABLE -> this.getTableSaveOrder();
case SPECIFIED -> this.getExportSaveOrder();
public SelfContainedSaveConfiguration getSelfContainedExportConfiguration() {
SaveOrder exportSaveOrder = getExportSaveOrder();
SelfContainedSaveOrder saveOrder = switch (exportSaveOrder.getOrderType()) {
case TABLE -> this.getSelfContainedTableSaveOrder();
case SPECIFIED -> SelfContainedSaveOrder.of(exportSaveOrder);
case ORIGINAL -> SaveOrder.getDefaultSaveOrder();
};

return new SaveConfiguration()
.withSaveOrder(SelfContainedSaveOrder.of(saveOrder))
.withReformatOnSave(getLibraryPreferences().shouldAlwaysReformatOnSave());
return new SelfContainedSaveConfiguration(
saveOrder, false, BibDatabaseWriter.SaveType.WITH_JABREF_META_DATA, getLibraryPreferences()
.shouldAlwaysReformatOnSave());
}

private List<TemplateExporter> getCustomExportFormats() {
LayoutFormatterPreferences layoutPreferences = getLayoutFormatterPreferences();
SaveConfiguration saveConfiguration = getExportConfiguration();
SelfContainedSaveConfiguration saveConfiguration = getSelfContainedExportConfiguration();
List<TemplateExporter> formats = new ArrayList<>();

for (String toImport : getSeries(CUSTOM_EXPORT_FORMAT)) {
Expand All @@ -2288,7 +2293,7 @@ private List<TemplateExporter> getCustomExportFormats() {
formatData.get(EXPORTER_FILENAME_INDEX),
formatData.get(EXPORTER_EXTENSION_INDEX),
layoutPreferences,
saveConfiguration.getSaveOrder());
saveConfiguration.getSelfContainedSaveOrder());
format.setCustomExport(true);
formats.add(format);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/preferences/PreferencesService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.jabref.logic.JabRefException;
import org.jabref.logic.bibtex.FieldPreferences;
import org.jabref.logic.citationkeypattern.CitationKeyPatternPreferences;
import org.jabref.logic.exporter.SaveConfiguration;
import org.jabref.logic.exporter.SelfContainedSaveConfiguration;
import org.jabref.logic.importer.ImportFormatPreferences;
import org.jabref.logic.importer.ImporterPreferences;
import org.jabref.logic.importer.fetcher.GrobidPreferences;
Expand Down Expand Up @@ -73,7 +73,7 @@ public interface PreferencesService {
/**
* Returns the export configuration. The contained SaveConfiguration is a {@link org.jabref.model.metadata.SelfContainedSaveOrder}
*/
SaveConfiguration getExportConfiguration();
SelfContainedSaveConfiguration getSelfContainedExportConfiguration();

BibEntryTypesManager getCustomEntryTypesRepository();

Expand Down
Loading

0 comments on commit 1e0bbea

Please sign in to comment.