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

Added option to import CFF files #7946

Merged
merged 9 commits into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Field, String> entryMap = new HashMap<Field, String>();
StandardEntryType entryType = StandardEntryType.Software;

// Map CFF fields to JabRef Fields
HashMap<String, StandardField> fieldMap = getFieldMappings();
for (Map.Entry<String, String> 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)
Expand All @@ -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<CffIdentifier> doiIds = IntStream.range(0, citation.ids.size())
calixtus marked this conversation as resolved.
Show resolved Hide resolved
.mapToObj(citation.ids::get)
Expand All @@ -125,6 +135,7 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException {
}
}

// Select SWHID to keep
if (citation.ids != null) {
List<String> swhIds = IntStream.range(0, citation.ids.size())
calixtus marked this conversation as resolved.
Show resolved Hide resolved
.mapToObj(citation.ids::get)
Expand All @@ -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<BibEntry> entriesList = new ArrayList<BibEntry>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<BibEntry> bibEntries = importer.importDatabase(file, StandardCharsets.UTF_8).getDatabase().getEntries();
BibEntry entry = bibEntries.get(0);
Expand All @@ -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<BibEntry> 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."));
koppor marked this conversation as resolved.
Show resolved Hide resolved
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<BibEntry> 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<BibEntry> 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<BibEntry> 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<BibEntry> 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<BibEntry> 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"));
}
}
Original file line number Diff line number Diff line change
@@ -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
...
Original file line number Diff line number Diff line change
@@ -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"
...
Original file line number Diff line number Diff line change
@@ -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
...
Original file line number Diff line number Diff line change
@@ -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"
...
Original file line number Diff line number Diff line change
@@ -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"
...
Original file line number Diff line number Diff line change
@@ -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"
...