diff --git a/CHANGELOG.md b/CHANGELOG.md index e771cbce9b8..d74b9a2e6f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `# ### Fixed +- We fixed the missing dot in the name of an exported file. [#3576](https://github.com/JabRef/jabref/issues/3576) ### Removed - We removed the Look and Feels from jgoodies, because these are not compatible with Java 9 diff --git a/src/main/java/org/jabref/Globals.java b/src/main/java/org/jabref/Globals.java index fb854526e0a..2e51be30211 100644 --- a/src/main/java/org/jabref/Globals.java +++ b/src/main/java/org/jabref/Globals.java @@ -10,6 +10,7 @@ import org.jabref.gui.util.DefaultTaskExecutor; import org.jabref.gui.util.FileUpdateMonitor; import org.jabref.gui.util.TaskExecutor; +import org.jabref.logic.exporter.ExporterFactory; import org.jabref.logic.importer.ImportFormatReader; import org.jabref.logic.journals.JournalAbbreviationLoader; import org.jabref.logic.protectedterms.ProtectedTermsLoader; @@ -48,6 +49,7 @@ public class Globals { * Manager for the state of the GUI. */ public static StateManager stateManager = new StateManager(); + public static ExporterFactory exportFactory; // Key binding preferences private static KeyBindingRepository keyBindingRepository; // Background tasks diff --git a/src/main/java/org/jabref/JabRefMain.java b/src/main/java/org/jabref/JabRefMain.java index 4df22d2f7df..fdf77b533ea 100644 --- a/src/main/java/org/jabref/JabRefMain.java +++ b/src/main/java/org/jabref/JabRefMain.java @@ -1,7 +1,6 @@ package org.jabref; import java.net.Authenticator; -import java.util.Map; import javax.swing.JFrame; import javax.swing.JOptionPane; @@ -13,13 +12,10 @@ import org.jabref.cli.ArgumentProcessor; import org.jabref.gui.remote.JabRefMessageHandler; -import org.jabref.logic.exporter.ExportFormat; -import org.jabref.logic.exporter.ExportFormats; -import org.jabref.logic.exporter.SavePreferences; +import org.jabref.logic.exporter.ExporterFactory; import org.jabref.logic.formatter.casechanger.ProtectTermsFormatter; import org.jabref.logic.journals.JournalAbbreviationLoader; import org.jabref.logic.l10n.Localization; -import org.jabref.logic.layout.LayoutFormatterPreferences; import org.jabref.logic.net.ProxyAuthenticator; import org.jabref.logic.net.ProxyPreferences; import org.jabref.logic.net.ProxyRegisterer; @@ -147,12 +143,7 @@ private static void start(String[] args) { Globals.prefs.getXMPPreferences()); EntryTypes.loadCustomEntryTypes(preferences.loadCustomEntryTypes(BibDatabaseMode.BIBTEX), preferences.loadCustomEntryTypes(BibDatabaseMode.BIBLATEX)); - Map customFormats = Globals.prefs.customExports.getCustomExportFormats(Globals.prefs, - Globals.journalAbbreviationLoader); - LayoutFormatterPreferences layoutPreferences = Globals.prefs - .getLayoutFormatterPreferences(Globals.journalAbbreviationLoader); - SavePreferences savePreferences = SavePreferences.loadForExportFromPreferences(Globals.prefs); - ExportFormats.initAllExports(customFormats, layoutPreferences, savePreferences); + Globals.exportFactory = ExporterFactory.create(Globals.prefs, Globals.journalAbbreviationLoader); // Initialize protected terms loader Globals.protectedTermsLoader = new ProtectedTermsLoader(Globals.prefs.getProtectedTermsPreferences()); diff --git a/src/main/java/org/jabref/cli/ArgumentProcessor.java b/src/main/java/org/jabref/cli/ArgumentProcessor.java index 38a612285dc..2764dd1930f 100644 --- a/src/main/java/org/jabref/cli/ArgumentProcessor.java +++ b/src/main/java/org/jabref/cli/ArgumentProcessor.java @@ -21,13 +21,13 @@ import org.jabref.logic.bibtexkeypattern.BibtexKeyGenerator; import org.jabref.logic.exporter.BibDatabaseWriter; import org.jabref.logic.exporter.BibtexDatabaseWriter; -import org.jabref.logic.exporter.ExportFormat; -import org.jabref.logic.exporter.ExportFormats; +import org.jabref.logic.exporter.Exporter; +import org.jabref.logic.exporter.ExporterFactory; import org.jabref.logic.exporter.FileSaveSession; -import org.jabref.logic.exporter.IExportFormat; import org.jabref.logic.exporter.SaveException; import org.jabref.logic.exporter.SavePreferences; import org.jabref.logic.exporter.SaveSession; +import org.jabref.logic.exporter.TemplateExporter; import org.jabref.logic.importer.ImportException; import org.jabref.logic.importer.ImportFormatReader; import org.jabref.logic.importer.OpenDatabase; @@ -249,7 +249,7 @@ private boolean exportMatches(List loaded) { formatName = data[2]; break; case 2: - //default ExportFormat: HTML table (with Abstract & BibTeX) + //default exporter: HTML table (with Abstract & BibTeX) formatName = "tablerefsabsbib"; break; default: @@ -260,14 +260,14 @@ private boolean exportMatches(List loaded) { } //export new database - IExportFormat format = ExportFormats.getExportFormat(formatName); - if (format == null) { + Optional exporter = Globals.exportFactory.getExporterByName(formatName); + if (!exporter.isPresent()) { System.err.println(Localization.lang("Unknown export format") + ": " + formatName); } else { - // We have an ExportFormat instance: + // We have an TemplateExporter instance: try { System.out.println(Localization.lang("Exporting") + ": " + data[1]); - format.performExport(databaseContext, data[1], + exporter.get().export(databaseContext, Paths.get(data[1]), databaseContext.getMetaData().getEncoding().orElse(Globals.prefs.getDefaultEncoding()), matches); } catch (Exception ex) { @@ -437,18 +437,17 @@ private void exportFile(List loaded, String[] data) { Globals.prefs.fileDirForDatabase = databaseContext .getFileDirectories(Globals.prefs.getFileDirectoryPreferences()); System.out.println(Localization.lang("Exporting") + ": " + data[0]); - IExportFormat format = ExportFormats.getExportFormat(data[1]); - if (format == null) { + Optional exporter = Globals.exportFactory.getExporterByName(data[1]); + if (!exporter.isPresent()) { System.err.println(Localization.lang("Unknown export format") + ": " + data[1]); } else { - // We have an ExportFormat instance: + // We have an exporter: try { - format.performExport(pr.getDatabaseContext(), data[0], + exporter.get().export(pr.getDatabaseContext(), Paths.get(data[0]), pr.getDatabaseContext().getMetaData().getEncoding() .orElse(Globals.prefs.getDefaultEncoding()), pr.getDatabaseContext().getDatabase().getEntries()); } catch (Exception ex) { - System.err.println(Localization.lang("Could not export file") + " '" + data[0] + "': " + Throwables.getStackTraceAsString(ex)); } @@ -462,12 +461,12 @@ private void importPreferences() { Globals.prefs.importPreferences(cli.getPreferencesImport()); EntryTypes.loadCustomEntryTypes(Globals.prefs.loadCustomEntryTypes(BibDatabaseMode.BIBTEX), Globals.prefs.loadCustomEntryTypes(BibDatabaseMode.BIBLATEX)); - Map customFormats = Globals.prefs.customExports.getCustomExportFormats(Globals.prefs, + Map customExporters = Globals.prefs.customExports.getCustomExportFormats(Globals.prefs, Globals.journalAbbreviationLoader); LayoutFormatterPreferences layoutPreferences = Globals.prefs .getLayoutFormatterPreferences(Globals.journalAbbreviationLoader); SavePreferences savePreferences = SavePreferences.loadForExportFromPreferences(Globals.prefs); - ExportFormats.initAllExports(customFormats, layoutPreferences, savePreferences); + Globals.exportFactory = ExporterFactory.create(customExporters, layoutPreferences, savePreferences); } catch (JabRefException ex) { LOGGER.error("Cannot import preferences", ex); } diff --git a/src/main/java/org/jabref/cli/JabRefCLI.java b/src/main/java/org/jabref/cli/JabRefCLI.java index fe0a4643f89..0333de6901a 100644 --- a/src/main/java/org/jabref/cli/JabRefCLI.java +++ b/src/main/java/org/jabref/cli/JabRefCLI.java @@ -4,7 +4,6 @@ import java.util.List; import org.jabref.Globals; -import org.jabref.logic.exporter.ExportFormats; import org.jabref.logic.l10n.Localization; import org.apache.commons.cli.CommandLine; @@ -246,7 +245,7 @@ public void printUsage() { String importFormats = Globals.IMPORT_FORMAT_READER.getImportFormatList(); String importFormatsList = String.format("%s:%n%s%n", Localization.lang("Available import formats"), importFormats); - String outFormats = ExportFormats.getConsoleExportList(70, 20, ""); + String outFormats = Globals.exportFactory.getExportersAsString(70, 20, ""); String outFormatsList = String.format("%s: %s%n", Localization.lang("Available export formats"), outFormats); String footer = '\n' + importFormatsList + outFormatsList + "\nPlease report issues at https://github.com/JabRef/jabref/issues."; diff --git a/src/main/java/org/jabref/gui/BasePanel.java b/src/main/java/org/jabref/gui/BasePanel.java index a9f8795bc42..5a22611fe69 100644 --- a/src/main/java/org/jabref/gui/BasePanel.java +++ b/src/main/java/org/jabref/gui/BasePanel.java @@ -116,7 +116,7 @@ import org.jabref.logic.layout.LayoutHelper; import org.jabref.logic.pdf.FileAnnotationCache; import org.jabref.logic.search.SearchQuery; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.logic.util.UpdateField; import org.jabref.logic.util.io.FileFinder; import org.jabref.logic.util.io.FileFinders; @@ -2221,8 +2221,8 @@ public SaveSelectedAction(SavePreferences.DatabaseSaveType saveType) { @Override public void action() throws SaveException { FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder() - .withDefaultExtension(FileExtensions.BIBTEX_DB) - .addExtensionFilter(FileExtensions.BIBTEX_DB) + .withDefaultExtension(FileType.BIBTEX_DB) + .addExtensionFilter(FileType.BIBTEX_DB) .withInitialDirectory(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)).build(); DialogService ds = new FXDialogService(); diff --git a/src/main/java/org/jabref/gui/DialogService.java b/src/main/java/org/jabref/gui/DialogService.java index adf70d30e01..1ddbe50a90c 100644 --- a/src/main/java/org/jabref/gui/DialogService.java +++ b/src/main/java/org/jabref/gui/DialogService.java @@ -9,7 +9,6 @@ import javafx.scene.control.Alert; import javafx.scene.control.ButtonType; import javafx.scene.control.DialogPane; -import javafx.stage.FileChooser; import org.jabref.gui.util.DirectoryDialogConfiguration; import org.jabref.gui.util.FileDialogConfiguration; @@ -50,13 +49,15 @@ public interface DialogService { /** * Create and display error dialog displaying the given exception. - * @param message the error message + * + * @param message the error message * @param exception the exception causing the error */ void showErrorDialogAndWait(String message, Throwable exception); /** * Create and display error dialog displaying the given exception. + * * @param exception the exception causing the error */ default void showErrorDialogAndWait(Exception exception) { @@ -65,6 +66,7 @@ default void showErrorDialogAndWait(Exception exception) { /** * Create and display error dialog displaying the given message. + * * @param message the error message */ void showErrorDialogAndWait(String message); @@ -107,7 +109,7 @@ default void showErrorDialogAndWait(Exception exception) { * @return Optional with the pressed Button as ButtonType */ Optional showCustomButtonDialogAndWait(Alert.AlertType type, String title, String content, - ButtonType... buttonTypes); + ButtonType... buttonTypes); /** * This will create and display a new dialog showing a custom {@link DialogPane} @@ -127,12 +129,14 @@ Optional showCustomButtonDialogAndWait(Alert.AlertType type, String /** * Constructs and shows a canceable {@link ProgressDialog}. Clicking cancel will cancel the underlying service and close the dialog + * * @param task The {@link Task} which executes the work and for which to show the dialog */ void showCanceableProgressDialogAndWait(Task task); /** * Notify the user in an non-blocking way (i.e., update status message instead of showing a dialog). + * * @param message the message to show. */ void notify(String message); @@ -141,6 +145,7 @@ Optional showCustomButtonDialogAndWait(Alert.AlertType type, String * Shows a new file save dialog. The method doesn't return until the * displayed file save dialog is dismissed. The return value specifies the * file chosen by the user or an empty {@link Optional} if no selection has been made. + * After a file was selected, the given file dialog configuration is updated with the selected extension type (if any). * * @return the selected file or an empty {@link Optional} if no file has been selected */ @@ -151,6 +156,7 @@ Optional showCustomButtonDialogAndWait(Alert.AlertType type, String * displayed open dialog is dismissed. The return value specifies * the file chosen by the user or an empty {@link Optional} if no selection has been * made. + * After a file was selected, the given file dialog configuration is updated with the selected extension type (if any). * * @return the selected file or an empty {@link Optional} if no file has been selected */ @@ -176,14 +182,6 @@ Optional showCustomButtonDialogAndWait(Alert.AlertType type, String */ Optional showDirectorySelectionDialog(DirectoryDialogConfiguration directoryDialogConfiguration); - /** - * Gets the configured {@link FileChooser}, should only be necessary in rare use cases. - * For normal usage use the show-Methods which directly return the selected file(s) - * @param fileDialogConfiguration - * @return A configured instance of the {@link FileChooser} - */ - FileChooser getConfiguredFileChooser(FileDialogConfiguration fileDialogConfiguration); - /** * Displays a Print Dialog. Allow the user to update job state such as printer and settings. These changes will be * available in the appropriate properties after the print dialog has returned. The print dialog is also used to diff --git a/src/main/java/org/jabref/gui/FXDialogService.java b/src/main/java/org/jabref/gui/FXDialogService.java index 4fb67ab560d..a42523f17bf 100644 --- a/src/main/java/org/jabref/gui/FXDialogService.java +++ b/src/main/java/org/jabref/gui/FXDialogService.java @@ -154,6 +154,7 @@ public void notify(String message) { public Optional showFileSaveDialog(FileDialogConfiguration fileDialogConfiguration) { FileChooser chooser = getConfiguredFileChooser(fileDialogConfiguration); File file = chooser.showSaveDialog(null); + Optional.ofNullable(chooser.getSelectedExtensionFilter()).ifPresent(fileDialogConfiguration::setSelectedExtensionFilter); return Optional.ofNullable(file).map(File::toPath); } @@ -161,6 +162,7 @@ public Optional showFileSaveDialog(FileDialogConfiguration fileDialogConfi public Optional showFileOpenDialog(FileDialogConfiguration fileDialogConfiguration) { FileChooser chooser = getConfiguredFileChooser(fileDialogConfiguration); File file = chooser.showOpenDialog(null); + Optional.ofNullable(chooser.getSelectedExtensionFilter()).ifPresent(fileDialogConfiguration::setSelectedExtensionFilter); return Optional.ofNullable(file).map(File::toPath); } @@ -184,8 +186,7 @@ private DirectoryChooser getConfiguredDirectoryChooser(DirectoryDialogConfigurat return chooser; } - @Override - public FileChooser getConfiguredFileChooser(FileDialogConfiguration fileDialogConfiguration) { + private FileChooser getConfiguredFileChooser(FileDialogConfiguration fileDialogConfiguration) { FileChooser chooser = new FileChooser(); chooser.getExtensionFilters().addAll(fileDialogConfiguration.getExtensionFilters()); chooser.setSelectedExtensionFilter(fileDialogConfiguration.getDefaultExtension()); diff --git a/src/main/java/org/jabref/gui/PreviewPanel.java b/src/main/java/org/jabref/gui/PreviewPanel.java index d453aa0fff2..fe0b096ba32 100644 --- a/src/main/java/org/jabref/gui/PreviewPanel.java +++ b/src/main/java/org/jabref/gui/PreviewPanel.java @@ -23,7 +23,7 @@ import org.jabref.gui.util.BackgroundTask; import org.jabref.gui.util.DefaultTaskExecutor; import org.jabref.logic.citationstyle.CitationStyle; -import org.jabref.logic.exporter.ExportFormats; +import org.jabref.logic.exporter.ExporterFactory; import org.jabref.logic.l10n.Localization; import org.jabref.logic.layout.Layout; import org.jabref.logic.layout.LayoutHelper; @@ -167,11 +167,11 @@ public void updateLayout(PreviewPreferences previewPreferences) { if (CitationStyle.isCitationStyleFile(style)) { if (basePanel.isPresent()) { layout = Optional.empty(); - CitationStyle citationStyle = CitationStyle.createCitationStyleFromFile(style); - if (citationStyle != null) { + CitationStyle.createCitationStyleFromFile(style) + .ifPresent(citationStyle -> { basePanel.get().getCitationStyleCache().setCitationStyle(citationStyle); basePanel.get().output(Localization.lang("Preview style changed to: %0", citationStyle.getTitle())); - } + }); } } else { updatePreviewLayout(previewPreferences.getPreviewStyle()); @@ -225,7 +225,7 @@ public BibEntry getEntry() { } public void update() { - ExportFormats.entryNumber = 1; // Set entry number in case that is included in the preview layout. + ExporterFactory.entryNumber = 1; // Set entry number in case that is included in the preview layout. if (citationStyleFuture.isPresent()) { citationStyleFuture.get().cancel(true); diff --git a/src/main/java/org/jabref/gui/auximport/FromAuxDialog.java b/src/main/java/org/jabref/gui/auximport/FromAuxDialog.java index 2de98920b40..15050314707 100644 --- a/src/main/java/org/jabref/gui/auximport/FromAuxDialog.java +++ b/src/main/java/org/jabref/gui/auximport/FromAuxDialog.java @@ -32,7 +32,7 @@ import org.jabref.logic.auxparser.AuxParser; import org.jabref.logic.auxparser.AuxParserResult; import org.jabref.logic.l10n.Localization; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.model.database.BibDatabase; import org.jabref.preferences.JabRefPreferences; @@ -159,8 +159,8 @@ private void initPanels() { JButton browseAuxFileButton = new JButton(Localization.lang("Browse")); FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder() - .addExtensionFilter(FileExtensions.AUX) - .withDefaultExtension(FileExtensions.AUX) + .addExtensionFilter(FileType.AUX) + .withDefaultExtension(FileType.AUX) .withInitialDirectory(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)).build(); DialogService ds = new FXDialogService(); diff --git a/src/main/java/org/jabref/gui/exporter/CustomExportDialog.java b/src/main/java/org/jabref/gui/exporter/CustomExportDialog.java index e0b367f4606..6c42a783a0c 100644 --- a/src/main/java/org/jabref/gui/exporter/CustomExportDialog.java +++ b/src/main/java/org/jabref/gui/exporter/CustomExportDialog.java @@ -28,7 +28,7 @@ import org.jabref.gui.util.DefaultTaskExecutor; import org.jabref.gui.util.FileDialogConfiguration; import org.jabref.logic.l10n.Localization; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.preferences.JabRefPreferences; import com.jgoodies.forms.builder.ButtonBarBuilder; @@ -95,8 +95,8 @@ public CustomExportDialog(final JabRefFrame parent) { JButton browse = new JButton(Localization.lang("Browse")); FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder() - .addExtensionFilter(FileExtensions.LAYOUT) - .withDefaultExtension(FileExtensions.LAYOUT) + .addExtensionFilter(FileType.LAYOUT) + .withDefaultExtension(FileType.LAYOUT) .withInitialDirectory(Globals.prefs.get(JabRefPreferences.EXPORT_WORKING_DIRECTORY)).build(); DialogService ds = new FXDialogService(); browse.addActionListener( diff --git a/src/main/java/org/jabref/gui/exporter/ExportAction.java b/src/main/java/org/jabref/gui/exporter/ExportAction.java index 6b520c9ea0a..b69bec7d191 100644 --- a/src/main/java/org/jabref/gui/exporter/ExportAction.java +++ b/src/main/java/org/jabref/gui/exporter/ExportAction.java @@ -1,28 +1,31 @@ package org.jabref.gui.exporter; import java.awt.event.ActionEvent; -import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.TreeSet; +import java.util.stream.Collectors; import javax.swing.AbstractAction; import javax.swing.Action; -import javax.swing.JFileChooser; import javax.swing.JOptionPane; -import javax.swing.filechooser.FileFilter; + +import javafx.stage.FileChooser; import org.jabref.Globals; +import org.jabref.gui.DialogService; +import org.jabref.gui.FXDialogService; import org.jabref.gui.JabRefFrame; import org.jabref.gui.actions.MnemonicAwareAction; +import org.jabref.gui.util.DefaultTaskExecutor; +import org.jabref.gui.util.FileDialogConfiguration; +import org.jabref.gui.util.FileFilterConverter; import org.jabref.gui.worker.AbstractWorker; -import org.jabref.logic.exporter.ExportFormat; -import org.jabref.logic.exporter.ExportFormats; -import org.jabref.logic.exporter.IExportFormat; -import org.jabref.logic.exporter.SavePreferences; +import org.jabref.logic.exporter.Exporter; +import org.jabref.logic.exporter.ExporterFactory; import org.jabref.logic.l10n.Localization; -import org.jabref.logic.layout.LayoutFormatterPreferences; +import org.jabref.logic.util.FileType; +import org.jabref.logic.util.io.FileUtil; import org.jabref.model.entry.BibEntry; import org.jabref.preferences.JabRefPreferences; @@ -64,37 +67,29 @@ public InternalExportAction(JabRefFrame frame, boolean selectedOnly) { @Override public void actionPerformed(ActionEvent e) { - Map customFormats = Globals.prefs.customExports.getCustomExportFormats(Globals.prefs, - Globals.journalAbbreviationLoader); - LayoutFormatterPreferences layoutPreferences = Globals.prefs - .getLayoutFormatterPreferences(Globals.journalAbbreviationLoader); - SavePreferences savePreferences = SavePreferences.loadForExportFromPreferences(Globals.prefs); - ExportFormats.initAllExports(customFormats, layoutPreferences, savePreferences); - JFileChooser fc = ExportAction - .createExportFileChooser(Globals.prefs.get(JabRefPreferences.EXPORT_WORKING_DIRECTORY)); - fc.showSaveDialog(frame); - File file = fc.getSelectedFile(); - if (file == null) { - return; - } - FileFilter ff = fc.getFileFilter(); - if (ff instanceof ExportFileFilter) { - - ExportFileFilter eff = (ExportFileFilter) ff; - String path = file.getPath(); - if (!path.endsWith(eff.getExtension().getExtensionsAsList().toString())) { - path = path + eff.getExtension(); + Globals.exportFactory = ExporterFactory.create(Globals.prefs, Globals.journalAbbreviationLoader); + FileDialogConfiguration fileDialogConfiguration = ExportAction.createExportFileChooser(Globals.exportFactory, Globals.prefs.get(JabRefPreferences.EXPORT_WORKING_DIRECTORY)); + DialogService dialogService = new FXDialogService(); + DefaultTaskExecutor.runInJavaFXThread(() -> + dialogService.showFileSaveDialog(fileDialogConfiguration) + .ifPresent(path -> export(path, fileDialogConfiguration.getSelectedExtensionFilter(), Globals.exportFactory.getExporters()))); + } + + private void export(Path file, FileChooser.ExtensionFilter selectedExtensionFilter, List exporters) { + String selectedExtension = selectedExtensionFilter.getExtensions().get(0).replace("*", ""); + if (!file.endsWith(selectedExtension)) { + FileUtil.addExtension(file, selectedExtension); } - file = new File(path); - if (file.exists()) { + + if (Files.exists(file)) { // Warn that the file exists: if (JOptionPane.showConfirmDialog(frame, - Localization.lang("'%0' exists. Overwrite file?", file.getName()), + Localization.lang("'%0' exists. Overwrite file?", file.getFileName().toString()), Localization.lang("Export"), JOptionPane.OK_CANCEL_OPTION) != JOptionPane.OK_OPTION) { return; } } - final IExportFormat format = eff.getExportFormat(); + final Exporter format = FileFilterConverter.getExporter(selectedExtensionFilter, exporters).orElseThrow(() -> new IllegalStateException("User didn't selected a file type for the extension")); List entries; if (selectedOnly) { // Selected entries @@ -112,10 +107,9 @@ public void actionPerformed(ActionEvent e) { // Make sure we remember which filter was used, to set // the default for next time: - Globals.prefs.put(JabRefPreferences.LAST_USED_EXPORT, format.getConsoleName()); - Globals.prefs.put(JabRefPreferences.EXPORT_WORKING_DIRECTORY, file.getParent()); + Globals.prefs.put(JabRefPreferences.LAST_USED_EXPORT, format.getId()); + Globals.prefs.put(JabRefPreferences.EXPORT_WORKING_DIRECTORY, file.getParent().getFileName().toString()); - final File finFile = file; final List finEntries = entries; AbstractWorker exportWorker = new AbstractWorker() { @@ -124,8 +118,8 @@ public void actionPerformed(ActionEvent e) { @Override public void run() { try { - format.performExport(frame.getCurrentBasePanel().getBibDatabaseContext(), - finFile.getPath(), + format.export(frame.getCurrentBasePanel().getBibDatabaseContext(), + file, frame.getCurrentBasePanel().getBibDatabaseContext().getMetaData().getEncoding() .orElse(Globals.prefs.getDefaultEncoding()), finEntries); @@ -160,35 +154,19 @@ public void update() { exportWorker.getWorker().run(); // Run the update method: exportWorker.update(); - } } } return new InternalExportAction(frame, selectedOnly); } - private static JFileChooser createExportFileChooser(String currentDir) { - String lastUsedFormat = Globals.prefs.get(JabRefPreferences.LAST_USED_EXPORT); - FileFilter defaultFilter = null; - JFileChooser fc = new JFileChooser(currentDir); - Set filters = new TreeSet<>(); - for (Map.Entry e : ExportFormats.getExportFormats().entrySet()) { - String formatName = e.getKey(); - IExportFormat format = e.getValue(); - ExportFileFilter exportFileFilter = new ExportFileFilter(format); - filters.add(exportFileFilter); - if (formatName.equals(lastUsedFormat)) { - defaultFilter = exportFileFilter; - } - } - for (FileFilter ff : filters) { - fc.addChoosableFileFilter(ff); - } - fc.setAcceptAllFileFilterUsed(false); - if (defaultFilter != null) { - fc.setFileFilter(defaultFilter); - } - return fc; + private static FileDialogConfiguration createExportFileChooser(ExporterFactory exportFactory, String currentDir) { + List fileTypes = exportFactory.getExporters().stream().map(Exporter::getFileType).collect(Collectors.toList()); + return new FileDialogConfiguration.Builder() + .addExtensionFilters(fileTypes) + .withDefaultExtension(Globals.prefs.get(JabRefPreferences.LAST_USED_EXPORT)) + .withInitialDirectory(currentDir) + .build(); } } diff --git a/src/main/java/org/jabref/gui/exporter/ExportFileFilter.java b/src/main/java/org/jabref/gui/exporter/ExportFileFilter.java index f6b65b1f0df..873a808f847 100644 --- a/src/main/java/org/jabref/gui/exporter/ExportFileFilter.java +++ b/src/main/java/org/jabref/gui/exporter/ExportFileFilter.java @@ -5,32 +5,31 @@ import javax.swing.filechooser.FileFilter; -import org.jabref.logic.exporter.IExportFormat; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.exporter.Exporter; +import org.jabref.logic.util.FileType; /** * File filter that lets the user choose export format while choosing file to - * export to. Contains a reference to the ExportFormat in question. + * export to. Contains a reference to the TemplateExporter in question. */ public class ExportFileFilter extends FileFilter implements Comparable { - private final IExportFormat format; - private final FileExtensions extension; + private final Exporter format; + private final FileType extension; private final String name; - - public ExportFileFilter(IExportFormat format) { + public ExportFileFilter(Exporter format) { this.format = format; - this.extension = format.getExtension(); + this.extension = format.getFileType(); this.name = format.getDisplayName() + " (*" + extension + ')'; } - public IExportFormat getExportFormat() { + public Exporter getExportFormat() { return format; } - public FileExtensions getExtension() { + public FileType getExtension() { return extension; } @@ -39,7 +38,7 @@ public boolean accept(File file) { if (file.isDirectory()) { return true; } else { - return file.getPath().toLowerCase(Locale.ROOT).endsWith(extension.getExtensionsAsList().toString()); + return file.getPath().toLowerCase(Locale.ROOT).endsWith(extension.getExtensions().toString()); } } diff --git a/src/main/java/org/jabref/gui/exporter/ExportToClipboardAction.java b/src/main/java/org/jabref/gui/exporter/ExportToClipboardAction.java index aecdddb5e0f..05cc5a433e2 100644 --- a/src/main/java/org/jabref/gui/exporter/ExportToClipboardAction.java +++ b/src/main/java/org/jabref/gui/exporter/ExportToClipboardAction.java @@ -6,10 +6,10 @@ import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.Reader; -import java.util.Collections; -import java.util.LinkedList; +import java.util.Comparator; import java.util.List; import java.util.Objects; +import java.util.stream.Collectors; import javax.swing.BorderFactory; import javax.swing.JList; @@ -20,8 +20,7 @@ import org.jabref.gui.BasePanel; import org.jabref.gui.JabRefFrame; import org.jabref.gui.worker.AbstractWorker; -import org.jabref.logic.exporter.ExportFormats; -import org.jabref.logic.exporter.IExportFormat; +import org.jabref.logic.exporter.Exporter; import org.jabref.logic.l10n.Localization; import org.jabref.model.entry.BibEntry; @@ -56,15 +55,11 @@ public void run() { return; } - List exportFormats = new LinkedList<>(ExportFormats.getExportFormats().values()); - Collections.sort(exportFormats, (e1, e2) -> e1.getDisplayName().compareTo(e2.getDisplayName())); - String[] exportFormatDisplayNames = new String[exportFormats.size()]; - for (int i = 0; i < exportFormats.size(); i++) { - IExportFormat exportFormat = exportFormats.get(i); - exportFormatDisplayNames[i] = exportFormat.getDisplayName(); - } + List exporters = Globals.exportFactory.getExporters(); + exporters.sort(Comparator.comparing(Exporter::getDisplayName)); + List exportFormatDisplayNames = exporters.stream().map(Exporter::getDisplayName).collect(Collectors.toList()); - JList list = new JList<>(exportFormatDisplayNames); + JList list = new JList<>(exportFormatDisplayNames.toArray(new String[exportFormatDisplayNames.size()])); list.setBorder(BorderFactory.createEtchedBorder()); list.setSelectionInterval(0, 0); list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); @@ -77,7 +72,7 @@ public void run() { return; } - IExportFormat format = exportFormats.get(list.getSelectedIndex()); + Exporter exporter = exporters.get(list.getSelectedIndex()); // Set the global variable for this database's file directory before exporting, // so formatters can resolve linked files correctly. @@ -94,7 +89,7 @@ public void run() { List entries = panel.getSelectedEntries(); // Write to file: - format.performExport(panel.getBibDatabaseContext(), tmp.getPath(), + exporter.export(panel.getBibDatabaseContext(), tmp.toPath(), panel.getBibDatabaseContext().getMetaData().getEncoding() .orElse(Globals.prefs.getDefaultEncoding()), entries); diff --git a/src/main/java/org/jabref/gui/exporter/SaveDatabaseAction.java b/src/main/java/org/jabref/gui/exporter/SaveDatabaseAction.java index 8c2720accc7..25bbbc2961d 100644 --- a/src/main/java/org/jabref/gui/exporter/SaveDatabaseAction.java +++ b/src/main/java/org/jabref/gui/exporter/SaveDatabaseAction.java @@ -32,7 +32,7 @@ import org.jabref.logic.l10n.Encodings; import org.jabref.logic.l10n.Localization; import org.jabref.logic.shared.prefs.SharedDatabasePreferences; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.logic.util.io.FileBasedLock; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.database.event.ChangePropagation; @@ -293,8 +293,8 @@ public void saveAs() throws Exception { // configure file dialog FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder() - .addExtensionFilter(FileExtensions.BIBTEX_DB) - .withDefaultExtension(FileExtensions.BIBTEX_DB) + .addExtensionFilter(FileType.BIBTEX_DB) + .withDefaultExtension(FileType.BIBTEX_DB) .withInitialDirectory(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)).build(); DialogService ds = new FXDialogService(); diff --git a/src/main/java/org/jabref/gui/importer/ImportCustomizationDialog.java b/src/main/java/org/jabref/gui/importer/ImportCustomizationDialog.java index 8d6658a85f5..b279e42b803 100644 --- a/src/main/java/org/jabref/gui/importer/ImportCustomizationDialog.java +++ b/src/main/java/org/jabref/gui/importer/ImportCustomizationDialog.java @@ -38,7 +38,7 @@ import org.jabref.logic.help.HelpFile; import org.jabref.logic.importer.fileformat.CustomImporter; import org.jabref.logic.l10n.Localization; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.preferences.JabRefPreferences; import com.jgoodies.forms.builder.ButtonBarBuilder; @@ -83,8 +83,8 @@ public ImportCustomizationDialog(final JabRefFrame frame) { addFromFolderButton.addActionListener(e -> { FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder() - .addExtensionFilter(FileExtensions.CLASS) - .withDefaultExtension(FileExtensions.JAR) + .addExtensionFilter(FileType.CLASS) + .withDefaultExtension(FileType.JAR) .withInitialDirectory(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)).build(); DialogService ds = new FXDialogService(); @@ -118,8 +118,8 @@ public ImportCustomizationDialog(final JabRefFrame frame) { JButton addFromJarButton = new JButton(Localization.lang("Add from JAR")); addFromJarButton.addActionListener(e -> { FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder() - .addExtensionFilters(EnumSet.of(FileExtensions.ZIP, FileExtensions.JAR)) - .withDefaultExtension(FileExtensions.JAR) + .addExtensionFilters(EnumSet.of(FileType.ZIP, FileType.JAR)) + .withDefaultExtension(FileType.JAR) .withInitialDirectory(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)).build(); DialogService ds = new FXDialogService(); diff --git a/src/main/java/org/jabref/gui/importer/ImportFileFilter.java b/src/main/java/org/jabref/gui/importer/ImportFileFilter.java deleted file mode 100644 index b4375ced703..00000000000 --- a/src/main/java/org/jabref/gui/importer/ImportFileFilter.java +++ /dev/null @@ -1,40 +0,0 @@ -package org.jabref.gui.importer; - -import java.util.Arrays; -import java.util.Collection; -import java.util.List; -import java.util.Optional; -import java.util.stream.Collectors; - -import javafx.stage.FileChooser; - -import org.jabref.logic.importer.Importer; -import org.jabref.logic.util.FileExtensions; - -class ImportFileFilter { - - private ImportFileFilter() { - } - - public static FileChooser.ExtensionFilter convert(Importer format) { - return new FileChooser.ExtensionFilter(format.getExtensions().getDescription(), - format.getExtensions().getExtensions()); - } - - public static FileChooser.ExtensionFilter convert(String description, Collection formats) { - List extensions = formats.stream().map(Importer::getExtensions).collect(Collectors.toList()); - List flatExtensions = extensions.stream().flatMap(extList -> Arrays.stream(extList.getExtensions())) - .map(ending -> "*." + ending) - .collect(Collectors.toList()); - - return new FileChooser.ExtensionFilter(description, flatExtensions.toArray(new String[flatExtensions.size()])); - } - - public static Optional convert(FileChooser.ExtensionFilter extensionFilter, - Collection formats) { - return formats.stream().filter(format -> format.getDescription().equals(extensionFilter.getDescription())) - .findFirst(); - - } - -} diff --git a/src/main/java/org/jabref/gui/importer/ImportFormats.java b/src/main/java/org/jabref/gui/importer/ImportFormats.java index d291fac7323..ba50dc59a90 100644 --- a/src/main/java/org/jabref/gui/importer/ImportFormats.java +++ b/src/main/java/org/jabref/gui/importer/ImportFormats.java @@ -1,7 +1,6 @@ package org.jabref.gui.importer; import java.awt.event.ActionEvent; -import java.io.File; import java.nio.file.Files; import java.nio.file.Path; import java.util.Collections; @@ -24,9 +23,10 @@ import org.jabref.gui.keyboard.KeyBinding; import org.jabref.gui.util.DefaultTaskExecutor; import org.jabref.gui.util.FileDialogConfiguration; +import org.jabref.gui.util.FileFilterConverter; import org.jabref.logic.importer.Importer; import org.jabref.logic.l10n.Localization; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.preferences.JabRefPreferences; import org.apache.commons.logging.Log; @@ -67,46 +67,37 @@ public ImportAction(boolean newDatabase) { @Override public void actionPerformed(ActionEvent e) { - SortedSet importers = Globals.IMPORT_FORMAT_READER.getImportFormats(); - List extensions = importers.stream().map(Importer::getExtensions) + List extensions = importers.stream().map(Importer::getFileType) .collect(Collectors.toList()); - FileChooser.ExtensionFilter allImports = ImportFileFilter - .convert(Localization.lang("Available import formats"), importers); + FileChooser.ExtensionFilter allImports = FileFilterConverter.forAllImporters(importers); FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder() + .addExtensionFilter(allImports) .addExtensionFilters(extensions) + .withDefaultExtension(allImports) .withInitialDirectory(Globals.prefs.get(JabRefPreferences.IMPORT_WORKING_DIRECTORY)) .build(); - DialogService ds = new FXDialogService(); - - FileChooser fs = ds.getConfiguredFileChooser(fileDialogConfiguration); - fs.getExtensionFilters().add(0, allImports); - fs.setSelectedExtensionFilter(allImports); - File f = DefaultTaskExecutor - .runInJavaFXThread(() -> fs.showOpenDialog(null)); - Optional selectedFile = Optional.ofNullable(f).map(File::toPath); - FileChooser.ExtensionFilter selectedExtension = fs.getSelectedExtensionFilter(); - // Add file filter for all supported types - - selectedFile.ifPresent(file -> { - try { - if (!Files.exists(file)) { - JOptionPane.showMessageDialog(frame, - Localization.lang("File not found") + ": '" + file.getFileName() + "'.", - Localization.lang("Import"), JOptionPane.ERROR_MESSAGE); - return; - } - Optional format = ImportFileFilter.convert(selectedExtension, importers); - ImportMenuItem importMenu = new ImportMenuItem(frame, newDatabase, format.orElse(null)); - importMenu.automatedImport(Collections.singletonList(file.toString())); - // Set last working dir for import - Globals.prefs.put(JabRefPreferences.IMPORT_WORKING_DIRECTORY, file.getParent().toString()); - } catch (Exception ex) { - LOGGER.warn("Cannot import file", ex); - } + DialogService dialogService = new FXDialogService(); + DefaultTaskExecutor.runInJavaFXThread(() -> { + dialogService.showFileOpenDialog(fileDialogConfiguration) + .ifPresent(path -> doImport(path, importers, fileDialogConfiguration.getSelectedExtensionFilter())); }); } + + private void doImport(Path file, SortedSet importers, FileChooser.ExtensionFilter selectedExtensionFilter) { + if (!Files.exists(file)) { + JOptionPane.showMessageDialog(frame, + Localization.lang("File not found") + ": '" + file.getFileName() + "'.", + Localization.lang("Import"), JOptionPane.ERROR_MESSAGE); + return; + } + Optional format = FileFilterConverter.getImporter(selectedExtensionFilter, importers); + ImportMenuItem importMenu = new ImportMenuItem(frame, newDatabase, format.orElse(null)); + importMenu.automatedImport(Collections.singletonList(file.toString())); + // Set last working dir for import + Globals.prefs.put(JabRefPreferences.IMPORT_WORKING_DIRECTORY, file.getParent().toString()); + } } return new ImportAction(openInNew); diff --git a/src/main/java/org/jabref/gui/importer/actions/AppendDatabaseAction.java b/src/main/java/org/jabref/gui/importer/actions/AppendDatabaseAction.java index 76b7350198a..fa6e790dfbf 100644 --- a/src/main/java/org/jabref/gui/importer/actions/AppendDatabaseAction.java +++ b/src/main/java/org/jabref/gui/importer/actions/AppendDatabaseAction.java @@ -24,7 +24,7 @@ import org.jabref.logic.importer.OpenDatabase; import org.jabref.logic.importer.ParserResult; import org.jabref.logic.l10n.Localization; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.logic.util.UpdateField; import org.jabref.model.database.BibDatabase; import org.jabref.model.database.BibDatabaseContext; @@ -152,7 +152,7 @@ public void action() { if (dialog.isOkPressed()) { FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder() - .withDefaultExtension(FileExtensions.BIBTEX_DB) + .withDefaultExtension(FileType.BIBTEX_DB) .withInitialDirectory(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)) .build(); DialogService dialogService = new FXDialogService(); diff --git a/src/main/java/org/jabref/gui/importer/actions/OpenDatabaseAction.java b/src/main/java/org/jabref/gui/importer/actions/OpenDatabaseAction.java index a51230d6b51..3be44105d36 100644 --- a/src/main/java/org/jabref/gui/importer/actions/OpenDatabaseAction.java +++ b/src/main/java/org/jabref/gui/importer/actions/OpenDatabaseAction.java @@ -38,7 +38,7 @@ import org.jabref.logic.l10n.Localization; import org.jabref.logic.shared.exception.InvalidDBMSConnectionPropertiesException; import org.jabref.logic.shared.exception.NotASharedDatabaseException; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.logic.util.io.FileBasedLock; import org.jabref.migrations.FileLinksUpgradeWarning; import org.jabref.model.database.BibDatabase; @@ -102,8 +102,8 @@ public void actionPerformed(ActionEvent e) { DialogService ds = new FXDialogService(); FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder() - .addExtensionFilter(FileExtensions.BIBTEX_DB) - .withDefaultExtension(FileExtensions.BIBTEX_DB) + .addExtensionFilter(FileType.BIBTEX_DB) + .withDefaultExtension(FileType.BIBTEX_DB) .withInitialDirectory(Paths.get(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY))) .build(); diff --git a/src/main/java/org/jabref/gui/journals/ManageJournalAbbreviationsViewModel.java b/src/main/java/org/jabref/gui/journals/ManageJournalAbbreviationsViewModel.java index 5bacfc4cc0b..62ae065b835 100644 --- a/src/main/java/org/jabref/gui/journals/ManageJournalAbbreviationsViewModel.java +++ b/src/main/java/org/jabref/gui/journals/ManageJournalAbbreviationsViewModel.java @@ -24,7 +24,7 @@ import org.jabref.logic.journals.JournalAbbreviationLoader; import org.jabref.logic.journals.JournalAbbreviationPreferences; import org.jabref.logic.l10n.Localization; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.preferences.PreferencesService; import org.apache.commons.logging.Log; @@ -163,7 +163,7 @@ public void createFileObjects() { */ public void addNewFile() { FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder() - .addExtensionFilter(FileExtensions.TXT) + .addExtensionFilter(FileType.TXT) .build(); dialogService.showFileSaveDialog(fileDialogConfiguration).ifPresent(this::openFile); @@ -195,7 +195,7 @@ private void openFile(Path filePath) { public void openFile() { FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder() - .addExtensionFilter(FileExtensions.TXT) + .addExtensionFilter(FileType.TXT) .build(); dialogService.showFileOpenDialog(fileDialogConfiguration).ifPresent(this::openFile); diff --git a/src/main/java/org/jabref/gui/openoffice/StyleSelectDialog.java b/src/main/java/org/jabref/gui/openoffice/StyleSelectDialog.java index b26740ce10f..157fe63d5d9 100644 --- a/src/main/java/org/jabref/gui/openoffice/StyleSelectDialog.java +++ b/src/main/java/org/jabref/gui/openoffice/StyleSelectDialog.java @@ -54,7 +54,7 @@ import org.jabref.logic.openoffice.OOBibStyle; import org.jabref.logic.openoffice.OpenOfficePreferences; import org.jabref.logic.openoffice.StyleLoader; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.logic.util.TestEntry; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.BibEntry; @@ -471,8 +471,8 @@ public AddFileDialog() { JButton browse = new JButton(Localization.lang("Browse")); FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder() - .addExtensionFilter(FileExtensions.JSTYLE) - .withDefaultExtension(FileExtensions.JSTYLE) + .addExtensionFilter(FileType.JSTYLE) + .withDefaultExtension(FileType.JSTYLE) .withInitialDirectory(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)).build(); DialogService ds = new FXDialogService(); diff --git a/src/main/java/org/jabref/gui/plaintextimport/TextInputDialog.java b/src/main/java/org/jabref/gui/plaintextimport/TextInputDialog.java index 6ce4035ede6..56d6aa492e8 100644 --- a/src/main/java/org/jabref/gui/plaintextimport/TextInputDialog.java +++ b/src/main/java/org/jabref/gui/plaintextimport/TextInputDialog.java @@ -77,7 +77,7 @@ import org.jabref.logic.importer.ParserResult; import org.jabref.logic.importer.fileformat.FreeCiteImporter; import org.jabref.logic.l10n.Localization; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.logic.util.OS; import org.jabref.logic.util.UpdateField; import org.jabref.model.EntryTypes; @@ -556,8 +556,8 @@ public LoadAction() { public void actionPerformed(ActionEvent e) { try { FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder() - .addExtensionFilter(FileExtensions.TXT) - .withDefaultExtension(FileExtensions.TXT) + .addExtensionFilter(FileType.TXT) + .withDefaultExtension(FileType.TXT) .withInitialDirectory(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)).build(); DialogService ds = new FXDialogService(); diff --git a/src/main/java/org/jabref/gui/preftabs/PreferencesDialog.java b/src/main/java/org/jabref/gui/preftabs/PreferencesDialog.java index 0e1b9be77d7..c1cf0abb5d8 100644 --- a/src/main/java/org/jabref/gui/preftabs/PreferencesDialog.java +++ b/src/main/java/org/jabref/gui/preftabs/PreferencesDialog.java @@ -8,7 +8,6 @@ import java.nio.file.Path; import java.util.ArrayList; import java.util.List; -import java.util.Map; import java.util.Optional; import java.util.prefs.BackingStoreException; @@ -32,13 +31,10 @@ import org.jabref.gui.maintable.MainTable; import org.jabref.gui.util.DefaultTaskExecutor; import org.jabref.gui.util.FileDialogConfiguration; -import org.jabref.logic.exporter.ExportFormat; -import org.jabref.logic.exporter.ExportFormats; -import org.jabref.logic.exporter.SavePreferences; +import org.jabref.logic.exporter.ExporterFactory; import org.jabref.logic.l10n.Localization; -import org.jabref.logic.layout.LayoutFormatterPreferences; import org.jabref.logic.shared.prefs.SharedDatabasePreferences; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.preferences.JabRefPreferences; import org.jabref.preferences.JabRefPreferencesFilter; @@ -163,8 +159,8 @@ public PreferencesDialog(JabRefFrame parent) { importPreferences.addActionListener(e -> { FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder() - .addExtensionFilter(FileExtensions.XML) - .withDefaultExtension(FileExtensions.XML) + .addExtensionFilter(FileType.XML) + .withDefaultExtension(FileType.XML) .withInitialDirectory(getPrefsExportPath()).build(); DialogService ds = new FXDialogService(); @@ -228,12 +224,8 @@ private String getPrefsExportPath() { private void updateAfterPreferenceChanges() { setValues(); - Map customFormats = Globals.prefs.customExports.getCustomExportFormats(Globals.prefs, - Globals.journalAbbreviationLoader); - LayoutFormatterPreferences layoutPreferences = Globals.prefs - .getLayoutFormatterPreferences(Globals.journalAbbreviationLoader); - SavePreferences savePreferences = SavePreferences.loadForExportFromPreferences(Globals.prefs); - ExportFormats.initAllExports(customFormats, layoutPreferences, savePreferences); + + Globals.exportFactory = ExporterFactory.create(Globals.prefs, Globals.journalAbbreviationLoader); Globals.prefs.updateEntryEditorTabList(); } @@ -290,8 +282,8 @@ public ExportAction() { public void actionPerformed(ActionEvent e) { FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder() - .addExtensionFilter(FileExtensions.XML) - .withDefaultExtension(FileExtensions.XML) + .addExtensionFilter(FileType.XML) + .withDefaultExtension(FileType.XML) .withInitialDirectory(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)).build(); DialogService ds = new FXDialogService(); Optional path = DefaultTaskExecutor diff --git a/src/main/java/org/jabref/gui/protectedterms/NewProtectedTermsFileDialog.java b/src/main/java/org/jabref/gui/protectedterms/NewProtectedTermsFileDialog.java index e0e59c9c609..39fede36dbe 100644 --- a/src/main/java/org/jabref/gui/protectedterms/NewProtectedTermsFileDialog.java +++ b/src/main/java/org/jabref/gui/protectedterms/NewProtectedTermsFileDialog.java @@ -25,7 +25,7 @@ import org.jabref.gui.util.FileDialogConfiguration; import org.jabref.logic.l10n.Localization; import org.jabref.logic.protectedterms.ProtectedTermsLoader; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.preferences.JabRefPreferences; import com.jgoodies.forms.builder.ButtonBarBuilder; @@ -60,8 +60,8 @@ private void setupDialog() { JButton browse = new JButton(Localization.lang("Browse")); FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder() - .addExtensionFilter(FileExtensions.TERMS) - .withDefaultExtension(FileExtensions.TERMS) + .addExtensionFilter(FileType.TERMS) + .withDefaultExtension(FileType.TERMS) .withInitialDirectory(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)).build(); DialogService ds = new FXDialogService(); diff --git a/src/main/java/org/jabref/gui/protectedterms/ProtectedTermsDialog.java b/src/main/java/org/jabref/gui/protectedterms/ProtectedTermsDialog.java index 7ff7856fa6b..dcfd92836c5 100644 --- a/src/main/java/org/jabref/gui/protectedterms/ProtectedTermsDialog.java +++ b/src/main/java/org/jabref/gui/protectedterms/ProtectedTermsDialog.java @@ -51,7 +51,7 @@ import org.jabref.logic.l10n.Localization; import org.jabref.logic.protectedterms.ProtectedTermsList; import org.jabref.logic.protectedterms.ProtectedTermsLoader; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.BibEntry; import org.jabref.preferences.JabRefPreferences; @@ -431,8 +431,8 @@ public AddFileDialog() { JButton browse = new JButton(Localization.lang("Browse")); FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder() - .addExtensionFilter(FileExtensions.TERMS) - .withDefaultExtension(FileExtensions.TERMS) + .addExtensionFilter(FileType.TERMS) + .withDefaultExtension(FileType.TERMS) .withInitialDirectory(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)).build(); DialogService ds = new FXDialogService(); diff --git a/src/main/java/org/jabref/gui/shared/ConnectToSharedDatabaseDialog.java b/src/main/java/org/jabref/gui/shared/ConnectToSharedDatabaseDialog.java index 9ce83ab598b..9c52fe0f02a 100644 --- a/src/main/java/org/jabref/gui/shared/ConnectToSharedDatabaseDialog.java +++ b/src/main/java/org/jabref/gui/shared/ConnectToSharedDatabaseDialog.java @@ -50,8 +50,7 @@ import org.jabref.logic.shared.exception.InvalidDBMSConnectionPropertiesException; import org.jabref.logic.shared.prefs.SharedDatabasePreferences; import org.jabref.logic.shared.security.Password; -import org.jabref.logic.util.FileExtensions; -import org.jabref.model.database.BibDatabaseContext; +import org.jabref.logic.util.FileType;import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.database.shared.DBMSType; import org.jabref.model.database.shared.DatabaseLocation; import org.jabref.model.database.shared.DatabaseNotSupportedException; @@ -470,8 +469,8 @@ private boolean isSharedDatabaseAlreadyPresent() { private void showFileChooser() { FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder() - .addExtensionFilter(FileExtensions.BIBTEX_DB) - .withDefaultExtension(FileExtensions.BIBTEX_DB) + .addExtensionFilter(FileType.BIBTEX_DB) + .withDefaultExtension(FileType.BIBTEX_DB) .withInitialDirectory(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)).build(); DialogService ds = new FXDialogService(); diff --git a/src/main/java/org/jabref/gui/util/FileDialogConfiguration.java b/src/main/java/org/jabref/gui/util/FileDialogConfiguration.java index 219b919d28e..4cc833069dc 100644 --- a/src/main/java/org/jabref/gui/util/FileDialogConfiguration.java +++ b/src/main/java/org/jabref/gui/util/FileDialogConfiguration.java @@ -8,11 +8,10 @@ import java.util.List; import java.util.Objects; import java.util.Optional; -import java.util.stream.Collectors; import javafx.stage.FileChooser; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; public class FileDialogConfiguration { @@ -20,6 +19,7 @@ public class FileDialogConfiguration { private final Path initialDirectory; private final FileChooser.ExtensionFilter defaultExtension; private final String initialFileName; + private FileChooser.ExtensionFilter selectedExtensionFilter; private FileDialogConfiguration(Path initialDirectory, List extensionFilters, FileChooser.ExtensionFilter defaultExtension, String initialFileName) { @@ -45,30 +45,33 @@ public List getExtensionFilters() { return extensionFilters; } + public FileChooser.ExtensionFilter getSelectedExtensionFilter() { + return selectedExtensionFilter; + } + + public void setSelectedExtensionFilter(FileChooser.ExtensionFilter selectedExtensionFilter) { + this.selectedExtensionFilter = selectedExtensionFilter; + } + public static class Builder { - private final List extensionFilter = new ArrayList<>(); + private final List extensionFilters = new ArrayList<>(); private Path initialDirectory; private FileChooser.ExtensionFilter defaultExtension; private String initialFileName; - public Builder addExtensionFilter(FileExtensions extension) { - extensionFilter.add(toFilter(extension)); + public Builder addExtensionFilter(FileType fileType) { + extensionFilters.add(FileFilterConverter.toExtensionFilter(fileType)); return this; } - private FileChooser.ExtensionFilter toFilter(FileExtensions extension) { - return new FileChooser.ExtensionFilter(extension.getDescription(), - extension.getExtensionsAsList().stream().map(ending -> "*." + ending).collect(Collectors.toList())); - } - - public Builder addExtensionFilters(Collection extensions) { - extensions.forEach(this::addExtensionFilter); + public Builder addExtensionFilters(Collection fileTypes) { + fileTypes.forEach(this::addExtensionFilter); return this; } public FileDialogConfiguration build() { - return new FileDialogConfiguration(initialDirectory, extensionFilter, defaultExtension, initialFileName); + return new FileDialogConfiguration(initialDirectory, extensionFilters, defaultExtension, initialFileName); } public Builder withInitialDirectory(Path directory) { @@ -98,16 +101,33 @@ public Builder withInitialDirectory(String directory) { return this; } - public Builder withDefaultExtension(FileExtensions extension) { - defaultExtension = toFilter(extension); + public Builder withDefaultExtension(FileType fileType) { + defaultExtension = FileFilterConverter.toExtensionFilter(fileType); return this; } public Builder withInitialFileName(String initialFileName) { this.initialFileName = initialFileName; return this; + } + + public Builder withDefaultExtension(String fileTypeDescription) { + extensionFilters.stream() + .filter(type -> type.getDescription().equalsIgnoreCase(fileTypeDescription)) + .findFirst() + .ifPresent(extensionFilter -> defaultExtension = extensionFilter); + + return this; + } + public Builder addExtensionFilter(FileChooser.ExtensionFilter extensionFilter) { + extensionFilters.add(extensionFilter); + return this; } + public Builder withDefaultExtension(FileChooser.ExtensionFilter extensionFilter) { + defaultExtension = extensionFilter; + return this; + } } } diff --git a/src/main/java/org/jabref/gui/util/FileFilterConverter.java b/src/main/java/org/jabref/gui/util/FileFilterConverter.java new file mode 100644 index 00000000000..2de1162b34a --- /dev/null +++ b/src/main/java/org/jabref/gui/util/FileFilterConverter.java @@ -0,0 +1,50 @@ +package org.jabref.gui.util; + +import java.util.Collection; +import java.util.List; +import java.util.Optional; +import java.util.SortedSet; +import java.util.stream.Collectors; + +import javafx.stage.FileChooser; + +import org.jabref.logic.exporter.Exporter; +import org.jabref.logic.importer.Importer; +import org.jabref.logic.l10n.Localization; +import org.jabref.logic.util.FileType; + +public class FileFilterConverter { + + private FileFilterConverter() { + } + + public static FileChooser.ExtensionFilter toExtensionFilter(FileType fileType) { + return new FileChooser.ExtensionFilter(fileType.getDescription(), + fileType.getExtensions().stream().map(ending -> "*." + ending).collect(Collectors.toList())); + } + + private static FileChooser.ExtensionFilter convertImporter(String description, Collection formats) { + List fileTypes = formats.stream().map(Importer::getFileType).collect(Collectors.toList()); + return toExtensionFilter(description, fileTypes); + } + + private static FileChooser.ExtensionFilter toExtensionFilter(String description, List fileTypes) { + List flatExtensions = fileTypes.stream().flatMap(extList -> extList.getExtensionsWithDot().stream()) + .map(ending -> "*" + ending) + .collect(Collectors.toList()); + + return new FileChooser.ExtensionFilter(description, flatExtensions.toArray(new String[flatExtensions.size()])); + } + + public static Optional getImporter(FileChooser.ExtensionFilter extensionFilter, Collection importers) { + return importers.stream().filter(importer -> importer.getDescription().equals(extensionFilter.getDescription())).findFirst(); + } + + public static Optional getExporter(FileChooser.ExtensionFilter extensionFilter, Collection exporters) { + return exporters.stream().filter(exporter -> exporter.getDescription().equals(extensionFilter.getDescription())).findFirst(); + } + + public static FileChooser.ExtensionFilter forAllImporters(SortedSet importers) { + return convertImporter(Localization.lang("Available import formats"), importers); + } +} diff --git a/src/main/java/org/jabref/gui/worker/CitationStyleToClipboardWorker.java b/src/main/java/org/jabref/gui/worker/CitationStyleToClipboardWorker.java index b50cb1df482..29ff7feea89 100644 --- a/src/main/java/org/jabref/gui/worker/CitationStyleToClipboardWorker.java +++ b/src/main/java/org/jabref/gui/worker/CitationStyleToClipboardWorker.java @@ -61,10 +61,10 @@ protected List doInBackground() throws Exception { // style. Therefore, we extract the style source from the file. String styleSource = null; if (CitationStyle.isCitationStyleFile(style)) { - final CitationStyle citationStyleFromFile = CitationStyle.createCitationStyleFromFile(style); - if (citationStyleFromFile != null && !citationStyleFromFile.getSource().isEmpty()) { - styleSource = citationStyleFromFile.getSource(); - } + styleSource = CitationStyle.createCitationStyleFromFile(style) + .filter(citationStyleFromFile -> !citationStyleFromFile.getSource().isEmpty()) + .map(CitationStyle::getSource) + .orElse(null); } if (styleSource != null) { return CitationStyleGenerator.generateCitations(selectedEntries, styleSource, outputFormat); diff --git a/src/main/java/org/jabref/logic/citationstyle/CitationStyle.java b/src/main/java/org/jabref/logic/citationstyle/CitationStyle.java index e6cd17fb137..01dee221351 100644 --- a/src/main/java/org/jabref/logic/citationstyle/CitationStyle.java +++ b/src/main/java/org/jabref/logic/citationstyle/CitationStyle.java @@ -24,7 +24,7 @@ import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import de.undercouch.citeproc.helper.CSLUtils; import org.apache.commons.logging.Log; @@ -62,7 +62,7 @@ private CitationStyle(final String filename, final String title, final String so /** * Creates an CitationStyle instance out of the style string */ - private static CitationStyle createCitationStyleFromSource(final String source, final String filename) { + private static Optional createCitationStyleFromSource(final String source, final String filename) { if ((filename != null) && !filename.isEmpty() && (source != null) && !source.isEmpty()) { try { DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); @@ -75,12 +75,12 @@ private static CitationStyle createCitationStyleFromSource(final String source, NodeList titleNode = ((Element) nodes.item(0)).getElementsByTagName("title"); String title = ((CharacterData) titleNode.item(0).getFirstChild()).getData(); - return new CitationStyle(filename, title, source); + return Optional.of(new CitationStyle(filename, title, source)); } catch (ParserConfigurationException | SAXException | IOException e) { LOGGER.error("Error while parsing source", e); } } - return null; + return Optional.empty(); } private static String stripInvalidProlog(String source) { @@ -95,10 +95,10 @@ private static String stripInvalidProlog(String source) { /** * Loads the CitationStyle from the given file */ - public static CitationStyle createCitationStyleFromFile(final String styleFile) { + public static Optional createCitationStyleFromFile(final String styleFile) { if (!isCitationStyleFile(styleFile)) { LOGGER.error("Can only load style files: " + styleFile); - return null; + return Optional.empty(); } try { @@ -117,7 +117,7 @@ public static CitationStyle createCitationStyleFromFile(final String styleFile) } catch (IOException e) { LOGGER.error("Error reading source file", e); } - return null; + return Optional.empty(); } /** @@ -125,7 +125,7 @@ public static CitationStyle createCitationStyleFromFile(final String styleFile) * @return default citation style */ public static CitationStyle getDefault() { - return createCitationStyleFromFile(DEFAULT); + return createCitationStyleFromFile(DEFAULT).orElse(new CitationStyle("", "Empty", "")); } /** @@ -156,10 +156,8 @@ public static List discoverCitationStyles() { List allStyles = Files.find(jarFs.getRootDirectories().iterator().next(), 1, (file, attr) -> file.toString().endsWith("csl")).collect(Collectors.toList()); for (Path style : allStyles) { - CitationStyle citationStyle = CitationStyle.createCitationStyleFromFile(style.getFileName().toString()); - if (citationStyle != null) { - STYLES.add(citationStyle); - } + CitationStyle.createCitationStyleFromFile(style.getFileName().toString()) + .ifPresent(STYLES::add); } } return STYLES; @@ -173,7 +171,7 @@ public static List discoverCitationStyles() { * Checks if the given style file is a CitationStyle */ public static boolean isCitationStyleFile(String styleFile) { - return Arrays.stream(FileExtensions.CITATION_STYLE.getExtensions()).anyMatch(styleFile::endsWith); + return FileType.CITATION_STYLE.getExtensions().stream().anyMatch(styleFile::endsWith); } public String getTitle() { diff --git a/src/main/java/org/jabref/logic/citationstyle/CitationStyleCache.java b/src/main/java/org/jabref/logic/citationstyle/CitationStyleCache.java index df2f16659a4..77d60f0f89a 100644 --- a/src/main/java/org/jabref/logic/citationstyle/CitationStyleCache.java +++ b/src/main/java/org/jabref/logic/citationstyle/CitationStyleCache.java @@ -30,7 +30,7 @@ public CitationStyleCache(BibDatabaseContext bibDatabaseContext) { } public CitationStyleCache(BibDatabaseContext bibDatabaseContext, CitationStyle citationStyle) { - this.citationStyle = citationStyle; + this.citationStyle = Objects.requireNonNull(citationStyle); citationStyleCache = CacheBuilder.newBuilder().maximumSize(CACHE_SIZE).build(new CacheLoader() { @Override public String load(BibEntry entry) { diff --git a/src/main/java/org/jabref/logic/exporter/BibTeXMLExportFormat.java b/src/main/java/org/jabref/logic/exporter/BibTeXMLExporter.java similarity index 93% rename from src/main/java/org/jabref/logic/exporter/BibTeXMLExportFormat.java rename to src/main/java/org/jabref/logic/exporter/BibTeXMLExporter.java index 1a259754d57..f526e952df6 100644 --- a/src/main/java/org/jabref/logic/exporter/BibTeXMLExportFormat.java +++ b/src/main/java/org/jabref/logic/exporter/BibTeXMLExporter.java @@ -4,6 +4,7 @@ import java.lang.reflect.Method; import java.math.BigInteger; import java.nio.charset.Charset; +import java.nio.file.Path; import java.util.Arrays; import java.util.List; import java.util.Locale; @@ -36,7 +37,7 @@ import org.jabref.logic.importer.fileformat.bibtexml.Proceedings; import org.jabref.logic.importer.fileformat.bibtexml.Techreport; import org.jabref.logic.importer.fileformat.bibtexml.Unpublished; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.BibEntry; @@ -46,21 +47,20 @@ /** * Export format for the BibTeXML format. */ -public class BibTeXMLExportFormat extends ExportFormat { +public class BibTeXMLExporter extends Exporter { private static final String BIBTEXML_NAMESPACE_URI = "http://bibtexml.sf.net/"; private static final Locale ENGLISH = Locale.ENGLISH; - private static final Log LOGGER = LogFactory.getLog(BibTeXMLExportFormat.class); + private static final Log LOGGER = LogFactory.getLog(BibTeXMLExporter.class); private JAXBContext context; - - public BibTeXMLExportFormat() { - super("BibTeXML", "bibtexml", null, null, FileExtensions.XML); + public BibTeXMLExporter() { + super("bibtexml", "BibTeXML", FileType.XML); } @Override - public void performExport(final BibDatabaseContext databaseContext, final String resultFile, final Charset encoding, - List entries) throws SaveException { + public void export(final BibDatabaseContext databaseContext, final Path resultFile, final Charset encoding, + List entries) throws SaveException { Objects.requireNonNull(databaseContext); Objects.requireNonNull(entries); if (entries.isEmpty()) { // Only export if entries exist @@ -71,7 +71,7 @@ public void performExport(final BibDatabaseContext databaseContext, final String for (BibEntry bibEntry : entries) { Entry entry = new Entry(); - bibEntry.getCiteKeyOptional().ifPresent(citeKey -> entry.setId(citeKey)); + bibEntry.getCiteKeyOptional().ifPresent(entry::setId); String type = bibEntry.getType().toLowerCase(ENGLISH); switch (type) { @@ -126,7 +126,7 @@ public void performExport(final BibDatabaseContext databaseContext, final String createMarshallerAndWriteToFile(file, resultFile); } - private void createMarshallerAndWriteToFile(File file, String resultFile) throws SaveException { + private void createMarshallerAndWriteToFile(File file, Path resultFile) throws SaveException { try { if (context == null) { context = JAXBContext.newInstance(File.class); @@ -134,7 +134,7 @@ private void createMarshallerAndWriteToFile(File file, String resultFile) throws Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); - marshaller.marshal(file, new java.io.File(resultFile)); + marshaller.marshal(file, resultFile.toFile()); } catch (JAXBException e) { throw new SaveException(e); } @@ -240,7 +240,7 @@ private void parse(T entryType, BibEntry bibEntry, Entry entry) { } private List getListOfSetMethods(T entryType) { - return Arrays.asList(entryType.getClass().getDeclaredMethods()).stream() + return Arrays.stream(entryType.getClass().getDeclaredMethods()) .filter(method -> method.getName().startsWith("set")).collect(Collectors.toList()); } diff --git a/src/main/java/org/jabref/logic/exporter/ExportFormats.java b/src/main/java/org/jabref/logic/exporter/ExportFormats.java deleted file mode 100644 index d9ec8f52e78..00000000000 --- a/src/main/java/org/jabref/logic/exporter/ExportFormats.java +++ /dev/null @@ -1,135 +0,0 @@ -package org.jabref.logic.exporter; - -import java.nio.charset.StandardCharsets; -import java.util.Collections; -import java.util.Map; -import java.util.TreeMap; - -import org.jabref.logic.l10n.Localization; -import org.jabref.logic.layout.LayoutFormatterPreferences; -import org.jabref.logic.util.FileExtensions; - -public class ExportFormats { - - // Global variable that is used for counting output entries when exporting: - public static int entryNumber; - - private static final Map EXPORT_FORMATS = new TreeMap<>(); - - private ExportFormats() { - } - - public static void initAllExports(Map customFormats, - LayoutFormatterPreferences layoutPreferences, SavePreferences savePreferences) { - - ExportFormats.EXPORT_FORMATS.clear(); - - // Initialize Build-In Export Formats - ExportFormats - .putFormat(new ExportFormat("HTML", "html", "html", null, FileExtensions.HTML, layoutPreferences, savePreferences)); - ExportFormats.putFormat(new ExportFormat(Localization.lang("Simple HTML"), "simplehtml", "simplehtml", null, - FileExtensions.HTML, layoutPreferences, savePreferences)); - ExportFormats.putFormat(new ExportFormat("DocBook 4.4", "docbook", "docbook", null, FileExtensions.XML, layoutPreferences, - savePreferences)); - ExportFormats.putFormat(new ExportFormat("DIN 1505", "din1505", "din1505winword", "din1505", FileExtensions.RTF, - layoutPreferences, savePreferences)); - ExportFormats.putFormat( - new ExportFormat("BibO RDF", "bibordf", "bibordf", null, FileExtensions.RDF, layoutPreferences, savePreferences)); - ExportFormats.putFormat(new ExportFormat(Localization.lang("HTML table"), "tablerefs", "tablerefs", "tablerefs", - FileExtensions.HTML, layoutPreferences, savePreferences)); - ExportFormats.putFormat(new ExportFormat(Localization.lang("HTML list"), "listrefs", "listrefs", "listrefs", - FileExtensions.HTML, layoutPreferences, savePreferences)); - ExportFormats.putFormat(new ExportFormat(Localization.lang("HTML table (with Abstract & BibTeX)"), - "tablerefsabsbib", "tablerefsabsbib", "tablerefsabsbib", FileExtensions.HTML, layoutPreferences, savePreferences)); - ExportFormats.putFormat(new ExportFormat("Harvard RTF", "harvard", "harvard", "harvard", FileExtensions.RDF, - layoutPreferences, savePreferences)); - ExportFormats.putFormat(new ExportFormat("ISO 690 RTF", "iso690rtf", "iso690RTF", "iso690rtf", FileExtensions.RTF, - layoutPreferences, savePreferences)); - ExportFormats.putFormat(new ExportFormat("ISO 690", "iso690txt", "iso690", "iso690txt", FileExtensions.TXT, - layoutPreferences, savePreferences)); - ExportFormats.putFormat(new ExportFormat("Endnote", "endnote", "EndNote", "endnote", FileExtensions.TXT, layoutPreferences, - savePreferences)); - ExportFormats.putFormat(new ExportFormat("OpenOffice/LibreOffice CSV", "oocsv", "openoffice-csv", "openoffice", - FileExtensions.CSV, layoutPreferences, savePreferences)); - ExportFormat ef = new ExportFormat("RIS", "ris", "ris", "ris", FileExtensions.RIS, layoutPreferences, savePreferences); - ef.setEncoding(StandardCharsets.UTF_8); - ExportFormats.putFormat(ef); - ExportFormats.putFormat( - new ExportFormat("MIS Quarterly", "misq", "misq", "misq", FileExtensions.RTF, layoutPreferences, savePreferences)); - - ExportFormats.putFormat(new BibTeXMLExportFormat()); - ExportFormats.putFormat(new OpenOfficeDocumentCreator()); - ExportFormats.putFormat(new OpenDocumentSpreadsheetCreator()); - ExportFormats.putFormat(new MSBibExportFormat()); - ExportFormats.putFormat(new ModsExportFormat()); - - // Now add custom export formats - for (IExportFormat format : customFormats.values()) { - ExportFormats.putFormat(format); - } - } - - /** - * Build a string listing of all available export formats. - * - * @param maxLineLength The max line length before a line break must be added. - * @param linePrefix If a line break is added, this prefix will be inserted at the - * beginning of the next line. - * @return The string describing available formats. - */ - public static String getConsoleExportList(int maxLineLength, int firstLineSubtr, String linePrefix) { - StringBuilder sb = new StringBuilder(); - int lastBreak = -firstLineSubtr; - - for (String name : ExportFormats.EXPORT_FORMATS.keySet()) { - if (((sb.length() + 2 + name.length()) - lastBreak) > maxLineLength) { - sb.append(",\n"); - lastBreak = sb.length(); - sb.append(linePrefix); - } else if (sb.length() > 0) { - sb.append(", "); - } - sb.append(name); - } - - return sb.toString(); - } - - /** - * Get a Map of all export formats. - * - * @return A Map containing all export formats, mapped to their console names. - */ - public static Map getExportFormats() { - // It is perhaps overly paranoid to make a defensive copy in this case: - return Collections.unmodifiableMap(ExportFormats.EXPORT_FORMATS); - } - - /** - * Look up the named export format. - * - * @param consoleName The export name given in the JabRef console help information. - * @return The ExportFormat, or null if no exportformat with that name is - * registered. - */ - public static IExportFormat getExportFormat(String consoleName) { - return ExportFormats.EXPORT_FORMATS.get(consoleName); - } - - public static FileExtensions getFileExtension(String consoleName) { - if (checkExportFormatExisit(consoleName)) { - ExportFormat exportFormat = (ExportFormat) EXPORT_FORMATS.get(consoleName); - return exportFormat.getExtension(); - } else { - return FileExtensions.DEFAULT; - } - } - - private static boolean checkExportFormatExisit(String consoleName) { - return EXPORT_FORMATS.keySet().contains(consoleName); - } - - private static void putFormat(IExportFormat format) { - ExportFormats.EXPORT_FORMATS.put(format.getConsoleName(), format); - } -} diff --git a/src/main/java/org/jabref/logic/exporter/Exporter.java b/src/main/java/org/jabref/logic/exporter/Exporter.java new file mode 100644 index 00000000000..781ca7ca353 --- /dev/null +++ b/src/main/java/org/jabref/logic/exporter/Exporter.java @@ -0,0 +1,57 @@ +package org.jabref.logic.exporter; + +import java.nio.charset.Charset; +import java.nio.file.Path; +import java.util.List; + +import org.jabref.logic.util.FileType; +import org.jabref.model.database.BibDatabaseContext; +import org.jabref.model.entry.BibEntry; + +public abstract class Exporter { + + private String id; + private String displayName; + private FileType fileType; + + public Exporter(String id, String displayName, FileType fileType) { + this.id = id; + this.displayName = displayName; + this.fileType = fileType; + } + + /** + * Returns a one-word ID (used, for example, to identify the exporter in the console). + */ + public String getId() { + return id; + } + + /** + * Returns the name of the exporter (to display to the user). + */ + public String getDisplayName() { + return displayName; + } + + /** + * Returns the type of files this exporter creates. + */ + public FileType getFileType() { + return fileType; + } + + /** + * Performs the export. + * + * @param databaseContext the database to export from + * @param file the file to write to + * @param encoding the encoding to use + * @param entries a list containing all entries that should be exported + */ + public abstract void export(BibDatabaseContext databaseContext, Path file, Charset encoding, List entries) throws Exception; + + public String getDescription() { + return getFileType().getDescription(); + } +} diff --git a/src/main/java/org/jabref/logic/exporter/ExporterFactory.java b/src/main/java/org/jabref/logic/exporter/ExporterFactory.java new file mode 100644 index 00000000000..3b7beeddabb --- /dev/null +++ b/src/main/java/org/jabref/logic/exporter/ExporterFactory.java @@ -0,0 +1,117 @@ +package org.jabref.logic.exporter; + +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; + +import org.jabref.logic.journals.JournalAbbreviationLoader; +import org.jabref.logic.l10n.Localization; +import org.jabref.logic.layout.LayoutFormatterPreferences; +import org.jabref.logic.util.FileType; +import org.jabref.preferences.JabRefPreferences; + +public class ExporterFactory { + + /** + * Global variable that is used for counting output entries when exporting: + * + * @deprecated find a better way to do this + */ + @Deprecated + public static int entryNumber; + + private final List exporters; + + private ExporterFactory(List exporters) { + this.exporters = Objects.requireNonNull(exporters); + } + + public static ExporterFactory create(Map customFormats, + LayoutFormatterPreferences layoutPreferences, SavePreferences savePreferences) { + + List exporters = new ArrayList<>(); + + // Initialize build-in exporters + exporters.add(new TemplateExporter("HTML", "html", "html", null, FileType.HTML, layoutPreferences, savePreferences)); + exporters.add(new TemplateExporter(Localization.lang("Simple HTML"), "simplehtml", "simplehtml", null, FileType.HTML, layoutPreferences, savePreferences)); + exporters.add(new TemplateExporter("DocBook 4.4", "docbook", "docbook", null, FileType.XML, layoutPreferences, savePreferences)); + exporters.add(new TemplateExporter("DIN 1505", "din1505", "din1505winword", "din1505", FileType.RTF, layoutPreferences, savePreferences)); + exporters.add(new TemplateExporter("BibO RDF", "bibordf", "bibordf", null, FileType.RDF, layoutPreferences, savePreferences)); + exporters.add(new TemplateExporter(Localization.lang("HTML table"), "tablerefs", "tablerefs", "tablerefs", FileType.HTML, layoutPreferences, savePreferences)); + exporters.add(new TemplateExporter(Localization.lang("HTML list"), "listrefs", "listrefs", "listrefs", FileType.HTML, layoutPreferences, savePreferences)); + exporters.add(new TemplateExporter(Localization.lang("HTML table (with Abstract & BibTeX)"), "tablerefsabsbib", "tablerefsabsbib", "tablerefsabsbib", FileType.HTML, layoutPreferences, savePreferences)); + exporters.add(new TemplateExporter("Harvard RTF", "harvard", "harvard", "harvard", FileType.RDF, layoutPreferences, savePreferences)); + exporters.add(new TemplateExporter("ISO 690 RTF", "iso690rtf", "iso690RTF", "iso690rtf", FileType.RTF, layoutPreferences, savePreferences)); + exporters.add(new TemplateExporter("ISO 690", "iso690txt", "iso690", "iso690txt", FileType.TXT, layoutPreferences, savePreferences)); + exporters.add(new TemplateExporter("Endnote", "endnote", "EndNote", "endnote", FileType.TXT, layoutPreferences, savePreferences)); + exporters.add(new TemplateExporter("OpenOffice/LibreOffice CSV", "oocsv", "openoffice-csv", "openoffice", FileType.CSV, layoutPreferences, savePreferences)); + exporters.add(new TemplateExporter("RIS", "ris", "ris", "ris", FileType.RIS, layoutPreferences, savePreferences).withEncoding(StandardCharsets.UTF_8)); + exporters.add(new TemplateExporter("MIS Quarterly", "misq", "misq", "misq", FileType.RTF, layoutPreferences, savePreferences)); + exporters.add(new BibTeXMLExporter()); + exporters.add(new OpenOfficeDocumentCreator()); + exporters.add(new OpenDocumentSpreadsheetCreator()); + exporters.add(new MSBibExporter()); + exporters.add(new ModsExporter()); + + // Now add custom export formats + exporters.addAll(customFormats.values()); + + return new ExporterFactory(exporters); + } + + public static ExporterFactory create(JabRefPreferences preferences, JournalAbbreviationLoader abbreviationLoader) { + Map customFormats = preferences.customExports.getCustomExportFormats(preferences, abbreviationLoader); + LayoutFormatterPreferences layoutPreferences = preferences.getLayoutFormatterPreferences(abbreviationLoader); + SavePreferences savePreferences = SavePreferences.loadForExportFromPreferences(preferences); + return create(customFormats, layoutPreferences, savePreferences); + } + + /** + * Build a string listing of all available exporters. + * + * @param maxLineLength The max line length before a line break must be added. + * @param linePrefix If a line break is added, this prefix will be inserted at the beginning of the next line. + * @return The string describing available exporters. + */ + public String getExportersAsString(int maxLineLength, int firstLineSubtraction, String linePrefix) { + StringBuilder builder = new StringBuilder(); + int lastBreak = -firstLineSubtraction; + + for (Exporter exporter : exporters) { + String name = exporter.getId(); + if (((builder.length() + 2 + name.length()) - lastBreak) > maxLineLength) { + builder.append(",\n"); + lastBreak = builder.length(); + builder.append(linePrefix); + } else if (builder.length() > 0) { + builder.append(", "); + } + builder.append(name); + } + + return builder.toString(); + } + + /** + * Get a list of all exporters. + * + * @return A list containing all exporters + */ + public List getExporters() { + return Collections.unmodifiableList(exporters); + } + + /** + * Look up the named exporter (case-insensitive). + * + * @param consoleName The export name given in the JabRef console help information. + * @return The exporter, or an empty option if no exporter with that name is registered. + */ + public Optional getExporterByName(String consoleName) { + return exporters.stream().filter(exporter -> exporter.getId().equalsIgnoreCase(consoleName)).findFirst(); + } +} diff --git a/src/main/java/org/jabref/logic/exporter/IExportFormat.java b/src/main/java/org/jabref/logic/exporter/IExportFormat.java deleted file mode 100644 index cdb700a6adf..00000000000 --- a/src/main/java/org/jabref/logic/exporter/IExportFormat.java +++ /dev/null @@ -1,41 +0,0 @@ -package org.jabref.logic.exporter; - -import java.nio.charset.Charset; -import java.util.List; - -import org.jabref.logic.util.FileExtensions; -import org.jabref.model.database.BibDatabaseContext; -import org.jabref.model.entry.BibEntry; - -public interface IExportFormat { - - /** - * Name to call this format in the console. - */ - String getConsoleName(); - - /** - * Name to display to the user (for instance in the Save file format drop - * down box. - */ - String getDisplayName(); - - FileExtensions getExtension(); - - /** - * Perform the export. - * - * @param databaseContext the database to export from. - * @param file - * The filename to write to. - * @param encoding - * The encoding to use. - * @param entries - * A list containing all entries that - * should be exported. The list of entries must be non null - * @throws Exception - */ - void performExport(BibDatabaseContext databaseContext, String file, Charset encoding, List entries) - throws Exception; - -} diff --git a/src/main/java/org/jabref/logic/exporter/MSBibExportFormat.java b/src/main/java/org/jabref/logic/exporter/MSBibExporter.java similarity index 76% rename from src/main/java/org/jabref/logic/exporter/MSBibExportFormat.java rename to src/main/java/org/jabref/logic/exporter/MSBibExporter.java index 1f1bd7394ce..42d468aa3ed 100644 --- a/src/main/java/org/jabref/logic/exporter/MSBibExportFormat.java +++ b/src/main/java/org/jabref/logic/exporter/MSBibExporter.java @@ -3,7 +3,7 @@ import java.io.IOException; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; -import java.nio.file.Paths; +import java.nio.file.Path; import java.util.List; import java.util.Objects; @@ -16,22 +16,22 @@ import javax.xml.transform.stream.StreamResult; import org.jabref.logic.msbib.MSBibDatabase; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.BibEntry; /** - * ExportFormat for exporting in MSBIB XML format. + * TemplateExporter for exporting in MSBIB XML format. */ -class MSBibExportFormat extends ExportFormat { +class MSBibExporter extends Exporter { - public MSBibExportFormat() { - super("MS Office 2007", "MSBib", null, null, FileExtensions.XML); + public MSBibExporter() { + super("MSBib", "MS Office 2007", FileType.XML); } @Override - public void performExport(final BibDatabaseContext databaseContext, final String file, - final Charset encoding, List entries) throws SaveException { + public void export(final BibDatabaseContext databaseContext, final Path file, + final Charset encoding, List entries) throws SaveException { Objects.requireNonNull(databaseContext); Objects.requireNonNull(entries); @@ -50,9 +50,9 @@ public void performExport(final BibDatabaseContext databaseContext, final String trans.setOutputProperty(OutputKeys.INDENT, "yes"); trans.transform(source, result); } catch (TransformerException | IllegalArgumentException | TransformerFactoryConfigurationError e) { - throw new Error(e); + throw new SaveException(e); } - finalizeSaveSession(session, Paths.get(file)); + session.finalize(file); } catch (IOException ex) { throw new SaveException(ex); } diff --git a/src/main/java/org/jabref/logic/exporter/ModsExportFormat.java b/src/main/java/org/jabref/logic/exporter/ModsExporter.java similarity index 97% rename from src/main/java/org/jabref/logic/exporter/ModsExportFormat.java rename to src/main/java/org/jabref/logic/exporter/ModsExporter.java index 0dbdb05a613..b9294f3549e 100644 --- a/src/main/java/org/jabref/logic/exporter/ModsExportFormat.java +++ b/src/main/java/org/jabref/logic/exporter/ModsExporter.java @@ -1,9 +1,9 @@ package org.jabref.logic.exporter; -import java.io.File; import java.io.IOException; import java.math.BigInteger; import java.nio.charset.Charset; +import java.nio.file.Path; import java.util.List; import java.util.Map; import java.util.Objects; @@ -43,16 +43,16 @@ import org.jabref.logic.importer.fileformat.mods.TitleInfoDefinition; import org.jabref.logic.importer.fileformat.mods.TypeOfResourceDefinition; import org.jabref.logic.importer.fileformat.mods.UrlDefinition; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.FieldName; /** - * ExportFormat for exporting in MODS XML format. + * TemplateExporter for exporting in MODS XML format. */ -class ModsExportFormat extends ExportFormat { +class ModsExporter extends Exporter { protected static final String MODS_NAMESPACE_URI = "http://www.loc.gov/mods/v3"; private static final String MINUS = "-"; @@ -60,14 +60,13 @@ class ModsExportFormat extends ExportFormat { private static final String MODS_SCHEMA_LOCATION = "http://www.loc.gov/standards/mods/v3/mods-3-6.xsd"; private JAXBContext context; - - public ModsExportFormat() { - super("MODS", "mods", null, null, FileExtensions.XML); + public ModsExporter() { + super("mods", "MODS", FileType.XML); } @Override - public void performExport(final BibDatabaseContext databaseContext, final String file, final Charset encoding, - List entries) throws SaveException, IOException { + public void export(final BibDatabaseContext databaseContext, final Path file, final Charset encoding, + List entries) throws SaveException, IOException { Objects.requireNonNull(databaseContext); Objects.requireNonNull(entries); if (entries.isEmpty()) { // Only export if entries exist @@ -168,7 +167,7 @@ public void performExport(final BibDatabaseContext databaseContext, final String } } - private void createMarshallerAndWriteToFile(String file, JAXBElement jaxbElement) + private void createMarshallerAndWriteToFile(Path file, JAXBElement jaxbElement) throws JAXBException { if (context == null) { @@ -180,7 +179,7 @@ private void createMarshallerAndWriteToFile(String file, JAXBElement entries) throws IOException { - Objects.requireNonNull(databaseContext); - Objects.requireNonNull(entries); - if (!entries.isEmpty()) { // Only export if entries exists - OpenDocumentSpreadsheetCreator.exportOpenDocumentSpreadsheet(new File(file), databaseContext.getDatabase(), entries); - } - } - - private static void storeOpenDocumentSpreadsheetFile(File file, InputStream source) throws IOException { + private static void storeOpenDocumentSpreadsheetFile(Path file, InputStream source) throws IOException { - try (ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(file)))) { + try (ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(Files.newOutputStream(file)))) { //addResourceFile("mimetype", "/resource/ods/mimetype", out); ZipEntry ze = new ZipEntry("mimetype"); @@ -94,7 +86,7 @@ private static void storeOpenDocumentSpreadsheetFile(File file, InputStream sour } } - private static void exportOpenDocumentSpreadsheet(File file, BibDatabase database, List entries) + private static void exportOpenDocumentSpreadsheet(Path file, BibDatabase database, List entries) throws IOException { // First store the xml formatted content to a temporary file. @@ -111,6 +103,16 @@ private static void exportOpenDocumentSpreadsheet(File file, BibDatabase databas } } + @Override + public void export(final BibDatabaseContext databaseContext, final Path file, + final Charset encoding, List entries) throws IOException { + Objects.requireNonNull(databaseContext); + Objects.requireNonNull(entries); + if (!entries.isEmpty()) { // Only export if entries exists + OpenDocumentSpreadsheetCreator.exportOpenDocumentSpreadsheet(file, databaseContext.getDatabase(), entries); + } + } + private static void exportOpenDocumentSpreadsheetXML(File tmpFile, BibDatabase database, List entries) { OpenDocumentRepresentation od = new OpenDocumentRepresentation(database, entries); diff --git a/src/main/java/org/jabref/logic/exporter/OpenOfficeDocumentCreator.java b/src/main/java/org/jabref/logic/exporter/OpenOfficeDocumentCreator.java index 704629d8935..9b174f19b9c 100644 --- a/src/main/java/org/jabref/logic/exporter/OpenOfficeDocumentCreator.java +++ b/src/main/java/org/jabref/logic/exporter/OpenOfficeDocumentCreator.java @@ -13,6 +13,8 @@ import java.net.URL; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.List; import java.util.Objects; import java.util.zip.ZipEntry; @@ -24,7 +26,7 @@ import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.model.database.BibDatabase; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.BibEntry; @@ -35,7 +37,7 @@ /** * @author alver */ -public class OpenOfficeDocumentCreator extends ExportFormat { +public class OpenOfficeDocumentCreator extends Exporter { private static final Log LOGGER = LogFactory.getLog(OpenOfficeDocumentCreator.class); @@ -44,21 +46,11 @@ public class OpenOfficeDocumentCreator extends ExportFormat { * Creates a new instance of OpenOfficeDocumentCreator */ public OpenOfficeDocumentCreator() { - super("OpenOffice/LibreOffice Calc", "oocalc", null, null, FileExtensions.SXC); + super("oocalc", "OpenOffice/LibreOffice Calc", FileType.SXC); } - @Override - public void performExport(final BibDatabaseContext databaseContext, final String file, - final Charset encoding, List entries) throws Exception { - Objects.requireNonNull(databaseContext); - Objects.requireNonNull(entries); - if (!entries.isEmpty()) { // Do not export if no entries - OpenOfficeDocumentCreator.exportOpenOfficeCalc(new File(file), databaseContext.getDatabase(), entries); - } - } - - private static void storeOpenOfficeFile(File file, InputStream source) throws Exception { - try (ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(file)))) { + private static void storeOpenOfficeFile(Path file, InputStream source) throws Exception { + try (ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(Files.newOutputStream(file)))) { ZipEntry zipEntry = new ZipEntry("content.xml"); out.putNextEntry(zipEntry); int c; @@ -77,7 +69,7 @@ private static void storeOpenOfficeFile(File file, InputStream source) throws Ex } } - private static void exportOpenOfficeCalc(File file, BibDatabase database, List entries) throws Exception { + private static void exportOpenOfficeCalc(Path file, BibDatabase database, List entries) throws Exception { // First store the xml formatted content to a temporary file. File tmpFile = File.createTempFile("oocalc", null); OpenOfficeDocumentCreator.exportOpenOfficeCalcXML(tmpFile, database, entries); @@ -93,6 +85,16 @@ private static void exportOpenOfficeCalc(File file, BibDatabase database, List entries) throws Exception { + Objects.requireNonNull(databaseContext); + Objects.requireNonNull(entries); + if (!entries.isEmpty()) { // Do not export if no entries + OpenOfficeDocumentCreator.exportOpenOfficeCalc(file, databaseContext.getDatabase(), entries); + } + } + private static void exportOpenOfficeCalcXML(File tmpFile, BibDatabase database, List entries) { OOCalcDatabase od = new OOCalcDatabase(database, entries); diff --git a/src/main/java/org/jabref/logic/exporter/SaveException.java b/src/main/java/org/jabref/logic/exporter/SaveException.java index 407b6143112..e26ceec5973 100644 --- a/src/main/java/org/jabref/logic/exporter/SaveException.java +++ b/src/main/java/org/jabref/logic/exporter/SaveException.java @@ -47,11 +47,11 @@ public SaveException(String message, String localizedMessage, BibEntry entry) { this.entry = entry; } - public SaveException(Exception base) { + public SaveException(Throwable base) { this(base.getMessage(), base.getLocalizedMessage()); } - public SaveException(Exception base, BibEntry entry) { + public SaveException(Throwable base, BibEntry entry) { this(base.getMessage(), base.getLocalizedMessage(), entry); } diff --git a/src/main/java/org/jabref/logic/exporter/SaveSession.java b/src/main/java/org/jabref/logic/exporter/SaveSession.java index 853276294f9..90b70ab68a9 100644 --- a/src/main/java/org/jabref/logic/exporter/SaveSession.java +++ b/src/main/java/org/jabref/logic/exporter/SaveSession.java @@ -1,5 +1,6 @@ package org.jabref.logic.exporter; +import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Path; import java.nio.file.Paths; @@ -9,8 +10,13 @@ import org.jabref.model.FieldChange; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + public abstract class SaveSession { + private static final Log LOGGER = LogFactory.getLog(SaveSession.class); + protected boolean backup; protected final Charset encoding; protected final VerifyingWriter writer; @@ -49,4 +55,14 @@ public List getFieldChanges() { public void addFieldChanges(List newUndoableFieldChanges) { this.undoableFieldChanges.addAll(newUndoableFieldChanges); } + + public void finalize(Path file) throws SaveException, IOException { + getWriter().flush(); + getWriter().close(); + + if (!getWriter().couldEncodeAll()) { + LOGGER.warn("Could not encode..."); + } + commit(file); + } } diff --git a/src/main/java/org/jabref/logic/exporter/ExportFormat.java b/src/main/java/org/jabref/logic/exporter/TemplateExporter.java similarity index 80% rename from src/main/java/org/jabref/logic/exporter/ExportFormat.java rename to src/main/java/org/jabref/logic/exporter/TemplateExporter.java index 3e35cefc300..68a7ecc4637 100644 --- a/src/main/java/org/jabref/logic/exporter/ExportFormat.java +++ b/src/main/java/org/jabref/logic/exporter/TemplateExporter.java @@ -9,7 +9,6 @@ import java.net.URL; import java.nio.charset.Charset; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -20,7 +19,7 @@ import org.jabref.logic.layout.Layout; import org.jabref.logic.layout.LayoutFormatterPreferences; import org.jabref.logic.layout.LayoutHelper; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.BibEntry; @@ -30,21 +29,16 @@ /** * Base class for export formats based on templates. */ -public class ExportFormat implements IExportFormat { +public class TemplateExporter extends Exporter { private static final String LAYOUT_PREFIX = "/resource/layout/"; - private static final Log LOGGER = LogFactory.getLog(ExportFormat.class); - private String displayName; - private String consoleName; - private String lfFileName; - private String directory; - private FileExtensions extension; - private Charset encoding; // If this value is set, it will be used to override - // the default encoding for the getCurrentBasePanel. - private LayoutFormatterPreferences layoutPreferences; - - private SavePreferences savePreferences; + private static final Log LOGGER = LogFactory.getLog(TemplateExporter.class); + private final String lfFileName; + private final String directory; + private final LayoutFormatterPreferences layoutPreferences; + private final SavePreferences savePreferences; + private Charset encoding; // If this value is set, it will be used to override the default encoding for the getCurrentBasePanel. private boolean customExport; /** @@ -57,12 +51,8 @@ public class ExportFormat implements IExportFormat { * @param directory Directory in which to find the layout file. * @param extension Should contain the . (for instance .txt). */ - public ExportFormat(String displayName, String consoleName, String lfFileName, String directory, FileExtensions extension) { - this.displayName = displayName; - this.consoleName = consoleName; - this.lfFileName = lfFileName; - this.directory = directory; - this.extension = extension; + public TemplateExporter(String displayName, String consoleName, String lfFileName, String directory, FileType extension) { + this(displayName, consoleName, lfFileName, directory, extension, null, null); } /** @@ -77,20 +67,15 @@ public ExportFormat(String displayName, String consoleName, String lfFileName, S * @param layoutPreferences Preferences for layout * @param savePreferences Preferences for saving */ - public ExportFormat(String displayName, String consoleName, String lfFileName, String directory, FileExtensions extension, - LayoutFormatterPreferences layoutPreferences, SavePreferences savePreferences) { - this(displayName, consoleName, lfFileName, directory, extension); + public TemplateExporter(String displayName, String consoleName, String lfFileName, String directory, FileType extension, + LayoutFormatterPreferences layoutPreferences, SavePreferences savePreferences) { + super(consoleName, displayName, extension); + this.lfFileName = Objects.requireNonNull(lfFileName); + this.directory = directory; this.layoutPreferences = layoutPreferences; this.savePreferences = savePreferences; } - /** - * Empty default constructor for subclasses - */ - protected ExportFormat() { - // intentionally empty - } - /** * Indicate whether this is a custom export. A custom export looks for its * layout files using a normal file path, while a built-in export looks in @@ -102,38 +87,15 @@ public void setCustomExport(boolean custom) { this.customExport = custom; } - /** - * @see IExportFormat#getConsoleName() - */ - @Override - public String getConsoleName() { - return consoleName; - } - - /** - * @see IExportFormat#getDisplayName() - */ - @Override - public String getDisplayName() { - return displayName; - } - /** * Set an encoding which will be used in preference to the default value * obtained from the basepanel. * * @param encoding The name of the encoding to use. */ - public void setEncoding(Charset encoding) { + public TemplateExporter withEncoding(Charset encoding) { this.encoding = encoding; - } - - /** - * @see IExportFormat#getExtension() - */ - @Override - public FileExtensions getExtension() { - return extension; + return this; } /** @@ -141,7 +103,7 @@ public FileExtensions getExtension() { * be read. *

*

- * Subclasses of ExportFormat are free to override and provide their own + * Subclasses of TemplateExporter are free to override and provide their own * implementation. * * @param filename the filename @@ -181,29 +143,28 @@ private Reader getReader(String filename) throws IOException { } @Override - public void performExport(final BibDatabaseContext databaseContext, final String file, - final Charset encoding, List entries) throws Exception { + public void export(final BibDatabaseContext databaseContext, final Path file, + final Charset encoding, List entries) throws Exception { Objects.requireNonNull(databaseContext); Objects.requireNonNull(entries); if (entries.isEmpty()) { // Do not export if no entries to export -- avoids exports with only template text return; } - Path outFile = Paths.get(file); - SaveSession ss = null; + SaveSession saveSession = null; if (this.encoding != null) { try { - ss = new FileSaveSession(this.encoding, false); + saveSession = new FileSaveSession(this.encoding, false); } catch (SaveException ex) { // Perhaps the overriding encoding doesn't work? // We will fall back on the default encoding. LOGGER.warn("Cannot get save session.", ex); } } - if (ss == null) { - ss = new FileSaveSession(encoding, false); + if (saveSession == null) { + saveSession = new FileSaveSession(encoding, false); } - try (VerifyingWriter ps = ss.getWriter()) { + try (VerifyingWriter ps = saveSession.getWriter()) { Layout beginLayout = null; @@ -252,9 +213,9 @@ public void performExport(final BibDatabaseContext databaseContext, final String Map layouts = new HashMap<>(); Layout layout; - ExportFormats.entryNumber = 0; + ExporterFactory.entryNumber = 0; for (BibEntry entry : sorted) { - ExportFormats.entryNumber++; // Increment entry counter. + ExporterFactory.entryNumber++; // Increment entry counter. // Get the layout String type = entry.getType(); if (layouts.containsKey(type)) { @@ -309,7 +270,7 @@ public void performExport(final BibDatabaseContext databaseContext, final String sb.append(String.join(", ", missingFormatters)); LOGGER.warn(sb); } - finalizeSaveSession(ss, outFile); + saveSession.finalize(file); } } @@ -351,14 +312,4 @@ private void readFormatterFile() { } } } - - public void finalizeSaveSession(final SaveSession ss, Path file) throws SaveException, IOException { - ss.getWriter().flush(); - ss.getWriter().close(); - - if (!ss.getWriter().couldEncodeAll()) { - LOGGER.warn("Could not encode..."); - } - ss.commit(file); - } } diff --git a/src/main/java/org/jabref/logic/importer/Importer.java b/src/main/java/org/jabref/logic/importer/Importer.java index d48b2a10f63..597a821ba2d 100644 --- a/src/main/java/org/jabref/logic/importer/Importer.java +++ b/src/main/java/org/jabref/logic/importer/Importer.java @@ -11,7 +11,7 @@ import java.nio.file.Path; import java.util.Objects; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; /** * Role of an importer for JabRef. @@ -133,14 +133,14 @@ public static BufferedReader getReader(Path filePath, Charset encoding) public abstract String getName(); /** - * Returns the file extensions that this importer can read - * @return {@link FileExtensions} correspoding to the importer + * Returns the type of files that this importer can read + * @return {@link FileType} corresponding to the importer */ - public abstract FileExtensions getExtensions(); + public abstract FileType getFileType(); /** - * Returns a one-word ID which identifies this import format. - * Used for example, to identify the format when used from the command line. + * Returns a one-word ID which identifies this importer. + * Used for example, to identify the importer when used from the command line. * * @return ID, must be unique and not null */ diff --git a/src/main/java/org/jabref/logic/importer/fileformat/BibTeXMLImporter.java b/src/main/java/org/jabref/logic/importer/fileformat/BibTeXMLImporter.java index 21299906087..1520638f9c8 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/BibTeXMLImporter.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/BibTeXMLImporter.java @@ -25,7 +25,7 @@ import org.jabref.logic.importer.fileformat.bibtexml.File; import org.jabref.logic.importer.fileformat.bibtexml.Inbook; import org.jabref.logic.importer.fileformat.bibtexml.Incollection; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.FieldName; @@ -53,8 +53,8 @@ public String getName() { } @Override - public FileExtensions getExtensions() { - return FileExtensions.BIBTEXML; + public FileType getFileType() { + return FileType.BIBTEXML; } @Override diff --git a/src/main/java/org/jabref/logic/importer/fileformat/BiblioscapeImporter.java b/src/main/java/org/jabref/logic/importer/fileformat/BiblioscapeImporter.java index e01a4446368..d235033a3d3 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/BiblioscapeImporter.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/BiblioscapeImporter.java @@ -11,7 +11,7 @@ import org.jabref.logic.importer.Importer; import org.jabref.logic.importer.ParserResult; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.FieldName; @@ -29,8 +29,8 @@ public String getName() { } @Override - public FileExtensions getExtensions() { - return FileExtensions.BILBIOSCAPE; + public FileType getFileType() { + return FileType.BILBIOSCAPE; } @Override diff --git a/src/main/java/org/jabref/logic/importer/fileformat/BibtexImporter.java b/src/main/java/org/jabref/logic/importer/fileformat/BibtexImporter.java index ec51bf4685f..8380e1ef1b9 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/BibtexImporter.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/BibtexImporter.java @@ -11,7 +11,7 @@ import org.jabref.logic.importer.ImportFormatPreferences; import org.jabref.logic.importer.Importer; import org.jabref.logic.importer.ParserResult; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; /** * This importer exists only to enable `--importToOpen someEntry.bib` @@ -76,8 +76,8 @@ public String getName() { } @Override - public FileExtensions getExtensions() { - return FileExtensions.BIBTEX_DB; + public FileType getFileType() { + return FileType.BIBTEX_DB; } @Override diff --git a/src/main/java/org/jabref/logic/importer/fileformat/CopacImporter.java b/src/main/java/org/jabref/logic/importer/fileformat/CopacImporter.java index d0e4d8dbe71..c5702291794 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/CopacImporter.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/CopacImporter.java @@ -9,7 +9,7 @@ import org.jabref.logic.importer.Importer; import org.jabref.logic.importer.ParserResult; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.FieldName; @@ -30,8 +30,8 @@ public String getName() { } @Override - public FileExtensions getExtensions() { - return FileExtensions.COPAC; + public FileType getFileType() { + return FileType.COPAC; } @Override diff --git a/src/main/java/org/jabref/logic/importer/fileformat/CustomImporter.java b/src/main/java/org/jabref/logic/importer/fileformat/CustomImporter.java index 671b7605b17..4b8ae55c65e 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/CustomImporter.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/CustomImporter.java @@ -12,7 +12,7 @@ import org.jabref.logic.importer.Importer; import org.jabref.logic.importer.ParserResult; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; /** * Object with data for a custom importer. @@ -87,8 +87,8 @@ public String getName() { } @Override - public FileExtensions getExtensions() { - return importer.getExtensions(); + public FileType getFileType() { + return importer.getFileType(); } @Override diff --git a/src/main/java/org/jabref/logic/importer/fileformat/EndnoteImporter.java b/src/main/java/org/jabref/logic/importer/fileformat/EndnoteImporter.java index 640ce5fe616..0e1efca0989 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/EndnoteImporter.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/EndnoteImporter.java @@ -12,7 +12,7 @@ import org.jabref.logic.importer.ImportFormatPreferences; import org.jabref.logic.importer.Importer; import org.jabref.logic.importer.ParserResult; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.model.entry.AuthorList; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.FieldName; @@ -44,8 +44,8 @@ public String getName() { } @Override - public FileExtensions getExtensions() { - return FileExtensions.ENDNOTE; + public FileType getFileType() { + return FileType.ENDNOTE; } @Override diff --git a/src/main/java/org/jabref/logic/importer/fileformat/FreeCiteImporter.java b/src/main/java/org/jabref/logic/importer/fileformat/FreeCiteImporter.java index 371db1f9be8..c11301f2896 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/FreeCiteImporter.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/FreeCiteImporter.java @@ -25,7 +25,7 @@ import org.jabref.logic.importer.Importer; import org.jabref.logic.importer.ParserResult; import org.jabref.logic.l10n.Localization; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.logic.util.OS; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.BibtexEntryTypes; @@ -228,8 +228,8 @@ public String getName() { } @Override - public FileExtensions getExtensions() { - return FileExtensions.FREECITE; + public FileType getFileType() { + return FileType.FREECITE; } @Override diff --git a/src/main/java/org/jabref/logic/importer/fileformat/InspecImporter.java b/src/main/java/org/jabref/logic/importer/fileformat/InspecImporter.java index 5781094cfd1..f0cc5bcd68f 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/InspecImporter.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/InspecImporter.java @@ -10,7 +10,7 @@ import org.jabref.logic.importer.Importer; import org.jabref.logic.importer.ParserResult; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.model.entry.AuthorList; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.FieldName; @@ -28,8 +28,8 @@ public String getName() { } @Override - public FileExtensions getExtensions() { - return FileExtensions.INSPEC; + public FileType getFileType() { + return FileType.INSPEC; } @Override diff --git a/src/main/java/org/jabref/logic/importer/fileformat/IsiImporter.java b/src/main/java/org/jabref/logic/importer/fileformat/IsiImporter.java index a9dc90f5179..e25196a4476 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/IsiImporter.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/IsiImporter.java @@ -15,7 +15,7 @@ import org.jabref.logic.formatter.casechanger.TitleCaseFormatter; import org.jabref.logic.importer.Importer; import org.jabref.logic.importer.ParserResult; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.FieldName; import org.jabref.model.entry.Month; @@ -51,8 +51,8 @@ public String getName() { } @Override - public FileExtensions getExtensions() { - return FileExtensions.ISI; + public FileType getFileType() { + return FileType.ISI; } @Override diff --git a/src/main/java/org/jabref/logic/importer/fileformat/MedlineImporter.java b/src/main/java/org/jabref/logic/importer/fileformat/MedlineImporter.java index 43d0262b0ef..7cf1ac272a7 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/MedlineImporter.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/MedlineImporter.java @@ -72,7 +72,7 @@ import org.jabref.logic.importer.fileformat.medline.Section; import org.jabref.logic.importer.fileformat.medline.Sections; import org.jabref.logic.importer.fileformat.medline.Text; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.FieldName; import org.jabref.model.strings.StringUtil; @@ -104,8 +104,8 @@ public String getName() { } @Override - public FileExtensions getExtensions() { - return FileExtensions.MEDLINE; + public FileType getFileType() { + return FileType.MEDLINE; } @Override diff --git a/src/main/java/org/jabref/logic/importer/fileformat/MedlinePlainImporter.java b/src/main/java/org/jabref/logic/importer/fileformat/MedlinePlainImporter.java index 5cdf1601f9a..a91340e45f0 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/MedlinePlainImporter.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/MedlinePlainImporter.java @@ -12,7 +12,7 @@ import org.jabref.logic.importer.Importer; import org.jabref.logic.importer.ParserResult; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.logic.util.OS; import org.jabref.model.entry.AuthorList; import org.jabref.model.entry.BibEntry; @@ -40,8 +40,8 @@ public String getName() { } @Override - public FileExtensions getExtensions() { - return FileExtensions.MEDLINE_PLAIN; + public FileType getFileType() { + return FileType.MEDLINE_PLAIN; } @Override diff --git a/src/main/java/org/jabref/logic/importer/fileformat/ModsImporter.java b/src/main/java/org/jabref/logic/importer/fileformat/ModsImporter.java index 325683a2326..8136aa7a2b8 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/ModsImporter.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/ModsImporter.java @@ -53,7 +53,7 @@ import org.jabref.logic.importer.fileformat.mods.SubjectDefinition; import org.jabref.logic.importer.fileformat.mods.TitleInfoDefinition; import org.jabref.logic.importer.fileformat.mods.UrlDefinition; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.FieldName; import org.jabref.preferences.JabRefPreferences; @@ -473,8 +473,8 @@ public String getName() { } @Override - public FileExtensions getExtensions() { - return FileExtensions.MODS; + public FileType getFileType() { + return FileType.MODS; } @Override diff --git a/src/main/java/org/jabref/logic/importer/fileformat/MrDLibImporter.java b/src/main/java/org/jabref/logic/importer/fileformat/MrDLibImporter.java index d6a5adffd4c..5bef0e35e3e 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/MrDLibImporter.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/MrDLibImporter.java @@ -18,7 +18,7 @@ import org.jabref.logic.importer.Importer; import org.jabref.logic.importer.ParserResult; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.model.database.BibDatabase; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.FieldName; @@ -73,8 +73,8 @@ public String getName() { } @Override - public FileExtensions getExtensions() { - return FileExtensions.XML; + public FileType getFileType() { + return FileType.XML; } @Override @@ -255,7 +255,4 @@ public void characters(char ch[], int start, int length) throws SAXException { } } - - ; - } diff --git a/src/main/java/org/jabref/logic/importer/fileformat/MsBibImporter.java b/src/main/java/org/jabref/logic/importer/fileformat/MsBibImporter.java index 2b920fe77dd..76d685022e3 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/MsBibImporter.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/MsBibImporter.java @@ -10,7 +10,7 @@ import org.jabref.logic.importer.Importer; import org.jabref.logic.importer.ParserResult; import org.jabref.logic.msbib.MSBibDatabase; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.w3c.dom.Document; import org.xml.sax.ErrorHandler; @@ -76,8 +76,8 @@ public String getName() { } @Override - public FileExtensions getExtensions() { - return FileExtensions.MSBIB; + public FileType getFileType() { + return FileType.MSBIB; } @Override diff --git a/src/main/java/org/jabref/logic/importer/fileformat/OvidImporter.java b/src/main/java/org/jabref/logic/importer/fileformat/OvidImporter.java index 3aea796e7bd..bad4a928aca 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/OvidImporter.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/OvidImporter.java @@ -11,7 +11,7 @@ import org.jabref.logic.importer.Importer; import org.jabref.logic.importer.ParserResult; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.model.entry.AuthorList; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.FieldName; @@ -47,8 +47,8 @@ public String getName() { } @Override - public FileExtensions getExtensions() { - return FileExtensions.OVID; + public FileType getFileType() { + return FileType.OVID; } @Override diff --git a/src/main/java/org/jabref/logic/importer/fileformat/PdfContentImporter.java b/src/main/java/org/jabref/logic/importer/fileformat/PdfContentImporter.java index c971161cef2..3725c2e8f84 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/PdfContentImporter.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/PdfContentImporter.java @@ -19,7 +19,7 @@ import org.jabref.logic.importer.ParserResult; import org.jabref.logic.importer.fetcher.DoiFetcher; import org.jabref.logic.l10n.Localization; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.logic.xmp.EncryptedPdfsNotSupportedException; import org.jabref.logic.xmp.XMPUtil; import org.jabref.model.entry.BibEntry; @@ -595,8 +595,8 @@ public String getName() { } @Override - public FileExtensions getExtensions() { - return FileExtensions.PDF_CONTENT; + public FileType getFileType() { + return FileType.PDF_CONTENT; } @Override diff --git a/src/main/java/org/jabref/logic/importer/fileformat/PdfXmpImporter.java b/src/main/java/org/jabref/logic/importer/fileformat/PdfXmpImporter.java index 1487480dcb9..526766e3685 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/PdfXmpImporter.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/PdfXmpImporter.java @@ -9,7 +9,7 @@ import org.jabref.logic.importer.Importer; import org.jabref.logic.importer.ParserResult; import org.jabref.logic.l10n.Localization; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.logic.xmp.XMPPreferences; import org.jabref.logic.xmp.XMPUtil; @@ -31,8 +31,8 @@ public String getName() { } @Override - public FileExtensions getExtensions() { - return FileExtensions.XMP; + public FileType getFileType() { + return FileType.XMP; } @Override diff --git a/src/main/java/org/jabref/logic/importer/fileformat/RepecNepImporter.java b/src/main/java/org/jabref/logic/importer/fileformat/RepecNepImporter.java index 4f61ce2ef83..f4b0961c66b 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/RepecNepImporter.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/RepecNepImporter.java @@ -11,7 +11,7 @@ import org.jabref.logic.importer.ImportFormatPreferences; import org.jabref.logic.importer.Importer; import org.jabref.logic.importer.ParserResult; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.Date; import org.jabref.model.entry.FieldName; @@ -166,8 +166,8 @@ public String getId() { } @Override - public FileExtensions getExtensions() { - return FileExtensions.REPEC; + public FileType getFileType() { + return FileType.REPEC; } @Override diff --git a/src/main/java/org/jabref/logic/importer/fileformat/RisImporter.java b/src/main/java/org/jabref/logic/importer/fileformat/RisImporter.java index 49af1941ba7..4b3c6ca428c 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/RisImporter.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/RisImporter.java @@ -12,7 +12,7 @@ import org.jabref.logic.importer.Importer; import org.jabref.logic.importer.ParserResult; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.logic.util.OS; import org.jabref.model.entry.AuthorList; import org.jabref.model.entry.BibEntry; @@ -35,8 +35,8 @@ public String getName() { } @Override - public FileExtensions getExtensions() { - return FileExtensions.RIS; + public FileType getFileType() { + return FileType.RIS; } @Override diff --git a/src/main/java/org/jabref/logic/importer/fileformat/SilverPlatterImporter.java b/src/main/java/org/jabref/logic/importer/fileformat/SilverPlatterImporter.java index 600d56ef32f..a14ab0c3138 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/SilverPlatterImporter.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/SilverPlatterImporter.java @@ -11,7 +11,7 @@ import org.jabref.logic.importer.Importer; import org.jabref.logic.importer.ParserResult; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.model.entry.AuthorList; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.FieldName; @@ -30,8 +30,8 @@ public String getName() { } @Override - public FileExtensions getExtensions() { - return FileExtensions.SILVER_PLATTER; + public FileType getFileType() { + return FileType.SILVER_PLATTER; } @Override diff --git a/src/main/java/org/jabref/logic/layout/format/Number.java b/src/main/java/org/jabref/logic/layout/format/Number.java index e6a6143fe48..e2daa7297f7 100644 --- a/src/main/java/org/jabref/logic/layout/format/Number.java +++ b/src/main/java/org/jabref/logic/layout/format/Number.java @@ -1,6 +1,6 @@ package org.jabref.logic.layout.format; -import org.jabref.logic.exporter.ExportFormats; +import org.jabref.logic.exporter.ExporterFactory; import org.jabref.logic.layout.ParamLayoutFormatter; /** @@ -16,6 +16,6 @@ public void setArgument(String arg) { @Override public String format(String fieldText) { - return String.valueOf(ExportFormats.entryNumber); + return String.valueOf(ExporterFactory.entryNumber); } } diff --git a/src/main/java/org/jabref/logic/util/FileExtensions.java b/src/main/java/org/jabref/logic/util/FileType.java similarity index 79% rename from src/main/java/org/jabref/logic/util/FileExtensions.java rename to src/main/java/org/jabref/logic/util/FileType.java index 45de519f1b3..983ae509004 100644 --- a/src/main/java/org/jabref/logic/util/FileExtensions.java +++ b/src/main/java/org/jabref/logic/util/FileType.java @@ -3,16 +3,16 @@ import java.util.Arrays; import java.util.List; import java.util.StringJoiner; +import java.util.stream.Collectors; import org.jabref.logic.l10n.Localization; /** - * This enum contains all kind of file extensions for open and save dialogs. + * This enum contains a list of file types used in open and save dialogs. * - * Important: Enter the extension without a dot! The dot is added implicitly. + * @implNote Enter the extensions without a dot! The dot is added implicitly. */ -public enum FileExtensions { - //important: No dot before the extension! +public enum FileType { BIBTEX_DB(String.format("%1s %2s", "BibTex", Localization.lang("Library")), "bib"), BIBTEXML(Localization.lang("%0 file", "BibTeXML"), "bibx", "xml"), BILBIOSCAPE(Localization.lang("%0 file", "Biblioscape"), "txt"), @@ -51,32 +51,31 @@ public enum FileExtensions { CSV(Localization.lang("%0 file", "CSV"), "csv"), DEFAULT(Localization.lang("%0 file", "DEFAULT"), "default"); - private final String[] extension; + private final String[] extensions; private final String description; - private FileExtensions(String description, String... extension) { - this.extension = extension; + FileType(String description, String... extensions) { this.description = description; + this.extensions = extensions; } - //Array because a) is varags and b) gets passed as varags parameter to FileExtensionNameFilter - public String[] getExtensions() { - return extension; - } - - public List getExtensionsAsList() { - return Arrays.asList(extension); + public List getExtensions() { + return Arrays.asList(extensions); } public String getDescription() { StringJoiner sj = new StringJoiner(", ", description + " (", ")"); - for (String ext : extension) { + for (String ext : extensions) { sj.add("*." + ext); } return sj.toString(); } public String getFirstExtensionWithDot() { - return "." + extension[0].trim(); + return "." + extensions[0].trim(); + } + + public List getExtensionsWithDot() { + return getExtensions().stream().map(extension -> "." + extension).collect(Collectors.toList()); } } diff --git a/src/main/java/org/jabref/preferences/CustomExportList.java b/src/main/java/org/jabref/preferences/CustomExportList.java index 85dfb89fe36..b6d90e916aa 100644 --- a/src/main/java/org/jabref/preferences/CustomExportList.java +++ b/src/main/java/org/jabref/preferences/CustomExportList.java @@ -7,11 +7,13 @@ import java.util.Optional; import java.util.TreeMap; -import org.jabref.logic.exporter.ExportFormat; -import org.jabref.logic.exporter.ExportFormats; +import org.jabref.Globals; +import org.jabref.logic.exporter.Exporter; import org.jabref.logic.exporter.SavePreferences; +import org.jabref.logic.exporter.TemplateExporter; import org.jabref.logic.journals.JournalAbbreviationLoader; import org.jabref.logic.layout.LayoutFormatterPreferences; +import org.jabref.logic.util.FileType; import ca.odell.glazedlists.BasicEventList; import ca.odell.glazedlists.EventList; @@ -33,7 +35,7 @@ public class CustomExportList { private final EventList> list; private final SortedList> sorted; - private final Map formats = new TreeMap<>(); + private final Map formats = new TreeMap<>(); public CustomExportList(Comparator> comp) { @@ -41,13 +43,9 @@ public CustomExportList(Comparator> comp) { sorted = new SortedList<>(list, comp); } - public Map getCustomExportFormats(JabRefPreferences prefs, - JournalAbbreviationLoader loader) { - Objects.requireNonNull(prefs); - Objects.requireNonNull(loader); - formats.clear(); - readPrefs(prefs, loader); - return formats; + private static FileType getFileExtension(String consoleName) { + Optional exporter = Globals.exportFactory.getExporterByName(consoleName); + return exporter.map(Exporter::getFileType).orElse(FileType.DEFAULT); } public int size() { @@ -58,6 +56,15 @@ public EventList> getSortedList() { return sorted; } + public Map getCustomExportFormats(JabRefPreferences prefs, + JournalAbbreviationLoader loader) { + Objects.requireNonNull(prefs); + Objects.requireNonNull(loader); + formats.clear(); + readPrefs(prefs, loader); + return formats; + } + private void readPrefs(JabRefPreferences prefs, JournalAbbreviationLoader loader) { Objects.requireNonNull(prefs); Objects.requireNonNull(loader); @@ -68,9 +75,9 @@ private void readPrefs(JabRefPreferences prefs, JournalAbbreviationLoader loader LayoutFormatterPreferences layoutPreferences = prefs.getLayoutFormatterPreferences(loader); SavePreferences savePreferences = SavePreferences.loadForExportFromPreferences(prefs); while (!((s = prefs.getStringList(JabRefPreferences.CUSTOM_EXPORT_FORMAT + i)).isEmpty())) { - Optional format = createFormat(s, layoutPreferences, savePreferences); + Optional format = createFormat(s, layoutPreferences, savePreferences); if (format.isPresent()) { - formats.put(format.get().getConsoleName(), format.get()); + formats.put(format.get().getId(), format.get()); list.add(s); } else { String customExportFormat = prefs.get(JabRefPreferences.CUSTOM_EXPORT_FORMAT + i); @@ -80,8 +87,8 @@ private void readPrefs(JabRefPreferences prefs, JournalAbbreviationLoader loader } } - private Optional createFormat(List s, LayoutFormatterPreferences layoutPreferences, - SavePreferences savePreferences) { + private Optional createFormat(List s, LayoutFormatterPreferences layoutPreferences, + SavePreferences savePreferences) { if (s.size() < 3) { return Optional.empty(); } @@ -91,7 +98,7 @@ private Optional createFormat(List s, LayoutFormatterPrefe } else { lfFileName = s.get(1); } - ExportFormat format = new ExportFormat(s.get(0), s.get(0), lfFileName, null, ExportFormats.getFileExtension(s.get(2)), layoutPreferences, + TemplateExporter format = new TemplateExporter(s.get(0), s.get(0), lfFileName, null, getFileExtension(s.get(2)), layoutPreferences, savePreferences); format.setCustomExport(true); return Optional.of(format); @@ -99,19 +106,11 @@ private Optional createFormat(List s, LayoutFormatterPrefe public void addFormat(List s, LayoutFormatterPreferences layoutPreferences, SavePreferences savePreferences) { createFormat(s, layoutPreferences, savePreferences).ifPresent(format -> { - formats.put(format.getConsoleName(), format); + formats.put(format.getId(), format); list.add(s); }); } - public void remove(List toRemove, LayoutFormatterPreferences layoutPreferences, - SavePreferences savePreferences) { - createFormat(toRemove, layoutPreferences, savePreferences).ifPresent(format -> { - formats.remove(format.getConsoleName()); - list.remove(toRemove); - }); - } - public void store(JabRefPreferences prefs) { if (list.isEmpty()) { @@ -132,4 +131,11 @@ private void purge(int from, JabRefPreferences prefs) { } } + public void remove(List toRemove, LayoutFormatterPreferences layoutPreferences, + SavePreferences savePreferences) { + createFormat(toRemove, layoutPreferences, savePreferences).ifPresent(format -> { + formats.remove(format.getId()); + list.remove(toRemove); + }); + } } diff --git a/src/test/java/org/jabref/gui/util/FileDialogConfigurationTest.java b/src/test/java/org/jabref/gui/util/FileDialogConfigurationTest.java index df4173bff24..cbdee007777 100644 --- a/src/test/java/org/jabref/gui/util/FileDialogConfigurationTest.java +++ b/src/test/java/org/jabref/gui/util/FileDialogConfigurationTest.java @@ -10,7 +10,7 @@ import javafx.stage.FileChooser; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.junit.Rule; import org.junit.Test; @@ -72,16 +72,16 @@ public void testWithNonExistingDirectoryAndParentNull() { @Test public void testSingleExtension() { FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder() - .withDefaultExtension(FileExtensions.BIBTEX_DB).build(); + .withDefaultExtension(FileType.BIBTEX_DB).build(); - FileChooser.ExtensionFilter filter = toFilter(FileExtensions.BIBTEX_DB); + FileChooser.ExtensionFilter filter = toFilter(FileType.BIBTEX_DB); assertEquals(filter.getExtensions(), fileDialogConfiguration.getDefaultExtension().getExtensions()); } @Test public void testMultipleExtension() { - EnumSet extensions = EnumSet.allOf(FileExtensions.class); + EnumSet extensions = EnumSet.allOf(FileType.class); FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder() .addExtensionFilters(extensions).build(); @@ -94,9 +94,9 @@ public void testMultipleExtension() { } - private FileChooser.ExtensionFilter toFilter(FileExtensions extension) { + private FileChooser.ExtensionFilter toFilter(FileType extension) { return new FileChooser.ExtensionFilter(extension.getDescription(), - extension.getExtensionsAsList().stream().map(ending -> "*." + ending).collect(Collectors.toList())); + extension.getExtensions().stream().map(ending -> "*." + ending).collect(Collectors.toList())); } } diff --git a/src/test/java/org/jabref/logic/exporter/BibTeXMLExporterTestFiles.java b/src/test/java/org/jabref/logic/exporter/BibTeXMLExporterTestFiles.java index 1c0fc05a526..14a4e457803 100644 --- a/src/test/java/org/jabref/logic/exporter/BibTeXMLExporterTestFiles.java +++ b/src/test/java/org/jabref/logic/exporter/BibTeXMLExporterTestFiles.java @@ -42,7 +42,7 @@ public class BibTeXMLExporterTestFiles { public BibDatabaseContext databaseContext; public Charset charset; public File tempFile; - public BibTeXMLExportFormat bibtexmlExportFormat; + public BibTeXMLExporter bibtexmlExportFormat; public BibtexImporter testImporter; @Parameter @@ -65,7 +65,7 @@ public void setUp() throws Exception { resourceDir = Paths.get(BibTeXMLExporterTestFiles.class.getResource("").toURI()); databaseContext = new BibDatabaseContext(); charset = StandardCharsets.UTF_8; - bibtexmlExportFormat = new BibTeXMLExportFormat(); + bibtexmlExportFormat = new BibTeXMLExporter(); tempFile = testFolder.newFile(); testImporter = new BibtexImporter(mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS)); } @@ -79,7 +79,7 @@ public final void testPerformExport() throws IOException, SaveException { List entries = testImporter.importDatabase(importFile, StandardCharsets.UTF_8).getDatabase() .getEntries(); - bibtexmlExportFormat.performExport(databaseContext, tempFile.getPath(), charset, entries); + bibtexmlExportFormat.export(databaseContext, tempFile.toPath(), charset, entries); Builder control = Input.from(Files.newInputStream(resourceDir.resolve(xmlFileName))); Builder test = Input.from(Files.newInputStream(Paths.get(tempFilename))); diff --git a/src/test/java/org/jabref/logic/exporter/CsvExportFormatTest.java b/src/test/java/org/jabref/logic/exporter/CsvExportFormatTest.java index b47819a932e..c620578c826 100644 --- a/src/test/java/org/jabref/logic/exporter/CsvExportFormatTest.java +++ b/src/test/java/org/jabref/logic/exporter/CsvExportFormatTest.java @@ -24,7 +24,7 @@ import static org.mockito.Mockito.mock; public class CsvExportFormatTest { - private IExportFormat exportFormat; + private Exporter exportFormat; public BibDatabaseContext databaseContext; public Charset charset; @@ -33,12 +33,12 @@ public class CsvExportFormatTest { @Before public void setUp() { - Map customFormats = new HashMap<>(); + Map customFormats = new HashMap<>(); LayoutFormatterPreferences layoutPreferences = mock(LayoutFormatterPreferences.class, Answers.RETURNS_DEEP_STUBS); SavePreferences savePreferences = mock(SavePreferences.class); - ExportFormats.initAllExports(customFormats, layoutPreferences, savePreferences); + ExporterFactory exporterFactory = ExporterFactory.create(customFormats, layoutPreferences, savePreferences); - exportFormat = ExportFormats.getExportFormat("oocsv"); + exportFormat = exporterFactory.getExporterByName("oocsv").get(); databaseContext = new BibDatabaseContext(); charset = StandardCharsets.UTF_8; @@ -52,12 +52,11 @@ public void tearDown() { @Test public void testPerformExportForSingleAuthor() throws Exception { File tmpFile = testFolder.newFile(); - String filename = tmpFile.getCanonicalPath(); BibEntry entry = new BibEntry(); entry.setField("author", "Someone, Van Something"); List entries = Arrays.asList(entry); - exportFormat.performExport(databaseContext, filename, charset, entries); + exportFormat.export(databaseContext, tmpFile.toPath(), charset, entries); List lines = Files.readAllLines(tmpFile.toPath()); assertEquals(2, lines.size()); @@ -69,12 +68,11 @@ public void testPerformExportForSingleAuthor() throws Exception { @Test public void testPerformExportForMultipleAuthors() throws Exception { File tmpFile = testFolder.newFile(); - String filename = tmpFile.getCanonicalPath(); BibEntry entry = new BibEntry(); entry.setField("author", "von Neumann, John and Smith, John and Black Brown, Peter"); List entries = Arrays.asList(entry); - exportFormat.performExport(databaseContext, filename, charset, entries); + exportFormat.export(databaseContext, tmpFile.toPath(), charset, entries); List lines = Files.readAllLines(tmpFile.toPath()); assertEquals(2, lines.size()); @@ -86,12 +84,11 @@ public void testPerformExportForMultipleAuthors() throws Exception { @Test public void testPerformExportForSingleEditor() throws Exception { File tmpFile = testFolder.newFile(); - String filename = tmpFile.getCanonicalPath(); BibEntry entry = new BibEntry(); entry.setField("editor", "Someone, Van Something"); List entries = Arrays.asList(entry); - exportFormat.performExport(databaseContext, filename, charset, entries); + exportFormat.export(databaseContext, tmpFile.toPath(), charset, entries); List lines = Files.readAllLines(tmpFile.toPath()); assertEquals(2, lines.size()); @@ -103,12 +100,11 @@ public void testPerformExportForSingleEditor() throws Exception { @Test public void testPerformExportForMultipleEditors() throws Exception { File tmpFile = testFolder.newFile(); - String filename = tmpFile.getCanonicalPath(); BibEntry entry = new BibEntry(); entry.setField("editor", "von Neumann, John and Smith, John and Black Brown, Peter"); List entries = Arrays.asList(entry); - exportFormat.performExport(databaseContext, filename, charset, entries); + exportFormat.export(databaseContext, tmpFile.toPath(), charset, entries); List lines = Files.readAllLines(tmpFile.toPath()); assertEquals(2, lines.size()); diff --git a/src/test/java/org/jabref/logic/exporter/ExportFormatTest.java b/src/test/java/org/jabref/logic/exporter/ExporterTest.java similarity index 66% rename from src/test/java/org/jabref/logic/exporter/ExportFormatTest.java rename to src/test/java/org/jabref/logic/exporter/ExporterTest.java index e32709b089a..66dfa4ccc00 100644 --- a/src/test/java/org/jabref/logic/exporter/ExportFormatTest.java +++ b/src/test/java/org/jabref/logic/exporter/ExporterTest.java @@ -1,9 +1,9 @@ package org.jabref.logic.exporter; -import java.io.File; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -26,15 +26,15 @@ import static org.mockito.Mockito.mock; @RunWith(Parameterized.class) -public class ExportFormatTest { +public class ExporterTest { - public IExportFormat exportFormat; + public Exporter exportFormat; public String exportFormatName; public BibDatabaseContext databaseContext; public Charset charset; public List entries; - public ExportFormatTest(IExportFormat format, String name) { + public ExporterTest(Exporter format, String name) { exportFormat = format; exportFormatName = name; } @@ -50,40 +50,37 @@ public void setUp() { entries = Collections.emptyList(); } + @Parameterized.Parameters(name = "{index}: {1}") + public static Collection exportFormats() { + Collection result = new ArrayList<>(); + + Map customFormats = new HashMap<>(); + LayoutFormatterPreferences layoutPreferences = mock(LayoutFormatterPreferences.class); + SavePreferences savePreferences = mock(SavePreferences.class); + ExporterFactory exporterFactory = ExporterFactory.create(customFormats, layoutPreferences, savePreferences); + + for (Exporter format : exporterFactory.getExporters()) { + result.add(new Object[]{format, format.getDisplayName()}); + } + return result; + } + @Test public void testExportingEmptyDatabaseYieldsEmptyFile() throws Exception { - File tmpFile = testFolder.newFile(); - String filename = tmpFile.getCanonicalPath(); - exportFormat.performExport(databaseContext, filename, charset, entries); - assertEquals(Collections.emptyList(), Files.readAllLines(tmpFile.toPath())); + Path tmpFile = testFolder.newFile().toPath(); + exportFormat.export(databaseContext, tmpFile, charset, entries); + assertEquals(Collections.emptyList(), Files.readAllLines(tmpFile)); } @Test(expected = NullPointerException.class) public void testExportingNullDatabaseThrowsNPE() throws Exception { - File tmpFile = testFolder.newFile(); - String filename = tmpFile.getCanonicalPath(); - exportFormat.performExport(null, filename, charset, entries); + Path tmpFile = testFolder.newFile().toPath(); + exportFormat.export(null, tmpFile, charset, entries); } @Test(expected = NullPointerException.class) public void testExportingNullEntriesThrowsNPE() throws Exception { - File tmpFile = testFolder.newFile(); - String filename = tmpFile.getCanonicalPath(); - exportFormat.performExport(databaseContext, filename, charset, null); - } - - @Parameterized.Parameters(name = "{index}: {1}") - public static Collection exportFormats() { - Collection result = new ArrayList<>(); - - Map customFormats = new HashMap<>(); - LayoutFormatterPreferences layoutPreferences = mock(LayoutFormatterPreferences.class); - SavePreferences savePreferences = mock(SavePreferences.class); - ExportFormats.initAllExports(customFormats, layoutPreferences, savePreferences); - - for (IExportFormat format : ExportFormats.getExportFormats().values()) { - result.add(new Object[] {format, format.getDisplayName()}); - } - return result; + Path tmpFile = testFolder.newFile().toPath(); + exportFormat.export(databaseContext, tmpFile, charset, null); } } diff --git a/src/test/java/org/jabref/logic/exporter/HtmlExportFormatTest.java b/src/test/java/org/jabref/logic/exporter/HtmlExportFormatTest.java index ddb38e608b2..b3661c61a88 100644 --- a/src/test/java/org/jabref/logic/exporter/HtmlExportFormatTest.java +++ b/src/test/java/org/jabref/logic/exporter/HtmlExportFormatTest.java @@ -24,7 +24,7 @@ import static org.mockito.Mockito.mock; public class HtmlExportFormatTest { - private IExportFormat exportFormat; + private Exporter exportFormat; public BibDatabaseContext databaseContext; public Charset charset; public List entries; @@ -34,12 +34,12 @@ public class HtmlExportFormatTest { @Before public void setUp() { - Map customFormats = new HashMap<>(); + Map customFormats = new HashMap<>(); LayoutFormatterPreferences layoutPreferences = mock(LayoutFormatterPreferences.class, Answers.RETURNS_DEEP_STUBS); SavePreferences savePreferences = mock(SavePreferences.class); - ExportFormats.initAllExports(customFormats, layoutPreferences, savePreferences); + ExporterFactory exporterFactory = ExporterFactory.create(customFormats, layoutPreferences, savePreferences); - exportFormat = ExportFormats.getExportFormat("html"); + exportFormat = exporterFactory.getExporterByName("html").get(); databaseContext = new BibDatabaseContext(); charset = StandardCharsets.UTF_8; @@ -58,8 +58,7 @@ public void tearDown() { @Test public void emitWellFormedHtml() throws Exception { File tmpFile = testFolder.newFile(); - String filename = tmpFile.getCanonicalPath(); - exportFormat.performExport(databaseContext, filename, charset, entries); + exportFormat.export(databaseContext, tmpFile.toPath(), charset, entries); List lines = Files.readAllLines(tmpFile.toPath()); assertEquals("", lines.get(lines.size() - 1)); } diff --git a/src/test/java/org/jabref/logic/exporter/MSBibExportFormatTestFiles.java b/src/test/java/org/jabref/logic/exporter/MSBibExportFormatTestFiles.java index b49ba27c409..ad60d24830e 100644 --- a/src/test/java/org/jabref/logic/exporter/MSBibExportFormatTestFiles.java +++ b/src/test/java/org/jabref/logic/exporter/MSBibExportFormatTestFiles.java @@ -1,6 +1,5 @@ package org.jabref.logic.exporter; -import java.io.File; import java.io.IOException; import java.net.URISyntaxException; import java.nio.charset.Charset; @@ -41,8 +40,8 @@ public class MSBibExportFormatTestFiles { public BibDatabaseContext databaseContext; public Charset charset; - public File tempFile; - public MSBibExportFormat msBibExportFormat; + public Path tempFile; + public MSBibExporter msBibExportFormat; public BibtexImporter testImporter; @Parameter @@ -66,8 +65,8 @@ public void setUp() throws Exception { resourceDir = Paths.get(MSBibExportFormatTestFiles.class.getResource("").toURI()); databaseContext = new BibDatabaseContext(); charset = StandardCharsets.UTF_8; - msBibExportFormat = new MSBibExportFormat(); - tempFile = testFolder.newFile(); + msBibExportFormat = new MSBibExporter(); + tempFile = testFolder.newFile().toPath(); testImporter = new BibtexImporter(mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS)); } @@ -75,15 +74,14 @@ public void setUp() throws Exception { public final void testPerformExport() throws IOException, SaveException { String xmlFileName = filename.replace(".bib", ".xml"); Path importFile = resourceDir.resolve(filename); - String tempFilename = tempFile.getCanonicalPath(); List entries = testImporter.importDatabase(importFile, StandardCharsets.UTF_8).getDatabase() .getEntries(); - msBibExportFormat.performExport(databaseContext, tempFile.getPath(), charset, entries); + msBibExportFormat.export(databaseContext, tempFile, charset, entries); Builder control = Input.from(Files.newInputStream(resourceDir.resolve(xmlFileName))); - Builder test = Input.from(Files.newInputStream(Paths.get(tempFilename))); + Builder test = Input.from(Files.newInputStream(tempFile)); assertThat(test, CompareMatcher.isSimilarTo(control) .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText)).throwComparisonFailure()); diff --git a/src/test/java/org/jabref/logic/exporter/ModsExportFormatTest.java b/src/test/java/org/jabref/logic/exporter/ModsExportFormatTest.java index 112e7354915..04ae0280930 100644 --- a/src/test/java/org/jabref/logic/exporter/ModsExportFormatTest.java +++ b/src/test/java/org/jabref/logic/exporter/ModsExportFormatTest.java @@ -1,18 +1,15 @@ package org.jabref.logic.exporter; -import java.io.File; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Collections; -import java.util.List; import org.jabref.logic.importer.ImportFormatPreferences; import org.jabref.logic.importer.fileformat.BibtexImporter; import org.jabref.model.database.BibDatabaseContext; -import org.jabref.model.entry.BibEntry; import org.junit.Assert; import org.junit.Before; @@ -27,37 +24,28 @@ public class ModsExportFormatTest { public Charset charset; - private ModsExportFormat modsExportFormat; + private ModsExporter modsExportFormat; private BibDatabaseContext databaseContext; private BibtexImporter bibtexImporter; - private File tempFile; + private Path tempFile; @Rule public TemporaryFolder testFolder = new TemporaryFolder(); private Path importFile; - @Before public void setUp() throws Exception { databaseContext = new BibDatabaseContext(); charset = StandardCharsets.UTF_8; - modsExportFormat = new ModsExportFormat(); + modsExportFormat = new ModsExporter(); bibtexImporter = new BibtexImporter(mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS)); - tempFile = testFolder.newFile(); + tempFile = testFolder.newFile().toPath(); importFile = Paths.get(ModsExportFormatTest.class.getResource("ModsExportFormatTestAllFields.bib").toURI()); } - @Test(expected = SaveException.class) - public final void testPerformExportTrowsSaveException() throws Exception { - List entries = bibtexImporter.importDatabase(importFile, charset).getDatabase().getEntries(); - - modsExportFormat.performExport(databaseContext, "", charset, entries); - } - @Test - public final void testPerformExportEmptyEntry() throws Exception { - String canonicalPath = tempFile.getCanonicalPath(); - modsExportFormat.performExport(databaseContext, canonicalPath, charset, Collections.emptyList()); - Assert.assertEquals(Collections.emptyList(), Files.readAllLines(Paths.get(canonicalPath))); + public final void exportForNoEntriesWritesNothing() throws Exception { + modsExportFormat.export(databaseContext, tempFile, charset, Collections.emptyList()); + Assert.assertEquals(Collections.emptyList(), Files.readAllLines(tempFile)); } } diff --git a/src/test/java/org/jabref/logic/exporter/ModsExportFormatTestFiles.java b/src/test/java/org/jabref/logic/exporter/ModsExportFormatTestFiles.java index bd774557ea9..01e2e87d23b 100644 --- a/src/test/java/org/jabref/logic/exporter/ModsExportFormatTestFiles.java +++ b/src/test/java/org/jabref/logic/exporter/ModsExportFormatTestFiles.java @@ -42,7 +42,7 @@ public class ModsExportFormatTestFiles { public Charset charset; private BibDatabaseContext databaseContext; private File tempFile; - private ModsExportFormat modsExportFormat; + private ModsExporter modsExportFormat; private BibtexImporter bibtexImporter; private ModsImporter modsImporter; private Path importFile; @@ -69,7 +69,7 @@ public void setUp() throws Exception { databaseContext = new BibDatabaseContext(); importFile = Paths.get(ModsExportFormatTestFiles.class.getResource(filename).toURI()); charset = StandardCharsets.UTF_8; - modsExportFormat = new ModsExportFormat(); + modsExportFormat = new ModsExporter(); tempFile = testFolder.newFile(); bibtexImporter = new BibtexImporter(mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS)); modsImporter = new ModsImporter(); @@ -82,7 +82,7 @@ public final void testPerformExport() throws Exception { List entries = bibtexImporter.importDatabase(importFile, charset).getDatabase().getEntries(); Path xmlFile = Paths.get(ModsExportFormatTestFiles.class.getResource(xmlFileName).toURI()); - modsExportFormat.performExport(databaseContext, tempFile.getPath(), charset, entries); + modsExportFormat.export(databaseContext, tempFile.toPath(), charset, entries); Builder control = Input.from(Files.newInputStream(xmlFile)); Builder test = Input.from(Files.newInputStream(Paths.get(tempFilename))); @@ -95,7 +95,7 @@ public final void testPerformExport() throws Exception { public final void testExportAsModsAndThenImportAsMods() throws Exception { List entries = bibtexImporter.importDatabase(importFile, charset).getDatabase().getEntries(); - modsExportFormat.performExport(databaseContext, tempFile.getPath(), charset, entries); + modsExportFormat.export(databaseContext, tempFile.toPath(), charset, entries); BibEntryAssert.assertEquals(entries, Paths.get(tempFile.getPath()), modsImporter); } @@ -107,7 +107,7 @@ public final void testImportAsModsAndExportAsMods() throws Exception { List entries = modsImporter.importDatabase(xmlFile, charset).getDatabase().getEntries(); - modsExportFormat.performExport(databaseContext, tempFile.getPath(), charset, entries); + modsExportFormat.export(databaseContext, tempFile.toPath(), charset, entries); Builder control = Input.from(Files.newInputStream(xmlFile)); Builder test = Input.from(Files.newInputStream(Paths.get(tempFilename))); diff --git a/src/test/java/org/jabref/logic/exporter/MsBibExportFormatTest.java b/src/test/java/org/jabref/logic/exporter/MsBibExportFormatTest.java index 7ee86a0f44d..ff291531f94 100644 --- a/src/test/java/org/jabref/logic/exporter/MsBibExportFormatTest.java +++ b/src/test/java/org/jabref/logic/exporter/MsBibExportFormatTest.java @@ -1,10 +1,10 @@ package org.jabref.logic.exporter; -import java.io.File; import java.io.IOException; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.Path; import java.util.Collections; import java.util.List; @@ -22,8 +22,8 @@ public class MsBibExportFormatTest { public BibDatabaseContext databaseContext; public Charset charset; - public File tempFile; - public MSBibExportFormat msBibExportFormat; + public Path tempFile; + public MSBibExporter msBibExportFormat; @Rule public TemporaryFolder testFolder = new TemporaryFolder(); @@ -33,15 +33,14 @@ public class MsBibExportFormatTest { public void setUp() throws Exception { databaseContext = new BibDatabaseContext(); charset = StandardCharsets.UTF_8; - msBibExportFormat = new MSBibExportFormat(); - tempFile = testFolder.newFile(); + msBibExportFormat = new MSBibExporter(); + tempFile = testFolder.newFile().toPath(); } @Test public final void testPerformExportWithNoEntry() throws IOException, SaveException { List entries = Collections.emptyList(); - String tempFileName = tempFile.getCanonicalPath(); - msBibExportFormat.performExport(databaseContext, tempFileName, charset, entries); - assertEquals(Collections.emptyList(), Files.readAllLines(tempFile.toPath())); + msBibExportFormat.export(databaseContext, tempFile, charset, entries); + assertEquals(Collections.emptyList(), Files.readAllLines(tempFile)); } } diff --git a/src/test/java/org/jabref/logic/importer/ImporterTest.java b/src/test/java/org/jabref/logic/importer/ImporterTest.java index 9fd770bf11a..1af6beb396d 100644 --- a/src/test/java/org/jabref/logic/importer/ImporterTest.java +++ b/src/test/java/org/jabref/logic/importer/ImporterTest.java @@ -68,8 +68,8 @@ public void getFormatterNameDoesNotReturnNull() { } @Test - public void getExtensionsDoesNotReturnNull() { - Assert.assertNotNull(format.getExtensions()); + public void getFileTypeDoesNotReturnNull() { + Assert.assertNotNull(format.getFileType()); } @Test diff --git a/src/test/java/org/jabref/logic/importer/fileformat/BibTeXMLImporterTest.java b/src/test/java/org/jabref/logic/importer/fileformat/BibTeXMLImporterTest.java index 99069661bba..6fd0be0f4ae 100644 --- a/src/test/java/org/jabref/logic/importer/fileformat/BibTeXMLImporterTest.java +++ b/src/test/java/org/jabref/logic/importer/fileformat/BibTeXMLImporterTest.java @@ -9,7 +9,7 @@ import java.util.stream.Collectors; import java.util.stream.Stream; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.junit.Before; import org.junit.Test; @@ -55,7 +55,7 @@ public void testGetCLIId() { @Test public void testsGetExtensions() { - assertEquals(FileExtensions.BIBTEXML, importer.getExtensions()); + assertEquals(FileType.BIBTEXML, importer.getFileType()); } @Test diff --git a/src/test/java/org/jabref/logic/importer/fileformat/BiblioscapeImporterTest.java b/src/test/java/org/jabref/logic/importer/fileformat/BiblioscapeImporterTest.java index 0948e0c6c89..158a164e7f2 100644 --- a/src/test/java/org/jabref/logic/importer/fileformat/BiblioscapeImporterTest.java +++ b/src/test/java/org/jabref/logic/importer/fileformat/BiblioscapeImporterTest.java @@ -5,7 +5,7 @@ import java.nio.file.Paths; import java.util.Collections; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.junit.Assert; import org.junit.Before; @@ -28,7 +28,7 @@ public void testGetFormatName() { @Test public void testsGetExtensions() { - Assert.assertEquals(FileExtensions.BILBIOSCAPE, importer.getExtensions()); + Assert.assertEquals(FileType.BILBIOSCAPE, importer.getFileType()); } @Test diff --git a/src/test/java/org/jabref/logic/importer/fileformat/BibtexImporterTest.java b/src/test/java/org/jabref/logic/importer/fileformat/BibtexImporterTest.java index 58df1830dcc..5e7079df57a 100644 --- a/src/test/java/org/jabref/logic/importer/fileformat/BibtexImporterTest.java +++ b/src/test/java/org/jabref/logic/importer/fileformat/BibtexImporterTest.java @@ -9,7 +9,7 @@ import java.util.Optional; import org.jabref.logic.importer.ImportFormatPreferences; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.model.entry.BibEntry; import org.junit.Before; @@ -112,7 +112,7 @@ public void testGetFormatName() { @Test public void testsGetExtensions() { - assertEquals(FileExtensions.BIBTEX_DB, importer.getExtensions()); + assertEquals(FileType.BIBTEX_DB, importer.getFileType()); } @Test diff --git a/src/test/java/org/jabref/logic/importer/fileformat/CopacImporterTest.java b/src/test/java/org/jabref/logic/importer/fileformat/CopacImporterTest.java index 01194dc5375..d4c8abbb27a 100644 --- a/src/test/java/org/jabref/logic/importer/fileformat/CopacImporterTest.java +++ b/src/test/java/org/jabref/logic/importer/fileformat/CopacImporterTest.java @@ -10,7 +10,7 @@ import java.util.stream.Collectors; import java.util.stream.Stream; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.model.entry.BibEntry; import org.junit.Assert; @@ -43,7 +43,7 @@ public void setUp() throws Exception { @Test public void testsGetExtensions() { - Assert.assertEquals(FileExtensions.COPAC, importer.getExtensions()); + Assert.assertEquals(FileType.COPAC, importer.getFileType()); } diff --git a/src/test/java/org/jabref/logic/importer/fileformat/EndnoteImporterTest.java b/src/test/java/org/jabref/logic/importer/fileformat/EndnoteImporterTest.java index 233c5d9a6b5..02062096e7a 100644 --- a/src/test/java/org/jabref/logic/importer/fileformat/EndnoteImporterTest.java +++ b/src/test/java/org/jabref/logic/importer/fileformat/EndnoteImporterTest.java @@ -13,7 +13,7 @@ import java.util.Optional; import org.jabref.logic.importer.ImportFormatPreferences; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.model.entry.BibEntry; import org.junit.Before; @@ -48,7 +48,7 @@ public void testGetCLIId() { @Test public void testsGetExtensions() { - assertEquals(FileExtensions.ENDNOTE, importer.getExtensions()); + assertEquals(FileType.ENDNOTE, importer.getFileType()); } diff --git a/src/test/java/org/jabref/logic/importer/fileformat/FreeCiteImporterTest.java b/src/test/java/org/jabref/logic/importer/fileformat/FreeCiteImporterTest.java index e777df08cef..42b386f37dc 100644 --- a/src/test/java/org/jabref/logic/importer/fileformat/FreeCiteImporterTest.java +++ b/src/test/java/org/jabref/logic/importer/fileformat/FreeCiteImporterTest.java @@ -7,7 +7,7 @@ import java.util.Optional; import org.jabref.logic.importer.ImportFormatPreferences; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.model.entry.BibEntry; import org.jabref.testutils.category.FetcherTest; @@ -46,7 +46,7 @@ public void testGetFormatName() { @Test public void testsGetExtensions() { - assertEquals(FileExtensions.FREECITE, importer.getExtensions()); + assertEquals(FileType.FREECITE, importer.getFileType()); } @Test diff --git a/src/test/java/org/jabref/logic/importer/fileformat/InspecImportTest.java b/src/test/java/org/jabref/logic/importer/fileformat/InspecImportTest.java index 86fa7f4ccc8..81ce7fefb0e 100644 --- a/src/test/java/org/jabref/logic/importer/fileformat/InspecImportTest.java +++ b/src/test/java/org/jabref/logic/importer/fileformat/InspecImportTest.java @@ -12,7 +12,7 @@ import java.util.List; import org.jabref.logic.bibtex.BibEntryAssert; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.model.entry.BibEntry; import org.junit.Before; @@ -115,7 +115,7 @@ public void testGetCLIId() { @Test public void testsGetExtensions() { - assertEquals(FileExtensions.INSPEC, importer.getExtensions()); + assertEquals(FileType.INSPEC, importer.getFileType()); } @Test diff --git a/src/test/java/org/jabref/logic/importer/fileformat/IsiImporterTest.java b/src/test/java/org/jabref/logic/importer/fileformat/IsiImporterTest.java index 69dc92b07d0..3dc1a3cdffe 100644 --- a/src/test/java/org/jabref/logic/importer/fileformat/IsiImporterTest.java +++ b/src/test/java/org/jabref/logic/importer/fileformat/IsiImporterTest.java @@ -10,7 +10,7 @@ import java.util.List; import java.util.Optional; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.model.entry.BibEntry; import org.junit.Rule; @@ -49,7 +49,7 @@ public void testGetCLIId() { @Test public void testsGetExtensions() { - assertEquals(FileExtensions.ISI, importer.getExtensions()); + assertEquals(FileType.ISI, importer.getFileType()); } @Test diff --git a/src/test/java/org/jabref/logic/importer/fileformat/MedlineImporterTest.java b/src/test/java/org/jabref/logic/importer/fileformat/MedlineImporterTest.java index dd1cb85de78..de134cc1284 100644 --- a/src/test/java/org/jabref/logic/importer/fileformat/MedlineImporterTest.java +++ b/src/test/java/org/jabref/logic/importer/fileformat/MedlineImporterTest.java @@ -9,7 +9,7 @@ import java.util.stream.Collectors; import java.util.stream.Stream; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.junit.Assert; import org.junit.Before; @@ -63,7 +63,7 @@ public void testGetCLIId() { @Test public void testsGetExtensions() { - assertEquals(FileExtensions.MEDLINE, importer.getExtensions()); + assertEquals(FileType.MEDLINE, importer.getFileType()); } @Test diff --git a/src/test/java/org/jabref/logic/importer/fileformat/MedlinePlainImporterTest.java b/src/test/java/org/jabref/logic/importer/fileformat/MedlinePlainImporterTest.java index 7e8216cd6a8..5cee8de526b 100644 --- a/src/test/java/org/jabref/logic/importer/fileformat/MedlinePlainImporterTest.java +++ b/src/test/java/org/jabref/logic/importer/fileformat/MedlinePlainImporterTest.java @@ -14,7 +14,7 @@ import java.util.Optional; import org.jabref.logic.bibtex.BibEntryAssert; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.model.entry.BibEntry; import org.apache.commons.codec.Charsets; @@ -41,7 +41,7 @@ public void setUp() { @Test public void testsGetExtensions() { - assertEquals(FileExtensions.MEDLINE_PLAIN, importer.getExtensions()); + assertEquals(FileType.MEDLINE_PLAIN, importer.getFileType()); } @Test diff --git a/src/test/java/org/jabref/logic/importer/fileformat/MrDLibImporterTest.java b/src/test/java/org/jabref/logic/importer/fileformat/MrDLibImporterTest.java index 4144d441a10..d6292d38205 100644 --- a/src/test/java/org/jabref/logic/importer/fileformat/MrDLibImporterTest.java +++ b/src/test/java/org/jabref/logic/importer/fileformat/MrDLibImporterTest.java @@ -6,7 +6,7 @@ import java.util.List; import org.jabref.logic.importer.ParserResult; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.FieldName; @@ -44,7 +44,7 @@ public void testGetName() { @Test public void testGetFileExtention() { - assertEquals(FileExtensions.XML, importer.getExtensions()); + assertEquals(FileType.XML, importer.getFileType()); } @Test diff --git a/src/test/java/org/jabref/logic/importer/fileformat/MsBibImporterTest.java b/src/test/java/org/jabref/logic/importer/fileformat/MsBibImporterTest.java index 5c4ae8bf885..e7d1541488e 100644 --- a/src/test/java/org/jabref/logic/importer/fileformat/MsBibImporterTest.java +++ b/src/test/java/org/jabref/logic/importer/fileformat/MsBibImporterTest.java @@ -9,7 +9,7 @@ import java.util.Collections; import java.util.List; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.model.entry.BibEntry; import org.junit.Test; @@ -22,7 +22,7 @@ public class MsBibImporterTest { @Test public void testsGetExtensions() { MsBibImporter importer = new MsBibImporter(); - assertEquals(FileExtensions.MSBIB, importer.getExtensions()); + assertEquals(FileType.MSBIB, importer.getFileType()); } @Test diff --git a/src/test/java/org/jabref/logic/importer/fileformat/OvidImporterTest.java b/src/test/java/org/jabref/logic/importer/fileformat/OvidImporterTest.java index da149a8e7e6..0db0f6cfabc 100644 --- a/src/test/java/org/jabref/logic/importer/fileformat/OvidImporterTest.java +++ b/src/test/java/org/jabref/logic/importer/fileformat/OvidImporterTest.java @@ -12,7 +12,7 @@ import java.util.Optional; import org.jabref.logic.bibtex.BibEntryAssert; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.model.entry.BibEntry; import org.junit.Assert; @@ -41,7 +41,7 @@ public void testGetCLIId() { @Test public void testsGetExtensions() { - Assert.assertEquals(FileExtensions.OVID, importer.getExtensions()); + Assert.assertEquals(FileType.OVID, importer.getFileType()); } @Test diff --git a/src/test/java/org/jabref/logic/importer/fileformat/PdfContentImporterTest.java b/src/test/java/org/jabref/logic/importer/fileformat/PdfContentImporterTest.java index 792ea6c75bb..fd323b75d19 100644 --- a/src/test/java/org/jabref/logic/importer/fileformat/PdfContentImporterTest.java +++ b/src/test/java/org/jabref/logic/importer/fileformat/PdfContentImporterTest.java @@ -8,7 +8,7 @@ import java.util.List; import org.jabref.logic.importer.ImportFormatPreferences; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.model.entry.BibEntry; import org.junit.Before; @@ -29,7 +29,7 @@ public void setUp() { @Test public void testsGetExtensions() { - assertEquals(FileExtensions.PDF_CONTENT, importer.getExtensions()); + assertEquals(FileType.PDF_CONTENT, importer.getFileType()); } @Test diff --git a/src/test/java/org/jabref/logic/importer/fileformat/PdfXmpImporterTest.java b/src/test/java/org/jabref/logic/importer/fileformat/PdfXmpImporterTest.java index b4cf96accd2..40dab408fd5 100644 --- a/src/test/java/org/jabref/logic/importer/fileformat/PdfXmpImporterTest.java +++ b/src/test/java/org/jabref/logic/importer/fileformat/PdfXmpImporterTest.java @@ -10,7 +10,7 @@ import java.util.Optional; import org.jabref.logic.importer.ParserResult; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.logic.xmp.XMPPreferences; import org.jabref.model.entry.BibEntry; @@ -40,7 +40,7 @@ public void testGetFormatName() { @Test public void testsGetExtensions() { - assertEquals(FileExtensions.XMP, importer.getExtensions()); + assertEquals(FileType.XMP, importer.getFileType()); } @Test diff --git a/src/test/java/org/jabref/logic/importer/fileformat/RISImporterTest.java b/src/test/java/org/jabref/logic/importer/fileformat/RISImporterTest.java index 6760f058c11..7e04be3313f 100644 --- a/src/test/java/org/jabref/logic/importer/fileformat/RISImporterTest.java +++ b/src/test/java/org/jabref/logic/importer/fileformat/RISImporterTest.java @@ -6,7 +6,7 @@ import java.nio.file.Path; import java.nio.file.Paths; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.junit.Assert; import org.junit.Before; @@ -34,7 +34,7 @@ public void testGetCLIId() { @Test public void testsGetExtensions() { - Assert.assertEquals(FileExtensions.RIS, importer.getExtensions()); + Assert.assertEquals(FileType.RIS, importer.getFileType()); } @Test diff --git a/src/test/java/org/jabref/logic/importer/fileformat/RepecNepImporterTest.java b/src/test/java/org/jabref/logic/importer/fileformat/RepecNepImporterTest.java index 03084a88464..a8503cded65 100644 --- a/src/test/java/org/jabref/logic/importer/fileformat/RepecNepImporterTest.java +++ b/src/test/java/org/jabref/logic/importer/fileformat/RepecNepImporterTest.java @@ -11,7 +11,7 @@ import org.jabref.logic.bibtex.BibEntryAssert; import org.jabref.logic.importer.ImportFormatPreferences; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.model.entry.BibEntry; import org.junit.Assert; @@ -98,7 +98,7 @@ public final void testGetCliId() { @Test public void testGetExtension() { - Assert.assertEquals(FileExtensions.REPEC, testImporter.getExtensions()); + Assert.assertEquals(FileType.REPEC, testImporter.getFileType()); } @Test diff --git a/src/test/java/org/jabref/logic/importer/fileformat/SilverPlatterImporterTest.java b/src/test/java/org/jabref/logic/importer/fileformat/SilverPlatterImporterTest.java index 20bf41ec370..2376d020a3d 100644 --- a/src/test/java/org/jabref/logic/importer/fileformat/SilverPlatterImporterTest.java +++ b/src/test/java/org/jabref/logic/importer/fileformat/SilverPlatterImporterTest.java @@ -9,7 +9,7 @@ import java.util.List; import org.jabref.logic.bibtex.BibEntryAssert; -import org.jabref.logic.util.FileExtensions; +import org.jabref.logic.util.FileType; import org.jabref.model.entry.BibEntry; import org.junit.Assert; @@ -43,7 +43,7 @@ public void setUp() throws Exception { @Test public void testsGetExtensions() { - assertEquals(FileExtensions.SILVER_PLATTER, testImporter.getExtensions()); + assertEquals(FileType.SILVER_PLATTER, testImporter.getFileType()); } @Test diff --git a/src/test/java/org/jabref/logic/util/FileExtensionsTest.java b/src/test/java/org/jabref/logic/util/FileExtensionsTest.java deleted file mode 100644 index 4ebb5313816..00000000000 --- a/src/test/java/org/jabref/logic/util/FileExtensionsTest.java +++ /dev/null @@ -1,36 +0,0 @@ -package org.jabref.logic.util; - -import java.util.Arrays; -import java.util.List; - -import org.junit.Assert; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class FileExtensionsTest { - - @Test - public void testSingleFileExtensionDescription() { - String singleDescription = "INSPEC file (*.txt)"; - assertEquals(singleDescription, FileExtensions.INSPEC.getDescription()); - } - - @Test - public void testMultiFileExtensionsDescription() { - String multiDescription = "MedlinePlain file (*.nbib, *.txt)"; - assertEquals(multiDescription, FileExtensions.MEDLINE_PLAIN.getDescription()); - } - - @Test - public void testExtensions() { - List extensions = Arrays.asList("nbib", "txt"); - Assert.assertArrayEquals(extensions.toArray(), FileExtensions.MEDLINE_PLAIN.getExtensions()); - } - - @Test - public void testFirstExtensionWithDot() { - assertEquals(".nbib", FileExtensions.MEDLINE_PLAIN.getFirstExtensionWithDot()); - } - -} diff --git a/src/test/java/org/jabref/logic/util/FileTypeTest.java b/src/test/java/org/jabref/logic/util/FileTypeTest.java new file mode 100644 index 00000000000..61c15a36e64 --- /dev/null +++ b/src/test/java/org/jabref/logic/util/FileTypeTest.java @@ -0,0 +1,26 @@ +package org.jabref.logic.util; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class FileTypeTest { + + @Test + public void testSingleFileExtensionDescription() { + String singleDescription = "INSPEC file (*.txt)"; + assertEquals(singleDescription, FileType.INSPEC.getDescription()); + } + + @Test + public void testMultiFileExtensionsDescription() { + String multiDescription = "MedlinePlain file (*.nbib, *.txt)"; + assertEquals(multiDescription, FileType.MEDLINE_PLAIN.getDescription()); + } + + @Test + public void testFirstExtensionWithDot() { + assertEquals(".nbib", FileType.MEDLINE_PLAIN.getFirstExtensionWithDot()); + } + +}