Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -118,7 +118,7 @@ public List<ImportFilesResultItemViewModel> call() {

try {
if (FileUtil.isPDFFile(file)) {
var pdfImporterResult = contentImporter.importPDFContent(file);
var pdfImporterResult = contentImporter.importPDFContent(file, bibDatabaseContext, filePreferences);
Copy link
Member

Choose a reason for hiding this comment

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

Can you change this to the strict returned type here?

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,29 @@ 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}.
* @param context Everything related to the BIB file
* @param preferences Preferences for linked files
Copy link
Member

@subhramit subhramit Oct 26, 2024

Choose a reason for hiding this comment

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

146 - To be more precise, context is the metadata for the library. However, it's a common JabRef parameter, so can remove it.
147 - Can remove as well, as the type of the parameter makes the description redundant.

*/
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,28 @@ 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);

// Set up results
List<BibEntry> result = importer.importDatabase(file, database, preferences).getDatabase().getEntries();

// Set up expected value
BibEntry expected = new BibEntry(StandardEntryType.InProceedings)
Copy link
Member

@subhramit subhramit Oct 26, 2024

Choose a reason for hiding this comment

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

Remove these two comments (redundant).

.withField(StandardField.AUTHOR, "1 ")
.withField(StandardField.TITLE, "Hello World")
// Expecting relative path
.withField(StandardField.FILE, ":minimal.pdf:PDF");

assertEquals(List.of(expected), result);
}
}