Skip to content
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

Crafting round-trip test for Cff importer/exporter #10957

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 16 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
172 changes: 113 additions & 59 deletions src/main/java/org/jabref/logic/importer/fileformat/CffImporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,19 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.jabref.logic.importer.Importer;
import org.jabref.logic.importer.ParserResult;
import org.jabref.logic.util.StandardFileType;
import org.jabref.model.entry.Author;
import org.jabref.model.entry.AuthorList;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.BiblatexSoftwareField;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.field.UnknownField;
import org.jabref.model.entry.types.StandardEntryType;

import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

Expand Down Expand Up @@ -57,9 +54,23 @@ private static class CffFormat {
@JsonProperty("identifiers")
private List<CffIdentifier> ids;

@JsonProperty("preferred-citation")
private JsonNode preferredCitation;

@JsonProperty("type")
private String type;

public CffFormat() {
}

public JsonNode getPreferredCitation() {
return preferredCitation;
}

public void setPreferredCitation(JsonNode preferredCitation) {
this.preferredCitation = preferredCitation;
}

@JsonAnySetter
private void setValues(String key, String value) {
values.put(key, value);
Expand Down Expand Up @@ -92,73 +103,100 @@ public CffIdentifier() {
public ParserResult importDatabase(BufferedReader reader) throws IOException {
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
CffFormat citation = mapper.readValue(reader, CffFormat.class);
HashMap<Field, String> entryMap = new HashMap<>();
StandardEntryType entryType = StandardEntryType.Software;

// Map CFF fields to JabRef Fields
HashMap<String, Field> fieldMap = getFieldMappings();
for (Map.Entry<String, String> property : citation.values.entrySet()) {
if (fieldMap.containsKey(property.getKey())) {
entryMap.put(fieldMap.get(property.getKey()), property.getValue());
} else if ("type".equals(property.getKey())) {
if ("dataset".equals(property.getValue())) {
entryType = StandardEntryType.Dataset;
}
} else if (getUnmappedFields().contains(property.getKey())) {
entryMap.put(new UnknownField(property.getKey()), property.getValue());
}
}

// Translate CFF author format to JabRef author format
String authorStr = citation.authors.stream()
.map(author -> author.values)
.map(vals -> vals.get("name") != null ?
new Author(vals.get("name"), "", "", "", "") :
new Author(vals.get("given-names"), null, vals.get("name-particle"),
vals.get("family-names"), vals.get("name-suffix")))
.collect(AuthorList.collect())
.getAsFirstLastNamesWithAnd();
entryMap.put(StandardField.AUTHOR, authorStr);

// Select DOI to keep
if ((entryMap.get(StandardField.DOI) == null) && (citation.ids != null)) {
List<CffIdentifier> doiIds = citation.ids.stream()
.filter(id -> "doi".equals(id.type))
.collect(Collectors.toList());
if (doiIds.size() == 1) {
entryMap.put(StandardField.DOI, doiIds.getFirst().value);
}
StandardEntryType entryType = StandardEntryType.Misc;

if (citation.type != null) {
entryType = mapType(citation.type);
}
BibEntry entry = new BibEntry(entryType);
HashMap<Field, String> entryMap = new HashMap<>();

// Select SWHID to keep
Raahitya-14 marked this conversation as resolved.
Show resolved Hide resolved
if (citation.ids != null) {
List<String> swhIds = citation.ids.stream()
.filter(id -> "swh".equals(id.type))
.map(id -> id.value)
.collect(Collectors.toList());

if (swhIds.size() == 1) {
entryMap.put(BiblatexSoftwareField.SWHID, swhIds.getFirst());
} else if (swhIds.size() > 1) {
List<String> relSwhIds = swhIds.stream()
.filter(id -> id.split(":").length > 3) // quick filter for invalid swhids
.filter(id -> "rel".equals(id.split(":")[2]))
.collect(Collectors.toList());
if (relSwhIds.size() == 1) {
entryMap.put(BiblatexSoftwareField.SWHID, relSwhIds.getFirst());
}
}
if (citation.getPreferredCitation() != null) {
preferredCitationMethod(citation.getPreferredCitation(), entryMap, entry);
}

BibEntry entry = new BibEntry(entryType);
entry.setField(entryMap);
mainCffContentMethod(citation, entryMap, entry);
entryMap.forEach(entry::setField);

List<BibEntry> entriesList = new ArrayList<>();
entriesList.add(entry);

return new ParserResult(entriesList);
}

private void preferredCitationMethod(JsonNode preferredCitation, Map<Field, String> entryMap, BibEntry entry) {
if (preferredCitation != null) {
if (preferredCitation.has("type")) {
String typeValue = preferredCitation.get("type").asText();
StandardEntryType entryType = mapType(typeValue);
entry.setType(entryType);
}
if (preferredCitation.has("title")) {
entryMap.put(StandardField.TITLE, preferredCitation.get("title").asText());
}
if (preferredCitation.has("doi")) {
entryMap.put(StandardField.DOI, preferredCitation.get("doi").asText());
}
if (preferredCitation.has("authors")) {
List<String> authorsList = new ArrayList<>();
preferredCitation.get("authors").forEach(authorNode -> {
String givenName = authorNode.has("given-names") ? authorNode.get("given-names").asText() : "";
String familyName = authorNode.has("family-names") ? authorNode.get("family-names").asText() : "";
authorsList.add((givenName + " " + familyName).trim());
Raahitya-14 marked this conversation as resolved.
Show resolved Hide resolved
});
String authors = String.join(" and ", authorsList);
entryMap.put(StandardField.AUTHOR, authors);
}
if (preferredCitation.has("journal")) {
entryMap.put(StandardField.JOURNAL, preferredCitation.get("journal").asText());
}
if (preferredCitation.has("volume")) {
entryMap.put(StandardField.VOLUME, preferredCitation.get("volume").asText());
}
if (preferredCitation.has("issue")) {
entryMap.put(StandardField.ISSUE, preferredCitation.get("issue").asText());
}
if (preferredCitation.has("year")) {
entryMap.put(StandardField.YEAR, preferredCitation.get("year").asText());
}
if (preferredCitation.has("start") && preferredCitation.has("end")) {
String pages = preferredCitation.get("start").asText() + "-" + preferredCitation.get("end").asText();
entryMap.put(StandardField.PAGES, pages);
}
}
}

private void mainCffContentMethod(CffFormat citation, Map<Field, String> entryMap, BibEntry entry) {
if (!entryMap.containsKey(StandardField.TITLE) && citation.values.containsKey("title")) {
entryMap.put(StandardField.TITLE, citation.values.get("title"));
}
if (!entryMap.containsKey(StandardField.AUTHOR) && citation.authors != null && !citation.authors.isEmpty()) {
List<String> authorsList = new ArrayList<>();
for (CffAuthor author : citation.authors) {
String givenName = author.values.getOrDefault("given-names", "");
String familyName = author.values.getOrDefault("family-names", "");
authorsList.add((givenName + " " + familyName).trim());
}
Raahitya-14 marked this conversation as resolved.
Show resolved Hide resolved
String authors = String.join(" and ", authorsList);
entryMap.put(StandardField.AUTHOR, authors);
}
if (!entryMap.containsKey(StandardField.DOI) && citation.values.containsKey("doi")) {
entryMap.put(StandardField.DOI, citation.values.get("doi"));
}
if (!entryMap.containsKey(StandardField.VERSION) && citation.values.containsKey("version")) {
entryMap.put(StandardField.VERSION, citation.values.get("version"));
}
if (!entryMap.containsKey(StandardField.YEAR) && citation.values.containsKey("date-released")) {
String dateReleased = citation.values.get("date-released");
String year = dateReleased.split("-")[0];
entryMap.put(StandardField.YEAR, year);
}
if (!entryMap.containsKey(StandardField.URL) && citation.values.containsKey("url")) {
entryMap.put(StandardField.URL, citation.values.get("url"));
}
}

@Override
public boolean isRecognizedFormat(BufferedReader reader) throws IOException {

Expand All @@ -173,8 +211,24 @@ public boolean isRecognizedFormat(BufferedReader reader) throws IOException {
}
}

private StandardEntryType mapType(String cffType) {
return switch (cffType) {
case "article" -> StandardEntryType.Article;
case "book" -> StandardEntryType.Book;
case "conference" -> StandardEntryType.InProceedings;
case "proceedings" -> StandardEntryType.Proceedings;
case "misc" -> StandardEntryType.Misc;
case "manual" -> StandardEntryType.Manual;
case "software" -> StandardEntryType.Software;
case "report" -> StandardEntryType.TechReport;
case "unpublished" -> StandardEntryType.Unpublished;
default -> StandardEntryType.Misc;
};
}

private HashMap<String, Field> getFieldMappings() {
HashMap<String, Field> fieldMappings = new HashMap<>();
fieldMappings.put("type", StandardField.TYPE);
fieldMappings.put("title", StandardField.TITLE);
fieldMappings.put("version", StandardField.VERSION);
fieldMappings.put("doi", StandardField.DOI);
Expand Down
73 changes: 73 additions & 0 deletions src/test/java/org/jabref/logic/importer/CffImporterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import java.io.BufferedReader;
import java.io.StringReader;
import java.util.List;

import org.jabref.logic.importer.ParserResult;
import org.jabref.logic.importer.fileformat.CffImporter;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.StandardField;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class CffImporterTest {
Raahitya-14 marked this conversation as resolved.
Show resolved Hide resolved

@Test
public void preferredCitationTest() throws Exception {
String cffContent = """
cff-version: 1.2.0
message: "If you use this software, please cite it as below."
authors:
- family-names: "Lisa"
given-names: "Mona"
orcid: "https://orcid.org/0000-0000-0000-0000"
- family-names: "Bot"
given-names: "Hew"
orcid: "https://orcid.org/0000-0000-0000-0000"
title: "My Research Software"
version: 2.0.4
doi: 10.5281/zenodo.1234
date-released: 2017-12-18
url: "https://github.com/github-linguist/linguist"
preferred-citation:
type: misc
authors:
- family-names: "Lisa"
given-names: "Mona"
orcid: "https://orcid.org/0000-0000-0000-0000"
- family-names: "Bot"
given-names: "Hew"
orcid: "https://orcid.org/0000-0000-0000-0000"
doi: "10.0000/00000"
journal: "Journal Title"
month: 9
start: 1
end: 10
title: "My awesome research software"
issue: 1
volume: 1
year: 2021
""";

CffImporter importer = new CffImporter();
try (BufferedReader reader = new BufferedReader(new StringReader(cffContent))) {
ParserResult result = importer.importDatabase(reader);
List<BibEntry> actualEntries = result.getDatabase().getEntries();

BibEntry expectedEntry = new BibEntry()
.withField(StandardField.AUTHOR, "Mona Lisa and Hew Bot")
.withField(StandardField.TITLE, "My awesome research software")
.withField(StandardField.DOI, "10.0000/00000")
.withField(StandardField.JOURNAL, "Journal Title")
.withField(StandardField.VOLUME, "1")
.withField(StandardField.URL, "https://github.com/github-linguist/linguist")
.withField(StandardField.VERSION, "2.0.4")
.withField(StandardField.ISSUE, "1")
.withField(StandardField.YEAR, "2021")
.withField(StandardField.PAGES, "1-10");

assertEquals(List.of(expectedEntry), actualEntries);
}
}
}
4 changes: 3 additions & 1 deletion src/test/java/org/jabref/logic/importer/ImporterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import org.jabref.logic.importer.fileformat.BiblioscapeImporter;
import org.jabref.logic.importer.fileformat.BibtexImporter;
import org.jabref.logic.importer.fileformat.CffImporter;
import org.jabref.logic.importer.fileformat.CitaviXmlImporter;
import org.jabref.logic.importer.fileformat.CopacImporter;
import org.jabref.logic.importer.fileformat.EndnoteImporter;
Expand Down Expand Up @@ -126,7 +127,8 @@ public static Stream<Importer> instancesToTest() {
new RepecNepImporter(importFormatPreferences),
new RisImporter(),
new SilverPlatterImporter(),
new CitaviXmlImporter()
new CitaviXmlImporter(),
new CffImporter()
);
// @formatter:on
}
Expand Down
Loading