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 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
113 changes: 100 additions & 13 deletions src/main/java/org/jabref/logic/importer/fileformat/CffImporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

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 +58,23 @@ private static class CffFormat {
@JsonProperty("identifiers")
private List<CffIdentifier> ids;

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

@JsonProperty("type")
private String type;

public JsonNode getPreferredCitation() {
return preferredCitation;
}

public CffFormat() {
}

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,6 +107,7 @@ public CffIdentifier() {
public ParserResult importDatabase(BufferedReader reader) throws IOException {
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
CffFormat citation = mapper.readValue(reader, CffFormat.class);
List<BibEntry> entriesList = new ArrayList<>();
HashMap<Field, String> entryMap = new HashMap<>();
StandardEntryType entryType = StandardEntryType.Software;

Expand All @@ -111,20 +127,20 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException {

// 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();
.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());
.filter(id -> "doi".equals(id.type))
.collect(Collectors.toList());
if (doiIds.size() == 1) {
entryMap.put(StandardField.DOI, doiIds.getFirst().value);
}
Expand All @@ -133,9 +149,9 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException {
// 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());
.filter(id -> "swh".equals(id.type))
.map(id -> id.value)
.collect(Collectors.toList());

if (swhIds.size() == 1) {
entryMap.put(BiblatexSoftwareField.SWHID, swhIds.getFirst());
Expand All @@ -149,16 +165,72 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException {
}
}
}
// Handle the main citation as a separate entry
BibEntry mainEntry = new BibEntry(entryType);
mainEntry.setField(entryMap);

HashMap<String, Field> fieldMappings = getFieldMappings();
// Now handle preferred citation as its own entry
if (citation.getPreferredCitation() != null) {
HashMap<Field, String> preferredEntryMap = new HashMap<>();
processPreferredCitation(citation.getPreferredCitation(), preferredEntryMap, entriesList, fieldMappings);
}

BibEntry entry = new BibEntry(entryType);
entry.setField(entryMap);

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

return new ParserResult(entriesList);
}

private void processPreferredCitation(JsonNode preferredCitation, HashMap<Field, String> entryMap, List<BibEntry> entriesList, HashMap<String, Field> fieldMappings) {
if (preferredCitation.isObject()) {
BibEntry preferredEntry = new BibEntry();
preferredCitation.fields().forEachRemaining(field -> {
String key = field.getKey();
JsonNode value = field.getValue();

if (fieldMappings.containsKey(key)) {
preferredEntry.setField(fieldMappings.get(key), value.asText());
} else if ("authors".equals(key) && value.isArray()) {
preferredEntry.setField(StandardField.AUTHOR, parseAuthors(value));
} else if ("journal".equals(key)) {
preferredEntry.setField(StandardField.JOURNAL, value.asText());
} else if ("doi".equals(key)) {
preferredEntry.setField(StandardField.DOI, value.asText());
} else if ("year".equals(key)) {
preferredEntry.setField(StandardField.YEAR, value.asText());
} else if ("volume".equals(key)) {
preferredEntry.setField(StandardField.VOLUME, value.asText());
} else if ("issue".equals(key)) {
preferredEntry.setField(StandardField.ISSUE, value.asText());
} else if ("pages".equals(key)) {
String pages = value.has("start") && value.has("end")
? value.get("start").asText() + "--" + value.get("end").asText()
: value.asText();
preferredEntry.setField(StandardField.PAGES, pages);
}
});
if (!preferredEntry.getField(StandardField.TITLE).orElse("").isEmpty()) {
entriesList.add(preferredEntry);
}
}
}

private String parseAuthors(JsonNode authorsNode) {
StringBuilder authors = new StringBuilder();
for (JsonNode authorNode : authorsNode) {
String givenNames = authorNode.has("given-names") ? authorNode.get("given-names").asText() : "";
String familyNames = authorNode.has("family-names") ? authorNode.get("family-names").asText() : "";
authors.append(givenNames).append(" ").append(familyNames).append(" and ");
}
if (authors.lastIndexOf(" and ") == authors.length() - 5) {
authors.delete(authors.length() - 5, authors.length());
}
return authors.toString();
}

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

Expand All @@ -173,6 +245,21 @@ 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.Dataset;
};
}

private HashMap<String, Field> getFieldMappings() {
HashMap<String, Field> fieldMappings = new HashMap<>();
fieldMappings.put("title", StandardField.TITLE);
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void importEntriesDataset() throws IOException, URISyntaxException {
BibEntry entry = bibEntries.getFirst();

BibEntry expected = getPopulatedEntry();
expected.setType(StandardEntryType.Dataset);
expected.setType(StandardEntryType.Software);

assertEquals(entry, expected);
}
Expand All @@ -144,6 +144,33 @@ public void importEntriesUnknownFields() throws IOException, URISyntaxException
assertEquals(entry, expected);
}

@Test
public void importCITATION() throws IOException, URISyntaxException {
Path file = Path.of(CffImporterTest.class.getResource("CITATION.cff").toURI());
List<BibEntry> bibEntries = importer.importDatabase(file).getDatabase().getEntries();
BibEntry entry = bibEntries.getFirst();

BibEntry expected = getPopulatedEntry1();

assertEquals(entry, expected);
}

public BibEntry getPopulatedEntry1() {
BibEntry entry = new BibEntry(StandardEntryType.Misc);

String authors = "Oliver Kopp and Carl Christian Snethlage and Christoph Schwentker";
entry.setField(StandardField.AUTHOR, authors);
entry.setField(StandardField.ISSUE, "138");
entry.setField(StandardField.TITLE, "JabRef: BibTeX-based literature management software");
entry.setField(StandardField.DOI, "10.47397/tb/44-3/tb138kopp-jabref");
entry.setField(StandardField.JOURNAL, "TUGboat");
entry.setField(StandardField.VOLUME, "44");
entry.setField(StandardField.YEAR, "2023");

return entry;
}


public BibEntry getPopulatedEntry() {
BibEntry entry = new BibEntry();
entry.setType(StandardEntryType.Software);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# YAML 1.2
---
cff-version: 1.2.0
title: JabRef
message: >-
If you use this software, please cite it using the
metadata from this file.
type: software
authors:
- given-names: Oliver
family-names: Kopp
orcid: 'https://orcid.org/0000-0001-6962-4290'
- given-names: Tobias
family-names: Diez
orcid: 'https://orcid.org/0000-0002-1407-7696'
- given-names: Christoph
family-names: Schwentker
- given-names: Carl Christian
family-names: Snethlage
- given-names: Jonatan
family-names: Asketorp
- given-names: Benedikt
family-names: Tutzer
- given-names: Thilo
family-names: Ertel
- given-names: Houssem
family-names: Nasri
repository-code: 'https://github.com/jabref/jabref/'
url: 'https://www.jabref.org'
abstract: >-
JabRef is an open-source, cross-platform citation and
reference management tool.
license: MIT
preferred-citation:
type: article
authors:
- family-names: "Kopp"
given-names: "Oliver"
orcid: "https://orcid.org/0000-0001-6962-4290"
- family-names: "Snethlage"
given-names: "Carl Christian"
- family-names: "Schwentker"
given-names: "Christoph"
doi: "10.47397/tb/44-3/tb138kopp-jabref"
journal: "TUGboat"
month: 11
start: 441
end: 447
title: "JabRef: BibTeX-based literature management software"
issue: 138
volume: 44
number: 3
year: 2023
...
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
# YAML 1.2
---
abstract: "Test abstract."
authors:
-
family-names: Smith
given-names: Joe
name-particle: van
cff-version: "1.1.0"
comment: "Test entry"
date-released: 2000-07-02
doi: "10.0000/TEST"
identifiers:
license: MIT
message: "Test entry."
abstract: "Test abstract."
title: Test
version: "1.0"
url: "www.google.com"
Expand Down
Loading