Skip to content

Commit

Permalink
Auto relativize PDF file paths when selecting "Search for unlinked lo…
Browse files Browse the repository at this point in the history
…cal files" (JabRef#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 <[email protected]>
Co-authored-by: Subhramit Basu Bhowmick <[email protected]>
  • Loading branch information
3 people authored Oct 27, 2024
1 parent 28fb91e commit b9f3951
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 5 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -127,7 +128,7 @@ public List<ImportFilesResultItemViewModel> call() {

try {
if (FileUtil.isPDFFile(file)) {
var pdfImporterResult = contentImporter.importPDFContent(file);
ParserResult pdfImporterResult = contentImporter.importPDFContent(file, bibDatabaseContext, filePreferences);
List<BibEntry> pdfEntriesInFile = pdfImporterResult.getDatabase().getEntries();

if (pdfImporterResult.hasWarnings()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Path> directories = context.getFileDirectories(filePreferences);

filePath = FileUtil.relativize(filePath, directories);

return importDatabase(filePath);
}

@Override
public String getName() {
return "PDF meta data merger";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<BibEntry> 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);
}
}

0 comments on commit b9f3951

Please sign in to comment.