Skip to content

Commit

Permalink
Refactor dublin core utility (#3756)
Browse files Browse the repository at this point in the history
* Refactored dublin core utility

The tool is now implemented as a console application with a read-evaluate-print cycle.

* CLI -xmp integration

Integrated the xmp functionality in the CLI infrastructure

* CLI - shutdown after using console application

System.exit() because Platform.exit() does not work in this case.

* Update changelog.md
  • Loading branch information
johannes-manner authored and Siedlerchr committed Feb 23, 2018
1 parent fc5d250 commit 4ddd90a
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 176 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,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 <kbd>ESC</kbd> 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)
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/jabref/cli/ArgumentProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ private List<ParserResult> processArguments() {
doAuxImport(loaded);
}

if (cli.isXmpFacilities()) {
XmpUtilMain.executeXmpConsoleApplicaton();
System.exit(0);
}

return loaded;
}

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/jabref/cli/JabRefCLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"); }
Expand Down Expand Up @@ -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;
}

Expand Down
176 changes: 0 additions & 176 deletions src/main/java/org/jabref/cli/XMPUtilMain.java

This file was deleted.

129 changes: 129 additions & 0 deletions src/main/java/org/jabref/cli/XmpUtilMain.java
Original file line number Diff line number Diff line change
@@ -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<BibEntry> 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();
}
}

0 comments on commit 4ddd90a

Please sign in to comment.