Skip to content

Commit 862078a

Browse files
authored
Fixes #6357: File directory (#6377)
* Fixes #6357: File directory Bug was introduced in 1b03f03. * Fix tests * Replace Paths.get * Fix code style
1 parent 7219e36 commit 862078a

File tree

111 files changed

+530
-685
lines changed

Some content is hidden

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

111 files changed

+530
-685
lines changed

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.io.File;
44
import java.io.IOException;
55
import java.nio.file.Path;
6-
import java.nio.file.Paths;
76
import java.util.ArrayList;
87
import java.util.Collections;
98
import java.util.List;
@@ -111,9 +110,9 @@ private static Optional<ParserResult> importFile(String argument) {
111110
}
112111
} else {
113112
if (OS.WINDOWS) {
114-
file = Paths.get(address);
113+
file = Path.of(address);
115114
} else {
116-
file = Paths.get(address.replace("~", System.getProperty("user.home")));
115+
file = Path.of(address.replace("~", System.getProperty("user.home")));
117116
}
118117
}
119118

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

@@ -441,7 +440,7 @@ private void exportFile(List<ParserResult> loaded, String[] data) {
441440
} else {
442441
// We have an exporter:
443442
try {
444-
exporter.get().export(pr.getDatabaseContext(), Paths.get(data[0]),
443+
exporter.get().export(pr.getDatabaseContext(), Path.of(data[0]),
445444
pr.getDatabaseContext().getMetaData().getEncoding()
446445
.orElse(Globals.prefs.getDefaultEncoding()),
447446
pr.getDatabaseContext().getDatabase().getEntries());

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

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

3-
import java.nio.file.Paths;
3+
import java.nio.file.Path;
44

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

2424
if (!auxFile.isEmpty() && (database != null)) {
2525
AuxParser auxParser = new DefaultAuxParser(database);
26-
AuxParserResult result = auxParser.parse(Paths.get(auxFile));
26+
AuxParserResult result = auxParser.parse(Path.of(auxFile));
2727
subDatabase = result.getGeneratedBibDatabase();
2828
// print statistics
2929
System.out.println(new AuxParserResultViewModel(result).getInformation(true));

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.io.File;
44
import java.nio.file.Path;
5-
import java.nio.file.Paths;
65
import java.util.ArrayList;
76
import java.util.Collections;
87
import java.util.HashMap;
@@ -320,7 +319,7 @@ public void setWindowTitle() {
320319
* The MacAdapter calls this method when a "BIB" file has been double-clicked from the Finder.
321320
*/
322321
public void openAction(String filePath) {
323-
Path file = Paths.get(filePath);
322+
Path file = Path.of(filePath);
324323
// all the logic is done in openIt. Even raising an existing panel
325324
getOpenDatabaseAction().openFile(file, true);
326325
}

src/main/java/org/jabref/gui/actions/ActionHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static BooleanExpression isFilePresentForSelectedEntry(StateManager state
4747
return Bindings.createBooleanBinding(() -> {
4848
List<LinkedFile> files = stateManager.getSelectedEntries().get(0).getFiles();
4949
if ((files.size() > 0) && stateManager.getActiveDatabase().isPresent()) {
50-
Optional<Path> filename = FileHelper.expandFilename(
50+
Optional<Path> filename = FileHelper.find(
5151
stateManager.getActiveDatabase().get(),
5252
files.get(0).getLink(),
5353
preferencesService.getFilePreferences());

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

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

3-
import java.nio.file.Paths;
3+
import java.nio.file.Path;
44

55
import javafx.fxml.FXML;
66
import javafx.scene.control.Button;
@@ -36,7 +36,7 @@ public class FromAuxDialog extends BaseDialog<Void> {
3636
private final DialogService dialogService;
3737
private final BasePanel basePanel;
3838
@FXML private ButtonType generateButtonType;
39-
private Button generateButton;
39+
private final Button generateButton;
4040
@FXML private TextField auxFileField;
4141
@FXML private ListView<String> notFoundList;
4242

@@ -74,7 +74,7 @@ private void parseActionPerformed() {
7474

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

src/main/java/org/jabref/gui/copyfiles/CopyFilesAction.java

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

33
import java.nio.file.Path;
4-
import java.nio.file.Paths;
54
import java.util.List;
65
import java.util.Optional;
76

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

4948
DirectoryDialogConfiguration dirDialogConfiguration = new DirectoryDialogConfiguration.Builder()
50-
.withInitialDirectory(Paths.get(Globals.prefs.get(JabRefPreferences.EXPORT_WORKING_DIRECTORY)))
49+
.withInitialDirectory(Path.of(Globals.prefs.get(JabRefPreferences.EXPORT_WORKING_DIRECTORY)))
5150
.build();
5251
Optional<Path> exportPath = dialogService.showDirectorySelectionDialog(dirDialogConfiguration);
5352
exportPath.ifPresent(path -> {

src/main/java/org/jabref/gui/copyfiles/CopySingleFileAction.java

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

33
import java.nio.file.Path;
4-
import java.nio.file.Paths;
54
import java.util.Optional;
65
import java.util.function.BiFunction;
76

@@ -17,9 +16,9 @@
1716

1817
public class CopySingleFileAction {
1918

20-
private LinkedFile linkedFile;
21-
private DialogService dialogService;
22-
private BibDatabaseContext databaseContext;
19+
private final LinkedFile linkedFile;
20+
private final DialogService dialogService;
21+
private final BibDatabaseContext databaseContext;
2322
private final BiFunction<Path, Path, Path> resolvePathFilename = (path, file) -> {
2423
return path.resolve(file.getFileName());
2524
};
@@ -32,7 +31,7 @@ public CopySingleFileAction(LinkedFile linkedFile, DialogService dialogService,
3231

3332
public void copyFile() {
3433
DirectoryDialogConfiguration dirDialogConfiguration = new DirectoryDialogConfiguration.Builder()
35-
.withInitialDirectory(Paths.get(Globals.prefs.get(JabRefPreferences.EXPORT_WORKING_DIRECTORY)))
34+
.withInitialDirectory(Path.of(Globals.prefs.get(JabRefPreferences.EXPORT_WORKING_DIRECTORY)))
3635
.build();
3736
Optional<Path> exportPath = dialogService.showDirectorySelectionDialog(dirDialogConfiguration);
3837
exportPath.ifPresent(this::copyFileToDestination);

src/main/java/org/jabref/gui/desktop/JabRefDesktop.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ public static void openExternalViewer(BibDatabaseContext databaseContext, String
5555
Field field = initialField;
5656
if (StandardField.PS.equals(field) || StandardField.PDF.equals(field)) {
5757
// Find the default directory for this field type:
58-
List<String> dir = databaseContext.getFileDirectories(field, Globals.prefs.getFilePreferences());
58+
List<Path> directories = databaseContext.getFileDirectoriesAsPaths(Globals.prefs.getFilePreferences());
5959

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

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

129-
Optional<Path> file = FileHelper.expandFilename(databaseContext, link, Globals.prefs.getFilePreferences());
129+
Optional<Path> file = FileHelper.find(databaseContext, link, Globals.prefs.getFilePreferences());
130130
if (file.isPresent() && Files.exists(file.get())) {
131131
// Open the file:
132132
String filePath = file.get().toString();
133133
openExternalFilePlatformIndependent(type, filePath);
134-
return true;
135134
} else {
136135
// No file matched the name, try to open it directly using the given app
137136
openExternalFilePlatformIndependent(type, link);
138-
return true;
139137
}
140-
}
141-
142-
public static boolean openExternalFileAnyFormat(Path file, final BibDatabaseContext databaseContext, final Optional<ExternalFileType> type) throws IOException {
143-
return openExternalFileAnyFormat(databaseContext, file.toString(), type);
138+
return true;
144139
}
145140

146141
private static void openExternalFilePlatformIndependent(Optional<ExternalFileType> fileType, String filePath)

src/main/java/org/jabref/gui/desktop/os/Linux.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.io.IOException;
66
import java.io.InputStreamReader;
77
import java.nio.file.Path;
8-
import java.nio.file.Paths;
98
import java.util.Locale;
109
import java.util.Optional;
1110

@@ -109,6 +108,6 @@ public String detectProgramPath(String programName, String directoryName) {
109108

110109
@Override
111110
public Path getApplicationDirectory() {
112-
return Paths.get("/usr/lib/");
111+
return Path.of("/usr/lib/");
113112
}
114113
}

src/main/java/org/jabref/gui/desktop/os/NativeDesktop.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.io.IOException;
44
import java.nio.file.Path;
5-
import java.nio.file.Paths;
65

76
public interface NativeDesktop {
87
void openFile(String filePath, String fileType) throws IOException;
@@ -35,6 +34,6 @@ public interface NativeDesktop {
3534
* @return the path to the user directory.
3635
*/
3736
default Path getUserDirectory() {
38-
return Paths.get(System.getProperty("user.home"));
37+
return Path.of(System.getProperty("user.home"));
3938
}
4039
}

0 commit comments

Comments
 (0)