Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Moved the main part of XMPUtil to jabref.XMPUtilMain and injected a b… #1642

Merged
merged 4 commits into from
Aug 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
175 changes: 175 additions & 0 deletions src/main/java/net/sf/jabref/cli/XMPUtilMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package net.sf.jabref.cli;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

import javax.xml.transform.TransformerException;

import net.sf.jabref.Globals;
import net.sf.jabref.importer.ParserResult;
import net.sf.jabref.importer.fileformat.BibtexParser;
import net.sf.jabref.logic.bibtex.BibEntryWriter;
import net.sf.jabref.logic.bibtex.LatexFieldFormatter;
import net.sf.jabref.logic.bibtex.LatexFieldFormatterPreferences;
import net.sf.jabref.logic.xmp.XMPPreferences;
import net.sf.jabref.logic.xmp.XMPUtil;
import net.sf.jabref.model.database.BibDatabaseMode;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.preferences.JabRefPreferences;

import org.apache.jempbox.impl.XMLUtil;
import org.apache.jempbox.xmp.XMPMetadata;

public class XMPUtilMain {

/**
* Command-line tool for working with XMP-data.
*
* Read or write XMP-metadata from or to pdf file.
*
* Usage:
* <dl>
* <dd>Read from PDF and print as bibtex:</dd>
* <dt>xmpUtil PDF</dt>
* <dd>Read from PDF and print raw XMP:</dd>
* <dt>xmpUtil -x PDF</dt>
* <dd>Write the entry in BIB given by KEY to the PDF:</dd>
* <dt>xmpUtil KEY BIB PDF</dt>
* <dd>Write all entries in BIB to the PDF:</dd>
* <dt>xmpUtil BIB PDF</dt>
* </dl>
*
* @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 = XMPPreferences.fromPreferences(Globals.prefs);

switch (args.length) {
case 0:
usage();
break;
case 1:

if (args[0].endsWith(".pdf")) {
// Read from pdf and write as BibTex
List<BibEntry> l = XMPUtil.readXMP(new File(args[0]), xmpPreferences);

BibEntryWriter bibtexEntryWriter = new BibEntryWriter(
new LatexFieldFormatter(LatexFieldFormatterPreferences.fromPreferences(Globals.prefs)), 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 = BibtexParser.parse(fr);
Collection<BibEntry> entries = result.getDatabase().getEntries();

if (entries.isEmpty()) {
System.err.println("Could not find BibEntry in " + args[0]);
} else {
System.out.println(XMPUtil.toXMP(entries, result.getDatabase(), xmpPreferences));
}
}
} else {
usage();
}
break;
case 2:
if ("-x".equals(args[0]) && args[1].endsWith(".pdf")) {
// Read from pdf and write as BibTex
Optional<XMPMetadata> meta = XMPUtil.readRawXMP(new File(args[1]));

if (meta.isPresent()) {
XMLUtil.save(meta.get().getXMPDocument(), System.out, StandardCharsets.UTF_8.name());
} else {
System.err.println("The given pdf does not contain any XMP-metadata.");
}
break;
}

if (args[0].endsWith(".bib") && args[1].endsWith(".pdf")) {
ParserResult result = BibtexParser.parse(new FileReader(args[0]));

Collection<BibEntry> entries = result.getDatabase().getEntries();

if (entries.isEmpty()) {
System.err.println("Could not find BibEntry in " + args[0]);
} else {
XMPUtil.writeXMP(new File(args[1]), entries, result.getDatabase(), false, xmpPreferences);
System.out.println("XMP written.");
}
break;
}

usage();
break;
case 3:
if (!args[1].endsWith(".bib") && !args[2].endsWith(".pdf")) {
usage();
break;
}

ParserResult result = BibtexParser.parse(new FileReader(args[1]));

Optional<BibEntry> bibEntry = result.getDatabase().getEntryByKey(args[0]);

if (bibEntry.isPresent()) {
XMPUtil.writeXMP(new File(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]);
}
break;

default:
usage();
}
}

/**
* Print usage information for the command line tool xmpUtil.
*
* @see XMPUtil#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 <pdf>");
System.out.println("Read from PDF and print raw XMP:");
System.out.println(" xmpUtil -x <pdf>");
System.out
.println("Write the entry in <bib> given by <key> to the PDF:");
System.out.println(" xmpUtil <key> <bib> <pdf>");
System.out.println("Write all entries in <bib> to the PDF:");
System.out.println(" xmpUtil <bib> <pdf>");
System.out.println("");
System.out
.println("To report bugs visit http://jabref.sourceforge.net");
}

}
3 changes: 2 additions & 1 deletion src/main/java/net/sf/jabref/external/DroppedFileHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.util.OS;
import net.sf.jabref.logic.util.io.FileUtil;
import net.sf.jabref.logic.xmp.XMPPreferences;
import net.sf.jabref.logic.xmp.XMPUtil;
import net.sf.jabref.model.database.BibDatabase;
import net.sf.jabref.model.entry.BibEntry;
Expand Down Expand Up @@ -235,7 +236,7 @@ private boolean tryXmpImport(String fileName, ExternalFileType fileType, NamedCo

List<BibEntry> xmpEntriesInFile;
try {
xmpEntriesInFile = XMPUtil.readXMP(fileName, Globals.prefs);
xmpEntriesInFile = XMPUtil.readXMP(fileName, XMPPreferences.fromPreferences(Globals.prefs));
} catch (IOException e) {
LOGGER.warn("Problem reading XMP", e);
return false;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/sf/jabref/external/WriteXMPAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import net.sf.jabref.gui.worker.AbstractWorker;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.util.io.FileUtil;
import net.sf.jabref.logic.xmp.XMPPreferences;
import net.sf.jabref.logic.xmp.XMPUtil;
import net.sf.jabref.model.database.BibDatabase;
import net.sf.jabref.model.entry.BibEntry;
Expand Down Expand Up @@ -159,7 +160,7 @@ public void run() {
for (File file : files) {
if (file.exists()) {
try {
XMPUtil.writeXMP(file, entry, database);
XMPUtil.writeXMP(file, entry, database, XMPPreferences.fromPreferences(Globals.prefs));
optDiag.getProgressArea().append(" " + Localization.lang("OK") + ".\n");
entriesChanged++;
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import javax.swing.Action;
import javax.xml.transform.TransformerException;

import net.sf.jabref.Globals;
import net.sf.jabref.gui.BasePanel;
import net.sf.jabref.gui.FileListEntry;
import net.sf.jabref.gui.FileListTableModel;
Expand All @@ -33,6 +34,7 @@
import net.sf.jabref.gui.worker.AbstractWorker;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.util.io.FileUtil;
import net.sf.jabref.logic.xmp.XMPPreferences;
import net.sf.jabref.logic.xmp.XMPUtil;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.FieldName;
Expand Down Expand Up @@ -126,7 +128,8 @@ public void run() {

} else {
try {
XMPUtil.writeXMP(file, entry, panel.getDatabase());
XMPUtil.writeXMP(file, entry, panel.getDatabase(),
XMPPreferences.fromPreferences(Globals.prefs));
if (files.size() == 1) {
message = Localization.lang("Wrote XMP-metadata");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ private void setupToolBar() {
leftPan.add(closeBut, BorderLayout.NORTH);

// Create type-label
TypedBibEntry typedEntry = new TypedBibEntry(entry, Optional.empty(), panel.getBibDatabaseContext().getMode());
TypedBibEntry typedEntry = new TypedBibEntry(entry, panel.getBibDatabaseContext().getMode());
leftPan.add(new TypeLabel(typedEntry.getTypeForDisplay()), BorderLayout.CENTER);
TypeButton typeButton = new TypeButton();

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/sf/jabref/gui/maintable/MainTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ private boolean matches(int row, Matcher<BibEntry> m) {
private boolean isComplete(int row) {
try {
BibEntry entry = getBibEntry(row);
TypedBibEntry typedEntry = new TypedBibEntry(entry, Optional.of(panel.getDatabase()), panel.getBibDatabaseContext().getMode());
TypedBibEntry typedEntry = new TypedBibEntry(entry, panel.getBibDatabaseContext());
return typedEntry.hasAllRequiredFields();
} catch (NullPointerException ex) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package net.sf.jabref.gui.util.comparator;

import java.util.Comparator;
import java.util.Optional;

import net.sf.jabref.BibDatabaseContext;
import net.sf.jabref.logic.TypedBibEntry;
Expand All @@ -35,8 +34,8 @@ public int compare(BibEntry e1, BibEntry e2) {
int score1 = 0;
int score2 = 0;

TypedBibEntry typedEntry1 = new TypedBibEntry(e1, Optional.of(database.getDatabase()), database.getMode());
TypedBibEntry typedEntry2 = new TypedBibEntry(e2, Optional.of(database.getDatabase()), database.getMode());
TypedBibEntry typedEntry1 = new TypedBibEntry(e1, database);
TypedBibEntry typedEntry2 = new TypedBibEntry(e2, database);
if (typedEntry1.hasAllRequiredFields()) {
score1++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.sf.jabref.external.ExternalFileType;
import net.sf.jabref.external.ExternalFileTypes;
import net.sf.jabref.gui.IconTheme;
import net.sf.jabref.logic.xmp.XMPPreferences;
import net.sf.jabref.logic.xmp.XMPUtil;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.pdfimport.PdfImporter;
Expand Down Expand Up @@ -122,7 +123,8 @@ private void addEntryDataFromPDDocumentInformation(File pdfFile, BibEntry entry)
*/
private void addEntryDataFromXMP(File aFile, BibEntry entry) {
try {
List<BibEntry> entrys = XMPUtil.readXMP(aFile.getAbsoluteFile(), Globals.prefs);
List<BibEntry> entrys = XMPUtil.readXMP(aFile.getAbsoluteFile(),
XMPPreferences.fromPreferences(Globals.prefs));
addEntrysToEntry(entry, entrys);
} catch (IOException e) {
// no canceling here, just no data added.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import net.sf.jabref.Globals;
import net.sf.jabref.importer.ParserResult;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.xmp.XMPPreferences;
import net.sf.jabref.logic.xmp.XMPUtil;

/**
Expand Down Expand Up @@ -55,7 +56,7 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException {
public ParserResult importDatabase(Path filePath, Charset defaultEncoding) {
Objects.requireNonNull(filePath);
try {
return new ParserResult(XMPUtil.readXMP(filePath, Globals.prefs));
return new ParserResult(XMPUtil.readXMP(filePath, XMPPreferences.fromPreferences(Globals.prefs)));
} catch (IOException exception) {
return ParserResult.fromErrorMessage(exception.getLocalizedMessage());
}
Expand All @@ -76,7 +77,7 @@ protected boolean isRecognizedFormat(BufferedReader reader) throws IOException {
@Override
public boolean isRecognizedFormat(Path filePath, Charset defaultEncoding) throws IOException {
Objects.requireNonNull(filePath);
return XMPUtil.hasMetadata(filePath, Globals.prefs);
return XMPUtil.hasMetadata(filePath, XMPPreferences.fromPreferences(Globals.prefs));
}

@Override
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/net/sf/jabref/logic/TypedBibEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,14 @@ public TypedBibEntry(BibEntry entry, BibDatabaseMode mode) {
this(entry, Optional.empty(), mode);
}

public TypedBibEntry(BibEntry entry, Optional<BibDatabase> database, BibDatabaseMode mode) {
private TypedBibEntry(BibEntry entry, Optional<BibDatabase> database, BibDatabaseMode mode) {
this.entry = Objects.requireNonNull(entry);
this.database = Objects.requireNonNull(database);
this.mode = mode;
}

public TypedBibEntry(BibEntry entry, BibDatabaseContext databaseContext) {
this(entry, databaseContext.getDatabase(), databaseContext.getMode());
}

public TypedBibEntry(BibEntry entry, BibDatabase database, BibDatabaseMode mode) {
this(entry, Optional.of(database), mode);
this(entry, Optional.of(databaseContext.getDatabase()), databaseContext.getMode());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void writeWithoutPrependedNewlines(BibEntry entry, Writer out, BibDatabas
private void writeRequiredFieldsFirstRemainingFieldsSecond(BibEntry entry, Writer out,
BibDatabaseMode bibDatabaseMode) throws IOException {
// Write header with type and bibtex-key.
TypedBibEntry typedEntry = new TypedBibEntry(entry, Optional.empty(), bibDatabaseMode);
TypedBibEntry typedEntry = new TypedBibEntry(entry, bibDatabaseMode);
out.write('@' + typedEntry.getTypeForDisplay() + '{');

writeKeyField(entry, out);
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/net/sf/jabref/logic/xmp/XMPPreferences.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package net.sf.jabref.logic.xmp;

import java.util.List;

import net.sf.jabref.preferences.JabRefPreferences;

public class XMPPreferences {

private final boolean useXMPPrivacyFilter;
private final List<String> xmpPrivacyFilter;
private final String keywordSeparator;


public XMPPreferences(boolean useXMPPrivacyFilter, List<String> xmpPrivacyFilter, String keywordSeparator) {
this.useXMPPrivacyFilter = useXMPPrivacyFilter;
this.xmpPrivacyFilter = xmpPrivacyFilter;
this.keywordSeparator = keywordSeparator;
}

public static XMPPreferences fromPreferences(JabRefPreferences jabrefPreferences) {
return new XMPPreferences(jabrefPreferences.getBoolean(JabRefPreferences.USE_XMP_PRIVACY_FILTER),
jabrefPreferences.getStringList(JabRefPreferences.XMP_PRIVACY_FILTERS),
jabrefPreferences.get(JabRefPreferences.KEYWORD_SEPARATOR));
}

public boolean isUseXMPPrivacyFilter() {
return useXMPPrivacyFilter;
}

public List<String> getXmpPrivacyFilter() {
return xmpPrivacyFilter;
}

public String getKeywordSeparator() {
return keywordSeparator;
}
}
Loading