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

CLI option to write XMP metadata to pdfs #7814

Merged
merged 15 commits into from
Jun 21, 2021
122 changes: 122 additions & 0 deletions src/main/java/org/jabref/cli/ArgumentProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.Set;
import java.util.Vector;
import java.util.prefs.BackingStoreException;

import org.jabref.gui.Globals;
Expand All @@ -24,6 +27,7 @@
import org.jabref.logic.exporter.ExporterFactory;
import org.jabref.logic.exporter.SavePreferences;
import org.jabref.logic.exporter.TemplateExporter;
import org.jabref.logic.exporter.XmpPdfExporter;
import org.jabref.logic.importer.FetcherException;
import org.jabref.logic.importer.ImportException;
import org.jabref.logic.importer.ImportFormatReader;
Expand All @@ -47,8 +51,11 @@
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.database.BibDatabaseMode;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.LinkedFile;
import org.jabref.model.strings.StringUtil;
import org.jabref.model.util.DummyFileUpdateMonitor;
import org.jabref.model.util.FileHelper;
import org.jabref.preferences.JabRefPreferences;
import org.jabref.preferences.SearchPreferences;

import com.google.common.base.Throwables;
Expand Down Expand Up @@ -215,6 +222,13 @@ private List<ParserResult> processArguments() {
automaticallySetFileLinks(loaded);
}

if (cli.isWriteXMPtoPdf()) {
System.out.println("writing for " + cli.getWriteXMPtoPdf());
if (!loaded.isEmpty()) {
writeXMPtoPdf(loaded, cli.getWriteXMPtoPdf(), Globals.prefs);
calixtus marked this conversation as resolved.
Show resolved Hide resolved
}
}

if (cli.isFileExport()) {
if (!loaded.isEmpty()) {
exportFile(loaded, cli.getFileExport().split(","));
Expand All @@ -239,6 +253,114 @@ private List<ParserResult> processArguments() {
return loaded;
}

private void writeXMPtoPdf(List<ParserResult> loaded, String filesAndCitekeys, JabRefPreferences preferences) {

ParserResult pr = loaded.get(loaded.size() - 1);
btut marked this conversation as resolved.
Show resolved Hide resolved
BibDatabaseContext databaseContext = pr.getDatabaseContext();
BibDatabase dataBase = pr.getDatabase();

XmpPdfExporter xmpPdfExporter = new XmpPdfExporter(preferences.getXmpPreferences());

if (filesAndCitekeys.equals("all")) {
btut marked this conversation as resolved.
Show resolved Hide resolved
for (BibEntry entry : dataBase.getEntries()) {
writeXMPtoPDFsOfEntry(databaseContext, dataBase, entry, preferences, xmpPdfExporter);
}
return;
}

Vector<String> citeKeys = new Vector<>();
Vector<String> pdfs = new Vector<>();
for (String fileOrCiteKey : filesAndCitekeys.split(",")) {
if (fileOrCiteKey.toLowerCase(Locale.ROOT).endsWith(".pdf")) {
pdfs.add(fileOrCiteKey);
} else {
citeKeys.add(fileOrCiteKey);
}
}

writeXMPtoPdfByCitekey(databaseContext, dataBase, citeKeys, preferences, xmpPdfExporter);
writeXMPtoPdfByFileNames(databaseContext, dataBase, pdfs, preferences, xmpPdfExporter);

}

private void writeXMPtoPDFsOfEntry(BibDatabaseContext databaseContext, BibDatabase dataBase, BibEntry entry, JabRefPreferences preferences, XmpPdfExporter xmpPdfExporter) {
boolean writtenToAFile = false;

for (LinkedFile file : entry.getFiles()) {
if (file.getFileType().toLowerCase(Locale.ROOT).equals("pdf")) {
Optional<Path> filePath = file.findIn(databaseContext, preferences.getFilePreferences());
if (filePath.isEmpty()) {
System.err.println(Localization.lang("Skipped - PDF %0 does not exist", filePath.get()));
btut marked this conversation as resolved.
Show resolved Hide resolved
} else {
try {
xmpPdfExporter.export(databaseContext, filePath.get(), preferences.getDefaultEncoding(), Arrays.asList(entry));
writtenToAFile = true;
} catch (Exception e) {
System.err.println(Localization.lang("Error writing XMP content: %0", filePath.get()));
}
}
}
}

if (!writtenToAFile) {
System.err.println(Localization.lang("Skipped - No PDF linked to entry %0", entry.getCitationKey()));
}
}

private void writeXMPtoPdfByCitekey(BibDatabaseContext databaseContext, BibDatabase dataBase, Vector<String> citeKeys, JabRefPreferences preferences, XmpPdfExporter xmpPdfExporter) {
for (String citeKey : citeKeys) {
Optional<BibEntry> entry = dataBase.getEntryByCitationKey(citeKey);
btut marked this conversation as resolved.
Show resolved Hide resolved
if (entry.isEmpty()) {
System.err.println(Localization.lang("Cannot find %0 in library.", citeKey));
return;
btut marked this conversation as resolved.
Show resolved Hide resolved
}
writeXMPtoPDFsOfEntry(databaseContext, dataBase, entry.get(), preferences, xmpPdfExporter);
}
btut marked this conversation as resolved.
Show resolved Hide resolved
}

private void writeXMPtoPdfByFileNames(BibDatabaseContext databaseContext, BibDatabase dataBase, Vector<String> fileNames, JabRefPreferences preferences, XmpPdfExporter xmpPdfExporter) {
List<Path> dirs = databaseContext.getFileDirectories(preferences.getFilePreferences());
dirs.add(Path.of("").toAbsolutePath());

for (String fileName : fileNames) {
Path filePath = Path.of(fileName);
if (!filePath.isAbsolute()) {
Optional<Path> optionalFilePath = FileHelper.find(fileName, dirs);
if (!optionalFilePath.isEmpty()) {
filePath = optionalFilePath.get();
}
}
System.out.println(filePath);
if (Files.exists(filePath)) {
boolean writtenToAFile = false;
for (BibEntry entry : dataBase.getEntries()) {
for (LinkedFile linkedFile : entry.getFiles()) {
if (linkedFile.getFileType().toLowerCase(Locale.ROOT).equals("pdf")) {
Optional<Path> linkedFilePath = linkedFile.findIn(dirs);
try {
if (!linkedFilePath.isEmpty() && Files.isSameFile(linkedFilePath.get(), filePath)) {
try {
xmpPdfExporter.export(databaseContext, filePath, preferences.getDefaultEncoding(), Arrays.asList(entry));
writtenToAFile = true;
} catch (Exception e) {
System.err.println(Localization.lang("Error writing XMP content: %0", filePath));
}
}
} catch (IOException e) {
System.err.println(Localization.lang("Error opening %0 and %1 for comparison", linkedFile.getLink(), fileName));
}
}
}
}
if (!writtenToAFile) {
System.err.println(Localization.lang("Skipped - PDF %0 not found in library", fileName));
}
} else {
System.err.println(Localization.lang("Skipped - PDF %0 does not exist", fileName));
}
}
}

private boolean exportMatches(List<ParserResult> loaded) {
String[] data = cli.getExportMatches().split(",");
String searchTerm = data[0].replace("\\$", " "); // enables blanks within the search term:
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/org/jabref/cli/JabRefCLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ public boolean isAutomaticallySetFileLinks() {
return cl.hasOption("automaticallySetFileLinks");
}

public boolean isWriteXMPtoPdf() {
return cl.hasOption("writeXMPtoPdf");
}

public String getWriteXMPtoPdf() {
return cl.getOptionValue("writeXMPtoPdf");
}

private static Options getOptions() {
Options options = new Options();

Expand Down Expand Up @@ -242,6 +250,14 @@ private static Options getOptions() {
.argName("KEY1[,KEY2][,KEYn] | all")
.build());

options.addOption(Option
.builder("w")
.longOpt("writeXMPtoPdf")
.desc(String.format("%s: '%s'", Localization.lang("Write BibTeXEntry as XMP metadata to PDF."), "-w pathToMyOwnPaper.pdf"))
.hasArg()
.argName("CITEKEY1[,CITEKEY2][,CITEKEYn] | PDF1[,PDF2][,PDFn] | all")
.build());

return options;
}

Expand Down
8 changes: 8 additions & 0 deletions src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ Cannot\ create\ group=Cannot create group

Cannot\ create\ group.\ Please\ create\ a\ library\ first.=Cannot create group. Please create a library first.

Cannot\ find\ %0\ in\ library.=Cannot find %0 in library.

Cannot\ open\ folder\ as\ the\ file\ is\ an\ online\ link.=Cannot open folder as the file is an online link.

case\ insensitive=case insensitive
Expand Down Expand Up @@ -794,10 +796,15 @@ Since\ the\ 'Review'\ field\ was\ deprecated\ in\ JabRef\ 4.2,\ these\ two\ fiel
Size=Size

Skipped\ -\ No\ PDF\ linked=Skipped - No PDF linked
Skipped\ -\ No\ PDF\ linked\ to\ entry\ %0=Skipped - No PDF linked to entry %0
Skipped\ -\ PDF\ does\ not\ exist=Skipped - PDF does not exist
Skipped\ -\ PDF\ %0\ does\ not\ exist=Skipped - PDF %0 does not exist
Skipped\ -\ PDF\ %0\ not\ found\ in\ library=Skipped - PDF %0 not found in library

Skipped\ entry.=Skipped entry.

Error\ opening\ %0\ and\ %1\ for\ comparison=Error opening %0 and %1 for comparison

source\ edit=source edit
Special\ name\ formatters=Special name formatters

Expand Down Expand Up @@ -2274,6 +2281,7 @@ Error\ importing.\ See\ the\ error\ log\ for\ details.=Error importing. See the
Error\ from\ import\:\ %0=Error from import\: %0
Error\ reading\ PDF\ content\:\ %0=Error reading PDF content\: %0
Error\ reading\ XMP\ content\:\ %0=Error reading XMP content\: %0
Error\ writing\ XMP\ content\:\ %0=Error writing XMP content\: %0
Importing\ bib\ entry=Importing bib entry
Importing\ using\ XMP\ data...=Importing using XMP data...
Importing\ using\ extracted\ PDF\ data=Importing using extracted PDF data
Expand Down