From b9f395118eaa21168855a9bc6eab27198bacef7d Mon Sep 17 00:00:00 2001 From: u7676493 Date: Mon, 28 Oct 2024 10:05:36 +1100 Subject: [PATCH] Auto relativize PDF file paths when selecting "Search for unlinked local files" (#12088) * When using "Find Unlinked Files" to import files, they will now be relativized. * Removes debug lines * Removes debug lines * Uses the strict return type on pdfImporterResult Removes unnecessary comments * Renamed preferences to filePreferences in importDatabase and importPDFContent * Update CHANGELOG.md to include changes * Update description of PdfMergeMetadataImporter.importDatabase It now correctly states which function is called to produce the return value * Update PdfMergeMetadataImporter.importDatabase Removed unnecessary if statement --------- Co-authored-by: Oliver Kopp Co-authored-by: Subhramit Basu Bhowmick --- CHANGELOG.md | 5 ++-- .../gui/externalfiles/ImportHandler.java | 3 ++- .../ExternalFilesContentImporter.java | 6 +++-- .../fileformat/PdfMergeMetadataImporter.java | 19 +++++++++++++++ .../PdfMergeMetadataImporterTest.java | 24 +++++++++++++++++++ 5 files changed, 52 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1be5603233f..8f4e08081bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -69,8 +69,9 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv - We changed instances of 'Search Selected' to 'Search Pre-configured' in Web Search Preferences UI. [#11871](https://github.com/JabRef/jabref/pull/11871) - We added a new CSS style class `main-table` for the main table. [#11881](https://github.com/JabRef/jabref/pull/11881) - When renaming a file, the old extension is now used if there is none provided in the new name. [#11903](https://github.com/JabRef/jabref/issues/11903) -- Added minimum window sizing for windows dedicated to creating new entries [#11944](https://github.com/JabRef/jabref/issues/11944) -- We changed the name of the library-based file directory from 'General File Directory' to 'Library-specific File Directory' per issue [#571](https://github.com/koppor/jabref/issues/571) +- When importing a file using "Find Unlinked Files", when one or more file directories are available, the file path will be relativized where possible [koppor#549](https://github.com/koppor/jabref/issues/549) +- We added minimum window sizing for windows dedicated to creating new entries [#11944](https://github.com/JabRef/jabref/issues/11944) +- We changed the name of the library-based file directory from 'General File Directory' to 'Library-specific File Directory' per issue. [#571](https://github.com/koppor/jabref/issues/571) - The CitationKey column is now a default shown column for the entry table. [#10510](https://github.com/JabRef/jabref/issues/10510) ### Fixed diff --git a/src/main/java/org/jabref/gui/externalfiles/ImportHandler.java b/src/main/java/org/jabref/gui/externalfiles/ImportHandler.java index 9be3e860390..ef6774c358d 100644 --- a/src/main/java/org/jabref/gui/externalfiles/ImportHandler.java +++ b/src/main/java/org/jabref/gui/externalfiles/ImportHandler.java @@ -35,6 +35,7 @@ import org.jabref.logic.importer.ImportFormatReader; import org.jabref.logic.importer.ImportFormatReader.UnknownFormatImport; import org.jabref.logic.importer.ParseException; +import org.jabref.logic.importer.ParserResult; import org.jabref.logic.importer.fileformat.BibtexParser; import org.jabref.logic.l10n.Localization; import org.jabref.logic.util.BackgroundTask; @@ -127,7 +128,7 @@ public List call() { try { if (FileUtil.isPDFFile(file)) { - var pdfImporterResult = contentImporter.importPDFContent(file); + ParserResult pdfImporterResult = contentImporter.importPDFContent(file, bibDatabaseContext, filePreferences); List pdfEntriesInFile = pdfImporterResult.getDatabase().getEntries(); if (pdfImporterResult.hasWarnings()) { diff --git a/src/main/java/org/jabref/logic/externalfiles/ExternalFilesContentImporter.java b/src/main/java/org/jabref/logic/externalfiles/ExternalFilesContentImporter.java index e75b1df2683..7a868577c42 100644 --- a/src/main/java/org/jabref/logic/externalfiles/ExternalFilesContentImporter.java +++ b/src/main/java/org/jabref/logic/externalfiles/ExternalFilesContentImporter.java @@ -3,10 +3,12 @@ import java.io.IOException; import java.nio.file.Path; +import org.jabref.logic.FilePreferences; import org.jabref.logic.importer.ImportFormatPreferences; import org.jabref.logic.importer.OpenDatabase; import org.jabref.logic.importer.ParserResult; import org.jabref.logic.importer.fileformat.PdfMergeMetadataImporter; +import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.util.FileUpdateMonitor; public class ExternalFilesContentImporter { @@ -17,9 +19,9 @@ public ExternalFilesContentImporter(ImportFormatPreferences importFormatPreferen this.importFormatPreferences = importFormatPreferences; } - public ParserResult importPDFContent(Path file) { + public ParserResult importPDFContent(Path file, BibDatabaseContext context, FilePreferences filePreferences) { try { - return new PdfMergeMetadataImporter(importFormatPreferences).importDatabase(file); + return new PdfMergeMetadataImporter(importFormatPreferences).importDatabase(file, context, filePreferences); } catch (IOException e) { return ParserResult.fromError(e); } diff --git a/src/main/java/org/jabref/logic/importer/fileformat/PdfMergeMetadataImporter.java b/src/main/java/org/jabref/logic/importer/fileformat/PdfMergeMetadataImporter.java index da023360ad0..21b18e27f84 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/PdfMergeMetadataImporter.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/PdfMergeMetadataImporter.java @@ -136,6 +136,25 @@ public ParserResult importDatabase(Path filePath) throws IOException { return new ParserResult(List.of(entry)); } + /** + * A modified version of {@link PdfMergeMetadataImporter#importDatabase(Path)}, but it + * relativizes the {@code filePath} if there are working directories before parsing it + * into {@link PdfMergeMetadataImporter#importDatabase(Path)} + * (Otherwise no path modification happens). + * + * @param filePath The unrelativized {@code filePath}. + */ + public ParserResult importDatabase(Path filePath, BibDatabaseContext context, FilePreferences filePreferences) throws IOException { + Objects.requireNonNull(context); + Objects.requireNonNull(filePreferences); + + List directories = context.getFileDirectories(filePreferences); + + filePath = FileUtil.relativize(filePath, directories); + + return importDatabase(filePath); + } + @Override public String getName() { return "PDF meta data merger"; diff --git a/src/test/java/org/jabref/logic/importer/fileformat/PdfMergeMetadataImporterTest.java b/src/test/java/org/jabref/logic/importer/fileformat/PdfMergeMetadataImporterTest.java index 3b2f561edd6..ca6c3d42dd5 100644 --- a/src/test/java/org/jabref/logic/importer/fileformat/PdfMergeMetadataImporterTest.java +++ b/src/test/java/org/jabref/logic/importer/fileformat/PdfMergeMetadataImporterTest.java @@ -6,9 +6,11 @@ import javafx.collections.FXCollections; +import org.jabref.logic.FilePreferences; import org.jabref.logic.importer.ImportFormatPreferences; import org.jabref.logic.importer.util.GrobidPreferences; import org.jabref.logic.util.StandardFileType; +import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.LinkedFile; import org.jabref.model.entry.field.StandardField; @@ -84,4 +86,26 @@ void importWorksAsExpected() throws Exception { assertEquals(Collections.singletonList(expected), result); } + + @Test + void importRelativizesFilePath() throws Exception { + // Initialize database and preferences + FilePreferences preferences = mock(FilePreferences.class); + BibDatabaseContext database = new BibDatabaseContext(); + + // Initialize file and working directory + Path file = Path.of(PdfMergeMetadataImporter.class.getResource("/pdfs/minimal.pdf").toURI()); + Path directory = Path.of(PdfMergeMetadataImporter.class.getResource("/pdfs/").toURI()); + preferences.setWorkingDirectory(directory); + + List result = importer.importDatabase(file, database, preferences).getDatabase().getEntries(); + + BibEntry expected = new BibEntry(StandardEntryType.InProceedings) + .withField(StandardField.AUTHOR, "1 ") + .withField(StandardField.TITLE, "Hello World") + // Expecting relative path + .withField(StandardField.FILE, ":minimal.pdf:PDF"); + + assertEquals(List.of(expected), result); + } }