diff --git a/src/main/java/net/sf/jabref/model/database/BibDatabaseContext.java b/src/main/java/net/sf/jabref/model/database/BibDatabaseContext.java index 8b54a4cf4e03..01603fcae6b9 100644 --- a/src/main/java/net/sf/jabref/model/database/BibDatabaseContext.java +++ b/src/main/java/net/sf/jabref/model/database/BibDatabaseContext.java @@ -191,11 +191,13 @@ public List getFileDirectories(String fieldName, FileDirectoryPreference } // 3. preferences directory - preferences.getFileDirectory(fieldName).ifPresent(fileDirs::add); + preferences.getFileDirectory(fieldName).ifPresent(path -> + fileDirs.add(path.toAbsolutePath().toString()) + ); // 4. BIB file directory - getDatabaseFile().ifPresent(databaseFile -> { - String parentDir = databaseFile.getParent(); + getDatabasePath().ifPresent(dbPath -> { + String parentDir = dbPath.getParent().toAbsolutePath().toString(); // Check if we should add it as primary file dir (first in the list) or not: if (preferences.isBibLocationAsPrimary()) { fileDirs.add(0, parentDir); @@ -203,6 +205,7 @@ public List getFileDirectories(String fieldName, FileDirectoryPreference fileDirs.add(parentDir); } }); + return fileDirs; } diff --git a/src/main/java/net/sf/jabref/model/metadata/FileDirectoryPreferences.java b/src/main/java/net/sf/jabref/model/metadata/FileDirectoryPreferences.java index fb29d641ac03..1016911f8fc3 100644 --- a/src/main/java/net/sf/jabref/model/metadata/FileDirectoryPreferences.java +++ b/src/main/java/net/sf/jabref/model/metadata/FileDirectoryPreferences.java @@ -1,20 +1,21 @@ package net.sf.jabref.model.metadata; +import java.nio.file.InvalidPathException; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Map; import java.util.Optional; import net.sf.jabref.model.entry.FieldName; public class FileDirectoryPreferences { + public static final String DIR_SUFFIX = "Directory"; private final String user; private final Map fieldFileDirectories; private final boolean bibLocationAsPrimary; - public static final String DIR_SUFFIX = "Directory"; - - public FileDirectoryPreferences(String user, Map fieldFileDirectories, - boolean bibLocationAsPrimary) { + public FileDirectoryPreferences(String user, Map fieldFileDirectories, boolean bibLocationAsPrimary) { this.user = user; this.fieldFileDirectories = fieldFileDirectories; this.bibLocationAsPrimary = bibLocationAsPrimary; @@ -24,11 +25,21 @@ public String getUser() { return user; } - public Optional getFileDirectory(String field) { - return Optional.ofNullable(fieldFileDirectories.get(field)); + public Optional getFileDirectory(String field) { + try { + String value = fieldFileDirectories.get(field); + // filter empty paths + if (value != null && !value.isEmpty()) { + Path path = Paths.get(value); + return Optional.of(path); + } + return Optional.empty(); + } catch(InvalidPathException ex) { + return Optional.empty(); + } } - public Optional getFileDirectory() { + public Optional getFileDirectory() { return getFileDirectory(FieldName.FILE); }