-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Merged
Merged
Add XMP Exporter #3895
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
531984d
Add Xmp GUI Export
johannes-manner 0b44c43
updated code with the requested changes
johannes-manner 67cbd43
XMP export works now for single elements
johannes-manner f22de6f
Changed the xmp preference distribution, added a split functionality …
johannes-manner 9693abc
Added exporter tests for XmpExporter
johannes-manner bfa3441
Change xmp file dialog description
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 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,72 @@ | ||
package org.jabref.logic.exporter; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.IOException; | ||
import java.nio.charset.Charset; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import org.jabref.logic.util.FileType; | ||
import org.jabref.logic.xmp.XmpPreferences; | ||
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 { | ||
|
||
private static final String XMP_SPLIT_PATTERN = "split"; | ||
|
||
private final XmpPreferences xmpPreferences; | ||
|
||
public XmpExporter(XmpPreferences xmpPreferences) { | ||
super("xmp", FileType.PLAIN_XMP.getDescription(), FileType.PLAIN_XMP); | ||
this.xmpPreferences = xmpPreferences; | ||
} | ||
|
||
@Override | ||
public void export(BibDatabaseContext databaseContext, Path file, Charset encoding, List<BibEntry> entries) throws Exception { | ||
Objects.requireNonNull(databaseContext); | ||
Objects.requireNonNull(file); | ||
Objects.requireNonNull(entries); | ||
|
||
if (entries.isEmpty()) { | ||
return; | ||
} | ||
|
||
// This is a distinction between writing all entries from the supplied list to a single .xmp file, | ||
// or write every entry to a separate file. | ||
if (file.getFileName().toString().trim().equals(XMP_SPLIT_PATTERN)) { | ||
|
||
for (BibEntry entry : entries) { | ||
// Avoid situations, where two cite keys are null | ||
Path entryFile; | ||
String suffix = entry.getId() + "_" + entry.getCiteKey() + ".xmp"; | ||
if (file.getParent() == null) { | ||
entryFile = Paths.get(suffix); | ||
} else { | ||
entryFile = Paths.get(file.getParent().toString() + "/" + suffix); | ||
} | ||
|
||
this.writeBibToXmp(entryFile, Arrays.asList(entry), encoding); | ||
} | ||
} else { | ||
this.writeBibToXmp(file, entries, encoding); | ||
} | ||
} | ||
|
||
private void writeBibToXmp(Path file, List<BibEntry> entries, Charset encoding) throws IOException { | ||
try (BufferedWriter writer = Files.newBufferedWriter(file, encoding)) { | ||
writer.write(XmpUtilWriter.generateXmpString(entries, this.xmpPreferences)); | ||
writer.flush(); | ||
} | ||
} | ||
} |
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 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 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
112 changes: 112 additions & 0 deletions
112
src/test/java/org/jabref/logic/exporter/XmpExporterTest.java
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,112 @@ | ||
package org.jabref.logic.exporter; | ||
|
||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.jabref.logic.layout.LayoutFormatterPreferences; | ||
import org.jabref.logic.xmp.XmpPreferences; | ||
import org.jabref.model.database.BibDatabaseContext; | ||
import org.jabref.model.entry.BibEntry; | ||
|
||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.TemporaryFolder; | ||
import org.mockito.Answers; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.mock; | ||
|
||
public class XmpExporterTest { | ||
|
||
private Exporter exporter; | ||
private BibDatabaseContext databaseContext; | ||
private Charset encoding; | ||
|
||
@Rule public TemporaryFolder testFolder = new TemporaryFolder(); | ||
|
||
@Before | ||
public void setUp() { | ||
Map<String, TemplateExporter> customFormats = new HashMap<>(); | ||
LayoutFormatterPreferences layoutPreferences = mock(LayoutFormatterPreferences.class, Answers.RETURNS_DEEP_STUBS); | ||
SavePreferences savePreferences = mock(SavePreferences.class); | ||
XmpPreferences xmpPreferences = mock(XmpPreferences.class); | ||
ExporterFactory exporterFactory = ExporterFactory.create(customFormats, layoutPreferences, savePreferences, xmpPreferences); | ||
|
||
exporter = exporterFactory.getExporterByName("xmp").get(); | ||
|
||
databaseContext = new BibDatabaseContext(); | ||
encoding = StandardCharsets.UTF_8; | ||
} | ||
|
||
@Test | ||
public void exportSingleEntry() throws Exception { | ||
|
||
Path file = testFolder.newFile().toPath(); | ||
|
||
BibEntry entry = new BibEntry(); | ||
entry.setField("author", "Alan Turing"); | ||
|
||
exporter.export(databaseContext, file, encoding, Arrays.asList(entry)); | ||
|
||
List<String> lines = Files.readAllLines(file); | ||
assertTrue(lines.size() == 21); | ||
assertEquals("<rdf:li>Alan Turing</rdf:li>", lines.get(7).trim()); | ||
} | ||
|
||
@Test | ||
public void writeMutlipleEntriesInASingleFile() throws Exception { | ||
|
||
Path file = testFolder.newFile().toPath(); | ||
|
||
BibEntry entryTuring = new BibEntry(); | ||
entryTuring.setField("author", "Alan Turing"); | ||
|
||
BibEntry entryArmbrust = new BibEntry(); | ||
entryArmbrust.setField("author", "Michael Armbrust"); | ||
entryArmbrust.setCiteKey("Armbrust2010"); | ||
|
||
exporter.export(databaseContext, file, encoding, Arrays.asList(entryTuring, entryArmbrust)); | ||
|
||
List<String> lines = Files.readAllLines(file); | ||
assertTrue(lines.size() == 39); | ||
assertEquals("<rdf:li>Alan Turing</rdf:li>", lines.get(7).trim()); | ||
assertEquals("<rdf:li>Michael Armbrust</rdf:li>", lines.get(20).trim()); | ||
} | ||
|
||
@Test | ||
public void writeMultipleEntriesInDifferentFiles() throws Exception { | ||
|
||
Path file = testFolder.newFile("split").toPath(); | ||
|
||
BibEntry entryTuring = new BibEntry(); | ||
entryTuring.setField("author", "Alan Turing"); | ||
|
||
BibEntry entryArmbrust = new BibEntry(); | ||
entryArmbrust.setField("author", "Michael Armbrust"); | ||
entryArmbrust.setCiteKey("Armbrust2010"); | ||
|
||
exporter.export(databaseContext, file, encoding, Arrays.asList(entryTuring, entryArmbrust)); | ||
|
||
List<String> lines = Files.readAllLines(file); | ||
assertTrue(lines.size() == 0); | ||
|
||
Path fileTuring = Paths.get(file.getParent().toString() + "/" + entryTuring.getId() + "_null.xmp"); | ||
List<String> linesTuring = Files.readAllLines(fileTuring); | ||
assertTrue(linesTuring.size() == 21); | ||
assertEquals("<rdf:li>Alan Turing</rdf:li>", linesTuring.get(7).trim()); | ||
|
||
Path fileArmbrust = Paths.get(file.getParent().toString() + "/" + entryArmbrust.getId() + "_Armbrust2010.xmp"); | ||
List<String> linesArmbrust = Files.readAllLines(fileArmbrust); | ||
assertTrue(linesArmbrust.size() == 26); | ||
assertEquals("<rdf:li>Michael Armbrust</rdf:li>", linesArmbrust.get(7).trim()); | ||
} | ||
} |
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.
How do the other exporters get their preferences? Shouldn't they have the same issue with the Globals dependency?
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.
They get their prefs in the Exporter Factory. In this case it must only be expanded to pass the xmp prefs from the existing pref object