Skip to content

Commit

Permalink
Lookup filetypes in enum set to prevent NPE due to uninitialized expo… (
Browse files Browse the repository at this point in the history
#3597)

* Lookup filetypes in enum set to prevent NPE due to uninitialized exporterFactory
Fixes #3596

* Refactor list parameter to three args
Add FileType parse

* fix checkstyle
  • Loading branch information
Siedlerchr authored Jan 4, 2018
1 parent 193bbbc commit 31e57e2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.AbstractAction;
Expand Down Expand Up @@ -74,8 +73,7 @@ public ExportCustomizationDialog(final JabRefFrame frame) {
CustomExportDialog ecd = new CustomExportDialog(frame);
ecd.setVisible(true);
if (ecd.okPressed()) {
List<String> newFormat = Arrays.asList(ecd.name(), ecd.layoutFile(), ecd.extension());
Globals.prefs.customExports.addFormat(newFormat,
Globals.prefs.customExports.addFormat(ecd.name(), ecd.layoutFile(), ecd.extension(),
Globals.prefs.getLayoutFormatterPreferences(Globals.journalAbbreviationLoader),
SavePreferences.loadForExportFromPreferences(Globals.prefs));
Globals.prefs.customExports.store(Globals.prefs);
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/jabref/logic/util/FileType.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.jabref.logic.util;

import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
import java.util.Optional;
import java.util.StringJoiner;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -51,6 +53,7 @@ public enum FileType {
CSV(Localization.lang("%0 file", "CSV"), "csv"),
DEFAULT(Localization.lang("%0 file", "DEFAULT"), "default");

private static final EnumSet<FileType> ALL_FILE_TYPES = EnumSet.allOf(FileType.class);
private final String[] extensions;
private final String description;

Expand Down Expand Up @@ -78,4 +81,9 @@ public String getFirstExtensionWithDot() {
public List<String> getExtensionsWithDot() {
return getExtensions().stream().map(extension -> "." + extension).collect(Collectors.toList());
}

public static FileType parse(String fileExtension) {
Optional<FileType> fileType = ALL_FILE_TYPES.stream().filter(f -> f.getExtensionsWithDot().stream().anyMatch(fileExtension::equals)).findFirst();
return fileType.orElse(FileType.DEFAULT);
}
}
52 changes: 24 additions & 28 deletions src/main/java/org/jabref/preferences/CustomExportList.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package org.jabref.preferences;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.TreeMap;

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;
Expand All @@ -31,23 +30,21 @@

public class CustomExportList {

private static final int EXPORTER_NAME_INDEX = 0;
private static final int EXPORTER_FILENAME_INDEX = 1;
private static final int EXPORTER_EXTENSION_INDEX = 2;

private static final Log LOGGER = LogFactory.getLog(CustomExportList.class);
private final EventList<List<String>> list;
private final SortedList<List<String>> sorted;

private final Map<String, TemplateExporter> formats = new TreeMap<>();


public CustomExportList(Comparator<List<String>> comp) {
list = new BasicEventList<>();
sorted = new SortedList<>(list, comp);
}

private static FileType getFileExtension(String consoleName) {
Optional<Exporter> exporter = Globals.exportFactory.getExporterByName(consoleName);
return exporter.map(Exporter::getFileType).orElse(FileType.DEFAULT);
}

public int size() {
return list.size();
}
Expand All @@ -57,7 +54,7 @@ public EventList<List<String>> getSortedList() {
}

public Map<String, TemplateExporter> getCustomExportFormats(JabRefPreferences prefs,
JournalAbbreviationLoader loader) {
JournalAbbreviationLoader loader) {
Objects.requireNonNull(prefs);
Objects.requireNonNull(loader);
formats.clear();
Expand All @@ -75,7 +72,7 @@ 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<TemplateExporter> format = createFormat(s, layoutPreferences, savePreferences);
Optional<TemplateExporter> format = createFormat(s.get(EXPORTER_NAME_INDEX), s.get(EXPORTER_FILENAME_INDEX), s.get(EXPORTER_EXTENSION_INDEX), layoutPreferences, savePreferences);
if (format.isPresent()) {
formats.put(format.get().getId(), format.get());
list.add(s);
Expand All @@ -87,30 +84,21 @@ private void readPrefs(JabRefPreferences prefs, JournalAbbreviationLoader loader
}
}

private Optional<TemplateExporter> createFormat(List<String> s, LayoutFormatterPreferences layoutPreferences,
SavePreferences savePreferences) {
if (s.size() < 3) {
return Optional.empty();
}
private Optional<TemplateExporter> createFormat(String exporterName, String filename, String extension, LayoutFormatterPreferences layoutPreferences,
SavePreferences savePreferences) {

String lfFileName;
if (s.get(1).endsWith(".layout")) {
lfFileName = s.get(1).substring(0, s.get(1).length() - 7);
if (extension.endsWith(".layout")) {
lfFileName = filename.substring(0, filename.length() - ".layout".length());
} else {
lfFileName = s.get(1);
lfFileName = filename;
}
TemplateExporter format = new TemplateExporter(s.get(0), s.get(0), lfFileName, null, getFileExtension(s.get(2)), layoutPreferences,
TemplateExporter format = new TemplateExporter(exporterName, filename, lfFileName, null, FileType.parse(extension), layoutPreferences,
savePreferences);
format.setCustomExport(true);
return Optional.of(format);
}

public void addFormat(List<String> s, LayoutFormatterPreferences layoutPreferences, SavePreferences savePreferences) {
createFormat(s, layoutPreferences, savePreferences).ifPresent(format -> {
formats.put(format.getId(), format);
list.add(s);
});
}

public void store(JabRefPreferences prefs) {

if (list.isEmpty()) {
Expand All @@ -132,10 +120,18 @@ private void purge(int from, JabRefPreferences prefs) {
}

public void remove(List<String> toRemove, LayoutFormatterPreferences layoutPreferences,
SavePreferences savePreferences) {
createFormat(toRemove, layoutPreferences, savePreferences).ifPresent(format -> {
SavePreferences savePreferences) {
createFormat(toRemove.get(EXPORTER_NAME_INDEX), toRemove.get(EXPORTER_FILENAME_INDEX), toRemove.get(EXPORTER_EXTENSION_INDEX), layoutPreferences, savePreferences).ifPresent(format -> {
formats.remove(format.getId());
list.remove(toRemove);
});
}

public void addFormat(String name, String layoutFile, String extension, LayoutFormatterPreferences layoutPreferences, SavePreferences savePreferences) {
createFormat(name, layoutFile, extension, layoutPreferences, savePreferences).ifPresent(format -> {
formats.put(format.getId(), format);
list.add(Arrays.asList(name, layoutFile, extension));
});

}
}

0 comments on commit 31e57e2

Please sign in to comment.