Skip to content

Commit

Permalink
Fixes #6357: File directory (#6377)
Browse files Browse the repository at this point in the history
* Fixes #6357: File directory

Bug was introduced in 1b03f03.

* Fix tests

* Replace Paths.get

* Fix code style
  • Loading branch information
tobiasdiez committed Apr 30, 2020
1 parent 7219e36 commit 862078a
Show file tree
Hide file tree
Showing 111 changed files with 530 additions and 685 deletions.
11 changes: 5 additions & 6 deletions src/main/java/org/jabref/cli/ArgumentProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -111,9 +110,9 @@ private static Optional<ParserResult> importFile(String argument) {
}
} else {
if (OS.WINDOWS) {
file = Paths.get(address);
file = Path.of(address);
} else {
file = Paths.get(address.replace("~", System.getProperty("user.home")));
file = Path.of(address.replace("~", System.getProperty("user.home")));
}
}

Expand Down Expand Up @@ -280,7 +279,7 @@ private boolean exportMatches(List<ParserResult> loaded) {
// We have an TemplateExporter instance:
try {
System.out.println(Localization.lang("Exporting") + ": " + data[1]);
exporter.get().export(databaseContext, Paths.get(data[1]),
exporter.get().export(databaseContext, Path.of(data[1]),
databaseContext.getMetaData().getEncoding().orElse(Globals.prefs.getDefaultEncoding()),
matches);
} catch (Exception ex) {
Expand Down Expand Up @@ -389,7 +388,7 @@ private void saveDatabase(BibDatabase newBase, String subName) {
try {
System.out.println(Localization.lang("Saving") + ": " + subName);
SavePreferences prefs = Globals.prefs.loadForSaveFromPreferences();
AtomicFileWriter fileWriter = new AtomicFileWriter(Paths.get(subName), prefs.getEncoding());
AtomicFileWriter fileWriter = new AtomicFileWriter(Path.of(subName), prefs.getEncoding());
BibDatabaseWriter databaseWriter = new BibtexDatabaseWriter(fileWriter, prefs, Globals.entryTypesManager);
databaseWriter.saveDatabase(new BibDatabaseContext(newBase));

Expand Down Expand Up @@ -441,7 +440,7 @@ private void exportFile(List<ParserResult> loaded, String[] data) {
} else {
// We have an exporter:
try {
exporter.get().export(pr.getDatabaseContext(), Paths.get(data[0]),
exporter.get().export(pr.getDatabaseContext(), Path.of(data[0]),
pr.getDatabaseContext().getMetaData().getEncoding()
.orElse(Globals.prefs.getDefaultEncoding()),
pr.getDatabaseContext().getDatabase().getEntries());
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/cli/AuxCommandLine.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.jabref.cli;

import java.nio.file.Paths;
import java.nio.file.Path;

import org.jabref.gui.auximport.AuxParserResultViewModel;
import org.jabref.logic.auxparser.DefaultAuxParser;
Expand All @@ -23,7 +23,7 @@ public BibDatabase perform() {

if (!auxFile.isEmpty() && (database != null)) {
AuxParser auxParser = new DefaultAuxParser(database);
AuxParserResult result = auxParser.parse(Paths.get(auxFile));
AuxParserResult result = auxParser.parse(Path.of(auxFile));
subDatabase = result.getGeneratedBibDatabase();
// print statistics
System.out.println(new AuxParserResultViewModel(result).getInformation(true));
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -320,7 +319,7 @@ public void setWindowTitle() {
* The MacAdapter calls this method when a "BIB" file has been double-clicked from the Finder.
*/
public void openAction(String filePath) {
Path file = Paths.get(filePath);
Path file = Path.of(filePath);
// all the logic is done in openIt. Even raising an existing panel
getOpenDatabaseAction().openFile(file, true);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/actions/ActionHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static BooleanExpression isFilePresentForSelectedEntry(StateManager state
return Bindings.createBooleanBinding(() -> {
List<LinkedFile> files = stateManager.getSelectedEntries().get(0).getFiles();
if ((files.size() > 0) && stateManager.getActiveDatabase().isPresent()) {
Optional<Path> filename = FileHelper.expandFilename(
Optional<Path> filename = FileHelper.find(
stateManager.getActiveDatabase().get(),
files.get(0).getLink(),
preferencesService.getFilePreferences());
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/jabref/gui/auximport/FromAuxDialog.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.jabref.gui.auximport;

import java.nio.file.Paths;
import java.nio.file.Path;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
Expand Down Expand Up @@ -36,7 +36,7 @@ public class FromAuxDialog extends BaseDialog<Void> {
private final DialogService dialogService;
private final BasePanel basePanel;
@FXML private ButtonType generateButtonType;
private Button generateButton;
private final Button generateButton;
@FXML private TextField auxFileField;
@FXML private ListView<String> notFoundList;

Expand Down Expand Up @@ -74,7 +74,7 @@ private void parseActionPerformed() {

if ((auxName != null) && (refBase != null) && !auxName.isEmpty()) {
AuxParser auxParser = new DefaultAuxParser(refBase);
auxParserResult = auxParser.parse(Paths.get(auxName));
auxParserResult = auxParser.parse(Path.of(auxName));
notFoundList.getItems().setAll(auxParserResult.getUnresolvedKeys());
statusInfos.setText(new AuxParserResultViewModel(auxParserResult).getInformation(false));

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/jabref/gui/copyfiles/CopyFilesAction.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.jabref.gui.copyfiles;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;

Expand Down Expand Up @@ -47,7 +46,7 @@ public void execute() {
List<BibEntry> entries = stateManager.getSelectedEntries();

DirectoryDialogConfiguration dirDialogConfiguration = new DirectoryDialogConfiguration.Builder()
.withInitialDirectory(Paths.get(Globals.prefs.get(JabRefPreferences.EXPORT_WORKING_DIRECTORY)))
.withInitialDirectory(Path.of(Globals.prefs.get(JabRefPreferences.EXPORT_WORKING_DIRECTORY)))
.build();
Optional<Path> exportPath = dialogService.showDirectorySelectionDialog(dirDialogConfiguration);
exportPath.ifPresent(path -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.jabref.gui.copyfiles;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
import java.util.function.BiFunction;

Expand All @@ -17,9 +16,9 @@

public class CopySingleFileAction {

private LinkedFile linkedFile;
private DialogService dialogService;
private BibDatabaseContext databaseContext;
private final LinkedFile linkedFile;
private final DialogService dialogService;
private final BibDatabaseContext databaseContext;
private final BiFunction<Path, Path, Path> resolvePathFilename = (path, file) -> {
return path.resolve(file.getFileName());
};
Expand All @@ -32,7 +31,7 @@ public CopySingleFileAction(LinkedFile linkedFile, DialogService dialogService,

public void copyFile() {
DirectoryDialogConfiguration dirDialogConfiguration = new DirectoryDialogConfiguration.Builder()
.withInitialDirectory(Paths.get(Globals.prefs.get(JabRefPreferences.EXPORT_WORKING_DIRECTORY)))
.withInitialDirectory(Path.of(Globals.prefs.get(JabRefPreferences.EXPORT_WORKING_DIRECTORY)))
.build();
Optional<Path> exportPath = dialogService.showDirectorySelectionDialog(dirDialogConfiguration);
exportPath.ifPresent(this::copyFileToDestination);
Expand Down
15 changes: 5 additions & 10 deletions src/main/java/org/jabref/gui/desktop/JabRefDesktop.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ public static void openExternalViewer(BibDatabaseContext databaseContext, String
Field field = initialField;
if (StandardField.PS.equals(field) || StandardField.PDF.equals(field)) {
// Find the default directory for this field type:
List<String> dir = databaseContext.getFileDirectories(field, Globals.prefs.getFilePreferences());
List<Path> directories = databaseContext.getFileDirectoriesAsPaths(Globals.prefs.getFilePreferences());

Optional<Path> file = FileHelper.expandFilename(link, dir);
Optional<Path> file = FileHelper.find(link, directories);

// Check that the file exists:
if (!file.isPresent() || !Files.exists(file.get())) {
if (file.isEmpty() || !Files.exists(file.get())) {
throw new IOException("File not found (" + field + "): '" + link + "'.");
}
link = file.get().toAbsolutePath().toString();
Expand Down Expand Up @@ -126,21 +126,16 @@ public static boolean openExternalFileAnyFormat(final BibDatabaseContext databas
return true;
}

Optional<Path> file = FileHelper.expandFilename(databaseContext, link, Globals.prefs.getFilePreferences());
Optional<Path> file = FileHelper.find(databaseContext, link, Globals.prefs.getFilePreferences());
if (file.isPresent() && Files.exists(file.get())) {
// Open the file:
String filePath = file.get().toString();
openExternalFilePlatformIndependent(type, filePath);
return true;
} else {
// No file matched the name, try to open it directly using the given app
openExternalFilePlatformIndependent(type, link);
return true;
}
}

public static boolean openExternalFileAnyFormat(Path file, final BibDatabaseContext databaseContext, final Optional<ExternalFileType> type) throws IOException {
return openExternalFileAnyFormat(databaseContext, file.toString(), type);
return true;
}

private static void openExternalFilePlatformIndependent(Optional<ExternalFileType> fileType, String filePath)
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/jabref/gui/desktop/os/Linux.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Locale;
import java.util.Optional;

Expand Down Expand Up @@ -109,6 +108,6 @@ public String detectProgramPath(String programName, String directoryName) {

@Override
public Path getApplicationDirectory() {
return Paths.get("/usr/lib/");
return Path.of("/usr/lib/");
}
}
3 changes: 1 addition & 2 deletions src/main/java/org/jabref/gui/desktop/os/NativeDesktop.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

public interface NativeDesktop {
void openFile(String filePath, String fileType) throws IOException;
Expand Down Expand Up @@ -35,6 +34,6 @@ public interface NativeDesktop {
* @return the path to the user directory.
*/
default Path getUserDirectory() {
return Paths.get(System.getProperty("user.home"));
return Path.of(System.getProperty("user.home"));
}
}
3 changes: 1 addition & 2 deletions src/main/java/org/jabref/gui/desktop/os/OSX.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;

import org.jabref.gui.externalfiletype.ExternalFileType;
Expand Down Expand Up @@ -48,6 +47,6 @@ public String detectProgramPath(String programName, String directoryName) {

@Override
public Path getApplicationDirectory() {
return Paths.get("/Applications");
return Path.of("/Applications");
}
}
11 changes: 5 additions & 6 deletions src/main/java/org/jabref/gui/desktop/os/Windows.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;

import org.jabref.gui.externalfiletype.ExternalFileType;
import org.jabref.gui.externalfiletype.ExternalFileTypes;

public class Windows implements NativeDesktop {
private static String DEFAULT_EXECUTABLE_EXTENSION = ".exe";
private static final String DEFAULT_EXECUTABLE_EXTENSION = ".exe";

@Override
public void openFile(String filePath, String fileType) throws IOException {
Expand All @@ -32,24 +31,24 @@ public String detectProgramPath(String programName, String directoryName) {
progFiles = System.getenv("ProgramFiles");
}
if ((directoryName != null) && !directoryName.isEmpty()) {
return Paths.get(progFiles, directoryName, programName + DEFAULT_EXECUTABLE_EXTENSION).toString();
return Path.of(progFiles, directoryName, programName + DEFAULT_EXECUTABLE_EXTENSION).toString();
}
return Paths.get(progFiles, programName + DEFAULT_EXECUTABLE_EXTENSION).toString();
return Path.of(progFiles, programName + DEFAULT_EXECUTABLE_EXTENSION).toString();
}

@Override
public Path getApplicationDirectory() {
String programDir = System.getenv("ProgramFiles");

if (programDir != null) {
return Paths.get(programDir);
return Path.of(programDir);
}
return getUserDirectory();
}

@Override
public void openFileWithApplication(String filePath, String application) throws IOException {
new ProcessBuilder(Paths.get(application).toString(), Paths.get(filePath).toString()).start();
new ProcessBuilder(Path.of(application).toString(), Path.of(filePath).toString()).start();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.jabref.gui.exporter;

import java.nio.file.Path;
import java.nio.file.Paths;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
Expand Down Expand Up @@ -55,7 +54,7 @@ public CreateModifyExporterDialogViewModel(ExporterViewModel exporter, DialogSer
}

public ExporterViewModel saveExporter() {
Path layoutFileDir = Paths.get(layoutFile.get()).getParent();
Path layoutFileDir = Path.of(layoutFile.get()).getParent();
if (layoutFileDir != null) {
String layoutFileDirString = layoutFileDir.toString();
preferences.setExportWorkingDirectory(layoutFileDirString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -304,9 +303,9 @@ private void startSearch() {
}

private Path getSearchDirectory() {
Path directory = Paths.get(textfieldDirectoryPath.getText());
Path directory = Path.of(textfieldDirectoryPath.getText());
if (Files.notExists(directory)) {
directory = Paths.get(System.getProperty("user.dir"));
directory = Path.of(System.getProperty("user.dir"));
textfieldDirectoryPath.setText(directory.toAbsolutePath().toString());
}
if (!Files.isDirectory(directory)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -101,7 +100,7 @@ public LinkedFileViewModel(LinkedFile linkedFile,
if (linkedFile.isOnlineLink()) {
return true;
} else {
Optional<Path> path = FileHelper.expandFilename(databaseContext, link, filePreferences);
Optional<Path> path = FileHelper.find(databaseContext, link, filePreferences);
return path.isPresent() && Files.exists(path.get());
}
},
Expand Down Expand Up @@ -192,7 +191,7 @@ public void open() {
public void openFolder() {
try {
if (!linkedFile.isOnlineLink()) {
Optional<Path> resolvedPath = FileHelper.expandFilename(
Optional<Path> resolvedPath = FileHelper.find(
databaseContext,
linkedFile.getLink(),
filePreferences);
Expand All @@ -216,7 +215,7 @@ public void renameToSuggestion() {

public void askForNameAndRename() {
String oldFile = this.linkedFile.getLink();
Path oldFilePath = Paths.get(oldFile);
Path oldFilePath = Path.of(oldFile);
Optional<String> askedFileName = dialogService.showInputDialogWithDefaultAndWait(Localization.lang("Rename file"), Localization.lang("New Filename"), oldFilePath.getFileName().toString());
askedFileName.ifPresent(this::renameFileToName);
}
Expand Down Expand Up @@ -293,7 +292,7 @@ public void moveToDefaultDirectory() {
* @return true if the suggested filename is same as current filename.
*/
public boolean isGeneratedNameSameAsOriginal() {
Path file = Paths.get(this.linkedFile.getLink());
Path file = Path.of(this.linkedFile.getLink());
String currentFileName = file.getFileName().toString();
String suggestedFileName = this.linkedFileHandler.getSuggestedFileName();

Expand Down
Loading

0 comments on commit 862078a

Please sign in to comment.