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

Refactor dublin core utility #3756

Merged
merged 5 commits into from
Feb 23, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
176 changes: 0 additions & 176 deletions src/main/java/org/jabref/cli/XMPUtilMain.java

This file was deleted.

128 changes: 128 additions & 0 deletions src/main/java/org/jabref/cli/XmpUtilMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
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.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.jabref.logic.importer.fileformat.BibtexParser;
import org.jabref.logic.importer.ParserResult;

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.
*
* @param args
Copy link
Member

Choose a reason for hiding this comment

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

Remove empty parameter description 😇

*/
public static void main(String[] args) {
Copy link
Member

@Siedlerchr Siedlerchr Feb 20, 2018

Choose a reason for hiding this comment

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

Like the idea, but why not integrate it with the rest of our cli infrastruture?
Maybe as part of an "Importer" or Exporter?
http://help.jabref.org/en/CommandLine#import-file--i-filenameimport-format


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());
}
}
}
}