From 32f729743886206756ac585529d34708a32285d7 Mon Sep 17 00:00:00 2001 From: aidan Date: Thu, 29 Jul 2021 15:04:28 -0700 Subject: [PATCH 1/8] Added option to import CFF files --- .../logic/importer/ImportFormatReader.java | 2 + .../importer/fileformat/CffImporter.java | 153 ++++++++++++++++++ .../jabref/logic/util/StandardFileType.java | 1 + 3 files changed, 156 insertions(+) create mode 100644 src/main/java/org/jabref/logic/importer/fileformat/CffImporter.java diff --git a/src/main/java/org/jabref/logic/importer/ImportFormatReader.java b/src/main/java/org/jabref/logic/importer/ImportFormatReader.java index 3b00f3e5057..7777d205e51 100644 --- a/src/main/java/org/jabref/logic/importer/ImportFormatReader.java +++ b/src/main/java/org/jabref/logic/importer/ImportFormatReader.java @@ -11,6 +11,7 @@ import org.jabref.logic.importer.fileformat.BibTeXMLImporter; 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.CopacImporter; import org.jabref.logic.importer.fileformat.EndnoteImporter; import org.jabref.logic.importer.fileformat.EndnoteXmlImporter; @@ -69,6 +70,7 @@ public void resetImportFormats(ImportFormatPreferences newImportFormatPreference formats.add(new RepecNepImporter(importFormatPreferences)); formats.add(new RisImporter()); formats.add(new SilverPlatterImporter()); + formats.add(new CffImporter()); // Get custom import formats formats.addAll(importFormatPreferences.getCustomImportList()); diff --git a/src/main/java/org/jabref/logic/importer/fileformat/CffImporter.java b/src/main/java/org/jabref/logic/importer/fileformat/CffImporter.java new file mode 100644 index 00000000000..5ddfea1c796 --- /dev/null +++ b/src/main/java/org/jabref/logic/importer/fileformat/CffImporter.java @@ -0,0 +1,153 @@ +package org.jabref.logic.importer.fileformat; + +import java.io.BufferedReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.jabref.logic.importer.Importer; +import org.jabref.logic.importer.ParserResult; +import org.jabref.logic.util.StandardFileType; +import org.jabref.model.database.BibDatabase; +import org.jabref.model.entry.BibEntry; +import org.jabref.model.entry.field.Field; +import org.jabref.model.entry.field.InternalField; +import org.jabref.model.entry.field.StandardField; +import org.jabref.model.entry.types.EntryType; +import org.jabref.model.entry.types.StandardEntryType; + +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; +import com.google.common.collect.ImmutableMap; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class CffImporter extends Importer { + private static final Logger LOGGER = LoggerFactory.getLogger(CffImporter.class); + + @Override + public String getName() { + return "CFF"; + } + + @Override + public StandardFileType getFileType() { + return StandardFileType.CFF; + } + + @Override + public String getId() { + return "cff"; + } + + @Override + public String getDescription() { + return "Importer for the CFF format. Is only used to cite software, one entry per file."; + } + + private static class CffFormat { + private HashMap vals = new HashMap(); + + @JsonProperty("authors") + private List authors; + + public CffFormat() { + } + + @JsonAnySetter + private void setValues(String key, String value) { + vals.put(key, value); + } + } + + private static class CffAuthor { + private HashMap vals = new HashMap(); + + public CffAuthor() { + } + + @JsonAnySetter + private void setValues(String key, String value) { + vals.put(key, value); + } + + } + + @Override + public ParserResult importDatabase(BufferedReader reader) throws IOException { + ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); + CffFormat citation = mapper.readValue(reader, CffFormat.class); + HashMap entryMap = new HashMap(); + + HashMap fieldMap = getFieldMappings(); + for (Map.Entry property : citation.vals.entrySet()) { + if (fieldMap.containsKey(property.getKey())) { + entryMap.put(fieldMap.get(property.getKey()), property.getValue()); + } + } + + List authors = new ArrayList(); + for (CffAuthor auth: citation.authors) { + String aName = auth.vals.get("name"); + String aGivenNames = auth.vals.get("given-names"); + String aFamilyNames = auth.vals.get("family-names"); + String aNameParticle = auth.vals.get("name-particle"); + String aAlias = auth.vals.get("alias"); + + if (aName != null) { + authors.add(aName); + } else if (aFamilyNames != null && aNameParticle != null && aGivenNames != null) { + authors.add(aGivenNames + " " + aNameParticle + " " + aFamilyNames); + } else if (aFamilyNames != null && aGivenNames != null) { + authors.add(aGivenNames + " " + aFamilyNames); + } else if (aFamilyNames != null) { + authors.add(aFamilyNames); + } else if (aAlias != null) { + authors.add(aAlias); + } + } + + String authorStr = String.join(", ", authors); + entryMap.put(StandardField.AUTHOR, authorStr); + + BibEntry entry = new BibEntry(StandardEntryType.Software); + entry.setField(entryMap); + + List entriesList = new ArrayList(); + entriesList.add(entry); + + return new ParserResult(entriesList); + } + + @Override + public boolean isRecognizedFormat(BufferedReader reader) throws IOException { + + ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); + CffFormat citation = mapper.readValue(reader, CffFormat.class); + + if (citation != null) { + return true; + } else { + return false; + } + } + + private HashMap getFieldMappings() { + HashMap hm = new HashMap(); + hm.put("title", StandardField.TITLE); + hm.put("version", StandardField.VERSION); + hm.put("doi", StandardField.DOI); + hm.put("license", StandardField.LICENSE); + hm.put("repository", StandardField.REPOSITORY); + hm.put("url", StandardField.URL); + hm.put("abstract", StandardField.ABSTRACT); + hm.put("message", StandardField.COMMENT); + + return hm; + } +} diff --git a/src/main/java/org/jabref/logic/util/StandardFileType.java b/src/main/java/org/jabref/logic/util/StandardFileType.java index 7a9d5d08c48..17b207a5145 100644 --- a/src/main/java/org/jabref/logic/util/StandardFileType.java +++ b/src/main/java/org/jabref/logic/util/StandardFileType.java @@ -42,6 +42,7 @@ public enum StandardFileType implements FileType { ZIP("Zip Archive", "zip"), CSS("CSS Styleshet", "css"), YAML("YAML Markup", "yaml"), + CFF("CFF", "cff"), ANY_FILE("Any", "*"); private final List extensions; From 4ddc41407bc15893e7bb7d583b0b370793e6c59f Mon Sep 17 00:00:00 2001 From: aidan Date: Mon, 16 Aug 2021 15:35:39 -0700 Subject: [PATCH 2/8] Added tests, also minor fixes --- .../importer/fileformat/CffImporter.java | 16 ++-- .../importer/fileformat/CffImporterTest.java | 86 +++++++++++++++++++ .../fileformat/CffImporterTestInvalid1.cff | 4 + .../fileformat/CffImporterTestInvalid2.cff | 1 + .../fileformat/CffImporterTestValid.cff | 19 ++++ 5 files changed, 119 insertions(+), 7 deletions(-) create mode 100644 src/test/java/org/jabref/logic/importer/fileformat/CffImporterTest.java create mode 100644 src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestInvalid1.cff create mode 100644 src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestInvalid2.cff create mode 100644 src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestValid.cff diff --git a/src/main/java/org/jabref/logic/importer/fileformat/CffImporter.java b/src/main/java/org/jabref/logic/importer/fileformat/CffImporter.java index 5ddfea1c796..4d6cd06ae8e 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/CffImporter.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/CffImporter.java @@ -10,20 +10,15 @@ import org.jabref.logic.importer.Importer; import org.jabref.logic.importer.ParserResult; import org.jabref.logic.util.StandardFileType; -import org.jabref.model.database.BibDatabase; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.field.Field; -import org.jabref.model.entry.field.InternalField; import org.jabref.model.entry.field.StandardField; -import org.jabref.model.entry.types.EntryType; import org.jabref.model.entry.types.StandardEntryType; import com.fasterxml.jackson.annotation.JsonAnySetter; import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; -import com.google.common.collect.ImmutableMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -128,9 +123,15 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException { public boolean isRecognizedFormat(BufferedReader reader) throws IOException { ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); - CffFormat citation = mapper.readValue(reader, CffFormat.class); + CffFormat citation; + + try { + citation = mapper.readValue(reader, CffFormat.class); + } catch (IOException e) { + return false; + } - if (citation != null) { + if (citation != null && citation.vals.get("title") != null) { return true; } else { return false; @@ -147,6 +148,7 @@ private HashMap getFieldMappings() { hm.put("url", StandardField.URL); hm.put("abstract", StandardField.ABSTRACT); hm.put("message", StandardField.COMMENT); + hm.put("date-released", StandardField.DATE); return hm; } diff --git a/src/test/java/org/jabref/logic/importer/fileformat/CffImporterTest.java b/src/test/java/org/jabref/logic/importer/fileformat/CffImporterTest.java new file mode 100644 index 00000000000..f5b55890412 --- /dev/null +++ b/src/test/java/org/jabref/logic/importer/fileformat/CffImporterTest.java @@ -0,0 +1,86 @@ +package org.jabref.logic.importer.fileformat; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; + +import org.jabref.logic.util.StandardFileType; +import org.jabref.model.entry.BibEntry; +import org.jabref.model.entry.field.StandardField; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class CffImporterTest { + + private CffImporter importer; + + @BeforeEach + public void setUp() { + importer = new CffImporter(); + } + + @Test + public void testGetFormatName() { + assertEquals("CFF", importer.getName()); + } + + @Test + public void testGetCLIId() { + assertEquals("cff", importer.getId()); + } + + @Test + public void testsGetExtensions() { + assertEquals(StandardFileType.CFF, importer.getFileType()); + } + + @Test + public void testGetDescription() { + assertEquals("Importer for the CFF format. Is only used to cite software, one " + + "entry per file.", importer.getDescription()); + } + + @Test + public void testIsRecognizedFormat() throws IOException, URISyntaxException { + Path file = Path.of(CffImporterTest.class.getResource("CffImporterTestValid.cff").toURI()); + assertTrue(importer.isRecognizedFormat(file, StandardCharsets.UTF_8)); + } + + @Test + public void testIsRecognizedFormatReject() throws IOException, URISyntaxException { + List list = Arrays.asList("CffImporterTestInvalid1.cff", "CffImporterTestInvalid2.cff"); + + for (String string : list) { + Path file = Path.of(CffImporterTest.class.getResource(string).toURI()); + assertFalse(importer.isRecognizedFormat(file, StandardCharsets.UTF_8)); + } + } + + @Test + public void testImportEntries() throws IOException, URISyntaxException { + Path file = Path.of(CffImporterTest.class.getResource("CffImporterTestValid.cff").toURI()); + List bibEntries = importer.importDatabase(file, StandardCharsets.UTF_8).getDatabase().getEntries(); + BibEntry entry = bibEntries.get(0); + + assertEquals(entry.getField(StandardField.AUTHOR), Optional.of("Joe van Smith")); + assertEquals(entry.getField(StandardField.TITLE), Optional.of("Test")); + assertEquals(entry.getField(StandardField.URL), Optional.of("www.google.com")); + assertEquals(entry.getField(StandardField.REPOSITORY), Optional.of("www.github.com")); + assertEquals(entry.getField(StandardField.DOI), Optional.of("10.0000/TEST")); + assertEquals(entry.getField(StandardField.DATE), Optional.of("2000-07-02")); + assertEquals(entry.getField(StandardField.COMMENT), Optional.of("Test entry.")); + assertEquals(entry.getField(StandardField.ABSTRACT), Optional.of("Test abstract.")); + assertEquals(entry.getField(StandardField.LICENSE), Optional.of("MIT")); + assertEquals(entry.getField(StandardField.VERSION), Optional.of("1.0")); + + } +} diff --git a/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestInvalid1.cff b/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestInvalid1.cff new file mode 100644 index 00000000000..cb263660960 --- /dev/null +++ b/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestInvalid1.cff @@ -0,0 +1,4 @@ +# YAML 1.2 +--- +test: 123 +... \ No newline at end of file diff --git a/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestInvalid2.cff b/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestInvalid2.cff new file mode 100644 index 00000000000..3f090015215 --- /dev/null +++ b/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestInvalid2.cff @@ -0,0 +1 @@ +aosdoioifjosdfikbasjc \ No newline at end of file diff --git a/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestValid.cff b/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestValid.cff new file mode 100644 index 00000000000..780c9ee529a --- /dev/null +++ b/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestValid.cff @@ -0,0 +1,19 @@ +# YAML 1.2 +--- +abstract: "Test abstract." +authors: + - + family-names: Smith + given-names: Joe + name-particle: van +cff-version: "1.1.0" +date-released: 2000-07-02 +doi: "10.0000/TEST" +identifiers: +license: MIT +message: "Test entry." +title: Test +version: "1.0" +url: "www.google.com" +repository: "www.github.com" +... \ No newline at end of file From 119c943ca8b70d8f8b293a5e4e6547ec93b16dcd Mon Sep 17 00:00:00 2001 From: aidan Date: Fri, 20 Aug 2021 18:56:02 -0500 Subject: [PATCH 3/8] Improved author handling, added identifier support --- .../importer/fileformat/CffImporter.java | 77 ++++++++++++++----- 1 file changed, 56 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/jabref/logic/importer/fileformat/CffImporter.java b/src/main/java/org/jabref/logic/importer/fileformat/CffImporter.java index 4d6cd06ae8e..d059b80f4fc 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/CffImporter.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/CffImporter.java @@ -6,10 +6,14 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.IntStream; 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.Field; import org.jabref.model.entry.field.StandardField; @@ -51,6 +55,9 @@ private static class CffFormat { @JsonProperty("authors") private List authors; + @JsonProperty("identifiers") + private List ids; + public CffFormat() { } @@ -73,6 +80,16 @@ private void setValues(String key, String value) { } + private static class CffIdentifier { + @JsonProperty("type") + private String type; + @JsonProperty("value") + private String value; + + public CffIdentifier() { + } + } + @Override public ParserResult importDatabase(BufferedReader reader) throws IOException { ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); @@ -86,29 +103,47 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException { } } - List authors = new ArrayList(); - for (CffAuthor auth: citation.authors) { - String aName = auth.vals.get("name"); - String aGivenNames = auth.vals.get("given-names"); - String aFamilyNames = auth.vals.get("family-names"); - String aNameParticle = auth.vals.get("name-particle"); - String aAlias = auth.vals.get("alias"); - - if (aName != null) { - authors.add(aName); - } else if (aFamilyNames != null && aNameParticle != null && aGivenNames != null) { - authors.add(aGivenNames + " " + aNameParticle + " " + aFamilyNames); - } else if (aFamilyNames != null && aGivenNames != null) { - authors.add(aGivenNames + " " + aFamilyNames); - } else if (aFamilyNames != null) { - authors.add(aFamilyNames); - } else if (aAlias != null) { - authors.add(aAlias); + String authorStr = IntStream.range(0, citation.authors.size()) + .mapToObj(citation.authors::get) + .map((author) -> author.vals) + .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); + + if (entryMap.get(StandardField.DOI) == null && citation.ids != null) { + List doiIds = IntStream.range(0, citation.ids.size()) + .mapToObj(citation.ids::get) + .filter(id -> id.type.equals("doi")) + .collect(Collectors.toList()); + if (doiIds.size() == 1) { + entryMap.put(StandardField.DOI, doiIds.get(0).value); } } - String authorStr = String.join(", ", authors); - entryMap.put(StandardField.AUTHOR, authorStr); + if (citation.ids != null) { + List swhIds = IntStream.range(0, citation.ids.size()) + .mapToObj(citation.ids::get) + .filter(id -> id.type.equals("swh")) + .map(id -> id.value) + .collect(Collectors.toList()); + + if (swhIds.size() == 1) { + entryMap.put(StandardField.SWHID, swhIds.get(0)); + } else if (swhIds.size() > 1) { + List relSwhIds = swhIds.stream() + .filter(id -> id.split(":").length > 3) // quick filter for invalid swhids + .filter(id -> id.split(":")[2].equals("rel")) + .collect(Collectors.toList()); + if (relSwhIds.size() == 1) { + entryMap.put(StandardField.SWHID, relSwhIds.get(0)); + } + } + } BibEntry entry = new BibEntry(StandardEntryType.Software); entry.setField(entryMap); @@ -149,7 +184,7 @@ private HashMap getFieldMappings() { hm.put("abstract", StandardField.ABSTRACT); hm.put("message", StandardField.COMMENT); hm.put("date-released", StandardField.DATE); - + hm.put("keywords", StandardField.KEYWORDS); return hm; } } From b9d3da44e297cd79f5489b67c0e7d10fa07b786f Mon Sep 17 00:00:00 2001 From: aidan Date: Wed, 25 Aug 2021 00:55:07 -0500 Subject: [PATCH 4/8] Support for unmapped fields and dataset type, also more tests --- .../importer/fileformat/CffImporter.java | 15 ++- .../importer/fileformat/CffImporterTest.java | 122 +++++++++++++++++- .../fileformat/CffImporterTestDataset.cff | 26 ++++ .../fileformat/CffImporterTestDoiSelect.cff | 25 ++++ .../CffImporterTestUnknownFields.cff | 20 +++ .../CffImporterTestValidMultAuthors.cff | 23 ++++ .../CffImporterTestValidSwhIdSelect1.cff | 29 +++++ .../CffImporterTestValidSwhIdSelect2.cff | 26 ++++ 8 files changed, 283 insertions(+), 3 deletions(-) create mode 100644 src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestDataset.cff create mode 100644 src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestDoiSelect.cff create mode 100644 src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestUnknownFields.cff create mode 100644 src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestValidMultAuthors.cff create mode 100644 src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestValidSwhIdSelect1.cff create mode 100644 src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestValidSwhIdSelect2.cff diff --git a/src/main/java/org/jabref/logic/importer/fileformat/CffImporter.java b/src/main/java/org/jabref/logic/importer/fileformat/CffImporter.java index d059b80f4fc..5b636e7254f 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/CffImporter.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/CffImporter.java @@ -17,6 +17,7 @@ import org.jabref.model.entry.BibEntry; 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; @@ -95,14 +96,23 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException { ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); CffFormat citation = mapper.readValue(reader, CffFormat.class); HashMap entryMap = new HashMap(); + StandardEntryType entryType = StandardEntryType.Software; + // Map CFF fields to JabRef Fields HashMap fieldMap = getFieldMappings(); for (Map.Entry property : citation.vals.entrySet()) { if (fieldMap.containsKey(property.getKey())) { entryMap.put(fieldMap.get(property.getKey()), property.getValue()); + } else if (property.getKey().equals("type")) { + if (property.getValue().equals("dataset")) { + entryType = StandardEntryType.Dataset; + } + } else { + entryMap.put(new UnknownField(property.getKey()), property.getValue()); } } + // Translate CFF author format to JabRef author format String authorStr = IntStream.range(0, citation.authors.size()) .mapToObj(citation.authors::get) .map((author) -> author.vals) @@ -112,9 +122,9 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException { 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 doiIds = IntStream.range(0, citation.ids.size()) .mapToObj(citation.ids::get) @@ -125,6 +135,7 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException { } } + // Select SWHID to keep if (citation.ids != null) { List swhIds = IntStream.range(0, citation.ids.size()) .mapToObj(citation.ids::get) @@ -145,7 +156,7 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException { } } - BibEntry entry = new BibEntry(StandardEntryType.Software); + BibEntry entry = new BibEntry(entryType); entry.setField(entryMap); List entriesList = new ArrayList(); diff --git a/src/test/java/org/jabref/logic/importer/fileformat/CffImporterTest.java b/src/test/java/org/jabref/logic/importer/fileformat/CffImporterTest.java index f5b55890412..efc5f09a737 100644 --- a/src/test/java/org/jabref/logic/importer/fileformat/CffImporterTest.java +++ b/src/test/java/org/jabref/logic/importer/fileformat/CffImporterTest.java @@ -11,6 +11,8 @@ import org.jabref.logic.util.StandardFileType; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.field.StandardField; +import org.jabref.model.entry.field.UnknownField; +import org.jabref.model.entry.types.StandardEntryType; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -66,7 +68,7 @@ public void testIsRecognizedFormatReject() throws IOException, URISyntaxExceptio } @Test - public void testImportEntries() throws IOException, URISyntaxException { + public void testImportEntriesBasic() throws IOException, URISyntaxException { Path file = Path.of(CffImporterTest.class.getResource("CffImporterTestValid.cff").toURI()); List bibEntries = importer.importDatabase(file, StandardCharsets.UTF_8).getDatabase().getEntries(); BibEntry entry = bibEntries.get(0); @@ -83,4 +85,122 @@ public void testImportEntries() throws IOException, URISyntaxException { assertEquals(entry.getField(StandardField.VERSION), Optional.of("1.0")); } + + @Test + public void testImportEntriesMultipleAuthors() throws IOException, URISyntaxException { + Path file = Path.of(CffImporterTest.class.getResource("CffImporterTestValidMultAuthors.cff").toURI()); + List bibEntries = importer.importDatabase(file, StandardCharsets.UTF_8).getDatabase().getEntries(); + BibEntry entry = bibEntries.get(0); + + assertEquals(entry.getField(StandardField.AUTHOR), Optional.of("Joe van Smith and Bob Jones, Jr.")); + assertEquals(entry.getField(StandardField.TITLE), Optional.of("Test")); + assertEquals(entry.getField(StandardField.URL), Optional.of("www.google.com")); + assertEquals(entry.getField(StandardField.REPOSITORY), Optional.of("www.github.com")); + assertEquals(entry.getField(StandardField.DOI), Optional.of("10.0000/TEST")); + assertEquals(entry.getField(StandardField.DATE), Optional.of("2000-07-02")); + assertEquals(entry.getField(StandardField.COMMENT), Optional.of("Test entry.")); + assertEquals(entry.getField(StandardField.ABSTRACT), Optional.of("Test abstract.")); + assertEquals(entry.getField(StandardField.LICENSE), Optional.of("MIT")); + assertEquals(entry.getField(StandardField.VERSION), Optional.of("1.0")); + + } + + @Test + public void testImportEntriesSwhIdSelect1() throws IOException, URISyntaxException { + Path file = Path.of(CffImporterTest.class.getResource("CffImporterTestValidSwhIdSelect1.cff").toURI()); + List bibEntries = importer.importDatabase(file, StandardCharsets.UTF_8).getDatabase().getEntries(); + BibEntry entry = bibEntries.get(0); + + assertEquals(entry.getField(StandardField.AUTHOR), Optional.of("Joe van Smith and Bob Jones, Jr.")); + assertEquals(entry.getField(StandardField.TITLE), Optional.of("Test")); + assertEquals(entry.getField(StandardField.URL), Optional.of("www.google.com")); + assertEquals(entry.getField(StandardField.REPOSITORY), Optional.of("www.github.com")); + assertEquals(entry.getField(StandardField.DOI), Optional.of("10.0000/TEST")); + assertEquals(entry.getField(StandardField.DATE), Optional.of("2000-07-02")); + assertEquals(entry.getField(StandardField.COMMENT), Optional.of("Test entry.")); + assertEquals(entry.getField(StandardField.ABSTRACT), Optional.of("Test abstract.")); + assertEquals(entry.getField(StandardField.LICENSE), Optional.of("MIT")); + assertEquals(entry.getField(StandardField.VERSION), Optional.of("1.0")); + assertEquals(entry.getField(StandardField.SWHID), Optional.of("swh:1:rel:22ece559cc7cc2364edc5e5593d63ae8bd229f9f")); + + } + + @Test + public void testImportEntriesSwhIdSelect2() throws IOException, URISyntaxException { + Path file = Path.of(CffImporterTest.class.getResource("CffImporterTestValidSwhIdSelect2.cff").toURI()); + List bibEntries = importer.importDatabase(file, StandardCharsets.UTF_8).getDatabase().getEntries(); + BibEntry entry = bibEntries.get(0); + + assertEquals(entry.getField(StandardField.AUTHOR), Optional.of("Joe van Smith and Bob Jones, Jr.")); + assertEquals(entry.getField(StandardField.TITLE), Optional.of("Test")); + assertEquals(entry.getField(StandardField.URL), Optional.of("www.google.com")); + assertEquals(entry.getField(StandardField.REPOSITORY), Optional.of("www.github.com")); + assertEquals(entry.getField(StandardField.DOI), Optional.of("10.0000/TEST")); + assertEquals(entry.getField(StandardField.DATE), Optional.of("2000-07-02")); + assertEquals(entry.getField(StandardField.COMMENT), Optional.of("Test entry.")); + assertEquals(entry.getField(StandardField.ABSTRACT), Optional.of("Test abstract.")); + assertEquals(entry.getField(StandardField.LICENSE), Optional.of("MIT")); + assertEquals(entry.getField(StandardField.VERSION), Optional.of("1.0")); + assertEquals(entry.getField(StandardField.SWHID), Optional.of("swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2")); + + } + + @Test + public void testImportEntriesDataset() throws IOException, URISyntaxException { + Path file = Path.of(CffImporterTest.class.getResource("CffImporterTestDataset.cff").toURI()); + List bibEntries = importer.importDatabase(file, StandardCharsets.UTF_8).getDatabase().getEntries(); + BibEntry entry = bibEntries.get(0); + + assertEquals(entry.getField(StandardField.AUTHOR), Optional.of("Joe van Smith and Bob Jones, Jr.")); + assertEquals(entry.getField(StandardField.TITLE), Optional.of("Test")); + assertEquals(entry.getField(StandardField.URL), Optional.of("www.google.com")); + assertEquals(entry.getField(StandardField.REPOSITORY), Optional.of("www.github.com")); + assertEquals(entry.getField(StandardField.DOI), Optional.of("10.0000/TEST")); + assertEquals(entry.getField(StandardField.DATE), Optional.of("2000-07-02")); + assertEquals(entry.getField(StandardField.COMMENT), Optional.of("Test entry.")); + assertEquals(entry.getField(StandardField.ABSTRACT), Optional.of("Test abstract.")); + assertEquals(entry.getField(StandardField.LICENSE), Optional.of("MIT")); + assertEquals(entry.getField(StandardField.VERSION), Optional.of("1.0")); + + assertEquals(entry.getType(), StandardEntryType.Dataset); + + } + + @Test + public void testImportEntriesDoiSelect() throws IOException, URISyntaxException { + Path file = Path.of(CffImporterTest.class.getResource("CffImporterTestDoiSelect.cff").toURI()); + List bibEntries = importer.importDatabase(file, StandardCharsets.UTF_8).getDatabase().getEntries(); + BibEntry entry = bibEntries.get(0); + + assertEquals(entry.getField(StandardField.AUTHOR), Optional.of("Joe van Smith and Bob Jones, Jr.")); + assertEquals(entry.getField(StandardField.TITLE), Optional.of("Test")); + assertEquals(entry.getField(StandardField.URL), Optional.of("www.google.com")); + assertEquals(entry.getField(StandardField.REPOSITORY), Optional.of("www.github.com")); + assertEquals(entry.getField(StandardField.DOI), Optional.of("10.0000/TEST")); + assertEquals(entry.getField(StandardField.DATE), Optional.of("2000-07-02")); + assertEquals(entry.getField(StandardField.COMMENT), Optional.of("Test entry.")); + assertEquals(entry.getField(StandardField.ABSTRACT), Optional.of("Test abstract.")); + assertEquals(entry.getField(StandardField.LICENSE), Optional.of("MIT")); + assertEquals(entry.getField(StandardField.VERSION), Optional.of("1.0")); + + } + + @Test + public void testImportEntriesUnknownFields() throws IOException, URISyntaxException { + Path file = Path.of(CffImporterTest.class.getResource("CffImporterTestUnknownFields.cff").toURI()); + List bibEntries = importer.importDatabase(file, StandardCharsets.UTF_8).getDatabase().getEntries(); + BibEntry entry = bibEntries.get(0); + + assertEquals(entry.getField(StandardField.AUTHOR), Optional.of("Joe van Smith")); + assertEquals(entry.getField(StandardField.TITLE), Optional.of("Test")); + assertEquals(entry.getField(StandardField.URL), Optional.of("www.google.com")); + assertEquals(entry.getField(StandardField.REPOSITORY), Optional.of("www.github.com")); + assertEquals(entry.getField(StandardField.DOI), Optional.of("10.0000/TEST")); + assertEquals(entry.getField(StandardField.DATE), Optional.of("2000-07-02")); + assertEquals(entry.getField(StandardField.COMMENT), Optional.of("Test entry.")); + assertEquals(entry.getField(StandardField.ABSTRACT), Optional.of("Test abstract.")); + assertEquals(entry.getField(StandardField.LICENSE), Optional.of("MIT")); + assertEquals(entry.getField(StandardField.VERSION), Optional.of("1.0")); + assertEquals(entry.getField(new UnknownField("commit")), Optional.of("10ad")); + } } diff --git a/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestDataset.cff b/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestDataset.cff new file mode 100644 index 00000000000..aeb4d6f4db9 --- /dev/null +++ b/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestDataset.cff @@ -0,0 +1,26 @@ +# YAML 1.2 +--- +abstract: "Test abstract." +authors: + - + family-names: Smith + given-names: Joe + name-particle: van + - + family-names: Jones + given-names: Bob + name-suffix: Jr. +cff-version: "1.1.0" +date-released: 2000-07-02 +identifiers: + - + type: doi + value: "10.0000/TEST" +license: MIT +message: "Test entry." +title: Test +version: "1.0" +url: "www.google.com" +repository: "www.github.com" +type: dataset +... \ No newline at end of file diff --git a/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestDoiSelect.cff b/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestDoiSelect.cff new file mode 100644 index 00000000000..f2836b8d455 --- /dev/null +++ b/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestDoiSelect.cff @@ -0,0 +1,25 @@ +# YAML 1.2 +--- +abstract: "Test abstract." +authors: + - + family-names: Smith + given-names: Joe + name-particle: van + - + family-names: Jones + given-names: Bob + name-suffix: Jr. +cff-version: "1.1.0" +date-released: 2000-07-02 +identifiers: + - + type: doi + value: "10.0000/TEST" +license: MIT +message: "Test entry." +title: Test +version: "1.0" +url: "www.google.com" +repository: "www.github.com" +... \ No newline at end of file diff --git a/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestUnknownFields.cff b/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestUnknownFields.cff new file mode 100644 index 00000000000..df0b51c4cc6 --- /dev/null +++ b/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestUnknownFields.cff @@ -0,0 +1,20 @@ +# YAML 1.2 +--- +abstract: "Test abstract." +authors: + - + family-names: Smith + given-names: Joe + name-particle: van +cff-version: "1.1.0" +date-released: 2000-07-02 +doi: "10.0000/TEST" +identifiers: +license: MIT +message: "Test entry." +title: Test +version: "1.0" +url: "www.google.com" +repository: "www.github.com" +commit: 10ad +... \ No newline at end of file diff --git a/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestValidMultAuthors.cff b/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestValidMultAuthors.cff new file mode 100644 index 00000000000..1a625007e4c --- /dev/null +++ b/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestValidMultAuthors.cff @@ -0,0 +1,23 @@ +# YAML 1.2 +--- +abstract: "Test abstract." +authors: + - + family-names: Smith + given-names: Joe + name-particle: van + - + family-names: Jones + given-names: Bob + name-suffix: Jr. +cff-version: "1.1.0" +date-released: 2000-07-02 +doi: "10.0000/TEST" +identifiers: +license: MIT +message: "Test entry." +title: Test +version: "1.0" +url: "www.google.com" +repository: "www.github.com" +... \ No newline at end of file diff --git a/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestValidSwhIdSelect1.cff b/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestValidSwhIdSelect1.cff new file mode 100644 index 00000000000..b9bbc988ed8 --- /dev/null +++ b/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestValidSwhIdSelect1.cff @@ -0,0 +1,29 @@ +# YAML 1.2 +--- +abstract: "Test abstract." +authors: + - + family-names: Smith + given-names: Joe + name-particle: van + - + family-names: Jones + given-names: Bob + name-suffix: Jr. +cff-version: "1.1.0" +date-released: 2000-07-02 +doi: "10.0000/TEST" +identifiers: + - + type: swh + value: "swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2" + - + type: swh + value: "swh:1:rel:22ece559cc7cc2364edc5e5593d63ae8bd229f9f" +license: MIT +message: "Test entry." +title: Test +version: "1.0" +url: "www.google.com" +repository: "www.github.com" +... \ No newline at end of file diff --git a/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestValidSwhIdSelect2.cff b/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestValidSwhIdSelect2.cff new file mode 100644 index 00000000000..134d0e5f23c --- /dev/null +++ b/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestValidSwhIdSelect2.cff @@ -0,0 +1,26 @@ +# YAML 1.2 +--- +abstract: "Test abstract." +authors: + - + family-names: Smith + given-names: Joe + name-particle: van + - + family-names: Jones + given-names: Bob + name-suffix: Jr. +cff-version: "1.1.0" +date-released: 2000-07-02 +doi: "10.0000/TEST" +identifiers: + - + type: swh + value: "swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2" +license: MIT +message: "Test entry." +title: Test +version: "1.0" +url: "www.google.com" +repository: "www.github.com" +... \ No newline at end of file From 44dd0288ae7812811cc5ace544c2046597c75122 Mon Sep 17 00:00:00 2001 From: aidan Date: Sat, 28 Aug 2021 02:14:28 -0500 Subject: [PATCH 5/8] Simplified tests, minor fixes --- .../importer/fileformat/CffImporter.java | 13 +- .../importer/fileformat/CffImporterTest.java | 111 ++++++------------ .../CffImporterTestUnknownFields.cff | 10 +- 3 files changed, 54 insertions(+), 80 deletions(-) diff --git a/src/main/java/org/jabref/logic/importer/fileformat/CffImporter.java b/src/main/java/org/jabref/logic/importer/fileformat/CffImporter.java index 5b636e7254f..6857b813b56 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/CffImporter.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/CffImporter.java @@ -107,7 +107,7 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException { if (property.getValue().equals("dataset")) { entryType = StandardEntryType.Dataset; } - } else { + } else if (getUnmappedFields().contains(property.getKey())) { entryMap.put(new UnknownField(property.getKey()), property.getValue()); } } @@ -198,4 +198,15 @@ private HashMap getFieldMappings() { hm.put("keywords", StandardField.KEYWORDS); return hm; } + + private List getUnmappedFields() { + List fields = new ArrayList(); + + fields.add("commit"); + fields.add("license-url"); + fields.add("repository-code"); + fields.add("repository-artifact"); + + return fields; + } } diff --git a/src/test/java/org/jabref/logic/importer/fileformat/CffImporterTest.java b/src/test/java/org/jabref/logic/importer/fileformat/CffImporterTest.java index efc5f09a737..bf2b7574fa2 100644 --- a/src/test/java/org/jabref/logic/importer/fileformat/CffImporterTest.java +++ b/src/test/java/org/jabref/logic/importer/fileformat/CffImporterTest.java @@ -6,7 +6,6 @@ import java.nio.file.Path; import java.util.Arrays; import java.util.List; -import java.util.Optional; import org.jabref.logic.util.StandardFileType; import org.jabref.model.entry.BibEntry; @@ -73,17 +72,9 @@ public void testImportEntriesBasic() throws IOException, URISyntaxException { List bibEntries = importer.importDatabase(file, StandardCharsets.UTF_8).getDatabase().getEntries(); BibEntry entry = bibEntries.get(0); - assertEquals(entry.getField(StandardField.AUTHOR), Optional.of("Joe van Smith")); - assertEquals(entry.getField(StandardField.TITLE), Optional.of("Test")); - assertEquals(entry.getField(StandardField.URL), Optional.of("www.google.com")); - assertEquals(entry.getField(StandardField.REPOSITORY), Optional.of("www.github.com")); - assertEquals(entry.getField(StandardField.DOI), Optional.of("10.0000/TEST")); - assertEquals(entry.getField(StandardField.DATE), Optional.of("2000-07-02")); - assertEquals(entry.getField(StandardField.COMMENT), Optional.of("Test entry.")); - assertEquals(entry.getField(StandardField.ABSTRACT), Optional.of("Test abstract.")); - assertEquals(entry.getField(StandardField.LICENSE), Optional.of("MIT")); - assertEquals(entry.getField(StandardField.VERSION), Optional.of("1.0")); + BibEntry expected = getPopulatedEntry().withField(StandardField.AUTHOR, "Joe van Smith"); + assertEquals(entry, expected); } @Test @@ -92,16 +83,9 @@ public void testImportEntriesMultipleAuthors() throws IOException, URISyntaxExce List bibEntries = importer.importDatabase(file, StandardCharsets.UTF_8).getDatabase().getEntries(); BibEntry entry = bibEntries.get(0); - assertEquals(entry.getField(StandardField.AUTHOR), Optional.of("Joe van Smith and Bob Jones, Jr.")); - assertEquals(entry.getField(StandardField.TITLE), Optional.of("Test")); - assertEquals(entry.getField(StandardField.URL), Optional.of("www.google.com")); - assertEquals(entry.getField(StandardField.REPOSITORY), Optional.of("www.github.com")); - assertEquals(entry.getField(StandardField.DOI), Optional.of("10.0000/TEST")); - assertEquals(entry.getField(StandardField.DATE), Optional.of("2000-07-02")); - assertEquals(entry.getField(StandardField.COMMENT), Optional.of("Test entry.")); - assertEquals(entry.getField(StandardField.ABSTRACT), Optional.of("Test abstract.")); - assertEquals(entry.getField(StandardField.LICENSE), Optional.of("MIT")); - assertEquals(entry.getField(StandardField.VERSION), Optional.of("1.0")); + BibEntry expected = getPopulatedEntry(); + + assertEquals(entry, expected); } @@ -111,18 +95,9 @@ public void testImportEntriesSwhIdSelect1() throws IOException, URISyntaxExcepti List bibEntries = importer.importDatabase(file, StandardCharsets.UTF_8).getDatabase().getEntries(); BibEntry entry = bibEntries.get(0); - assertEquals(entry.getField(StandardField.AUTHOR), Optional.of("Joe van Smith and Bob Jones, Jr.")); - assertEquals(entry.getField(StandardField.TITLE), Optional.of("Test")); - assertEquals(entry.getField(StandardField.URL), Optional.of("www.google.com")); - assertEquals(entry.getField(StandardField.REPOSITORY), Optional.of("www.github.com")); - assertEquals(entry.getField(StandardField.DOI), Optional.of("10.0000/TEST")); - assertEquals(entry.getField(StandardField.DATE), Optional.of("2000-07-02")); - assertEquals(entry.getField(StandardField.COMMENT), Optional.of("Test entry.")); - assertEquals(entry.getField(StandardField.ABSTRACT), Optional.of("Test abstract.")); - assertEquals(entry.getField(StandardField.LICENSE), Optional.of("MIT")); - assertEquals(entry.getField(StandardField.VERSION), Optional.of("1.0")); - assertEquals(entry.getField(StandardField.SWHID), Optional.of("swh:1:rel:22ece559cc7cc2364edc5e5593d63ae8bd229f9f")); + BibEntry expected = getPopulatedEntry().withField(StandardField.SWHID, "swh:1:rel:22ece559cc7cc2364edc5e5593d63ae8bd229f9f"); + assertEquals(entry, expected); } @Test @@ -131,18 +106,9 @@ public void testImportEntriesSwhIdSelect2() throws IOException, URISyntaxExcepti List bibEntries = importer.importDatabase(file, StandardCharsets.UTF_8).getDatabase().getEntries(); BibEntry entry = bibEntries.get(0); - assertEquals(entry.getField(StandardField.AUTHOR), Optional.of("Joe van Smith and Bob Jones, Jr.")); - assertEquals(entry.getField(StandardField.TITLE), Optional.of("Test")); - assertEquals(entry.getField(StandardField.URL), Optional.of("www.google.com")); - assertEquals(entry.getField(StandardField.REPOSITORY), Optional.of("www.github.com")); - assertEquals(entry.getField(StandardField.DOI), Optional.of("10.0000/TEST")); - assertEquals(entry.getField(StandardField.DATE), Optional.of("2000-07-02")); - assertEquals(entry.getField(StandardField.COMMENT), Optional.of("Test entry.")); - assertEquals(entry.getField(StandardField.ABSTRACT), Optional.of("Test abstract.")); - assertEquals(entry.getField(StandardField.LICENSE), Optional.of("MIT")); - assertEquals(entry.getField(StandardField.VERSION), Optional.of("1.0")); - assertEquals(entry.getField(StandardField.SWHID), Optional.of("swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2")); + BibEntry expected = getPopulatedEntry().withField(StandardField.SWHID, "swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2"); + assertEquals(entry, expected); } @Test @@ -151,19 +117,10 @@ public void testImportEntriesDataset() throws IOException, URISyntaxException { List bibEntries = importer.importDatabase(file, StandardCharsets.UTF_8).getDatabase().getEntries(); BibEntry entry = bibEntries.get(0); - assertEquals(entry.getField(StandardField.AUTHOR), Optional.of("Joe van Smith and Bob Jones, Jr.")); - assertEquals(entry.getField(StandardField.TITLE), Optional.of("Test")); - assertEquals(entry.getField(StandardField.URL), Optional.of("www.google.com")); - assertEquals(entry.getField(StandardField.REPOSITORY), Optional.of("www.github.com")); - assertEquals(entry.getField(StandardField.DOI), Optional.of("10.0000/TEST")); - assertEquals(entry.getField(StandardField.DATE), Optional.of("2000-07-02")); - assertEquals(entry.getField(StandardField.COMMENT), Optional.of("Test entry.")); - assertEquals(entry.getField(StandardField.ABSTRACT), Optional.of("Test abstract.")); - assertEquals(entry.getField(StandardField.LICENSE), Optional.of("MIT")); - assertEquals(entry.getField(StandardField.VERSION), Optional.of("1.0")); - - assertEquals(entry.getType(), StandardEntryType.Dataset); + BibEntry expected = getPopulatedEntry(); + expected.setType(StandardEntryType.Dataset); + assertEquals(entry, expected); } @Test @@ -172,17 +129,9 @@ public void testImportEntriesDoiSelect() throws IOException, URISyntaxException List bibEntries = importer.importDatabase(file, StandardCharsets.UTF_8).getDatabase().getEntries(); BibEntry entry = bibEntries.get(0); - assertEquals(entry.getField(StandardField.AUTHOR), Optional.of("Joe van Smith and Bob Jones, Jr.")); - assertEquals(entry.getField(StandardField.TITLE), Optional.of("Test")); - assertEquals(entry.getField(StandardField.URL), Optional.of("www.google.com")); - assertEquals(entry.getField(StandardField.REPOSITORY), Optional.of("www.github.com")); - assertEquals(entry.getField(StandardField.DOI), Optional.of("10.0000/TEST")); - assertEquals(entry.getField(StandardField.DATE), Optional.of("2000-07-02")); - assertEquals(entry.getField(StandardField.COMMENT), Optional.of("Test entry.")); - assertEquals(entry.getField(StandardField.ABSTRACT), Optional.of("Test abstract.")); - assertEquals(entry.getField(StandardField.LICENSE), Optional.of("MIT")); - assertEquals(entry.getField(StandardField.VERSION), Optional.of("1.0")); + BibEntry expected = getPopulatedEntry(); + assertEquals(entry, expected); } @Test @@ -191,16 +140,26 @@ public void testImportEntriesUnknownFields() throws IOException, URISyntaxExcept List bibEntries = importer.importDatabase(file, StandardCharsets.UTF_8).getDatabase().getEntries(); BibEntry entry = bibEntries.get(0); - assertEquals(entry.getField(StandardField.AUTHOR), Optional.of("Joe van Smith")); - assertEquals(entry.getField(StandardField.TITLE), Optional.of("Test")); - assertEquals(entry.getField(StandardField.URL), Optional.of("www.google.com")); - assertEquals(entry.getField(StandardField.REPOSITORY), Optional.of("www.github.com")); - assertEquals(entry.getField(StandardField.DOI), Optional.of("10.0000/TEST")); - assertEquals(entry.getField(StandardField.DATE), Optional.of("2000-07-02")); - assertEquals(entry.getField(StandardField.COMMENT), Optional.of("Test entry.")); - assertEquals(entry.getField(StandardField.ABSTRACT), Optional.of("Test abstract.")); - assertEquals(entry.getField(StandardField.LICENSE), Optional.of("MIT")); - assertEquals(entry.getField(StandardField.VERSION), Optional.of("1.0")); - assertEquals(entry.getField(new UnknownField("commit")), Optional.of("10ad")); + BibEntry expected = getPopulatedEntry().withField(new UnknownField("commit"), "10ad"); + + assertEquals(entry, expected); + } + + public BibEntry getPopulatedEntry() { + BibEntry entry = new BibEntry(); + entry.setType(StandardEntryType.Software); + + entry.setField(StandardField.AUTHOR, "Joe van Smith and Bob Jones, Jr."); + entry.setField(StandardField.TITLE, "Test"); + entry.setField(StandardField.URL, "www.google.com"); + entry.setField(StandardField.REPOSITORY, "www.github.com"); + entry.setField(StandardField.DOI, "10.0000/TEST"); + entry.setField(StandardField.DATE, "2000-07-02"); + entry.setField(StandardField.COMMENT, "Test entry."); + entry.setField(StandardField.ABSTRACT, "Test abstract."); + entry.setField(StandardField.LICENSE, "MIT"); + entry.setField(StandardField.VERSION, "1.0"); + + return entry; } } diff --git a/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestUnknownFields.cff b/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestUnknownFields.cff index df0b51c4cc6..43008aab9dc 100644 --- a/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestUnknownFields.cff +++ b/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestUnknownFields.cff @@ -1,15 +1,19 @@ # YAML 1.2 --- abstract: "Test abstract." -authors: +authors: - family-names: Smith given-names: Joe name-particle: van + - + family-names: Jones + given-names: Bob + name-suffix: Jr. cff-version: "1.1.0" date-released: 2000-07-02 doi: "10.0000/TEST" -identifiers: +identifiers: license: MIT message: "Test entry." title: Test @@ -17,4 +21,4 @@ version: "1.0" url: "www.google.com" repository: "www.github.com" commit: 10ad -... \ No newline at end of file +... From 78eb210c610997529c25753e05e499fe1cfb76d6 Mon Sep 17 00:00:00 2001 From: aidan Date: Sat, 28 Aug 2021 02:20:56 -0500 Subject: [PATCH 6/8] CHANGELOG.md update --- CHANGELOG.md | 1 + .../java/org/jabref/logic/importer/fileformat/CffImporter.java | 1 + 2 files changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fd26fa03dce..087fb80f7d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve ### Added +- We added import support for CFF files. [#7945](https://github.com/JabRef/jabref/issues/7945) - We added the option to copy the DOI of an entry directly from the context menu copy submenu. [#7826](https://github.com/JabRef/jabref/issues/7826) - We added a fulltext search feature. [#2838](https://github.com/JabRef/jabref/pull/2838) diff --git a/src/main/java/org/jabref/logic/importer/fileformat/CffImporter.java b/src/main/java/org/jabref/logic/importer/fileformat/CffImporter.java index 6857b813b56..530af25d707 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/CffImporter.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/CffImporter.java @@ -50,6 +50,7 @@ public String getDescription() { return "Importer for the CFF format. Is only used to cite software, one entry per file."; } + // POJO classes for yaml data private static class CffFormat { private HashMap vals = new HashMap(); From 33329c893395bf301755076afe912cbf9c4e62c8 Mon Sep 17 00:00:00 2001 From: aidan Date: Tue, 31 Aug 2021 23:38:09 -0500 Subject: [PATCH 7/8] code style fixes --- .../importer/fileformat/CffImporter.java | 41 ++++++++----------- 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/src/main/java/org/jabref/logic/importer/fileformat/CffImporter.java b/src/main/java/org/jabref/logic/importer/fileformat/CffImporter.java index 530af25d707..71610a3c2f6 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/CffImporter.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/CffImporter.java @@ -7,7 +7,6 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; -import java.util.stream.IntStream; import org.jabref.logic.importer.Importer; import org.jabref.logic.importer.ParserResult; @@ -114,8 +113,7 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException { } // Translate CFF author format to JabRef author format - String authorStr = IntStream.range(0, citation.authors.size()) - .mapToObj(citation.authors::get) + String authorStr = citation.authors.stream() .map((author) -> author.vals) .map((vals) -> vals.get("name") != null ? new Author(vals.get("name"), "", "", "", "") : @@ -127,8 +125,7 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException { // Select DOI to keep if (entryMap.get(StandardField.DOI) == null && citation.ids != null) { - List doiIds = IntStream.range(0, citation.ids.size()) - .mapToObj(citation.ids::get) + List doiIds = citation.ids.stream() .filter(id -> id.type.equals("doi")) .collect(Collectors.toList()); if (doiIds.size() == 1) { @@ -138,8 +135,7 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException { // Select SWHID to keep if (citation.ids != null) { - List swhIds = IntStream.range(0, citation.ids.size()) - .mapToObj(citation.ids::get) + List swhIds = citation.ids.stream() .filter(id -> id.type.equals("swh")) .map(id -> id.value) .collect(Collectors.toList()); @@ -174,30 +170,25 @@ public boolean isRecognizedFormat(BufferedReader reader) throws IOException { try { citation = mapper.readValue(reader, CffFormat.class); + return citation != null && citation.vals.get("title") != null; } catch (IOException e) { return false; } - - if (citation != null && citation.vals.get("title") != null) { - return true; - } else { - return false; - } } private HashMap getFieldMappings() { - HashMap hm = new HashMap(); - hm.put("title", StandardField.TITLE); - hm.put("version", StandardField.VERSION); - hm.put("doi", StandardField.DOI); - hm.put("license", StandardField.LICENSE); - hm.put("repository", StandardField.REPOSITORY); - hm.put("url", StandardField.URL); - hm.put("abstract", StandardField.ABSTRACT); - hm.put("message", StandardField.COMMENT); - hm.put("date-released", StandardField.DATE); - hm.put("keywords", StandardField.KEYWORDS); - return hm; + HashMap fieldMappings = new HashMap(); + fieldMappings.put("title", StandardField.TITLE); + fieldMappings.put("version", StandardField.VERSION); + fieldMappings.put("doi", StandardField.DOI); + fieldMappings.put("license", StandardField.LICENSE); + fieldMappings.put("repository", StandardField.REPOSITORY); + fieldMappings.put("url", StandardField.URL); + fieldMappings.put("abstract", StandardField.ABSTRACT); + fieldMappings.put("message", StandardField.COMMENT); + fieldMappings.put("date-released", StandardField.DATE); + fieldMappings.put("keywords", StandardField.KEYWORDS); + return fieldMappings; } private List getUnmappedFields() { From 4a9d13dae5b985afa77b3cabb03399f72c0c1cf6 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage Date: Wed, 1 Sep 2021 12:20:53 +0200 Subject: [PATCH 8/8] Fixed minor style issues --- .../importer/fileformat/CffImporter.java | 25 ++++++++----------- .../importer/fileformat/CffImporterTest.java | 4 +-- .../fileformat/CffImporterTestDataset.cff | 8 +++--- .../fileformat/CffImporterTestDoiSelect.cff | 8 +++--- .../fileformat/CffImporterTestInvalid1.cff | 2 +- .../fileformat/CffImporterTestInvalid2.cff | 2 +- .../fileformat/CffImporterTestValid.cff | 6 ++--- .../CffImporterTestValidMultAuthors.cff | 6 ++--- .../CffImporterTestValidSwhIdSelect1.cff | 10 ++++---- .../CffImporterTestValidSwhIdSelect2.cff | 8 +++--- 10 files changed, 38 insertions(+), 41 deletions(-) diff --git a/src/main/java/org/jabref/logic/importer/fileformat/CffImporter.java b/src/main/java/org/jabref/logic/importer/fileformat/CffImporter.java index 71610a3c2f6..c683841ae77 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/CffImporter.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/CffImporter.java @@ -23,11 +23,8 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public class CffImporter extends Importer { - private static final Logger LOGGER = LoggerFactory.getLogger(CffImporter.class); @Override public String getName() { @@ -51,7 +48,7 @@ public String getDescription() { // POJO classes for yaml data private static class CffFormat { - private HashMap vals = new HashMap(); + private final HashMap values = new HashMap<>(); @JsonProperty("authors") private List authors; @@ -64,19 +61,19 @@ public CffFormat() { @JsonAnySetter private void setValues(String key, String value) { - vals.put(key, value); + values.put(key, value); } } private static class CffAuthor { - private HashMap vals = new HashMap(); + private final HashMap values = new HashMap<>(); public CffAuthor() { } @JsonAnySetter private void setValues(String key, String value) { - vals.put(key, value); + values.put(key, value); } } @@ -95,12 +92,12 @@ public CffIdentifier() { public ParserResult importDatabase(BufferedReader reader) throws IOException { ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); CffFormat citation = mapper.readValue(reader, CffFormat.class); - HashMap entryMap = new HashMap(); + HashMap entryMap = new HashMap<>(); StandardEntryType entryType = StandardEntryType.Software; // Map CFF fields to JabRef Fields HashMap fieldMap = getFieldMappings(); - for (Map.Entry property : citation.vals.entrySet()) { + for (Map.Entry property : citation.values.entrySet()) { if (fieldMap.containsKey(property.getKey())) { entryMap.put(fieldMap.get(property.getKey()), property.getValue()); } else if (property.getKey().equals("type")) { @@ -114,7 +111,7 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException { // Translate CFF author format to JabRef author format String authorStr = citation.authors.stream() - .map((author) -> author.vals) + .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"), @@ -156,7 +153,7 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException { BibEntry entry = new BibEntry(entryType); entry.setField(entryMap); - List entriesList = new ArrayList(); + List entriesList = new ArrayList<>(); entriesList.add(entry); return new ParserResult(entriesList); @@ -170,14 +167,14 @@ public boolean isRecognizedFormat(BufferedReader reader) throws IOException { try { citation = mapper.readValue(reader, CffFormat.class); - return citation != null && citation.vals.get("title") != null; + return citation != null && citation.values.get("title") != null; } catch (IOException e) { return false; } } private HashMap getFieldMappings() { - HashMap fieldMappings = new HashMap(); + HashMap fieldMappings = new HashMap<>(); fieldMappings.put("title", StandardField.TITLE); fieldMappings.put("version", StandardField.VERSION); fieldMappings.put("doi", StandardField.DOI); @@ -192,7 +189,7 @@ private HashMap getFieldMappings() { } private List getUnmappedFields() { - List fields = new ArrayList(); + List fields = new ArrayList<>(); fields.add("commit"); fields.add("license-url"); diff --git a/src/test/java/org/jabref/logic/importer/fileformat/CffImporterTest.java b/src/test/java/org/jabref/logic/importer/fileformat/CffImporterTest.java index bf2b7574fa2..5dd61a6d262 100644 --- a/src/test/java/org/jabref/logic/importer/fileformat/CffImporterTest.java +++ b/src/test/java/org/jabref/logic/importer/fileformat/CffImporterTest.java @@ -46,8 +46,8 @@ public void testsGetExtensions() { @Test public void testGetDescription() { - assertEquals("Importer for the CFF format. Is only used to cite software, one " - + "entry per file.", importer.getDescription()); + assertEquals("Importer for the CFF format. Is only used to cite software, one entry per file.", + importer.getDescription()); } @Test diff --git a/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestDataset.cff b/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestDataset.cff index aeb4d6f4db9..8900971cb80 100644 --- a/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestDataset.cff +++ b/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestDataset.cff @@ -1,7 +1,7 @@ # YAML 1.2 --- abstract: "Test abstract." -authors: +authors: - family-names: Smith given-names: Joe @@ -12,8 +12,8 @@ authors: name-suffix: Jr. cff-version: "1.1.0" date-released: 2000-07-02 -identifiers: - - +identifiers: + - type: doi value: "10.0000/TEST" license: MIT @@ -23,4 +23,4 @@ version: "1.0" url: "www.google.com" repository: "www.github.com" type: dataset -... \ No newline at end of file +... diff --git a/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestDoiSelect.cff b/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestDoiSelect.cff index f2836b8d455..28993253709 100644 --- a/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestDoiSelect.cff +++ b/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestDoiSelect.cff @@ -1,7 +1,7 @@ # YAML 1.2 --- abstract: "Test abstract." -authors: +authors: - family-names: Smith given-names: Joe @@ -12,8 +12,8 @@ authors: name-suffix: Jr. cff-version: "1.1.0" date-released: 2000-07-02 -identifiers: - - +identifiers: + - type: doi value: "10.0000/TEST" license: MIT @@ -22,4 +22,4 @@ title: Test version: "1.0" url: "www.google.com" repository: "www.github.com" -... \ No newline at end of file +... diff --git a/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestInvalid1.cff b/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestInvalid1.cff index cb263660960..be4a0e39ec4 100644 --- a/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestInvalid1.cff +++ b/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestInvalid1.cff @@ -1,4 +1,4 @@ # YAML 1.2 --- test: 123 -... \ No newline at end of file +... diff --git a/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestInvalid2.cff b/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestInvalid2.cff index 3f090015215..c75d438d6aa 100644 --- a/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestInvalid2.cff +++ b/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestInvalid2.cff @@ -1 +1 @@ -aosdoioifjosdfikbasjc \ No newline at end of file +aosdoioifjosdfikbasjc diff --git a/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestValid.cff b/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestValid.cff index 780c9ee529a..472ba55169d 100644 --- a/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestValid.cff +++ b/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestValid.cff @@ -1,7 +1,7 @@ # YAML 1.2 --- abstract: "Test abstract." -authors: +authors: - family-names: Smith given-names: Joe @@ -9,11 +9,11 @@ authors: cff-version: "1.1.0" date-released: 2000-07-02 doi: "10.0000/TEST" -identifiers: +identifiers: license: MIT message: "Test entry." title: Test version: "1.0" url: "www.google.com" repository: "www.github.com" -... \ No newline at end of file +... diff --git a/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestValidMultAuthors.cff b/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestValidMultAuthors.cff index 1a625007e4c..f69962c1760 100644 --- a/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestValidMultAuthors.cff +++ b/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestValidMultAuthors.cff @@ -1,7 +1,7 @@ # YAML 1.2 --- abstract: "Test abstract." -authors: +authors: - family-names: Smith given-names: Joe @@ -13,11 +13,11 @@ authors: cff-version: "1.1.0" date-released: 2000-07-02 doi: "10.0000/TEST" -identifiers: +identifiers: license: MIT message: "Test entry." title: Test version: "1.0" url: "www.google.com" repository: "www.github.com" -... \ No newline at end of file +... diff --git a/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestValidSwhIdSelect1.cff b/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestValidSwhIdSelect1.cff index b9bbc988ed8..2944a7a7ef2 100644 --- a/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestValidSwhIdSelect1.cff +++ b/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestValidSwhIdSelect1.cff @@ -1,7 +1,7 @@ # YAML 1.2 --- abstract: "Test abstract." -authors: +authors: - family-names: Smith given-names: Joe @@ -13,11 +13,11 @@ authors: cff-version: "1.1.0" date-released: 2000-07-02 doi: "10.0000/TEST" -identifiers: - - +identifiers: + - type: swh value: "swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2" - - + - type: swh value: "swh:1:rel:22ece559cc7cc2364edc5e5593d63ae8bd229f9f" license: MIT @@ -26,4 +26,4 @@ title: Test version: "1.0" url: "www.google.com" repository: "www.github.com" -... \ No newline at end of file +... diff --git a/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestValidSwhIdSelect2.cff b/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestValidSwhIdSelect2.cff index 134d0e5f23c..eaa2e655c2a 100644 --- a/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestValidSwhIdSelect2.cff +++ b/src/test/resources/org/jabref/logic/importer/fileformat/CffImporterTestValidSwhIdSelect2.cff @@ -1,7 +1,7 @@ # YAML 1.2 --- abstract: "Test abstract." -authors: +authors: - family-names: Smith given-names: Joe @@ -13,8 +13,8 @@ authors: cff-version: "1.1.0" date-released: 2000-07-02 doi: "10.0000/TEST" -identifiers: - - +identifiers: + - type: swh value: "swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2" license: MIT @@ -23,4 +23,4 @@ title: Test version: "1.0" url: "www.google.com" repository: "www.github.com" -... \ No newline at end of file +...