diff --git a/CHANGELOG.md b/CHANGELOG.md index 27924dbf0db..09d60d7aa42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,8 @@ The new default removes the linked file from the entry instead of deleting the f - The magnifier icon at the search shows the [search mode](https://help.jabref.org/en/Search#search-modes) again. [#3535](https://github.com/JabRef/jabref/issues/3535) - We added a new cleanup operation that replaces ligatures with their expanded form. [#3613](https://github.com/JabRef/jabref/issues/3613) - Pressing ESC while searching will clear the search field and select the first entry, if available, in the table. [koppor#293](https://github.com/koppor/jabref/issues/293) +- We changed the metadata reading and writing. DublinCore is now the only metadata format, JabRef supports. (https://github.com/JabRef/jabref/pull/3710) +- We added another CLI functionality for reading and writing metadata to pdfs. (see https://github.com/JabRef/jabref/pull/3756 and see http://help.jabref.org/en/CommandLine) ### Fixed - We fixed several performance problems with the management of journal abbreviations [#3323](https://github.com/JabRef/jabref/issues/3323) diff --git a/src/main/java/org/jabref/cli/ArgumentProcessor.java b/src/main/java/org/jabref/cli/ArgumentProcessor.java index b5579040cbe..cd4e96ce523 100644 --- a/src/main/java/org/jabref/cli/ArgumentProcessor.java +++ b/src/main/java/org/jabref/cli/ArgumentProcessor.java @@ -223,6 +223,11 @@ private List processArguments() { doAuxImport(loaded); } + if (cli.isXmpFacilities()) { + XmpUtilMain.executeXmpConsoleApplicaton(); + System.exit(0); + } + return loaded; } diff --git a/src/main/java/org/jabref/cli/JabRefCLI.java b/src/main/java/org/jabref/cli/JabRefCLI.java index f427c8e0dbb..e6585516889 100644 --- a/src/main/java/org/jabref/cli/JabRefCLI.java +++ b/src/main/java/org/jabref/cli/JabRefCLI.java @@ -140,6 +140,10 @@ public String getExportMatches() { return cl.getOptionValue("exportMatches"); } + public boolean isXmpFacilities() { + return cl.hasOption("readAndWriteXmpMetadata"); + } + public boolean isGenerateBibtexKeys() { return cl.hasOption("generateBibtexKeys"); } public boolean isAutomaticallySetFileLinks() { return cl.hasOption("automaticallySetFileLinks"); } @@ -232,6 +236,11 @@ private Options getOptions() { desc(Localization.lang("Automatically set file links")). build()); + options.addOption(Option.builder("xmp"). + longOpt("readAndWriteXmpMetadata"). + desc("Read and write xmp metadata from/to pdf files"). + build()); + return options; } diff --git a/src/main/java/org/jabref/cli/XMPUtilMain.java b/src/main/java/org/jabref/cli/XMPUtilMain.java deleted file mode 100644 index 430c49541bd..00000000000 --- a/src/main/java/org/jabref/cli/XMPUtilMain.java +++ /dev/null @@ -1,176 +0,0 @@ -package org.jabref.cli; - -import java.io.FileReader; -import java.io.IOException; -import java.io.StringWriter; -import java.nio.file.Paths; -import java.util.Collection; -import java.util.List; -import java.util.Optional; - -import javax.xml.transform.TransformerException; - -import org.jabref.Globals; -import org.jabref.logic.bibtex.BibEntryWriter; -import org.jabref.logic.bibtex.LatexFieldFormatter; -import org.jabref.logic.importer.ImportFormatPreferences; -import org.jabref.logic.importer.ParserResult; -import org.jabref.logic.importer.fileformat.BibtexParser; -import org.jabref.logic.xmp.XmpPreferences; -import org.jabref.logic.xmp.XmpUtilReader; -import org.jabref.logic.xmp.XmpUtilWriter; -import org.jabref.model.database.BibDatabaseMode; -import org.jabref.model.entry.BibEntry; -import org.jabref.preferences.JabRefPreferences; - -import org.apache.xmpbox.XMPMetadata; -import org.apache.xmpbox.xml.XmpSerializer; - -public class XMPUtilMain { - - private XMPUtilMain() { - } - - /** - * Command-line tool for working with XMP-data. - * - * Read or write XMP-metadata from or to pdf file. - * - * Usage: - *
- *
Read from PDF and print as bibtex:
- *
xmpUtil PDF
- *
Read from PDF and print raw XMP:
- *
xmpUtil -x PDF
- *
Write the entry in BIB given by KEY to the PDF:
- *
xmpUtil KEY BIB PDF
- *
Write all entries in BIB to the PDF:
- *
xmpUtil BIB PDF
- *
- * - * @param args - * Command line strings passed to utility. - * @throws IOException - * If any of the given files could not be read or written. - * @throws TransformerException - * If the given BibEntry is malformed. - */ - public static void main(String[] args) throws IOException, TransformerException { - - // Don't forget to initialize the preferences - if (Globals.prefs == null) { - Globals.prefs = JabRefPreferences.getInstance(); - } - - XmpPreferences xmpPreferences = Globals.prefs.getXMPPreferences(); - ImportFormatPreferences importFormatPreferences = Globals.prefs.getImportFormatPreferences(); - - int argsLength = args.length; - if (argsLength == 0) { - usage(); - } else if (argsLength == 1) { - if (args[0].endsWith(".pdf")) { - // Read from pdf and write as BibTex - List l = XmpUtilReader.readXmp(args[0], xmpPreferences); - - BibEntryWriter bibtexEntryWriter = new BibEntryWriter( - new LatexFieldFormatter(Globals.prefs.getLatexFieldFormatterPreferences()), false); - - for (BibEntry entry : l) { - StringWriter sw = new StringWriter(); - bibtexEntryWriter.write(entry, sw, BibDatabaseMode.BIBTEX); - System.out.println(sw.getBuffer()); - } - - } else if (args[0].endsWith(".bib")) { - // Read from BIB and write as XMP - try (FileReader fr = new FileReader(args[0])) { - ParserResult result = new BibtexParser(importFormatPreferences, Globals.getFileUpdateMonitor()).parse(fr); - Collection entries = result.getDatabase().getEntries(); - - if (entries.isEmpty()) { - System.err.println("Could not find BibEntry in " + args[0]); - } - } - } else { - usage(); - } - } else if (argsLength == 2) { - if ("-x".equals(args[0]) && args[1].endsWith(".pdf")) { - // Read from pdf and write as BibTex - List meta = XmpUtilReader.readRawXmp(Paths.get(args[1])); - - if (!meta.isEmpty()) { - XmpSerializer serializer = new XmpSerializer(); - serializer.serialize(meta.get(0), System.out, true); - } else { - System.err.println("The given pdf does not contain any XMP-metadata."); - } - return; - } - - if (args[0].endsWith(".bib") && args[1].endsWith(".pdf")) { - try (FileReader reader = new FileReader(args[0])) { - ParserResult result = new BibtexParser(importFormatPreferences, Globals.getFileUpdateMonitor()).parse(reader); - - List entries = result.getDatabase().getEntries(); - - if (entries.isEmpty()) { - System.err.println("Could not find BibEntry in " + args[0]); - } else { - XmpUtilWriter.writeXmp(Paths.get(args[1]), entries, result.getDatabase(), xmpPreferences); - System.out.println("XMP written."); - } - } - return; - } - - usage(); - } else if (argsLength == 3) { - if (!args[1].endsWith(".bib") && !args[2].endsWith(".pdf")) { - usage(); - return; - } - - try (FileReader reader = new FileReader(args[1])) { - ParserResult result = new BibtexParser(importFormatPreferences, Globals.getFileUpdateMonitor()).parse(reader); - - Optional bibEntry = result.getDatabase().getEntryByKey(args[0]); - - if (bibEntry.isPresent()) { - XmpUtilWriter.writeXmp(Paths.get(args[2]), bibEntry.get(), result.getDatabase(), xmpPreferences); - - System.out.println("XMP written."); - } else { - System.err.println("Could not find BibEntry " + args[0] + " in " + args[0]); - } - } - } else { - usage(); - } - } - - /** - * Print usage information for the command line tool xmpUtil. - * - * @see XMPUtilMain#main(String[]) - */ - private static void usage() { - System.out.println("Read or write XMP-metadata from or to pdf file."); - System.out.println(""); - System.out.println("Usage:"); - System.out.println("Read from PDF and print as bibtex:"); - System.out.println(" xmpUtil "); - System.out.println("Read from PDF and print raw XMP:"); - System.out.println(" xmpUtil -x "); - System.out - .println("Write the entry in given by to the PDF:"); - System.out.println(" xmpUtil "); - System.out.println("Write all entries in to the PDF:"); - System.out.println(" xmpUtil "); - System.out.println(""); - System.out - .println("To report bugs visit https://issues.jabref.org"); - } - -} diff --git a/src/main/java/org/jabref/cli/XmpUtilMain.java b/src/main/java/org/jabref/cli/XmpUtilMain.java new file mode 100644 index 00000000000..5ff5ffc6a0d --- /dev/null +++ b/src/main/java/org/jabref/cli/XmpUtilMain.java @@ -0,0 +1,129 @@ +package org.jabref.cli; + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.StringWriter; +import java.nio.file.Paths; +import java.util.List; + +import javax.xml.transform.TransformerException; + +import org.jabref.Globals; +import org.jabref.logic.bibtex.BibEntryWriter; +import org.jabref.logic.bibtex.LatexFieldFormatter; +import org.jabref.logic.importer.ImportFormatPreferences; +import org.jabref.logic.importer.ParserResult; +import org.jabref.logic.importer.fileformat.BibtexParser; +import org.jabref.logic.xmp.XmpPreferences; +import org.jabref.logic.xmp.XmpUtilReader; +import org.jabref.logic.xmp.XmpUtilWriter; +import org.jabref.model.database.BibDatabaseMode; +import org.jabref.model.entry.BibEntry; +import org.jabref.preferences.JabRefPreferences; + +public class XmpUtilMain { + + private static XmpPreferences xmpPreferences; + private static ImportFormatPreferences importFormatPreferences; + + private XmpUtilMain() { + } + + /** + * Reads metadata from pdf and print all included bib entries to the console. + * + * @param filename Filename of the pdf file (.pdf) + */ + private static void readPdfAndPrintBib(String filename) throws IOException { + if (filename.endsWith(".pdf")) { + List entryList = XmpUtilReader.readXmp(filename, xmpPreferences); + + BibEntryWriter bibtexEntryWriter = new BibEntryWriter( + new LatexFieldFormatter(Globals.prefs.getLatexFieldFormatterPreferences()), false); + + for (BibEntry entry : entryList) { + StringWriter writer = new StringWriter(); + bibtexEntryWriter.write(entry, writer, BibDatabaseMode.BIBTEX); + System.out.println(writer.getBuffer()); + } + } else { + System.err.println("Insert a file path (.pdf)"); + } + } + + /** + * Writes all entries included in the bib file to the metadata section of the pdf file. + * + * @param bibFile Filename of the bib file (.bib) + * @param pdfFile Filename of the pdf file (.pdf) + */ + private static void writeBibFileToPdfMetadata(String bibFile, String pdfFile) throws FileNotFoundException, IOException, TransformerException { + if (bibFile.endsWith(".bib") && pdfFile.endsWith(".pdf")) { + try (FileReader reader = new FileReader(bibFile)) { + ParserResult result = new BibtexParser(importFormatPreferences, Globals.getFileUpdateMonitor()).parse(reader); + XmpUtilWriter.writeXmp(Paths.get(pdfFile), result.getDatabase().getEntries(), result.getDatabase(), xmpPreferences); + System.out.println("Metadata sucessfully written to Pdf."); + } + } else { + System.err.println("Insert correct file paths (.bib and .pdf)"); + } + } + + /** + * Print usage information for the console tool xmpUtil. + */ + private static void printMenu() { + System.out.println("---------------------Menu-----------------------"); + System.out.println("(0) Exit"); + System.out.println("(1) Read metadata from PDF and print as bibtex"); + System.out.println("(2) Write entries in bib file to Pdf metadata"); + System.out.println(" To report bugs visit https://issues.jabref.org"); + System.out.println("-------------------------------------------------"); + System.out.print("Choose an option: "); + } + + /** + * The tool is implemented as a console application with a read-evaluate-print cycle. + */ + public static void executeXmpConsoleApplicaton() { + if (Globals.prefs == null) { + Globals.prefs = JabRefPreferences.getInstance(); + } + + xmpPreferences = Globals.prefs.getXMPPreferences(); + importFormatPreferences = Globals.prefs.getImportFormatPreferences(); + + BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in)); + + int option = -1; + while (option != 0) { + try { + XmpUtilMain.printMenu(); + option = Integer.parseInt(consoleReader.readLine()); + + if (option == 0) { + break; + } else if (option == 1) { + System.out.print("Insert your filename (.pdf): "); + String filename = consoleReader.readLine().trim(); + XmpUtilMain.readPdfAndPrintBib(filename); + } else if (option == 2) { + System.out.print("Insert your filename (.bib): "); + String bibFile = consoleReader.readLine().trim(); + System.out.print("Insert your filename (.pdf): "); + String pdfFile = consoleReader.readLine().trim(); + XmpUtilMain.writeBibFileToPdfMetadata(bibFile, pdfFile); + } + } catch (IOException | TransformerException e) { + System.err.println(e.getMessage()); + } + } + } + + public static void main(String[] args) { + XmpUtilMain.executeXmpConsoleApplicaton(); + } +}