Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto relativize PDF file paths when selecting "Search for unlinked local files" #12088

Merged
merged 13 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,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 @@ -118,7 +119,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 preferences) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name the preferences variable filePreferences - also in the method importDatabase

try {
return new PdfMergeMetadataImporter(importFormatPreferences).importDatabase(file);
return new PdfMergeMetadataImporter(importFormatPreferences).importDatabase(file, context, preferences);
} catch (IOException e) {
return ParserResult.fromError(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,27 @@ 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}
* (Otherwise no path modification happens).
*
* @param filePath The unrelativized {@code filePath}.
*/
public ParserResult importDatabase(Path filePath, BibDatabaseContext context, FilePreferences preferences) throws IOException {
Objects.requireNonNull(context);
Objects.requireNonNull(preferences);

List<Path> directories = context.getFileDirectories(preferences);

if (!directories.isEmpty()) {
filePath = FileUtil.relativize(filePath, directories);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't that method also have a check for emptyness? Then, this call can be moved out of the if and the jf be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have just added a commit that does that

}

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);
}
}
Loading