diff --git a/src/main/java/org/jabref/gui/JabRefMain.java b/src/main/java/org/jabref/gui/JabRefMain.java index e26b1286c9b..777e35a514d 100644 --- a/src/main/java/org/jabref/gui/JabRefMain.java +++ b/src/main/java/org/jabref/gui/JabRefMain.java @@ -162,10 +162,12 @@ private static void applyPreferences(PreferencesService preferences) { Globals.journalAbbreviationRepository = JournalAbbreviationLoader.loadRepository(preferences.getJournalAbbreviationPreferences()); // Build list of Import and Export formats - Globals.IMPORT_FORMAT_READER.resetImportFormats(preferences.getImporterPreferences(), + Globals.IMPORT_FORMAT_READER.resetImportFormats( + preferences.getImporterPreferences(), preferences.getImportFormatPreferences(), - preferences.getXmpPreferences(), Globals.getFileUpdateMonitor()); - Globals.entryTypesManager.addCustomOrModifiedTypes(preferences.getBibEntryTypes(BibDatabaseMode.BIBTEX), + Globals.getFileUpdateMonitor()); + Globals.entryTypesManager.addCustomOrModifiedTypes( + preferences.getBibEntryTypes(BibDatabaseMode.BIBTEX), preferences.getBibEntryTypes(BibDatabaseMode.BIBLATEX)); Globals.exportFactory = ExporterFactory.create( preferences.getCustomExportFormats(Globals.journalAbbreviationRepository), diff --git a/src/main/java/org/jabref/gui/bibtexextractor/BibtexExtractorViewModel.java b/src/main/java/org/jabref/gui/bibtexextractor/BibtexExtractorViewModel.java index 61db2e93b47..a8e007dda62 100644 --- a/src/main/java/org/jabref/gui/bibtexextractor/BibtexExtractorViewModel.java +++ b/src/main/java/org/jabref/gui/bibtexextractor/BibtexExtractorViewModel.java @@ -64,7 +64,7 @@ public StringProperty inputTextProperty() { } public void startParsing() { - if (preferencesService.getImporterPreferences().isGrobidEnabled()) { + if (preferencesService.getGrobidPreferences().isGrobidEnabled()) { parseUsingGrobid(); } else { parseUsingBibtexExtractor(); @@ -78,7 +78,7 @@ private void parseUsingBibtexExtractor() { } private void parseUsingGrobid() { - GrobidCitationFetcher grobidCitationFetcher = new GrobidCitationFetcher(preferencesService.getImporterPreferences(), preferencesService.getImportFormatPreferences()); + GrobidCitationFetcher grobidCitationFetcher = new GrobidCitationFetcher(preferencesService.getGrobidPreferences(), preferencesService.getImportFormatPreferences()); BackgroundTask.wrap(() -> grobidCitationFetcher.performSearch(inputTextProperty.getValue())) .onRunning(() -> dialogService.notify(Localization.lang("Your text is being parsed..."))) .onFailure((e) -> { diff --git a/src/main/java/org/jabref/gui/bibtexextractor/ExtractBibtexAction.java b/src/main/java/org/jabref/gui/bibtexextractor/ExtractBibtexAction.java index ce198561af4..70c693a5fe9 100644 --- a/src/main/java/org/jabref/gui/bibtexextractor/ExtractBibtexAction.java +++ b/src/main/java/org/jabref/gui/bibtexextractor/ExtractBibtexAction.java @@ -24,7 +24,7 @@ public ExtractBibtexAction(DialogService dialogService, PreferencesService prefe @Override public void execute() { DialogService dialogService = Injector.instantiateModelOrService(DialogService.class); - GrobidOptInDialogHelper.showAndWaitIfUserIsUndecided(dialogService, preferencesService.getImporterPreferences()); + GrobidOptInDialogHelper.showAndWaitIfUserIsUndecided(dialogService, preferencesService.getGrobidPreferences()); dialogService.showCustomDialogAndWait(new ExtractBibtexDialog()); } } diff --git a/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java b/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java index b5f2d72ca39..6e3d662bd6e 100644 --- a/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java +++ b/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java @@ -378,10 +378,9 @@ private void setupToolBar() { if (fetcher instanceof PdfMergeMetadataImporter.EntryBasedFetcherWrapper) { // Handle Grobid Opt-In in case of the PdfMergeMetadataImporter fetcherMenuItem.setOnAction(event -> { - GrobidOptInDialogHelper.showAndWaitIfUserIsUndecided(dialogService, preferencesService.getImporterPreferences()); + GrobidOptInDialogHelper.showAndWaitIfUserIsUndecided(dialogService, preferencesService.getGrobidPreferences()); PdfMergeMetadataImporter.EntryBasedFetcherWrapper pdfMergeMetadataImporter = new PdfMergeMetadataImporter.EntryBasedFetcherWrapper( - preferencesService.getImporterPreferences(), preferencesService.getImportFormatPreferences(), preferencesService.getFilePreferences(), databaseContext); diff --git a/src/main/java/org/jabref/gui/externalfiles/ImportHandler.java b/src/main/java/org/jabref/gui/externalfiles/ImportHandler.java index f8ab4c2354c..b417e49fc3e 100644 --- a/src/main/java/org/jabref/gui/externalfiles/ImportHandler.java +++ b/src/main/java/org/jabref/gui/externalfiles/ImportHandler.java @@ -79,10 +79,7 @@ public ImportHandler(BibDatabaseContext database, this.importFormatReader = importFormatReader; this.linker = new ExternalFilesEntryLinker(preferencesService.getFilePreferences(), database); - this.contentImporter = new ExternalFilesContentImporter( - preferencesService.getGeneralPreferences(), - preferencesService.getImporterPreferences(), - preferencesService.getImportFormatPreferences()); + this.contentImporter = new ExternalFilesContentImporter(preferencesService.getImportFormatPreferences()); this.undoManager = undoManager; } diff --git a/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java b/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java index b2f9e1749d7..20dd904978b 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java +++ b/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java @@ -548,8 +548,8 @@ public void parsePdfMetadataAndShowMergeDialog() { dialog.addSource(Localization.lang("Entry"), entry); dialog.addSource(Localization.lang("Verbatim"), wrapImporterToSupplier(new PdfVerbatimBibTextImporter(preferences.getImportFormatPreferences()), filePath)); dialog.addSource(Localization.lang("Embedded"), wrapImporterToSupplier(new PdfEmbeddedBibFileImporter(preferences.getImportFormatPreferences()), filePath)); - if (preferences.getImporterPreferences().isGrobidEnabled()) { - dialog.addSource("Grobid", wrapImporterToSupplier(new PdfGrobidImporter(preferences.getImporterPreferences(), preferences.getImportFormatPreferences()), filePath)); + if (preferences.getGrobidPreferences().isGrobidEnabled()) { + dialog.addSource("Grobid", wrapImporterToSupplier(new PdfGrobidImporter(preferences.getImportFormatPreferences()), filePath)); } dialog.addSource(Localization.lang("XMP metadata"), wrapImporterToSupplier(new PdfXmpImporter(preferences.getXmpPreferences()), filePath)); dialog.addSource(Localization.lang("Content"), wrapImporterToSupplier(new PdfContentImporter(preferences.getImportFormatPreferences()), filePath)); diff --git a/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditor.java b/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditor.java index 08a329d508d..8a1a0c81fc9 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditor.java +++ b/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditor.java @@ -190,7 +190,7 @@ private Node createFileDisplay(LinkedFileViewModel linkedFile) { parsePdfMetadata.setTooltip(new Tooltip(Localization.lang("Parse Metadata from PDF."))); parsePdfMetadata.visibleProperty().bind(linkedFile.isOfflinePdfProperty()); parsePdfMetadata.setOnAction(event -> { - GrobidOptInDialogHelper.showAndWaitIfUserIsUndecided(dialogService, preferencesService.getImporterPreferences()); + GrobidOptInDialogHelper.showAndWaitIfUserIsUndecided(dialogService, preferencesService.getGrobidPreferences()); linkedFile.parsePdfMetadataAndShowMergeDialog(); }); parsePdfMetadata.getStyleClass().setAll("icon-button"); diff --git a/src/main/java/org/jabref/gui/importer/GrobidOptInDialogHelper.java b/src/main/java/org/jabref/gui/importer/GrobidOptInDialogHelper.java index d58d6c8d5d4..cf21a38a30c 100644 --- a/src/main/java/org/jabref/gui/importer/GrobidOptInDialogHelper.java +++ b/src/main/java/org/jabref/gui/importer/GrobidOptInDialogHelper.java @@ -1,7 +1,7 @@ package org.jabref.gui.importer; import org.jabref.gui.DialogService; -import org.jabref.logic.importer.ImporterPreferences; +import org.jabref.logic.importer.fetcher.GrobidPreferences; import org.jabref.logic.l10n.Localization; /** @@ -18,7 +18,7 @@ public class GrobidOptInDialogHelper { * @param dialogService the DialogService to use * @return if the user enabled Grobid, either in the past or after being asked by the dialog. */ - public static boolean showAndWaitIfUserIsUndecided(DialogService dialogService, ImporterPreferences preferences) { + public static boolean showAndWaitIfUserIsUndecided(DialogService dialogService, GrobidPreferences preferences) { if (preferences.isGrobidEnabled()) { return true; } diff --git a/src/main/java/org/jabref/gui/importer/ImportAction.java b/src/main/java/org/jabref/gui/importer/ImportAction.java index 3b08767886d..4b32d0d74db 100644 --- a/src/main/java/org/jabref/gui/importer/ImportAction.java +++ b/src/main/java/org/jabref/gui/importer/ImportAction.java @@ -121,8 +121,11 @@ private List doImport(List files) if (importer.isEmpty()) { // Unknown format: DefaultTaskExecutor.runAndWaitInJavaFXThread(() -> { - if (fileIsPdf(filename) && GrobidOptInDialogHelper.showAndWaitIfUserIsUndecided(frame.getDialogService(), prefs.getImporterPreferences())) { - Globals.IMPORT_FORMAT_READER.resetImportFormats(prefs.getImporterPreferences(), prefs.getImportFormatPreferences(), prefs.getXmpPreferences(), Globals.getFileUpdateMonitor()); + if (fileIsPdf(filename) && GrobidOptInDialogHelper.showAndWaitIfUserIsUndecided(frame.getDialogService(), prefs.getGrobidPreferences())) { + Globals.IMPORT_FORMAT_READER.resetImportFormats( + prefs.getImporterPreferences(), + prefs.getImportFormatPreferences(), + Globals.getFileUpdateMonitor()); } frame.getDialogService().notify(Localization.lang("Importing in unknown format") + "..."); }); @@ -130,8 +133,13 @@ private List doImport(List files) imports.add(Globals.IMPORT_FORMAT_READER.importUnknownFormat(filename, Globals.getFileUpdateMonitor())); } else { DefaultTaskExecutor.runAndWaitInJavaFXThread(() -> { - if ((importer.get() instanceof PdfGrobidImporter) || ((importer.get() instanceof PdfMergeMetadataImporter) && GrobidOptInDialogHelper.showAndWaitIfUserIsUndecided(frame.getDialogService(), prefs.getImporterPreferences()))) { - Globals.IMPORT_FORMAT_READER.resetImportFormats(prefs.getImporterPreferences(), prefs.getImportFormatPreferences(), prefs.getXmpPreferences(), Globals.getFileUpdateMonitor()); + if ((importer.get() instanceof PdfGrobidImporter) || ( + (importer.get() instanceof PdfMergeMetadataImporter) + && GrobidOptInDialogHelper.showAndWaitIfUserIsUndecided(frame.getDialogService(), prefs.getGrobidPreferences()))) { + Globals.IMPORT_FORMAT_READER.resetImportFormats( + prefs.getImporterPreferences(), + prefs.getImportFormatPreferences(), + Globals.getFileUpdateMonitor()); } frame.getDialogService().notify(Localization.lang("Importing in %0 format", importer.get().getName()) + "..."); }); diff --git a/src/main/java/org/jabref/gui/preferences/customimporter/CustomImporterTabViewModel.java b/src/main/java/org/jabref/gui/preferences/customimporter/CustomImporterTabViewModel.java index 08012693858..2f8065d5615 100644 --- a/src/main/java/org/jabref/gui/preferences/customimporter/CustomImporterTabViewModel.java +++ b/src/main/java/org/jabref/gui/preferences/customimporter/CustomImporterTabViewModel.java @@ -42,7 +42,7 @@ public CustomImporterTabViewModel(PreferencesService preferences, DialogService @Override public void setValues() { - Set importersLogic = preferences.getCustomImportFormats(); + Set importersLogic = preferences.getImporterPreferences().getCustomImportList(); for (CustomImporter importer : importersLogic) { importers.add(new ImporterViewModel(importer)); } @@ -50,13 +50,13 @@ public void setValues() { @Override public void storeSettings() { - preferences.storeCustomImportFormats(importers.stream() - .map(ImporterViewModel::getLogic) - .collect(Collectors.toSet())); + preferences.getImporterPreferences().getCustomImportList().clear(); + preferences.getImporterPreferences().getCustomImportList().addAll(importers.stream() + .map(ImporterViewModel::getLogic) + .collect(Collectors.toSet())); Globals.IMPORT_FORMAT_READER.resetImportFormats( preferences.getImporterPreferences(), preferences.getImportFormatPreferences(), - preferences.getXmpPreferences(), Globals.getFileUpdateMonitor()); } diff --git a/src/main/java/org/jabref/gui/preferences/importexport/ImportExportTab.java b/src/main/java/org/jabref/gui/preferences/importexport/ImportExportTab.java index 86fc0ba223a..49c7fd20563 100644 --- a/src/main/java/org/jabref/gui/preferences/importexport/ImportExportTab.java +++ b/src/main/java/org/jabref/gui/preferences/importexport/ImportExportTab.java @@ -46,7 +46,7 @@ public String getTabName() { } public void initialize() { - this.viewModel = new ImportExportTabViewModel(preferencesService, preferencesService.getDOIPreferences(), dialogService, preferencesService.getImportExportPreferences()); + this.viewModel = new ImportExportTabViewModel(preferencesService, dialogService); useCustomDOI.selectedProperty().bindBidirectional(viewModel.useCustomDOIProperty()); useCustomDOIName.textProperty().bindBidirectional(viewModel.useCustomDOINameProperty()); diff --git a/src/main/java/org/jabref/gui/preferences/importexport/ImportExportTabViewModel.java b/src/main/java/org/jabref/gui/preferences/importexport/ImportExportTabViewModel.java index 3c3fe3472bd..c3e1edef304 100644 --- a/src/main/java/org/jabref/gui/preferences/importexport/ImportExportTabViewModel.java +++ b/src/main/java/org/jabref/gui/preferences/importexport/ImportExportTabViewModel.java @@ -27,6 +27,7 @@ import org.jabref.logic.importer.ImporterPreferences; import org.jabref.logic.importer.WebFetchers; import org.jabref.logic.importer.fetcher.CustomizableKeyFetcher; +import org.jabref.logic.importer.fetcher.GrobidPreferences; import org.jabref.logic.l10n.Localization; import org.jabref.logic.net.URLDownload; import org.jabref.logic.preferences.DOIPreferences; @@ -61,17 +62,19 @@ public class ImportExportTabViewModel implements PreferenceTabViewModel { private final DialogService dialogService; private final PreferencesService preferencesService; private final DOIPreferences doiPreferences; + private final GrobidPreferences grobidPreferences; private final ImporterPreferences importerPreferences; private final SaveOrderConfig initialExportOrder; private final ImportExportPreferences importExportPreferences; - public ImportExportTabViewModel(PreferencesService preferencesService, DOIPreferences doiPreferences, DialogService dialogService, ImportExportPreferences importExportPreferences) { + public ImportExportTabViewModel(PreferencesService preferencesService, DialogService dialogService) { this.dialogService = dialogService; this.preferencesService = preferencesService; this.importerPreferences = preferencesService.getImporterPreferences(); - this.doiPreferences = doiPreferences; + this.grobidPreferences = preferencesService.getGrobidPreferences(); + this.doiPreferences = preferencesService.getDOIPreferences(); this.initialExportOrder = preferencesService.getExportSaveOrder(); - this.importExportPreferences = importExportPreferences; + this.importExportPreferences = preferencesService.getImportExportPreferences(); } @Override @@ -95,8 +98,8 @@ public void setValues() { .map(SortCriterionViewModel::new) .toList()); - grobidEnabledProperty.setValue(importerPreferences.isGrobidEnabled()); - grobidURLProperty.setValue(importerPreferences.getGrobidURL()); + grobidEnabledProperty.setValue(grobidPreferences.isGrobidEnabled()); + grobidURLProperty.setValue(grobidPreferences.getGrobidURL()); apiKeys.setValue(FXCollections.observableArrayList(preferencesService.getImporterPreferences().getApiKeys())); } @@ -104,9 +107,9 @@ public void setValues() { @Override public void storeSettings() { importerPreferences.setGenerateNewKeyOnImport(generateKeyOnImportProperty.getValue()); - importerPreferences.setGrobidEnabled(grobidEnabledProperty.getValue()); - importerPreferences.setGrobidOptOut(importerPreferences.isGrobidOptOut()); - importerPreferences.setGrobidURL(grobidURLProperty.getValue()); + grobidPreferences.setGrobidEnabled(grobidEnabledProperty.getValue()); + grobidPreferences.setGrobidOptOut(grobidPreferences.isGrobidOptOut()); + grobidPreferences.setGrobidURL(grobidURLProperty.getValue()); doiPreferences.setUseCustom(useCustomDOIProperty.get()); doiPreferences.setDefaultBaseURI(useCustomDOINameProperty.getValue().trim()); diff --git a/src/main/java/org/jabref/logic/externalfiles/ExternalFilesContentImporter.java b/src/main/java/org/jabref/logic/externalfiles/ExternalFilesContentImporter.java index 3158a96652e..e75b1df2683 100644 --- a/src/main/java/org/jabref/logic/externalfiles/ExternalFilesContentImporter.java +++ b/src/main/java/org/jabref/logic/externalfiles/ExternalFilesContentImporter.java @@ -4,28 +4,22 @@ import java.nio.file.Path; import org.jabref.logic.importer.ImportFormatPreferences; -import org.jabref.logic.importer.ImporterPreferences; import org.jabref.logic.importer.OpenDatabase; import org.jabref.logic.importer.ParserResult; import org.jabref.logic.importer.fileformat.PdfMergeMetadataImporter; import org.jabref.model.util.FileUpdateMonitor; -import org.jabref.preferences.GeneralPreferences; public class ExternalFilesContentImporter { - private final GeneralPreferences generalPreferences; - private final ImporterPreferences importerPreferences; private final ImportFormatPreferences importFormatPreferences; - public ExternalFilesContentImporter(GeneralPreferences generalPreferences, ImporterPreferences importerPreferences, ImportFormatPreferences importFormatPreferences) { - this.generalPreferences = generalPreferences; - this.importerPreferences = importerPreferences; + public ExternalFilesContentImporter(ImportFormatPreferences importFormatPreferences) { this.importFormatPreferences = importFormatPreferences; } public ParserResult importPDFContent(Path file) { try { - return new PdfMergeMetadataImporter(importerPreferences, importFormatPreferences).importDatabase(file); + return new PdfMergeMetadataImporter(importFormatPreferences).importDatabase(file); } catch (IOException e) { return ParserResult.fromError(e); } diff --git a/src/main/java/org/jabref/logic/importer/ImportFormatPreferences.java b/src/main/java/org/jabref/logic/importer/ImportFormatPreferences.java index f051370ed81..8b8fa654053 100644 --- a/src/main/java/org/jabref/logic/importer/ImportFormatPreferences.java +++ b/src/main/java/org/jabref/logic/importer/ImportFormatPreferences.java @@ -1,48 +1,38 @@ package org.jabref.logic.importer; -import java.util.Set; - import org.jabref.logic.bibtex.FieldContentFormatterPreferences; import org.jabref.logic.citationkeypattern.CitationKeyPatternPreferences; -import org.jabref.logic.importer.fileformat.CustomImporter; +import org.jabref.logic.importer.fetcher.GrobidPreferences; import org.jabref.logic.preferences.DOIPreferences; import org.jabref.logic.xmp.XmpPreferences; public class ImportFormatPreferences { - private final Set customImportList; private final Character keywordSeparator; private final CitationKeyPatternPreferences citationKeyPatternPreferences; private final FieldContentFormatterPreferences fieldContentFormatterPreferences; private final XmpPreferences xmpPreferences; private final DOIPreferences doiPreferences; + private final GrobidPreferences grobidPreferences; - public ImportFormatPreferences(Set customImportList, - Character keywordSeparator, + public ImportFormatPreferences(Character keywordSeparator, CitationKeyPatternPreferences citationKeyPatternPreferences, FieldContentFormatterPreferences fieldContentFormatterPreferences, XmpPreferences xmpPreferences, - DOIPreferences doiPreferences) { - this.customImportList = customImportList; + DOIPreferences doiPreferences, + GrobidPreferences grobidPreferences) { this.keywordSeparator = keywordSeparator; this.citationKeyPatternPreferences = citationKeyPatternPreferences; this.fieldContentFormatterPreferences = fieldContentFormatterPreferences; this.xmpPreferences = xmpPreferences; this.doiPreferences = doiPreferences; + this.grobidPreferences = grobidPreferences; } public DOIPreferences getDoiPreferences() { return doiPreferences; } - /** - * @deprecated importer should not know about the other custom importers - */ - @Deprecated - public Set getCustomImportList() { - return customImportList; - } - public Character getKeywordSeparator() { return keywordSeparator; } @@ -58,4 +48,8 @@ public FieldContentFormatterPreferences getFieldContentFormatterPreferences() { public XmpPreferences getXmpPreferences() { return xmpPreferences; } + + public GrobidPreferences getGrobidPreferences() { + return grobidPreferences; + } } diff --git a/src/main/java/org/jabref/logic/importer/ImportFormatReader.java b/src/main/java/org/jabref/logic/importer/ImportFormatReader.java index b9b6f4b850c..67ceeb59647 100644 --- a/src/main/java/org/jabref/logic/importer/ImportFormatReader.java +++ b/src/main/java/org/jabref/logic/importer/ImportFormatReader.java @@ -34,7 +34,6 @@ import org.jabref.logic.importer.fileformat.RisImporter; import org.jabref.logic.importer.fileformat.SilverPlatterImporter; import org.jabref.logic.l10n.Localization; -import org.jabref.logic.xmp.XmpPreferences; import org.jabref.model.database.BibDatabases; import org.jabref.model.entry.BibEntry; import org.jabref.model.strings.StringUtil; @@ -52,7 +51,9 @@ public class ImportFormatReader { private ImportFormatPreferences importFormatPreferences; - public void resetImportFormats(ImporterPreferences importerPreferences, ImportFormatPreferences newImportFormatPreferences, XmpPreferences xmpPreferences, FileUpdateMonitor fileMonitor) { + public void resetImportFormats(ImporterPreferences importerPreferences, + ImportFormatPreferences newImportFormatPreferences, + FileUpdateMonitor fileMonitor) { this.importFormatPreferences = newImportFormatPreferences; formats.clear(); @@ -68,14 +69,14 @@ public void resetImportFormats(ImporterPreferences importerPreferences, ImportFo formats.add(new ModsImporter(importFormatPreferences)); formats.add(new MsBibImporter()); formats.add(new OvidImporter()); - formats.add(new PdfMergeMetadataImporter(importerPreferences, importFormatPreferences)); + formats.add(new PdfMergeMetadataImporter(importFormatPreferences)); formats.add(new PdfVerbatimBibTextImporter(importFormatPreferences)); formats.add(new PdfContentImporter(importFormatPreferences)); formats.add(new PdfEmbeddedBibFileImporter(importFormatPreferences)); - if (importerPreferences.isGrobidEnabled()) { - formats.add(new PdfGrobidImporter(importerPreferences, importFormatPreferences)); + if (importFormatPreferences.getGrobidPreferences().isGrobidEnabled()) { + formats.add(new PdfGrobidImporter(importFormatPreferences)); } - formats.add(new PdfXmpImporter(xmpPreferences)); + formats.add(new PdfXmpImporter(importFormatPreferences.getXmpPreferences())); formats.add(new RepecNepImporter(importFormatPreferences)); formats.add(new RisImporter()); formats.add(new SilverPlatterImporter()); @@ -85,7 +86,7 @@ public void resetImportFormats(ImporterPreferences importerPreferences, ImportFo formats.add(new CitaviXmlImporter()); // Get custom import formats - formats.addAll(importFormatPreferences.getCustomImportList()); + formats.addAll(importerPreferences.getCustomImportList()); } /** diff --git a/src/main/java/org/jabref/logic/importer/ImporterPreferences.java b/src/main/java/org/jabref/logic/importer/ImporterPreferences.java index 0e62500c03a..198eab8e51e 100644 --- a/src/main/java/org/jabref/logic/importer/ImporterPreferences.java +++ b/src/main/java/org/jabref/logic/importer/ImporterPreferences.java @@ -4,30 +4,23 @@ import javafx.beans.property.BooleanProperty; import javafx.beans.property.SimpleBooleanProperty; -import javafx.beans.property.SimpleStringProperty; -import javafx.beans.property.StringProperty; import javafx.collections.FXCollections; import javafx.collections.ObservableSet; +import org.jabref.logic.importer.fileformat.CustomImporter; import org.jabref.logic.preferences.FetcherApiKey; public class ImporterPreferences { private final BooleanProperty generateNewKeyOnImport; - private final BooleanProperty grobidEnabled; - private final BooleanProperty grobidOptOut; - private final StringProperty grobidURL; private final ObservableSet apiKeys; + private final ObservableSet customImportList; public ImporterPreferences(boolean generateNewKeyOnImport, - boolean grobidEnabled, - boolean grobidOptOut, - String grobidURL, + Set customImportList, Set apiKeys) { this.generateNewKeyOnImport = new SimpleBooleanProperty(generateNewKeyOnImport); - this.grobidEnabled = new SimpleBooleanProperty(grobidEnabled); - this.grobidOptOut = new SimpleBooleanProperty(grobidOptOut); - this.grobidURL = new SimpleStringProperty(grobidURL); + this.customImportList = FXCollections.observableSet(customImportList); this.apiKeys = FXCollections.observableSet(apiKeys); } @@ -39,47 +32,15 @@ public BooleanProperty generateNewKeyOnImportProperty() { return generateNewKeyOnImport; } - public boolean isGrobidEnabled() { - return grobidEnabled.get(); - } - - public BooleanProperty grobidEnabledProperty() { - return grobidEnabled; - } - - public boolean isGrobidOptOut() { - return grobidOptOut.get(); - } - - public BooleanProperty grobidOptOutProperty() { - return grobidOptOut; - } - - public String getGrobidURL() { - return grobidURL.get(); - } - - public StringProperty grobidURLProperty() { - return grobidURL; - } - public void setGenerateNewKeyOnImport(boolean generateNewKeyOnImport) { this.generateNewKeyOnImport.set(generateNewKeyOnImport); } - public void setGrobidEnabled(boolean grobidEnabled) { - this.grobidEnabled.set(grobidEnabled); - } - - public void setGrobidOptOut(boolean grobidOptOut) { - this.grobidOptOut.set(grobidOptOut); - } - - public void setGrobidURL(String grobidURL) { - this.grobidURL.set(grobidURL); - } - public ObservableSet getApiKeys() { return apiKeys; } + + public ObservableSet getCustomImportList() { + return customImportList; + } } diff --git a/src/main/java/org/jabref/logic/importer/WebFetchers.java b/src/main/java/org/jabref/logic/importer/WebFetchers.java index 783e0bcb335..5a78fe3e4e1 100644 --- a/src/main/java/org/jabref/logic/importer/WebFetchers.java +++ b/src/main/java/org/jabref/logic/importer/WebFetchers.java @@ -146,7 +146,10 @@ public static SortedSet getIdBasedFetchers(ImportFormatPreferenc /** * @return sorted set containing entry based fetchers */ - public static SortedSet getEntryBasedFetchers(ImporterPreferences importerPreferences, ImportFormatPreferences importFormatPreferences, FilePreferences filePreferences, BibDatabaseContext databaseContext) { + public static SortedSet getEntryBasedFetchers(ImporterPreferences importerPreferences, + ImportFormatPreferences importFormatPreferences, + FilePreferences filePreferences, + BibDatabaseContext databaseContext) { SortedSet set = new TreeSet<>(Comparator.comparing(WebFetcher::getName)); set.add(new AstrophysicsDataSystem(importFormatPreferences, importerPreferences)); set.add(new DoiFetcher(importFormatPreferences)); @@ -154,7 +157,7 @@ public static SortedSet getEntryBasedFetchers(ImporterPrefere set.add(new MathSciNet(importFormatPreferences)); set.add(new CrossRef()); set.add(new ZbMATH(importFormatPreferences)); - set.add(new PdfMergeMetadataImporter.EntryBasedFetcherWrapper(importerPreferences, importFormatPreferences, filePreferences, databaseContext)); + set.add(new PdfMergeMetadataImporter.EntryBasedFetcherWrapper(importFormatPreferences, filePreferences, databaseContext)); set.add(new SemanticScholar()); set.add(new ResearchGate(importFormatPreferences)); return set; diff --git a/src/main/java/org/jabref/logic/importer/fetcher/GrobidCitationFetcher.java b/src/main/java/org/jabref/logic/importer/fetcher/GrobidCitationFetcher.java index 4dccde35980..c33d7e83ac8 100644 --- a/src/main/java/org/jabref/logic/importer/fetcher/GrobidCitationFetcher.java +++ b/src/main/java/org/jabref/logic/importer/fetcher/GrobidCitationFetcher.java @@ -10,7 +10,6 @@ import org.jabref.logic.importer.FetcherException; import org.jabref.logic.importer.ImportFormatPreferences; -import org.jabref.logic.importer.ImporterPreferences; import org.jabref.logic.importer.ParseException; import org.jabref.logic.importer.SearchBasedFetcher; import org.jabref.logic.importer.util.GrobidService; @@ -24,11 +23,11 @@ public class GrobidCitationFetcher implements SearchBasedFetcher { private static final Logger LOGGER = LoggerFactory.getLogger(GrobidCitationFetcher.class); - private ImportFormatPreferences importFormatPreferences; - private GrobidService grobidService; + private final ImportFormatPreferences importFormatPreferences; + private final GrobidService grobidService; - public GrobidCitationFetcher(ImporterPreferences importerPreferences, ImportFormatPreferences importFormatPreferences) { - this(importFormatPreferences, new GrobidService(importerPreferences)); + public GrobidCitationFetcher(GrobidPreferences grobidPreferences, ImportFormatPreferences importFormatPreferences) { + this(importFormatPreferences, new GrobidService(grobidPreferences)); } GrobidCitationFetcher(ImportFormatPreferences importFormatPreferences, GrobidService grobidService) { diff --git a/src/main/java/org/jabref/logic/importer/fetcher/GrobidPreferences.java b/src/main/java/org/jabref/logic/importer/fetcher/GrobidPreferences.java new file mode 100644 index 00000000000..e87b54f08fe --- /dev/null +++ b/src/main/java/org/jabref/logic/importer/fetcher/GrobidPreferences.java @@ -0,0 +1,56 @@ +package org.jabref.logic.importer.fetcher; + +import javafx.beans.property.BooleanProperty; +import javafx.beans.property.SimpleBooleanProperty; +import javafx.beans.property.SimpleStringProperty; +import javafx.beans.property.StringProperty; + +public class GrobidPreferences { + private final BooleanProperty grobidEnabled; + private final BooleanProperty grobidOptOut; + private final StringProperty grobidURL; + + public GrobidPreferences(boolean grobidEnabled, + boolean grobidOptOut, + String grobidURL) { + this.grobidEnabled = new SimpleBooleanProperty(grobidEnabled); + this.grobidOptOut = new SimpleBooleanProperty(grobidOptOut); + this.grobidURL = new SimpleStringProperty(grobidURL); + } + + public boolean isGrobidEnabled() { + return grobidEnabled.get(); + } + + public BooleanProperty grobidEnabledProperty() { + return grobidEnabled; + } + + public void setGrobidEnabled(boolean grobidEnabled) { + this.grobidEnabled.set(grobidEnabled); + } + + public boolean isGrobidOptOut() { + return grobidOptOut.get(); + } + + public BooleanProperty grobidOptOutProperty() { + return grobidOptOut; + } + + public void setGrobidOptOut(boolean grobidOptOut) { + this.grobidOptOut.set(grobidOptOut); + } + + public String getGrobidURL() { + return grobidURL.get(); + } + + public StringProperty grobidURLProperty() { + return grobidURL; + } + + public void setGrobidURL(String grobidURL) { + this.grobidURL.set(grobidURL); + } +} diff --git a/src/main/java/org/jabref/logic/importer/fileformat/PdfGrobidImporter.java b/src/main/java/org/jabref/logic/importer/fileformat/PdfGrobidImporter.java index 9312838ade0..bb013d58118 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/PdfGrobidImporter.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/PdfGrobidImporter.java @@ -9,7 +9,6 @@ import org.jabref.logic.importer.ImportFormatPreferences; import org.jabref.logic.importer.Importer; -import org.jabref.logic.importer.ImporterPreferences; import org.jabref.logic.importer.ParserResult; import org.jabref.logic.importer.util.GrobidService; import org.jabref.logic.util.StandardFileType; @@ -25,8 +24,8 @@ public class PdfGrobidImporter extends Importer { private final GrobidService grobidService; private final ImportFormatPreferences importFormatPreferences; - public PdfGrobidImporter(ImporterPreferences importerPreferences, ImportFormatPreferences importFormatPreferences) { - this.grobidService = new GrobidService(importerPreferences); + public PdfGrobidImporter(ImportFormatPreferences importFormatPreferences) { + this.grobidService = new GrobidService(importFormatPreferences.getGrobidPreferences()); this.importFormatPreferences = importFormatPreferences; } diff --git a/src/main/java/org/jabref/logic/importer/fileformat/PdfMergeMetadataImporter.java b/src/main/java/org/jabref/logic/importer/fileformat/PdfMergeMetadataImporter.java index a53ff26d6b3..54361405566 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/PdfMergeMetadataImporter.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/PdfMergeMetadataImporter.java @@ -15,7 +15,6 @@ import org.jabref.logic.importer.FetcherException; import org.jabref.logic.importer.ImportFormatPreferences; import org.jabref.logic.importer.Importer; -import org.jabref.logic.importer.ImporterPreferences; import org.jabref.logic.importer.ParserResult; import org.jabref.logic.importer.fetcher.DoiFetcher; import org.jabref.logic.importer.fetcher.IsbnFetcher; @@ -41,13 +40,13 @@ public class PdfMergeMetadataImporter extends Importer { private final List metadataImporters; private final ImportFormatPreferences importFormatPreferences; - public PdfMergeMetadataImporter(ImporterPreferences importerPreferences, ImportFormatPreferences importFormatPreferences) { + public PdfMergeMetadataImporter(ImportFormatPreferences importFormatPreferences) { this.importFormatPreferences = importFormatPreferences; this.metadataImporters = new ArrayList<>(); this.metadataImporters.add(new PdfVerbatimBibTextImporter(importFormatPreferences)); this.metadataImporters.add(new PdfEmbeddedBibFileImporter(importFormatPreferences)); - if (importerPreferences.isGrobidEnabled()) { - this.metadataImporters.add(new PdfGrobidImporter(importerPreferences, importFormatPreferences)); + if (importFormatPreferences.getGrobidPreferences().isGrobidEnabled()) { + this.metadataImporters.add(new PdfGrobidImporter(importFormatPreferences)); } this.metadataImporters.add(new PdfXmpImporter(importFormatPreferences.getXmpPreferences())); this.metadataImporters.add(new PdfContentImporter(importFormatPreferences)); @@ -149,8 +148,8 @@ public static class EntryBasedFetcherWrapper extends PdfMergeMetadataImporter im private final FilePreferences filePreferences; private final BibDatabaseContext databaseContext; - public EntryBasedFetcherWrapper(ImporterPreferences importerPreferences, ImportFormatPreferences importFormatPreferences, FilePreferences filePreferences, BibDatabaseContext context) { - super(importerPreferences, importFormatPreferences); + public EntryBasedFetcherWrapper(ImportFormatPreferences importFormatPreferences, FilePreferences filePreferences, BibDatabaseContext context) { + super(importFormatPreferences); this.filePreferences = filePreferences; this.databaseContext = context; } diff --git a/src/main/java/org/jabref/logic/importer/util/GrobidService.java b/src/main/java/org/jabref/logic/importer/util/GrobidService.java index f781fa71892..f824b351d4f 100644 --- a/src/main/java/org/jabref/logic/importer/util/GrobidService.java +++ b/src/main/java/org/jabref/logic/importer/util/GrobidService.java @@ -7,8 +7,8 @@ import java.util.Optional; import org.jabref.logic.importer.ImportFormatPreferences; -import org.jabref.logic.importer.ImporterPreferences; import org.jabref.logic.importer.ParseException; +import org.jabref.logic.importer.fetcher.GrobidPreferences; import org.jabref.logic.importer.fileformat.BibtexParser; import org.jabref.model.entry.BibEntry; import org.jabref.model.util.DummyFileUpdateMonitor; @@ -30,7 +30,7 @@ public class GrobidService { public enum ConsolidateCitations { NO(0), WITH_METADATA(1), WITH_DOI_ONLY(2); - private int code; + private final int code; ConsolidateCitations(int code) { this.code = code; @@ -41,11 +41,11 @@ public int getCode() { } } - private final ImporterPreferences importerPreferences; + private final GrobidPreferences grobidPreferences; - public GrobidService(ImporterPreferences importerPreferences) { - this.importerPreferences = importerPreferences; - if (!importerPreferences.isGrobidEnabled()) { + public GrobidService(GrobidPreferences grobidPreferences) { + this.grobidPreferences = grobidPreferences; + if (!grobidPreferences.isGrobidEnabled()) { throw new UnsupportedOperationException("Grobid was used but not enabled."); } } @@ -57,7 +57,7 @@ public GrobidService(ImporterPreferences importerPreferences) { * @throws IOException if an I/O exception during the call occurred or no BibTeX entry could be determined */ public Optional processCitation(String rawCitation, ImportFormatPreferences importFormatPreferences, ConsolidateCitations consolidateCitations) throws IOException, ParseException { - Connection.Response response = Jsoup.connect(importerPreferences.getGrobidURL() + "/api/processCitation") + Connection.Response response = Jsoup.connect(grobidPreferences.getGrobidURL() + "/api/processCitation") .header("Accept", MediaTypes.APPLICATION_BIBTEX) .data("citations", rawCitation) .data("consolidateCitations", String.valueOf(consolidateCitations.getCode())) @@ -75,7 +75,7 @@ public Optional processCitation(String rawCitation, ImportFormatPrefer } public List processPDF(Path filePath, ImportFormatPreferences importFormatPreferences) throws IOException, ParseException { - Connection.Response response = Jsoup.connect(importerPreferences.getGrobidURL() + "/api/processHeaderDocument") + Connection.Response response = Jsoup.connect(grobidPreferences.getGrobidURL() + "/api/processHeaderDocument") .header("Accept", MediaTypes.APPLICATION_BIBTEX) .data("input", filePath.toString(), Files.newInputStream(filePath)) .method(Connection.Method.POST) diff --git a/src/main/java/org/jabref/preferences/JabRefPreferences.java b/src/main/java/org/jabref/preferences/JabRefPreferences.java index 73d703848e1..1ec582152f6 100644 --- a/src/main/java/org/jabref/preferences/JabRefPreferences.java +++ b/src/main/java/org/jabref/preferences/JabRefPreferences.java @@ -74,6 +74,7 @@ import org.jabref.logic.importer.ImportFormatPreferences; import org.jabref.logic.importer.ImporterPreferences; import org.jabref.logic.importer.fetcher.DoiFetcher; +import org.jabref.logic.importer.fetcher.GrobidPreferences; import org.jabref.logic.importer.fileformat.CustomImporter; import org.jabref.logic.journals.JournalAbbreviationPreferences; import org.jabref.logic.journals.JournalAbbreviationRepository; @@ -429,13 +430,13 @@ public class JabRefPreferences implements PreferencesService { private List searchDialogTableColunns; private List searchDialogColumnSortOrder; - private Set customImporters; private String userName; private PreviewPreferences previewPreferences; private SidePanePreferences sidePanePreferences; private AppearancePreferences appearancePreferences; private ImporterPreferences importerPreferences; + private GrobidPreferences grobidPreferences; private ProtectedTermsPreferences protectedTermsPreferences; private MrDlibPreferences mrDlibPreferences; private EntryEditorPreferences entryEditorPreferences; @@ -760,6 +761,24 @@ private static String convertListToString(List value) { return value.stream().map(val -> StringUtil.quote(val, STRINGLIST_DELIMITER.toString(), '\\')).collect(Collectors.joining(STRINGLIST_DELIMITER.toString())); } + private static List convertStringToList(String toConvert) { + if (StringUtil.isBlank(toConvert)) { + return Collections.emptyList(); + } + + StringReader reader = new StringReader(toConvert); + List result = new ArrayList<>(); + Optional rs; + try { + while ((rs = getNextUnit(reader)).isPresent()) { + result.add(rs.get()); + } + } catch (IOException ignored) { + // Ignored + } + return result; + } + private static Preferences getPrefsNodeForCustomizedEntryTypes(BibDatabaseMode mode) { return switch (mode) { case BIBTEX -> PREFS_NODE.node(CUSTOMIZED_BIBTEX_TYPES); @@ -932,22 +951,7 @@ public void putStringList(String key, List value) { * Returns a List of Strings containing the chosen columns. */ public List getStringList(String key) { - String names = get(key); - if (names == null) { - return Collections.emptyList(); - } - - StringReader rd = new StringReader(names); - List res = new ArrayList<>(); - Optional rs; - try { - while ((rs = getNextUnit(rd)).isPresent()) { - res.add(rs.get()); - } - } catch (IOException ignored) { - // Ignored - } - return res; + return convertStringToList(get(key)); } /** @@ -1045,6 +1049,20 @@ private Object getObject(Preferences prefs, String key) { } } + /** + * Returns a list of Strings stored by key+N with N being an incrementing number + */ + private List getSeries(String key) { + int i = 0; + List series = new ArrayList<>(); + String item; + while (!StringUtil.isBlank(item = get(key + i))) { + series.add(item); + i++; + } + return series; + } + /** * Removes all entries keyed by prefix+number, where number is equal to or higher than the given number. * @@ -1454,24 +1472,17 @@ private Map> getEntryEditorTabList() { */ private void updateEntryEditorTabList() { Map> tabs = new LinkedHashMap<>(); - int i = 0; - String name; - if (hasKey(CUSTOM_TAB_NAME + 0)) { - // The user has modified from the default values: - while (hasKey(CUSTOM_TAB_NAME + i)) { - name = get(CUSTOM_TAB_NAME + i); - Set entry = FieldFactory.parseFieldList(get(CUSTOM_TAB_FIELDS + i)); - tabs.put(name, entry); - i++; - } - } else { - // Nothing set, so we use the default values: - while (get(CUSTOM_TAB_NAME + "_def" + i) != null) { - name = get(CUSTOM_TAB_NAME + "_def" + i); - Set entry = FieldFactory.parseFieldList(get(CUSTOM_TAB_FIELDS + "_def" + i)); - tabs.put(name, entry); - i++; - } + List tabNames = getSeries(CUSTOM_TAB_NAME); + List tabFields = getSeries(CUSTOM_TAB_FIELDS); + + if (tabNames.isEmpty() || tabNames.size() != tabFields.size()) { + // Nothing set, so we use the default values + tabNames = getSeries(CUSTOM_TAB_NAME + "_def"); + tabFields = getSeries(CUSTOM_TAB_FIELDS + "_def"); + } + + for (int i = 0; i < tabNames.size(); i++) { + tabs.put(tabNames.get(i), FieldFactory.parseFieldList(tabFields.get(i))); } entryEditorTabList = tabs; } @@ -2101,12 +2112,12 @@ public AppearancePreferences getAppearancePreferences() { @Override public ImportFormatPreferences getImportFormatPreferences() { return new ImportFormatPreferences( - getCustomImportFormats(), getKeywordDelimiter(), getCitationKeyPatternPreferences(), getFieldContentParserPreferences(), getXmpPreferences(), - getDOIPreferences()); + getDOIPreferences(), + getGrobidPreferences()); } @Override @@ -2306,77 +2317,22 @@ public ImportExportPreferences getImportExportPreferences() { return importExportPreferences; } - @Override - public Set getCustomImportFormats() { - if (this.customImporters == null) { - updateCustomImportFormats(); - } - return this.customImporters; - } - - private void updateCustomImportFormats() { - Set importers = new TreeSet<>(); - int i = 0; - - List importerString; - while (!((importerString = getStringList(CUSTOM_IMPORT_FORMAT + i)).isEmpty())) { - try { - if (importerString.size() == 2) { - // New format: basePath, className - importers.add(new CustomImporter(importerString.get(0), importerString.get(1))); - } else { - // Old format: name, cliId, className, basePath - importers.add(new CustomImporter(importerString.get(3), importerString.get(2))); - } - } catch (Exception e) { - LOGGER.warn("Could not load " + importerString.get(0) + " from preferences. Will ignore.", e); - } - i++; - } - - this.customImporters = importers; - } - - @Override - public void storeCustomImportFormats(Set importers) { - purgeCustomImportFormats(); - CustomImporter[] importersArray = importers.toArray(new CustomImporter[0]); - for (int i = 0; i < importersArray.length; i++) { - putStringList(CUSTOM_IMPORT_FORMAT + i, importersArray[i].getAsStringList()); - } - - this.customImporters = importers; - } - - private void purgeCustomImportFormats() { - for (int i = 0; !(getStringList(CUSTOM_IMPORT_FORMAT + i).isEmpty()); i++) { - remove(CUSTOM_IMPORT_FORMAT + i); - } - } - @Override public List getCustomExportFormats(JournalAbbreviationRepository abbreviationRepository) { - int i = 0; - List formats = new ArrayList<>(); - String exporterName; - String filename; - String extension; LayoutFormatterPreferences layoutPreferences = getLayoutFormatterPreferences(abbreviationRepository); SavePreferences savePreferences = getSavePreferencesForExport(); - List formatData; - while (!((formatData = getStringList(CUSTOM_EXPORT_FORMAT + i)).isEmpty())) { - exporterName = formatData.get(EXPORTER_NAME_INDEX); - filename = formatData.get(EXPORTER_FILENAME_INDEX); - extension = formatData.get(EXPORTER_EXTENSION_INDEX); + List formats = new ArrayList<>(); + + for (String toImport : getSeries(CUSTOM_EXPORT_FORMAT)) { + List formatData = convertStringToList(toImport); TemplateExporter format = new TemplateExporter( - exporterName, - filename, - extension, + formatData.get(EXPORTER_NAME_INDEX), + formatData.get(EXPORTER_FILENAME_INDEX), + formatData.get(EXPORTER_EXTENSION_INDEX), layoutPreferences, savePreferences); format.setCustomExport(true); formats.add(format); - i++; } return formats; } @@ -2384,7 +2340,7 @@ public List getCustomExportFormats(JournalAbbreviationReposito @Override public void storeCustomExportFormats(List exporters) { if (exporters.isEmpty()) { - purgeCustomExportFormats(0); + purgeSeries(CUSTOM_EXPORT_FORMAT, 0); } else { for (int i = 0; i < exporters.size(); i++) { List exporterData = new ArrayList<>(); @@ -2394,15 +2350,7 @@ public void storeCustomExportFormats(List exporters) { exporterData.add(EXPORTER_EXTENSION_INDEX, exporters.get(i).getFileType().getExtensions().get(0)); putStringList(CUSTOM_EXPORT_FORMAT + i, exporterData); } - purgeCustomExportFormats(exporters.size()); - } - } - - private void purgeCustomExportFormats(int from) { - int i = from; - while (!getStringList(CUSTOM_EXPORT_FORMAT + i).isEmpty()) { - remove(CUSTOM_EXPORT_FORMAT + i); - i++; + purgeSeries(CUSTOM_EXPORT_FORMAT, exporters.size()); } } @@ -2821,21 +2769,46 @@ public ImporterPreferences getImporterPreferences() { importerPreferences = new ImporterPreferences( getBoolean(GENERATE_KEY_ON_IMPORT), - getBoolean(GROBID_ENABLED), - getBoolean(GROBID_OPT_OUT), - get(GROBID_URL), + getCustomImportFormats(), getFetcherKeys() ); EasyBind.listen(importerPreferences.generateNewKeyOnImportProperty(), (obs, oldValue, newValue) -> putBoolean(GENERATE_KEY_ON_IMPORT, newValue)); - EasyBind.listen(importerPreferences.grobidEnabledProperty(), (obs, oldValue, newValue) -> putBoolean(GROBID_ENABLED, newValue)); - EasyBind.listen(importerPreferences.grobidOptOutProperty(), (obs, oldValue, newValue) -> putBoolean(GROBID_OPT_OUT, newValue)); - EasyBind.listen(importerPreferences.grobidURLProperty(), (obs, oldValue, newValue) -> put(GROBID_URL, newValue)); importerPreferences.getApiKeys().addListener((InvalidationListener) c -> storeFetcherKeys(importerPreferences.getApiKeys())); + importerPreferences.getCustomImportList().addListener((InvalidationListener) c -> storeCustomImportFormats(importerPreferences.getCustomImportList())); return importerPreferences; } + private Set getCustomImportFormats() { + Set importers = new TreeSet<>(); + + for (String toImport : getSeries(CUSTOM_IMPORT_FORMAT)) { + List importerString = convertStringToList(toImport); + try { + if (importerString.size() == 2) { + // New format: basePath, className + importers.add(new CustomImporter(importerString.get(0), importerString.get(1))); + } else { + // Old format: name, cliId, className, basePath + importers.add(new CustomImporter(importerString.get(3), importerString.get(2))); + } + } catch (Exception e) { + LOGGER.warn("Could not load " + importerString.get(0) + " from preferences. Will ignore.", e); + } + } + + return importers; + } + + public void storeCustomImportFormats(Set importers) { + purgeSeries(CUSTOM_IMPORT_FORMAT, 0); + CustomImporter[] importersArray = importers.toArray(new CustomImporter[0]); + for (int i = 0; i < importersArray.length; i++) { + putStringList(CUSTOM_IMPORT_FORMAT + i, importersArray[i].getAsStringList()); + } + } + private Set getFetcherKeys() { Set fetcherApiKeys = new HashSet<>(); @@ -2869,4 +2842,22 @@ private void storeFetcherKeys(Set fetcherApiKeys) { putStringList(FETCHER_CUSTOM_KEY_USES, uses); putStringList(FETCHER_CUSTOM_KEYS, keys); } + + @Override + public GrobidPreferences getGrobidPreferences() { + if (Objects.nonNull(grobidPreferences)) { + return grobidPreferences; + } + + grobidPreferences = new GrobidPreferences( + getBoolean(GROBID_ENABLED), + getBoolean(GROBID_OPT_OUT), + get(GROBID_URL)); + + EasyBind.listen(grobidPreferences.grobidEnabledProperty(), (obs, oldValue, newValue) -> putBoolean(GROBID_ENABLED, newValue)); + EasyBind.listen(grobidPreferences.grobidOptOutProperty(), (obs, oldValue, newValue) -> putBoolean(GROBID_OPT_OUT, newValue)); + EasyBind.listen(grobidPreferences.grobidURLProperty(), (obs, oldValue, newValue) -> put(GROBID_URL, newValue)); + + return grobidPreferences; + } } diff --git a/src/main/java/org/jabref/preferences/PreferencesService.java b/src/main/java/org/jabref/preferences/PreferencesService.java index d3f7c692248..f3329750992 100644 --- a/src/main/java/org/jabref/preferences/PreferencesService.java +++ b/src/main/java/org/jabref/preferences/PreferencesService.java @@ -25,7 +25,7 @@ import org.jabref.logic.exporter.TemplateExporter; import org.jabref.logic.importer.ImportFormatPreferences; import org.jabref.logic.importer.ImporterPreferences; -import org.jabref.logic.importer.fileformat.CustomImporter; +import org.jabref.logic.importer.fetcher.GrobidPreferences; import org.jabref.logic.journals.JournalAbbreviationPreferences; import org.jabref.logic.journals.JournalAbbreviationRepository; import org.jabref.logic.l10n.Language; @@ -233,12 +233,10 @@ public interface PreferencesService { void storeCustomExportFormats(List exporters); - Set getCustomImportFormats(); - - void storeCustomImportFormats(Set customImporters); - ImporterPreferences getImporterPreferences(); + GrobidPreferences getGrobidPreferences(); + //************************************************************************************************************* // Preview preferences //************************************************************************************************************* diff --git a/src/test/java/org/jabref/logic/importer/ImportFormatReaderIntegrationTest.java b/src/test/java/org/jabref/logic/importer/ImportFormatReaderIntegrationTest.java index ac6262322fb..8fc04e795b3 100644 --- a/src/test/java/org/jabref/logic/importer/ImportFormatReaderIntegrationTest.java +++ b/src/test/java/org/jabref/logic/importer/ImportFormatReaderIntegrationTest.java @@ -4,12 +4,11 @@ import java.nio.file.Path; import java.util.ArrayList; import java.util.Collection; -import java.util.Set; import java.util.stream.Stream; -import org.jabref.logic.xmp.XmpPreferences; +import javafx.collections.FXCollections; + import org.jabref.model.util.DummyFileUpdateMonitor; -import org.jabref.preferences.GeneralPreferences; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.params.ParameterizedTest; @@ -27,10 +26,12 @@ class ImportFormatReaderIntegrationTest { @BeforeEach void setUp() { reader = new ImportFormatReader(); - ImportFormatPreferences importFormatPreferences = mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS); - when(importFormatPreferences.getCustomImportList()).thenReturn(Set.of()); - GeneralPreferences generalPreferences = mock(GeneralPreferences.class, Answers.RETURNS_DEEP_STUBS); - reader.resetImportFormats(mock(ImporterPreferences.class), importFormatPreferences, mock(XmpPreferences.class), new DummyFileUpdateMonitor()); + ImporterPreferences importerPreferences = mock(ImporterPreferences.class, Answers.RETURNS_DEEP_STUBS); + when(importerPreferences.getCustomImportList()).thenReturn(FXCollections.emptyObservableSet()); + reader.resetImportFormats( + importerPreferences, + mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS), + new DummyFileUpdateMonitor()); } @ParameterizedTest diff --git a/src/test/java/org/jabref/logic/importer/ImportFormatReaderTestParameterless.java b/src/test/java/org/jabref/logic/importer/ImportFormatReaderTestParameterless.java index b0eadfda8dd..cdde8e71913 100644 --- a/src/test/java/org/jabref/logic/importer/ImportFormatReaderTestParameterless.java +++ b/src/test/java/org/jabref/logic/importer/ImportFormatReaderTestParameterless.java @@ -1,12 +1,11 @@ package org.jabref.logic.importer; import java.nio.file.Path; -import java.util.Set; -import org.jabref.logic.xmp.XmpPreferences; +import javafx.collections.FXCollections; + import org.jabref.model.util.DummyFileUpdateMonitor; import org.jabref.model.util.FileUpdateMonitor; -import org.jabref.preferences.GeneralPreferences; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -24,10 +23,11 @@ class ImportFormatReaderTestParameterless { @BeforeEach void setUp() { reader = new ImportFormatReader(); - GeneralPreferences generalPreferences = mock(GeneralPreferences.class, Answers.RETURNS_DEEP_STUBS); + ImporterPreferences importerPreferences = mock(ImporterPreferences.class, Answers.RETURNS_DEEP_STUBS); + when(importerPreferences.getCustomImportList()).thenReturn(FXCollections.emptyObservableSet()); + ImportFormatPreferences importFormatPreferences = mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS); - when(importFormatPreferences.getCustomImportList()).thenReturn(Set.of()); - reader.resetImportFormats(mock(ImporterPreferences.class), importFormatPreferences, mock(XmpPreferences.class), fileMonitor); + reader.resetImportFormats(importerPreferences, importFormatPreferences, fileMonitor); } @Test @@ -37,17 +37,17 @@ void importUnknownFormatThrowsExceptionIfNoMatchingImporterWasFound() throws Exc } @Test - void importUnknownFormatThrowsExceptionIfPathIsNull() throws Exception { + void importUnknownFormatThrowsExceptionIfPathIsNull() { assertThrows(NullPointerException.class, () -> reader.importUnknownFormat(null, fileMonitor)); } @Test - void importUnknownFormatThrowsExceptionIfDataIsNull() throws Exception { + void importUnknownFormatThrowsExceptionIfDataIsNull() { assertThrows(NullPointerException.class, () -> reader.importUnknownFormat(null)); } @Test - void importFromFileWithUnknownFormatThrowsException() throws Exception { + void importFromFileWithUnknownFormatThrowsException() { assertThrows(ImportException.class, () -> reader.importFromFile("someunknownformat", Path.of("somepath"))); } } diff --git a/src/test/java/org/jabref/logic/importer/WebFetchersTest.java b/src/test/java/org/jabref/logic/importer/WebFetchersTest.java index a65bb74f256..3486496f289 100644 --- a/src/test/java/org/jabref/logic/importer/WebFetchersTest.java +++ b/src/test/java/org/jabref/logic/importer/WebFetchersTest.java @@ -21,6 +21,7 @@ import io.github.classgraph.ScanResult; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.Answers; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.mock; @@ -34,7 +35,7 @@ class WebFetchersTest { @BeforeEach void setUp() { - importFormatPreferences = mock(ImportFormatPreferences.class); + importFormatPreferences = mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS); importerPreferences = mock(ImporterPreferences.class); FieldContentFormatterPreferences fieldContentFormatterPreferences = mock(FieldContentFormatterPreferences.class); when(importFormatPreferences.getFieldContentFormatterPreferences()).thenReturn(fieldContentFormatterPreferences); @@ -65,7 +66,11 @@ void getIdBasedFetchersReturnsAllFetcherDerivingFromIdBasedFetcher() { @Test void getEntryBasedFetchersReturnsAllFetcherDerivingFromEntryBasedFetcher() { - Set idFetchers = WebFetchers.getEntryBasedFetchers(mock(ImporterPreferences.class), importFormatPreferences, mock(FilePreferences.class), mock(BibDatabaseContext.class)); + Set idFetchers = WebFetchers.getEntryBasedFetchers( + mock(ImporterPreferences.class), + importFormatPreferences, + mock(FilePreferences.class), + mock(BibDatabaseContext.class)); try (ScanResult scanResult = classGraph.scan()) { ClassInfoList controlClasses = scanResult.getClassesImplementing(EntryBasedFetcher.class.getCanonicalName()); diff --git a/src/test/java/org/jabref/logic/importer/fetcher/GrobidCitationFetcherTest.java b/src/test/java/org/jabref/logic/importer/fetcher/GrobidCitationFetcherTest.java index fdd76c17152..2852909d212 100644 --- a/src/test/java/org/jabref/logic/importer/fetcher/GrobidCitationFetcherTest.java +++ b/src/test/java/org/jabref/logic/importer/fetcher/GrobidCitationFetcherTest.java @@ -8,7 +8,6 @@ import org.jabref.logic.importer.FetcherException; import org.jabref.logic.importer.ImportFormatPreferences; -import org.jabref.logic.importer.ImporterPreferences; import org.jabref.logic.importer.ParseException; import org.jabref.logic.importer.util.GrobidService; import org.jabref.model.entry.BibEntry; @@ -33,8 +32,11 @@ public class GrobidCitationFetcherTest { static ImportFormatPreferences importFormatPreferences = mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS); - static ImporterPreferences importerPreferences = new ImporterPreferences(false, true, false, "http://grobid.jabref.org:8070", Collections.emptySet()); - static GrobidCitationFetcher grobidCitationFetcher = new GrobidCitationFetcher(importerPreferences, importFormatPreferences); + static GrobidPreferences grobidPreferences = new GrobidPreferences( + true, + false, + "http://grobid.jabref.org:8070"); + static GrobidCitationFetcher grobidCitationFetcher = new GrobidCitationFetcher(grobidPreferences, importFormatPreferences); static String example1 = "Derwing, T. M., Rossiter, M. J., & Munro, M. J. (2002). Teaching native speakers to listen to foreign-accented speech. Journal of Multilingual and Multicultural Development, 23(4), 245-259."; static BibEntry example1AsBibEntry = new BibEntry(StandardEntryType.Article).withCitationKey("-1") diff --git a/src/test/java/org/jabref/logic/importer/fileformat/PdfGrobidImporterTest.java b/src/test/java/org/jabref/logic/importer/fileformat/PdfGrobidImporterTest.java index e9749aca122..196c3aae38c 100644 --- a/src/test/java/org/jabref/logic/importer/fileformat/PdfGrobidImporterTest.java +++ b/src/test/java/org/jabref/logic/importer/fileformat/PdfGrobidImporterTest.java @@ -7,7 +7,7 @@ import java.util.Optional; import org.jabref.logic.importer.ImportFormatPreferences; -import org.jabref.logic.importer.ImporterPreferences; +import org.jabref.logic.importer.fetcher.GrobidPreferences; import org.jabref.logic.util.StandardFileType; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.field.StandardField; @@ -27,16 +27,18 @@ public class PdfGrobidImporterTest { private PdfGrobidImporter importer; - private ImportFormatPreferences importFormatPreferences; @BeforeEach public void setUp() { - ImporterPreferences importerPreferences = mock(ImporterPreferences.class, Answers.RETURNS_DEEP_STUBS); - when(importerPreferences.isGrobidEnabled()).thenReturn(true); - when(importerPreferences.getGrobidURL()).thenReturn("http://grobid.jabref.org:8070"); - importFormatPreferences = mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS); + GrobidPreferences grobidPreferences = mock(GrobidPreferences.class, Answers.RETURNS_DEEP_STUBS); + when(grobidPreferences.isGrobidEnabled()).thenReturn(true); + when(grobidPreferences.getGrobidURL()).thenReturn("http://grobid.jabref.org:8070"); + + ImportFormatPreferences importFormatPreferences = mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS); when(importFormatPreferences.getKeywordSeparator()).thenReturn(','); - importer = new PdfGrobidImporter(importerPreferences, importFormatPreferences); + when(importFormatPreferences.getGrobidPreferences()).thenReturn(grobidPreferences); + + importer = new PdfGrobidImporter(importFormatPreferences); } @Test diff --git a/src/test/java/org/jabref/logic/importer/fileformat/PdfMergeMetadataImporterTest.java b/src/test/java/org/jabref/logic/importer/fileformat/PdfMergeMetadataImporterTest.java index f584b22c717..1afa5056d18 100644 --- a/src/test/java/org/jabref/logic/importer/fileformat/PdfMergeMetadataImporterTest.java +++ b/src/test/java/org/jabref/logic/importer/fileformat/PdfMergeMetadataImporterTest.java @@ -5,7 +5,7 @@ import java.util.List; import org.jabref.logic.importer.ImportFormatPreferences; -import org.jabref.logic.importer.ImporterPreferences; +import org.jabref.logic.importer.fetcher.GrobidPreferences; import org.jabref.logic.util.StandardFileType; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.LinkedFile; @@ -27,16 +27,18 @@ class PdfMergeMetadataImporterTest { private PdfMergeMetadataImporter importer; - private ImportFormatPreferences importFormatPreferences; @BeforeEach void setUp() { - ImporterPreferences importerPreferences = mock(ImporterPreferences.class, Answers.RETURNS_DEEP_STUBS); - when(importerPreferences.isGrobidEnabled()).thenReturn(true); - when(importerPreferences.getGrobidURL()).thenReturn("http://grobid.jabref.org:8070"); - importFormatPreferences = mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS); + GrobidPreferences grobidPreferences = mock(GrobidPreferences.class, Answers.RETURNS_DEEP_STUBS); + when(grobidPreferences.isGrobidEnabled()).thenReturn(true); + when(grobidPreferences.getGrobidURL()).thenReturn("http://grobid.jabref.org:8070"); + + ImportFormatPreferences importFormatPreferences = mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS); when(importFormatPreferences.getFieldContentFormatterPreferences().getNonWrappableFields()).thenReturn(List.of()); - importer = new PdfMergeMetadataImporter(importerPreferences, importFormatPreferences); + when(importFormatPreferences.getGrobidPreferences()).thenReturn(grobidPreferences); + + importer = new PdfMergeMetadataImporter(importFormatPreferences); } @Test diff --git a/src/test/java/org/jabref/logic/importer/util/GrobidServiceTest.java b/src/test/java/org/jabref/logic/importer/util/GrobidServiceTest.java index f1054257e04..9e81c64ede5 100644 --- a/src/test/java/org/jabref/logic/importer/util/GrobidServiceTest.java +++ b/src/test/java/org/jabref/logic/importer/util/GrobidServiceTest.java @@ -3,13 +3,12 @@ import java.io.IOException; import java.net.URISyntaxException; import java.nio.file.Path; -import java.util.Collections; import java.util.List; import java.util.Optional; import org.jabref.logic.importer.ImportFormatPreferences; -import org.jabref.logic.importer.ImporterPreferences; import org.jabref.logic.importer.ParseException; +import org.jabref.logic.importer.fetcher.GrobidPreferences; import org.jabref.logic.importer.fileformat.PdfGrobidImporterTest; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.field.StandardField; @@ -31,17 +30,15 @@ public class GrobidServiceTest { private static GrobidService grobidService; - private static ImporterPreferences importerPreferences = new ImporterPreferences( - false, + private static GrobidPreferences grobidPreferences = new GrobidPreferences( true, false, - "http://grobid.jabref.org:8070", - Collections.emptySet()); + "http://grobid.jabref.org:8070"); private static ImportFormatPreferences importFormatPreferences; @BeforeAll public static void setup() { - grobidService = new GrobidService(importerPreferences); + grobidService = new GrobidService(grobidPreferences); importFormatPreferences = mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS); when(importFormatPreferences.getKeywordSeparator()).thenReturn(','); } @@ -84,12 +81,10 @@ public void processInvalidCitationTest() { @Test public void failsWhenGrobidDisabled() { - ImporterPreferences importSettingsWithGrobidDisabled = new ImporterPreferences( - false, + GrobidPreferences importSettingsWithGrobidDisabled = new GrobidPreferences( false, false, - "http://grobid.jabref.org:8070", - Collections.emptySet()); + "http://grobid.jabref.org:8070"); assertThrows(UnsupportedOperationException.class, () -> new GrobidService(importSettingsWithGrobidDisabled)); }