-
-
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
Add XMP Exporter #3895
Add XMP Exporter #3895
Changes from 2 commits
531984d
0b44c43
67cbd43
f22de6f
9693abc
bfa3441
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.jabref.logic.exporter; | ||
|
||
import java.io.BufferedWriter; | ||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import org.jabref.Globals; | ||
import org.jabref.logic.util.FileType; | ||
import org.jabref.logic.xmp.XmpUtilWriter; | ||
import org.jabref.model.database.BibDatabaseContext; | ||
import org.jabref.model.entry.BibEntry; | ||
|
||
/** | ||
* A custom exporter to write bib entries to a .xmp file for further processing | ||
* in other scenarios and applications. The xmp metadata are written in dublin | ||
* core format. | ||
*/ | ||
public class XmpExporter extends Exporter { | ||
|
||
public XmpExporter() { | ||
super("XmpBib", FileType.ENDNOTE_XMP.getDescription(), FileType.ENDNOTE_XMP); | ||
} | ||
|
||
@Override | ||
public void export(BibDatabaseContext databaseContext, Path file, Charset encoding, List<BibEntry> entries) throws Exception { | ||
Objects.requireNonNull(databaseContext); | ||
Objects.requireNonNull(entries); | ||
|
||
if (entries.isEmpty()) { | ||
return; | ||
} | ||
|
||
try (BufferedWriter writer = Files.newBufferedWriter(file, StandardCharsets.UTF_8)) { | ||
writer.write(XmpUtilWriter.generateXmpString(entries, Globals.prefs.getXMPPreferences())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please pass the |
||
writer.flush(); | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ | |
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.UnsupportedEncodingException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
|
@@ -82,6 +84,16 @@ public static void writeXmp(Path file, BibEntry entry, | |
XmpUtilWriter.writeXmp(file, bibEntryList, database, xmpPreferences); | ||
} | ||
|
||
/** | ||
* Writes the information of the bib entry to the dublin core schema using | ||
* a custom extractor. | ||
* | ||
* @param dcSchema Dublin core schema, which is filled with the bib entry. | ||
* @param entry The entry, which is added to the dublin core metadata. | ||
* @param database maybenull An optional database which the given bibtex entries belong to, which will be used to | ||
* resolve strings. If the database is null the strings will not be resolved. | ||
* @param xmpPreferences The user's xmp preferences. | ||
*/ | ||
private static void writeToDCSchema(DublinCoreSchema dcSchema, BibEntry entry, BibDatabase database, | ||
XmpPreferences xmpPreferences) { | ||
|
||
|
@@ -91,7 +103,6 @@ private static void writeToDCSchema(DublinCoreSchema dcSchema, BibEntry entry, B | |
dcExtractor.fillDublinCoreSchema(); | ||
} | ||
|
||
|
||
/** | ||
* Try to write the given BibTexEntry as a DublinCore XMP Schema | ||
* | ||
|
@@ -159,6 +170,38 @@ private static void writeDublinCore(PDDocument document, | |
catalog.setMetadata(metadataStream); | ||
} | ||
|
||
/** | ||
* This method generates an xmp metadata string in dublin core format. | ||
* <br/> | ||
* | ||
* @param entries A list of entries, which are added to the dublin core metadata. | ||
* @param xmpPreferences The user's xmp preferences. | ||
* | ||
* @return If something goes wrong (e.g. an exception is thrown), the method returns an empty string, | ||
* otherwise it returns the xmp metadata as a string in dublin core format. | ||
*/ | ||
public static String generateXmpString(List<BibEntry> entries, XmpPreferences xmpPreferences) { | ||
XMPMetadata meta = XMPMetadata.createXMPMetadata(); | ||
for (BibEntry entry : entries) { | ||
DublinCoreSchema dcSchema = meta.createAndAddDublinCoreSchema(); | ||
XmpUtilWriter.writeToDCSchema(dcSchema, entry, null, xmpPreferences); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to add an additional method |
||
} | ||
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) { | ||
XmpSerializer serializer = new XmpSerializer(); | ||
serializer.serialize(meta, os, true); | ||
return os.toString(StandardCharsets.UTF_8.name()); | ||
} catch (TransformerException e) { | ||
LOGGER.warn("Tranformation into xmp not possible: " + e.getMessage()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not logging the exception itself, too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean the stack trace of the exception? Otherwise, I would use something like this: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logger methods have a second parameter which accepts a throwable /exception. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code usually looks something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Personally, I would wrap these Exceptions in an IOException and then rethrow. |
||
return ""; | ||
} catch (UnsupportedEncodingException e) { | ||
LOGGER.warn("Unsupported encoding to UTF-8 of bib entries in xmp metadata."); | ||
return ""; | ||
} catch (IOException e) { | ||
LOGGER.warn("IO Exception thrown by closing the output stream."); | ||
return ""; | ||
} | ||
} | ||
|
||
/** | ||
* Try to write the given BibTexEntry in the Document Information (the | ||
* properties of the pdf). | ||
|
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.
Can you please pass the
Globals.prefs.getXMPPreferences()
dependency as a constructor argument. We are currently trying to reduce the number of calls toGlobals
.