-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
be91964
commit 73fb95f
Showing
9 changed files
with
261 additions
and
8 deletions.
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()); | ||
} | ||
} |