-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Siedlerchr
merged 5 commits into
JabRef:master
from
johannes-manner:dublin-core-utility
Feb 23, 2018
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f1e3d6e
Refactored dublin core utility
johannes-manner 9ab476b
CLI -xmp integration
johannes-manner 623dbf1
CLI - shutdown after using console application
johannes-manner 96db578
Merge branch 'master' of https://github.com/JabRef/jabref into dublin…
johannes-manner 5be375d
Update changelog.md
johannes-manner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do we actually need this tool as a separate executable now that the functionality is included in the usual CLI?
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.
It's a convenient tool to add metadata to pdf files. If there is the possiblity to build a standalone jar with the multi-model build, why not use the chance to offer this tool to more users and make this tool more popular?