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 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
190 changes: 190 additions & 0 deletions src/main/java/org/jabref/logic/importer/fileformat/CffImporter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
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 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;
import org.jabref.model.entry.types.StandardEntryType;

import com.fasterxml.jackson.annotation.JsonAnySetter;
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() {
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<String, String> vals = new HashMap<String, String>();

@JsonProperty("authors")
private List<CffAuthor> authors;

@JsonProperty("identifiers")
private List<CffIdentifier> ids;

public CffFormat() {
}

@JsonAnySetter
private void setValues(String key, String value) {
vals.put(key, value);
}
}

private static class CffAuthor {
private HashMap<String, String> vals = new HashMap<String, String>();

public CffAuthor() {
}

@JsonAnySetter
private void setValues(String key, String value) {
vals.put(key, 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());
CffFormat citation = mapper.readValue(reader, CffFormat.class);
HashMap<Field, String> entryMap = new HashMap<Field, String>();

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());
}
}

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<CffIdentifier> doiIds = IntStream.range(0, citation.ids.size())
calixtus marked this conversation as resolved.
Show resolved Hide resolved
.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);
}
}

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)
.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<String> relSwhIds = swhIds.stream()
.filter(id -> id.split(":").length > 3) // quick filter for invalid swhids
.filter(id -> id.split(":")[2].equals("rel"))
.collect(Collectors.toList());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here you can also use findFirst again as above

if (relSwhIds.size() == 1) {
entryMap.put(StandardField.SWHID, relSwhIds.get(0));
}
}
}

BibEntry entry = new BibEntry(StandardEntryType.Software);
entry.setField(entryMap);

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

return new ParserResult(entriesList);
}

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

ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
CffFormat citation;

try {
citation = mapper.readValue(reader, CffFormat.class);
} catch (IOException e) {
return false;
}

if (citation != null && citation.vals.get("title") != null) {
calixtus marked this conversation as resolved.
Show resolved Hide resolved
return true;
} else {
return false;
}
}

private HashMap<String, StandardField> getFieldMappings() {
HashMap<String, StandardField> hm = new HashMap<String, StandardField>();
calixtus marked this conversation as resolved.
Show resolved Hide resolved
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;
}
}
1 change: 1 addition & 0 deletions src/main/java/org/jabref/logic/util/StandardFileType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> extensions;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> 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<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"));

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# YAML 1.2
---
test: 123
...
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
aosdoioifjosdfikbasjc
Original file line number Diff line number Diff line change
@@ -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"
...