Skip to content

Commit d381839

Browse files
authored
Refactor export code to fix #3576 (#3578)
* Refactor exports to fix #3576 * Add changelog * Fix build * Implement feedback and fix tests * Include feedback * Fix checkstyle...again...joy!
1 parent c438554 commit d381839

File tree

98 files changed

+808
-898
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+808
-898
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
1515

1616

1717
### Fixed
18+
- We fixed the missing dot in the name of an exported file. [#3576](https://github.com/JabRef/jabref/issues/3576)
1819

1920
### Removed
2021
- We removed the Look and Feels from jgoodies, because these are not compatible with Java 9

src/main/java/org/jabref/Globals.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.jabref.gui.util.DefaultTaskExecutor;
1111
import org.jabref.gui.util.FileUpdateMonitor;
1212
import org.jabref.gui.util.TaskExecutor;
13+
import org.jabref.logic.exporter.ExporterFactory;
1314
import org.jabref.logic.importer.ImportFormatReader;
1415
import org.jabref.logic.journals.JournalAbbreviationLoader;
1516
import org.jabref.logic.protectedterms.ProtectedTermsLoader;
@@ -48,6 +49,7 @@ public class Globals {
4849
* Manager for the state of the GUI.
4950
*/
5051
public static StateManager stateManager = new StateManager();
52+
public static ExporterFactory exportFactory;
5153
// Key binding preferences
5254
private static KeyBindingRepository keyBindingRepository;
5355
// Background tasks

src/main/java/org/jabref/JabRefMain.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.jabref;
22

33
import java.net.Authenticator;
4-
import java.util.Map;
54

65
import javax.swing.JFrame;
76
import javax.swing.JOptionPane;
@@ -13,13 +12,10 @@
1312

1413
import org.jabref.cli.ArgumentProcessor;
1514
import org.jabref.gui.remote.JabRefMessageHandler;
16-
import org.jabref.logic.exporter.ExportFormat;
17-
import org.jabref.logic.exporter.ExportFormats;
18-
import org.jabref.logic.exporter.SavePreferences;
15+
import org.jabref.logic.exporter.ExporterFactory;
1916
import org.jabref.logic.formatter.casechanger.ProtectTermsFormatter;
2017
import org.jabref.logic.journals.JournalAbbreviationLoader;
2118
import org.jabref.logic.l10n.Localization;
22-
import org.jabref.logic.layout.LayoutFormatterPreferences;
2319
import org.jabref.logic.net.ProxyAuthenticator;
2420
import org.jabref.logic.net.ProxyPreferences;
2521
import org.jabref.logic.net.ProxyRegisterer;
@@ -147,12 +143,7 @@ private static void start(String[] args) {
147143
Globals.prefs.getXMPPreferences());
148144
EntryTypes.loadCustomEntryTypes(preferences.loadCustomEntryTypes(BibDatabaseMode.BIBTEX),
149145
preferences.loadCustomEntryTypes(BibDatabaseMode.BIBLATEX));
150-
Map<String, ExportFormat> customFormats = Globals.prefs.customExports.getCustomExportFormats(Globals.prefs,
151-
Globals.journalAbbreviationLoader);
152-
LayoutFormatterPreferences layoutPreferences = Globals.prefs
153-
.getLayoutFormatterPreferences(Globals.journalAbbreviationLoader);
154-
SavePreferences savePreferences = SavePreferences.loadForExportFromPreferences(Globals.prefs);
155-
ExportFormats.initAllExports(customFormats, layoutPreferences, savePreferences);
146+
Globals.exportFactory = ExporterFactory.create(Globals.prefs, Globals.journalAbbreviationLoader);
156147

157148
// Initialize protected terms loader
158149
Globals.protectedTermsLoader = new ProtectedTermsLoader(Globals.prefs.getProtectedTermsPreferences());

src/main/java/org/jabref/cli/ArgumentProcessor.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
import org.jabref.logic.bibtexkeypattern.BibtexKeyGenerator;
2222
import org.jabref.logic.exporter.BibDatabaseWriter;
2323
import org.jabref.logic.exporter.BibtexDatabaseWriter;
24-
import org.jabref.logic.exporter.ExportFormat;
25-
import org.jabref.logic.exporter.ExportFormats;
24+
import org.jabref.logic.exporter.Exporter;
25+
import org.jabref.logic.exporter.ExporterFactory;
2626
import org.jabref.logic.exporter.FileSaveSession;
27-
import org.jabref.logic.exporter.IExportFormat;
2827
import org.jabref.logic.exporter.SaveException;
2928
import org.jabref.logic.exporter.SavePreferences;
3029
import org.jabref.logic.exporter.SaveSession;
30+
import org.jabref.logic.exporter.TemplateExporter;
3131
import org.jabref.logic.importer.ImportException;
3232
import org.jabref.logic.importer.ImportFormatReader;
3333
import org.jabref.logic.importer.OpenDatabase;
@@ -249,7 +249,7 @@ private boolean exportMatches(List<ParserResult> loaded) {
249249
formatName = data[2];
250250
break;
251251
case 2:
252-
//default ExportFormat: HTML table (with Abstract & BibTeX)
252+
//default exporter: HTML table (with Abstract & BibTeX)
253253
formatName = "tablerefsabsbib";
254254
break;
255255
default:
@@ -260,14 +260,14 @@ private boolean exportMatches(List<ParserResult> loaded) {
260260
}
261261

262262
//export new database
263-
IExportFormat format = ExportFormats.getExportFormat(formatName);
264-
if (format == null) {
263+
Optional<Exporter> exporter = Globals.exportFactory.getExporterByName(formatName);
264+
if (!exporter.isPresent()) {
265265
System.err.println(Localization.lang("Unknown export format") + ": " + formatName);
266266
} else {
267-
// We have an ExportFormat instance:
267+
// We have an TemplateExporter instance:
268268
try {
269269
System.out.println(Localization.lang("Exporting") + ": " + data[1]);
270-
format.performExport(databaseContext, data[1],
270+
exporter.get().export(databaseContext, Paths.get(data[1]),
271271
databaseContext.getMetaData().getEncoding().orElse(Globals.prefs.getDefaultEncoding()),
272272
matches);
273273
} catch (Exception ex) {
@@ -437,18 +437,17 @@ private void exportFile(List<ParserResult> loaded, String[] data) {
437437
Globals.prefs.fileDirForDatabase = databaseContext
438438
.getFileDirectories(Globals.prefs.getFileDirectoryPreferences());
439439
System.out.println(Localization.lang("Exporting") + ": " + data[0]);
440-
IExportFormat format = ExportFormats.getExportFormat(data[1]);
441-
if (format == null) {
440+
Optional<Exporter> exporter = Globals.exportFactory.getExporterByName(data[1]);
441+
if (!exporter.isPresent()) {
442442
System.err.println(Localization.lang("Unknown export format") + ": " + data[1]);
443443
} else {
444-
// We have an ExportFormat instance:
444+
// We have an exporter:
445445
try {
446-
format.performExport(pr.getDatabaseContext(), data[0],
446+
exporter.get().export(pr.getDatabaseContext(), Paths.get(data[0]),
447447
pr.getDatabaseContext().getMetaData().getEncoding()
448448
.orElse(Globals.prefs.getDefaultEncoding()),
449449
pr.getDatabaseContext().getDatabase().getEntries());
450450
} catch (Exception ex) {
451-
452451
System.err.println(Localization.lang("Could not export file") + " '" + data[0] + "': "
453452
+ Throwables.getStackTraceAsString(ex));
454453
}
@@ -462,12 +461,12 @@ private void importPreferences() {
462461
Globals.prefs.importPreferences(cli.getPreferencesImport());
463462
EntryTypes.loadCustomEntryTypes(Globals.prefs.loadCustomEntryTypes(BibDatabaseMode.BIBTEX),
464463
Globals.prefs.loadCustomEntryTypes(BibDatabaseMode.BIBLATEX));
465-
Map<String, ExportFormat> customFormats = Globals.prefs.customExports.getCustomExportFormats(Globals.prefs,
464+
Map<String, TemplateExporter> customExporters = Globals.prefs.customExports.getCustomExportFormats(Globals.prefs,
466465
Globals.journalAbbreviationLoader);
467466
LayoutFormatterPreferences layoutPreferences = Globals.prefs
468467
.getLayoutFormatterPreferences(Globals.journalAbbreviationLoader);
469468
SavePreferences savePreferences = SavePreferences.loadForExportFromPreferences(Globals.prefs);
470-
ExportFormats.initAllExports(customFormats, layoutPreferences, savePreferences);
469+
Globals.exportFactory = ExporterFactory.create(customExporters, layoutPreferences, savePreferences);
471470
} catch (JabRefException ex) {
472471
LOGGER.error("Cannot import preferences", ex);
473472
}

src/main/java/org/jabref/cli/JabRefCLI.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.util.List;
55

66
import org.jabref.Globals;
7-
import org.jabref.logic.exporter.ExportFormats;
87
import org.jabref.logic.l10n.Localization;
98

109
import org.apache.commons.cli.CommandLine;
@@ -246,7 +245,7 @@ public void printUsage() {
246245
String importFormats = Globals.IMPORT_FORMAT_READER.getImportFormatList();
247246
String importFormatsList = String.format("%s:%n%s%n", Localization.lang("Available import formats"), importFormats);
248247

249-
String outFormats = ExportFormats.getConsoleExportList(70, 20, "");
248+
String outFormats = Globals.exportFactory.getExportersAsString(70, 20, "");
250249
String outFormatsList = String.format("%s: %s%n", Localization.lang("Available export formats"), outFormats);
251250

252251
String footer = '\n' + importFormatsList + outFormatsList + "\nPlease report issues at https://github.com/JabRef/jabref/issues.";

src/main/java/org/jabref/gui/BasePanel.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
import org.jabref.logic.layout.LayoutHelper;
117117
import org.jabref.logic.pdf.FileAnnotationCache;
118118
import org.jabref.logic.search.SearchQuery;
119-
import org.jabref.logic.util.FileExtensions;
119+
import org.jabref.logic.util.FileType;
120120
import org.jabref.logic.util.UpdateField;
121121
import org.jabref.logic.util.io.FileFinder;
122122
import org.jabref.logic.util.io.FileFinders;
@@ -2221,8 +2221,8 @@ public SaveSelectedAction(SavePreferences.DatabaseSaveType saveType) {
22212221
@Override
22222222
public void action() throws SaveException {
22232223
FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder()
2224-
.withDefaultExtension(FileExtensions.BIBTEX_DB)
2225-
.addExtensionFilter(FileExtensions.BIBTEX_DB)
2224+
.withDefaultExtension(FileType.BIBTEX_DB)
2225+
.addExtensionFilter(FileType.BIBTEX_DB)
22262226
.withInitialDirectory(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)).build();
22272227

22282228
DialogService ds = new FXDialogService();

src/main/java/org/jabref/gui/DialogService.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import javafx.scene.control.Alert;
1010
import javafx.scene.control.ButtonType;
1111
import javafx.scene.control.DialogPane;
12-
import javafx.stage.FileChooser;
1312

1413
import org.jabref.gui.util.DirectoryDialogConfiguration;
1514
import org.jabref.gui.util.FileDialogConfiguration;
@@ -50,13 +49,15 @@ public interface DialogService {
5049

5150
/**
5251
* Create and display error dialog displaying the given exception.
53-
* @param message the error message
52+
*
53+
* @param message the error message
5454
* @param exception the exception causing the error
5555
*/
5656
void showErrorDialogAndWait(String message, Throwable exception);
5757

5858
/**
5959
* Create and display error dialog displaying the given exception.
60+
*
6061
* @param exception the exception causing the error
6162
*/
6263
default void showErrorDialogAndWait(Exception exception) {
@@ -65,6 +66,7 @@ default void showErrorDialogAndWait(Exception exception) {
6566

6667
/**
6768
* Create and display error dialog displaying the given message.
69+
*
6870
* @param message the error message
6971
*/
7072
void showErrorDialogAndWait(String message);
@@ -107,7 +109,7 @@ default void showErrorDialogAndWait(Exception exception) {
107109
* @return Optional with the pressed Button as ButtonType
108110
*/
109111
Optional<ButtonType> showCustomButtonDialogAndWait(Alert.AlertType type, String title, String content,
110-
ButtonType... buttonTypes);
112+
ButtonType... buttonTypes);
111113

112114
/**
113115
* This will create and display a new dialog showing a custom {@link DialogPane}
@@ -127,12 +129,14 @@ Optional<ButtonType> showCustomButtonDialogAndWait(Alert.AlertType type, String
127129

128130
/**
129131
* Constructs and shows a canceable {@link ProgressDialog}. Clicking cancel will cancel the underlying service and close the dialog
132+
*
130133
* @param task The {@link Task} which executes the work and for which to show the dialog
131134
*/
132135
<V> void showCanceableProgressDialogAndWait(Task<V> task);
133136

134137
/**
135138
* Notify the user in an non-blocking way (i.e., update status message instead of showing a dialog).
139+
*
136140
* @param message the message to show.
137141
*/
138142
void notify(String message);
@@ -141,6 +145,7 @@ Optional<ButtonType> showCustomButtonDialogAndWait(Alert.AlertType type, String
141145
* Shows a new file save dialog. The method doesn't return until the
142146
* displayed file save dialog is dismissed. The return value specifies the
143147
* file chosen by the user or an empty {@link Optional} if no selection has been made.
148+
* After a file was selected, the given file dialog configuration is updated with the selected extension type (if any).
144149
*
145150
* @return the selected file or an empty {@link Optional} if no file has been selected
146151
*/
@@ -151,6 +156,7 @@ Optional<ButtonType> showCustomButtonDialogAndWait(Alert.AlertType type, String
151156
* displayed open dialog is dismissed. The return value specifies
152157
* the file chosen by the user or an empty {@link Optional} if no selection has been
153158
* made.
159+
* After a file was selected, the given file dialog configuration is updated with the selected extension type (if any).
154160
*
155161
* @return the selected file or an empty {@link Optional} if no file has been selected
156162
*/
@@ -176,14 +182,6 @@ Optional<ButtonType> showCustomButtonDialogAndWait(Alert.AlertType type, String
176182
*/
177183
Optional<Path> showDirectorySelectionDialog(DirectoryDialogConfiguration directoryDialogConfiguration);
178184

179-
/**
180-
* Gets the configured {@link FileChooser}, should only be necessary in rare use cases.
181-
* For normal usage use the show-Methods which directly return the selected file(s)
182-
* @param fileDialogConfiguration
183-
* @return A configured instance of the {@link FileChooser}
184-
*/
185-
FileChooser getConfiguredFileChooser(FileDialogConfiguration fileDialogConfiguration);
186-
187185
/**
188186
* Displays a Print Dialog. Allow the user to update job state such as printer and settings. These changes will be
189187
* available in the appropriate properties after the print dialog has returned. The print dialog is also used to

src/main/java/org/jabref/gui/FXDialogService.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,15 @@ public void notify(String message) {
154154
public Optional<Path> showFileSaveDialog(FileDialogConfiguration fileDialogConfiguration) {
155155
FileChooser chooser = getConfiguredFileChooser(fileDialogConfiguration);
156156
File file = chooser.showSaveDialog(null);
157+
Optional.ofNullable(chooser.getSelectedExtensionFilter()).ifPresent(fileDialogConfiguration::setSelectedExtensionFilter);
157158
return Optional.ofNullable(file).map(File::toPath);
158159
}
159160

160161
@Override
161162
public Optional<Path> showFileOpenDialog(FileDialogConfiguration fileDialogConfiguration) {
162163
FileChooser chooser = getConfiguredFileChooser(fileDialogConfiguration);
163164
File file = chooser.showOpenDialog(null);
165+
Optional.ofNullable(chooser.getSelectedExtensionFilter()).ifPresent(fileDialogConfiguration::setSelectedExtensionFilter);
164166
return Optional.ofNullable(file).map(File::toPath);
165167
}
166168

@@ -184,8 +186,7 @@ private DirectoryChooser getConfiguredDirectoryChooser(DirectoryDialogConfigurat
184186
return chooser;
185187
}
186188

187-
@Override
188-
public FileChooser getConfiguredFileChooser(FileDialogConfiguration fileDialogConfiguration) {
189+
private FileChooser getConfiguredFileChooser(FileDialogConfiguration fileDialogConfiguration) {
189190
FileChooser chooser = new FileChooser();
190191
chooser.getExtensionFilters().addAll(fileDialogConfiguration.getExtensionFilters());
191192
chooser.setSelectedExtensionFilter(fileDialogConfiguration.getDefaultExtension());

src/main/java/org/jabref/gui/PreviewPanel.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import org.jabref.gui.util.BackgroundTask;
2424
import org.jabref.gui.util.DefaultTaskExecutor;
2525
import org.jabref.logic.citationstyle.CitationStyle;
26-
import org.jabref.logic.exporter.ExportFormats;
26+
import org.jabref.logic.exporter.ExporterFactory;
2727
import org.jabref.logic.l10n.Localization;
2828
import org.jabref.logic.layout.Layout;
2929
import org.jabref.logic.layout.LayoutHelper;
@@ -167,11 +167,11 @@ public void updateLayout(PreviewPreferences previewPreferences) {
167167
if (CitationStyle.isCitationStyleFile(style)) {
168168
if (basePanel.isPresent()) {
169169
layout = Optional.empty();
170-
CitationStyle citationStyle = CitationStyle.createCitationStyleFromFile(style);
171-
if (citationStyle != null) {
170+
CitationStyle.createCitationStyleFromFile(style)
171+
.ifPresent(citationStyle -> {
172172
basePanel.get().getCitationStyleCache().setCitationStyle(citationStyle);
173173
basePanel.get().output(Localization.lang("Preview style changed to: %0", citationStyle.getTitle()));
174-
}
174+
});
175175
}
176176
} else {
177177
updatePreviewLayout(previewPreferences.getPreviewStyle());
@@ -225,7 +225,7 @@ public BibEntry getEntry() {
225225
}
226226

227227
public void update() {
228-
ExportFormats.entryNumber = 1; // Set entry number in case that is included in the preview layout.
228+
ExporterFactory.entryNumber = 1; // Set entry number in case that is included in the preview layout.
229229

230230
if (citationStyleFuture.isPresent()) {
231231
citationStyleFuture.get().cancel(true);

src/main/java/org/jabref/gui/auximport/FromAuxDialog.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import org.jabref.logic.auxparser.AuxParser;
3333
import org.jabref.logic.auxparser.AuxParserResult;
3434
import org.jabref.logic.l10n.Localization;
35-
import org.jabref.logic.util.FileExtensions;
35+
import org.jabref.logic.util.FileType;
3636
import org.jabref.model.database.BibDatabase;
3737
import org.jabref.preferences.JabRefPreferences;
3838

@@ -159,8 +159,8 @@ private void initPanels() {
159159
JButton browseAuxFileButton = new JButton(Localization.lang("Browse"));
160160

161161
FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder()
162-
.addExtensionFilter(FileExtensions.AUX)
163-
.withDefaultExtension(FileExtensions.AUX)
162+
.addExtensionFilter(FileType.AUX)
163+
.withDefaultExtension(FileType.AUX)
164164
.withInitialDirectory(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)).build();
165165
DialogService ds = new FXDialogService();
166166

0 commit comments

Comments
 (0)