Skip to content

Commit

Permalink
Fix JabRef#1446 Only add non empty paths (JabRef#2348)
Browse files Browse the repository at this point in the history
* Fix JabRef#1446 Only add non empty paths, add user dir default as last resort

* Fix tests

* Remove comment
  • Loading branch information
stefan-kolb authored and tobiasdiez committed Dec 10, 2016
1 parent 845e785 commit 7f9e483
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,18 +191,21 @@ public List<String> 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);
} else {
fileDirs.add(parentDir);
}
});

return fileDirs;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> fieldFileDirectories;
private final boolean bibLocationAsPrimary;
public static final String DIR_SUFFIX = "Directory";


public FileDirectoryPreferences(String user, Map<String, String> fieldFileDirectories,
boolean bibLocationAsPrimary) {
public FileDirectoryPreferences(String user, Map<String, String> fieldFileDirectories, boolean bibLocationAsPrimary) {
this.user = user;
this.fieldFileDirectories = fieldFileDirectories;
this.bibLocationAsPrimary = bibLocationAsPrimary;
Expand All @@ -24,11 +25,21 @@ public String getUser() {
return user;
}

public Optional<String> getFileDirectory(String field) {
return Optional.ofNullable(fieldFileDirectories.get(field));
public Optional<Path> 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<String> getFileDirectory() {
public Optional<Path> getFileDirectory() {
return getFileDirectory(FieldName.FILE);
}

Expand Down

0 comments on commit 7f9e483

Please sign in to comment.