-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Auto relativize PDF file paths when selecting "Search for unlinked local files" #12088
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
Changes from 2 commits
d798ddf
eb8722b
4484154
8ea772c
5865d20
3aeecd2
b57bf68
019500f
de348cc
2e01a24
936787f
098c713
ae55866
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 preferences) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Name the |
||
try { | ||
return new PdfMergeMetadataImporter(importFormatPreferences).importDatabase(file); | ||
return new PdfMergeMetadataImporter(importFormatPreferences).importDatabase(file, context, preferences); | ||
} catch (IOException e) { | ||
return ParserResult.fromError(e); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
*/ | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} |
There was a problem hiding this comment.
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?