Skip to content

Commit

Permalink
Preferences cleanup C (ExporterFactory & setPreviewStyle) (#9998)
Browse files Browse the repository at this point in the history
* Simple code maintenance (moving around, simplifying, extracting)

* Removed get-set-PreviewStyle, added javadoc, simplified tests

* Moved entryNumber from ExporterFactory to NumberClass

* Extracted JournalAbbreviationRepository from LayoutFormatterPreferences

* Focused tests and removed superfluous constructor

* Changed UnknownFormatImport to record

* Fixed newInstance() deprecation

* Refactored LinkedFilesEditor

* Extracted JournalAbbreviationsRepository from constructor in exporter

* Extracted JournalAbbreviationsRepository from getCustomExportFormats

* Checkstyle
  • Loading branch information
calixtus authored Jun 11, 2023
1 parent 39c31af commit 27a22af
Show file tree
Hide file tree
Showing 62 changed files with 824 additions and 860 deletions.
103 changes: 82 additions & 21 deletions src/main/java/org/jabref/cli/ArgumentProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.jabref.logic.importer.SearchBasedFetcher;
import org.jabref.logic.importer.WebFetchers;
import org.jabref.logic.importer.fileformat.BibtexParser;
import org.jabref.logic.journals.JournalAbbreviationRepository;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.net.URLDownload;
import org.jabref.logic.search.DatabaseSearcher;
Expand Down Expand Up @@ -167,8 +168,8 @@ private Optional<ParserResult> importFile(Path file, String importFormat) {
ImportFormatReader.UnknownFormatImport importResult =
importFormatReader.importUnknownFormat(file, new DummyFileUpdateMonitor());

System.out.println(Localization.lang("Format used") + ": " + importResult.format);
return Optional.of(importResult.parserResult);
System.out.println(Localization.lang("Format used") + ": " + importResult.format());
return Optional.of(importResult.parserResult());
}
} catch (ImportException ex) {
System.err
Expand Down Expand Up @@ -241,13 +242,14 @@ private List<ParserResult> processArguments() {

if (cli.isWriteMetadatatoPdf() || cli.isWriteXMPtoPdf() || cli.isEmbeddBibfileInPdf()) {
if (!loaded.isEmpty()) {
writeMetadatatoPdf(loaded,
writeMetadataToPdf(loaded,
cli.getWriteMetadatatoPdf(),
preferencesService.getXmpPreferences(),
preferencesService.getFilePreferences(),
preferencesService.getLibraryPreferences().getDefaultBibDatabaseMode(),
Globals.entryTypesManager,
preferencesService.getFieldPreferences(),
Globals.journalAbbreviationRepository,
cli.isWriteXMPtoPdf() || cli.isWriteMetadatatoPdf(),
cli.isEmbeddBibfileInPdf() || cli.isWriteMetadatatoPdf());
}
Expand Down Expand Up @@ -277,7 +279,16 @@ private List<ParserResult> processArguments() {
return loaded;
}

private void writeMetadatatoPdf(List<ParserResult> loaded, String filesAndCitekeys, XmpPreferences xmpPreferences, FilePreferences filePreferences, BibDatabaseMode databaseMode, BibEntryTypesManager entryTypesManager, FieldPreferences fieldPreferences, boolean writeXMP, boolean embeddBibfile) {
private void writeMetadataToPdf(List<ParserResult> loaded,
String filesAndCitekeys,
XmpPreferences xmpPreferences,
FilePreferences filePreferences,
BibDatabaseMode databaseMode,
BibEntryTypesManager entryTypesManager,
FieldPreferences fieldPreferences,
JournalAbbreviationRepository abbreviationRepository,
boolean writeXMP,
boolean embeddBibfile) {
if (loaded.isEmpty()) {
LOGGER.error("The write xmp option depends on a valid import option.");
return;
Expand All @@ -291,7 +302,16 @@ private void writeMetadatatoPdf(List<ParserResult> loaded, String filesAndCiteke

if ("all".equals(filesAndCitekeys)) {
for (BibEntry entry : dataBase.getEntries()) {
writeMetadatatoPDFsOfEntry(databaseContext, entry.getCitationKey().orElse("<no cite key defined>"), entry, filePreferences, xmpPdfExporter, embeddedBibFilePdfExporter, writeXMP, embeddBibfile);
writeMetadataToPDFsOfEntry(
databaseContext,
entry.getCitationKey().orElse("<no cite key defined>"),
entry,
filePreferences,
xmpPdfExporter,
embeddedBibFilePdfExporter,
abbreviationRepository,
writeXMP,
embeddBibfile);
}
return;
}
Expand All @@ -306,21 +326,47 @@ private void writeMetadatatoPdf(List<ParserResult> loaded, String filesAndCiteke
}
}

writeMetadatatoPdfByCitekey(databaseContext, dataBase, citeKeys, filePreferences, xmpPdfExporter, embeddedBibFilePdfExporter, writeXMP, embeddBibfile);
writeMetadatatoPdfByFileNames(databaseContext, dataBase, pdfs, filePreferences, xmpPdfExporter, embeddedBibFilePdfExporter, writeXMP, embeddBibfile);
writeMetadataToPdfByCitekey(
databaseContext,
dataBase,
citeKeys,
filePreferences,
xmpPdfExporter,
embeddedBibFilePdfExporter,
abbreviationRepository,
writeXMP,
embeddBibfile);
writeMetadataToPdfByFileNames(
databaseContext,
dataBase,
pdfs,
filePreferences,
xmpPdfExporter,
embeddedBibFilePdfExporter,
abbreviationRepository,
writeXMP,
embeddBibfile);
}

private void writeMetadatatoPDFsOfEntry(BibDatabaseContext databaseContext, String citeKey, BibEntry entry, FilePreferences filePreferences, XmpPdfExporter xmpPdfExporter, EmbeddedBibFilePdfExporter embeddedBibFilePdfExporter, boolean writeXMP, boolean embeddBibfile) {
private void writeMetadataToPDFsOfEntry(BibDatabaseContext databaseContext,
String citeKey,
BibEntry entry,
FilePreferences filePreferences,
XmpPdfExporter xmpPdfExporter,
EmbeddedBibFilePdfExporter embeddedBibFilePdfExporter,
JournalAbbreviationRepository abbreviationRepository,
boolean writeXMP,
boolean embeddBibfile) {
try {
if (writeXMP) {
if (xmpPdfExporter.exportToAllFilesOfEntry(databaseContext, filePreferences, entry, List.of(entry))) {
if (xmpPdfExporter.exportToAllFilesOfEntry(databaseContext, filePreferences, entry, List.of(entry), abbreviationRepository)) {
System.out.printf("Successfully written XMP metadata on at least one linked file of %s%n", citeKey);
} else {
System.err.printf("Cannot write XMP metadata on any linked files of %s. Make sure there is at least one linked file and the path is correct.%n", citeKey);
}
}
if (embeddBibfile) {
if (embeddedBibFilePdfExporter.exportToAllFilesOfEntry(databaseContext, filePreferences, entry, List.of(entry))) {
if (embeddedBibFilePdfExporter.exportToAllFilesOfEntry(databaseContext, filePreferences, entry, List.of(entry), abbreviationRepository)) {
System.out.printf("Successfully embedded metadata on at least one linked file of %s%n", citeKey);
} else {
System.out.printf("Cannot embedd metadata on any linked files of %s. Make sure there is at least one linked file and the path is correct.%n", citeKey);
Expand All @@ -331,20 +377,36 @@ private void writeMetadatatoPDFsOfEntry(BibDatabaseContext databaseContext, Stri
}
}

private void writeMetadatatoPdfByCitekey(BibDatabaseContext databaseContext, BibDatabase dataBase, List<String> citeKeys, FilePreferences filePreferences, XmpPdfExporter xmpPdfExporter, EmbeddedBibFilePdfExporter embeddedBibFilePdfExporter, boolean writeXMP, boolean embeddBibfile) {
private void writeMetadataToPdfByCitekey(BibDatabaseContext databaseContext,
BibDatabase dataBase,
List<String> citeKeys,
FilePreferences filePreferences,
XmpPdfExporter xmpPdfExporter,
EmbeddedBibFilePdfExporter embeddedBibFilePdfExporter,
JournalAbbreviationRepository abbreviationRepository,
boolean writeXMP,
boolean embeddBibfile) {
for (String citeKey : citeKeys) {
List<BibEntry> bibEntryList = dataBase.getEntriesByCitationKey(citeKey);
if (bibEntryList.isEmpty()) {
System.err.printf("Skipped - Cannot find %s in library.%n", citeKey);
continue;
}
for (BibEntry entry : bibEntryList) {
writeMetadatatoPDFsOfEntry(databaseContext, citeKey, entry, filePreferences, xmpPdfExporter, embeddedBibFilePdfExporter, writeXMP, embeddBibfile);
writeMetadataToPDFsOfEntry(databaseContext, citeKey, entry, filePreferences, xmpPdfExporter, embeddedBibFilePdfExporter, abbreviationRepository, writeXMP, embeddBibfile);
}
}
}

private void writeMetadatatoPdfByFileNames(BibDatabaseContext databaseContext, BibDatabase dataBase, List<String> pdfs, FilePreferences filePreferences, XmpPdfExporter xmpPdfExporter, EmbeddedBibFilePdfExporter embeddedBibFilePdfExporter, boolean writeXMP, boolean embeddBibfile) {
private void writeMetadataToPdfByFileNames(BibDatabaseContext databaseContext,
BibDatabase dataBase,
List<String> pdfs,
FilePreferences filePreferences,
XmpPdfExporter xmpPdfExporter,
EmbeddedBibFilePdfExporter embeddedBibFilePdfExporter,
JournalAbbreviationRepository abbreviationRepository,
boolean writeXMP,
boolean embeddBibfile) {
for (String fileName : pdfs) {
Path filePath = Path.of(fileName);
if (!filePath.isAbsolute()) {
Expand All @@ -353,14 +415,14 @@ private void writeMetadatatoPdfByFileNames(BibDatabaseContext databaseContext, B
if (Files.exists(filePath)) {
try {
if (writeXMP) {
if (xmpPdfExporter.exportToFileByPath(databaseContext, dataBase, filePreferences, filePath)) {
if (xmpPdfExporter.exportToFileByPath(databaseContext, dataBase, filePreferences, filePath, abbreviationRepository)) {
System.out.printf("Successfully written XMP metadata of at least one entry to %s%n", fileName);
} else {
System.out.printf("File %s is not linked to any entry in database.%n", fileName);
}
}
if (embeddBibfile) {
if (embeddedBibFilePdfExporter.exportToFileByPath(databaseContext, dataBase, filePreferences, filePath)) {
if (embeddedBibFilePdfExporter.exportToFileByPath(databaseContext, dataBase, filePreferences, filePath, abbreviationRepository)) {
System.out.printf("Successfully embedded XMP metadata of at least one entry to %s%n", fileName);
} else {
System.out.printf("File %s is not linked to any entry in database.%n", fileName);
Expand Down Expand Up @@ -416,16 +478,15 @@ private boolean exportMatches(List<ParserResult> loaded) {
// export new database
ExporterFactory exporterFactory = ExporterFactory.create(
preferencesService,
Globals.entryTypesManager,
Globals.journalAbbreviationRepository);
Globals.entryTypesManager);
Optional<Exporter> exporter = exporterFactory.getExporterByName(formatName);
if (exporter.isEmpty()) {
System.err.println(Localization.lang("Unknown export format") + ": " + formatName);
} else {
// We have an TemplateExporter instance:
try {
System.out.println(Localization.lang("Exporting") + ": " + data[1]);
exporter.get().export(databaseContext, Path.of(data[1]), matches, Collections.emptyList());
exporter.get().export(databaseContext, Path.of(data[1]), matches, Collections.emptyList(), Globals.journalAbbreviationRepository);
} catch (Exception ex) {
System.err.println(Localization.lang("Could not export file") + " '" + data[1] + "': "
+ Throwables.getStackTraceAsString(ex));
Expand Down Expand Up @@ -591,8 +652,7 @@ private void exportFile(List<ParserResult> loaded, String[] data) {
System.out.println(Localization.lang("Exporting") + ": " + data[0]);
ExporterFactory exporterFactory = ExporterFactory.create(
preferencesService,
Globals.entryTypesManager,
Globals.journalAbbreviationRepository);
Globals.entryTypesManager);
Optional<Exporter> exporter = exporterFactory.getExporterByName(data[1]);
if (exporter.isEmpty()) {
System.err.println(Localization.lang("Unknown export format") + ": " + data[1]);
Expand All @@ -603,7 +663,8 @@ private void exportFile(List<ParserResult> loaded, String[] data) {
pr.getDatabaseContext(),
Path.of(data[0]),
pr.getDatabaseContext().getDatabase().getEntries(),
fileDirForDatabase);
fileDirForDatabase,
Globals.journalAbbreviationRepository);
} catch (Exception ex) {
System.err.println(Localization.lang("Could not export file") + " '" + data[0] + "': "
+ Throwables.getStackTraceAsString(ex));
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/jabref/cli/JabRefCLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,7 @@ public static void printUsage(PreferencesService preferencesService) {

ExporterFactory exporterFactory = ExporterFactory.create(
preferencesService,
Globals.entryTypesManager,
Globals.journalAbbreviationRepository);
Globals.entryTypesManager);
List<Pair<String, String>> exportFormats = exporterFactory
.getExporters().stream()
.map(format -> new Pair<>(format.getName(), format.getId()))
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/org/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ public void showLibraryTab(LibraryTab libraryTab) {
}

public void init() {
sidePane = new SidePane(prefs, taskExecutor, dialogService, stateManager, undoManager);
sidePane = new SidePane(prefs, Globals.journalAbbreviationRepository, taskExecutor, dialogService, stateManager, undoManager);
tabbedPane = new TabPane();
tabbedPane.setTabDragPolicy(TabPane.TabDragPolicy.REORDER);

Expand Down Expand Up @@ -808,12 +808,12 @@ private MenuBar createMenu() {

factory.createMenuItem(StandardActions.COPY, new EditAction(StandardActions.COPY, this, stateManager)),
factory.createSubMenu(StandardActions.COPY_MORE,
factory.createMenuItem(StandardActions.COPY_TITLE, new CopyMoreAction(StandardActions.COPY_TITLE, dialogService, stateManager, Globals.getClipboardManager(), prefs)),
factory.createMenuItem(StandardActions.COPY_KEY, new CopyMoreAction(StandardActions.COPY_KEY, dialogService, stateManager, Globals.getClipboardManager(), prefs)),
factory.createMenuItem(StandardActions.COPY_CITE_KEY, new CopyMoreAction(StandardActions.COPY_CITE_KEY, dialogService, stateManager, Globals.getClipboardManager(), prefs)),
factory.createMenuItem(StandardActions.COPY_KEY_AND_TITLE, new CopyMoreAction(StandardActions.COPY_KEY_AND_TITLE, dialogService, stateManager, Globals.getClipboardManager(), prefs)),
factory.createMenuItem(StandardActions.COPY_KEY_AND_LINK, new CopyMoreAction(StandardActions.COPY_KEY_AND_LINK, dialogService, stateManager, Globals.getClipboardManager(), prefs)),
factory.createMenuItem(StandardActions.COPY_CITATION_PREVIEW, new CopyCitationAction(CitationStyleOutputFormat.HTML, dialogService, stateManager, Globals.getClipboardManager(), Globals.TASK_EXECUTOR, prefs.getPreviewPreferences())),
factory.createMenuItem(StandardActions.COPY_TITLE, new CopyMoreAction(StandardActions.COPY_TITLE, dialogService, stateManager, Globals.getClipboardManager(), prefs, Globals.journalAbbreviationRepository)),
factory.createMenuItem(StandardActions.COPY_KEY, new CopyMoreAction(StandardActions.COPY_KEY, dialogService, stateManager, Globals.getClipboardManager(), prefs, Globals.journalAbbreviationRepository)),
factory.createMenuItem(StandardActions.COPY_CITE_KEY, new CopyMoreAction(StandardActions.COPY_CITE_KEY, dialogService, stateManager, Globals.getClipboardManager(), prefs, Globals.journalAbbreviationRepository)),
factory.createMenuItem(StandardActions.COPY_KEY_AND_TITLE, new CopyMoreAction(StandardActions.COPY_KEY_AND_TITLE, dialogService, stateManager, Globals.getClipboardManager(), prefs, Globals.journalAbbreviationRepository)),
factory.createMenuItem(StandardActions.COPY_KEY_AND_LINK, new CopyMoreAction(StandardActions.COPY_KEY_AND_LINK, dialogService, stateManager, Globals.getClipboardManager(), prefs, Globals.journalAbbreviationRepository)),
factory.createMenuItem(StandardActions.COPY_CITATION_PREVIEW, new CopyCitationAction(CitationStyleOutputFormat.HTML, dialogService, stateManager, Globals.getClipboardManager(), Globals.TASK_EXECUTOR, prefs, Globals.journalAbbreviationRepository)),
factory.createMenuItem(StandardActions.EXPORT_SELECTED_TO_CLIPBOARD, new ExportToClipboardAction(dialogService, stateManager, Globals.getClipboardManager(), Globals.TASK_EXECUTOR, prefs))),

factory.createMenuItem(StandardActions.PASTE, new EditAction(StandardActions.PASTE, this, stateManager)),
Expand Down
Loading

0 comments on commit 27a22af

Please sign in to comment.