|
| 1 | +package org.jabref.logic.exporter; |
| 2 | + |
| 3 | +import java.io.FileWriter; |
| 4 | +import java.io.IOException; |
| 5 | +import java.nio.charset.StandardCharsets; |
| 6 | +import java.nio.file.Path; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.LinkedHashMap; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.Objects; |
| 13 | +import java.util.Optional; |
| 14 | + |
| 15 | +import org.jabref.logic.util.StandardFileType; |
| 16 | +import org.jabref.model.database.BibDatabaseContext; |
| 17 | +import org.jabref.model.entry.Author; |
| 18 | +import org.jabref.model.entry.AuthorList; |
| 19 | +import org.jabref.model.entry.BibEntry; |
| 20 | +import org.jabref.model.entry.Date; |
| 21 | +import org.jabref.model.entry.field.BiblatexSoftwareField; |
| 22 | +import org.jabref.model.entry.field.Field; |
| 23 | +import org.jabref.model.entry.field.StandardField; |
| 24 | +import org.jabref.model.entry.field.UnknownField; |
| 25 | +import org.jabref.model.entry.types.EntryType; |
| 26 | +import org.jabref.model.entry.types.StandardEntryType; |
| 27 | + |
| 28 | +import org.yaml.snakeyaml.DumperOptions; |
| 29 | +import org.yaml.snakeyaml.Yaml; |
| 30 | + |
| 31 | +public class CffExporter extends Exporter { |
| 32 | + // Fields that are taken 1:1 from BibTeX to CFF |
| 33 | + public static final List<String> UNMAPPED_FIELDS = List.of( |
| 34 | + "abbreviation", "collection-doi", "collection-title", "collection-type", "commit", "copyright", |
| 35 | + "data-type", "database", "date-accessed", "date-downloaded", "date-published", "department", "end", |
| 36 | + "entry", "filename", "format", "issue-date", "issue-title", "license-url", "loc-end", "loc-start", |
| 37 | + "medium", "nihmsid", "number-volumes", "patent-states", "pmcid", "repository-artifact", "repository-code", |
| 38 | + "scope", "section", "start", "term", "thesis-type", "volume-title", "year-original" |
| 39 | + ); |
| 40 | + |
| 41 | + public static final Map<Field, String> FIELDS_MAP = Map.ofEntries( |
| 42 | + Map.entry(StandardField.ABSTRACT, "abstract"), |
| 43 | + Map.entry(StandardField.DATE, "date-released"), |
| 44 | + Map.entry(StandardField.DOI, "doi"), |
| 45 | + Map.entry(StandardField.KEYWORDS, "keywords"), |
| 46 | + Map.entry(BiblatexSoftwareField.LICENSE, "license"), |
| 47 | + Map.entry(StandardField.COMMENT, "message"), |
| 48 | + Map.entry(BiblatexSoftwareField.REPOSITORY, "repository"), |
| 49 | + Map.entry(StandardField.TITLE, "title"), |
| 50 | + Map.entry(StandardField.URL, "url"), |
| 51 | + Map.entry(StandardField.VERSION, "version"), |
| 52 | + Map.entry(StandardField.EDITION, "edition"), |
| 53 | + Map.entry(StandardField.ISBN, "isbn"), |
| 54 | + Map.entry(StandardField.ISSN, "issn"), |
| 55 | + Map.entry(StandardField.ISSUE, "issue"), |
| 56 | + Map.entry(StandardField.JOURNAL, "journal"), |
| 57 | + Map.entry(StandardField.MONTH, "month"), |
| 58 | + Map.entry(StandardField.NOTE, "notes"), |
| 59 | + Map.entry(StandardField.NUMBER, "number"), |
| 60 | + Map.entry(StandardField.PAGES, "pages"), |
| 61 | + Map.entry(StandardField.PUBSTATE, "status"), |
| 62 | + Map.entry(StandardField.VOLUME, "volume"), |
| 63 | + Map.entry(StandardField.YEAR, "year") |
| 64 | + ); |
| 65 | + |
| 66 | + public static final Map<EntryType, String> TYPES_MAP = Map.ofEntries( |
| 67 | + Map.entry(StandardEntryType.Article, "article"), |
| 68 | + Map.entry(StandardEntryType.Book, "book"), |
| 69 | + Map.entry(StandardEntryType.Booklet, "pamphlet"), |
| 70 | + Map.entry(StandardEntryType.Proceedings, "conference"), |
| 71 | + Map.entry(StandardEntryType.InProceedings, "conference-paper"), |
| 72 | + Map.entry(StandardEntryType.Misc, "misc"), |
| 73 | + Map.entry(StandardEntryType.Manual, "manual"), |
| 74 | + Map.entry(StandardEntryType.Software, "software"), |
| 75 | + Map.entry(StandardEntryType.Dataset, "dataset"), |
| 76 | + Map.entry(StandardEntryType.Report, "report"), |
| 77 | + Map.entry(StandardEntryType.Unpublished, "unpublished") |
| 78 | + ); |
| 79 | + |
| 80 | + public CffExporter() { |
| 81 | + super("cff", "CFF", StandardFileType.CFF); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public void export(BibDatabaseContext databaseContext, Path file, List<BibEntry> entries) throws Exception { |
| 86 | + Objects.requireNonNull(databaseContext); |
| 87 | + Objects.requireNonNull(file); |
| 88 | + Objects.requireNonNull(entries); |
| 89 | + |
| 90 | + // Do not export if no entries to export -- avoids exports with only template text |
| 91 | + if (entries.isEmpty()) { |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + // Make a copy of the list to avoid modifying the original list |
| 96 | + final List<BibEntry> entriesToTransform = new ArrayList<>(entries); |
| 97 | + |
| 98 | + // Set up YAML options |
| 99 | + DumperOptions options = new DumperOptions(); |
| 100 | + options.setWidth(Integer.MAX_VALUE); |
| 101 | + options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); |
| 102 | + options.setPrettyFlow(true); |
| 103 | + options.setIndentWithIndicator(true); |
| 104 | + options.setIndicatorIndent(2); |
| 105 | + Yaml yaml = new Yaml(options); |
| 106 | + |
| 107 | + BibEntry main = null; |
| 108 | + boolean mainIsDummy = false; |
| 109 | + int countOfSoftwareAndDataSetEntries = 0; |
| 110 | + for (BibEntry entry : entriesToTransform) { |
| 111 | + if (entry.getType() == StandardEntryType.Software || entry.getType() == StandardEntryType.Dataset) { |
| 112 | + main = entry; |
| 113 | + countOfSoftwareAndDataSetEntries++; |
| 114 | + } |
| 115 | + } |
| 116 | + if (countOfSoftwareAndDataSetEntries == 1) { |
| 117 | + // If there is only one software or dataset entry, use it as the main entry |
| 118 | + entriesToTransform.remove(main); |
| 119 | + } else { |
| 120 | + // If there are no software or dataset entries, create a dummy main entry holding the given entries |
| 121 | + main = new BibEntry(StandardEntryType.Software); |
| 122 | + mainIsDummy = true; |
| 123 | + } |
| 124 | + |
| 125 | + // Transform main entry to CFF format |
| 126 | + Map<String, Object> cffData = transformEntry(main, true, mainIsDummy); |
| 127 | + |
| 128 | + // Preferred citation |
| 129 | + if (main.hasField(StandardField.CITES)) { |
| 130 | + String citeKey = main.getField(StandardField.CITES).orElse("").split(",")[0]; |
| 131 | + List<BibEntry> citedEntries = databaseContext.getDatabase().getEntriesByCitationKey(citeKey); |
| 132 | + entriesToTransform.removeAll(citedEntries); |
| 133 | + if (!citedEntries.isEmpty()) { |
| 134 | + BibEntry citedEntry = citedEntries.getFirst(); |
| 135 | + cffData.put("preferred-citation", transformEntry(citedEntry, false, false)); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + // References |
| 140 | + List<Map<String, Object>> related = new ArrayList<>(); |
| 141 | + if (main.hasField(StandardField.RELATED)) { |
| 142 | + main.getEntryLinkList(StandardField.RELATED, databaseContext.getDatabase()) |
| 143 | + .stream() |
| 144 | + .map(link -> link.getLinkedEntry()) |
| 145 | + .filter(Optional::isPresent) |
| 146 | + .map(Optional::get) |
| 147 | + .forEach(entry -> { |
| 148 | + related.add(transformEntry(entry, false, false)); |
| 149 | + entriesToTransform.remove(entry); |
| 150 | + }); |
| 151 | + } |
| 152 | + |
| 153 | + // Add remaining entries as references |
| 154 | + for (BibEntry entry : entriesToTransform) { |
| 155 | + related.add(transformEntry(entry, false, false)); |
| 156 | + } |
| 157 | + if (!related.isEmpty()) { |
| 158 | + cffData.put("references", related); |
| 159 | + } |
| 160 | + |
| 161 | + try (FileWriter writer = new FileWriter(file.toFile(), StandardCharsets.UTF_8)) { |
| 162 | + yaml.dump(cffData, writer); |
| 163 | + } catch (IOException ex) { |
| 164 | + throw new SaveException(ex); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + private Map<String, Object> transformEntry(BibEntry entry, boolean main, boolean dummy) { |
| 169 | + Map<String, Object> cffData = new LinkedHashMap<>(); |
| 170 | + Map<Field, String> fields = new HashMap<>(entry.getFieldMap()); |
| 171 | + |
| 172 | + if (main) { |
| 173 | + // Mandatory CFF version field |
| 174 | + cffData.put("cff-version", "1.2.0"); |
| 175 | + |
| 176 | + // Mandatory message field |
| 177 | + String message = fields.getOrDefault(StandardField.COMMENT, |
| 178 | + "If you use this software, please cite it using the metadata from this file."); |
| 179 | + cffData.put("message", message); |
| 180 | + fields.remove(StandardField.COMMENT); |
| 181 | + } |
| 182 | + |
| 183 | + // Mandatory title field |
| 184 | + String title = fields.getOrDefault(StandardField.TITLE, "No title specified."); |
| 185 | + cffData.put("title", title); |
| 186 | + fields.remove(StandardField.TITLE); |
| 187 | + |
| 188 | + // Mandatory authors field |
| 189 | + List<Author> authors = AuthorList.parse(fields.getOrDefault(StandardField.AUTHOR, "")) |
| 190 | + .getAuthors(); |
| 191 | + parseAuthors(cffData, authors); |
| 192 | + fields.remove(StandardField.AUTHOR); |
| 193 | + |
| 194 | + // Type |
| 195 | + if (!dummy) { |
| 196 | + cffData.put("type", TYPES_MAP.getOrDefault(entry.getType(), "misc")); |
| 197 | + } |
| 198 | + |
| 199 | + // Keywords |
| 200 | + String keywords = fields.getOrDefault(StandardField.KEYWORDS, null); |
| 201 | + if (keywords != null) { |
| 202 | + cffData.put("keywords", keywords.split(",\\s*")); |
| 203 | + } |
| 204 | + fields.remove(StandardField.KEYWORDS); |
| 205 | + |
| 206 | + // Date |
| 207 | + String date = fields.getOrDefault(StandardField.DATE, null); |
| 208 | + if (date != null) { |
| 209 | + parseDate(cffData, date); |
| 210 | + } |
| 211 | + fields.remove(StandardField.DATE); |
| 212 | + |
| 213 | + // Remaining fields not handled above |
| 214 | + for (Field field : fields.keySet()) { |
| 215 | + if (FIELDS_MAP.containsKey(field)) { |
| 216 | + cffData.put(FIELDS_MAP.get(field), fields.get(field)); |
| 217 | + } else if (field instanceof UnknownField) { |
| 218 | + // Check that field is accepted by CFF format specification |
| 219 | + if (UNMAPPED_FIELDS.contains(field.getName())) { |
| 220 | + cffData.put(field.getName(), fields.get(field)); |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | + return cffData; |
| 225 | + } |
| 226 | + |
| 227 | + private void parseAuthors(Map<String, Object> data, List<Author> authors) { |
| 228 | + List<Map<String, String>> authorsList = new ArrayList<>(); |
| 229 | + authors.forEach(author -> { |
| 230 | + Map<String, String> authorMap = new LinkedHashMap<>(); |
| 231 | + if (author.getFamilyName().isPresent()) { |
| 232 | + authorMap.put("family-names", author.getFamilyName().get()); |
| 233 | + } |
| 234 | + if (author.getGivenName().isPresent()) { |
| 235 | + authorMap.put("given-names", author.getGivenName().get()); |
| 236 | + } |
| 237 | + if (author.getNamePrefix().isPresent()) { |
| 238 | + authorMap.put("name-particle", author.getNamePrefix().get()); |
| 239 | + } |
| 240 | + if (author.getNameSuffix().isPresent()) { |
| 241 | + authorMap.put("name-suffix", author.getNameSuffix().get()); |
| 242 | + } |
| 243 | + authorsList.add(authorMap); |
| 244 | + }); |
| 245 | + data.put("authors", authorsList.isEmpty() ? List.of(Map.of("name", "/")) : authorsList); |
| 246 | + } |
| 247 | + |
| 248 | + private void parseDate(Map<String, Object> data, String date) { |
| 249 | + Optional<Date> parsedDateOpt = Date.parse(date); |
| 250 | + if (parsedDateOpt.isEmpty()) { |
| 251 | + data.put("issue-date", date); |
| 252 | + return; |
| 253 | + } |
| 254 | + Date parsedDate = parsedDateOpt.get(); |
| 255 | + if (parsedDate.getYear().isPresent() && parsedDate.getMonth().isPresent() && parsedDate.getDay().isPresent()) { |
| 256 | + data.put("date-released", parsedDate.getNormalized()); |
| 257 | + return; |
| 258 | + } |
| 259 | + parsedDate.getMonth().ifPresent(month -> data.put("month", month.getNumber())); |
| 260 | + parsedDate.getYear().ifPresent(year -> data.put("year", year)); |
| 261 | + } |
| 262 | +} |
| 263 | + |
0 commit comments