Verified Owner
Top Description Fields Constructors Methods
org.jabref.logic.importer.fileformat

public Class HayagrivaImporter

extends Importer
Class Inheritance
Annotations
@NullMarked
Imports
java.io.BufferedReader, .IOException, .Reader, java.util.ArrayList, .List, .Locale, .Map, .Optional, .Set, java.util.regex.Matcher, .Pattern, java.util.stream.Collectors, org.jabref.logic.importer.Importer, .ParserResult, org.jabref.logic.l10n.Localization, org.jabref.logic.util.FileType, .StandardFileType, org.jabref.model.entry.BibEntry, org.jabref.model.entry.field.FieldFactory, .StandardField, .UnknownField, org.jabref.model.entry.types.EntryType, .StandardEntryType, org.jspecify.annotations.NullMarked, .Nullable, tools.jackson.core.JacksonException, .StreamReadFeature, tools.jackson.databind.JsonNode, tools.jackson.dataformat.yaml.YAMLMapper

Importer for the Hayagriva YAML format used by Typst (https://github.com/typst/hayagriva).

Hayagriva allows most fields to take either a plain scalar or a structured form: title and url may be a string or a map with a value key, author/editor may be a string, a list of strings, or a list of maps, parent may be a map or a list of maps, and serial-number may be a scalar or a map. Parsing therefore works on the untyped node tree instead of fixed POJOs, extracting what JabRef can represent and ignoring the rest.

Field Summary

Modifier and TypeField and Description
private static final Set<EntryType>
private static final Set<String>
private static final Set<String>
private static final YAMLMapper
private static final int
MAX_LOOKAHEAD_CHARS

Generous room for leading comments plus the first entry, whose type key conventionally appears within its first few lines.

private static final Set<String>
RECOGNIZED_TYPES

All entry types of the Hayagriva specification (https://github.com/typst/hayagriva/blob/main/docs/file-format.md#entry-type); used for format recognition.

private static final Pattern
private static final Map<String, EntryType>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private void
applyAffiliated(BibEntry bibEntry, @Nullable JsonNode affiliatedNode)

affiliated lists further people by their role (director, translator, ...).

private void
applyParents(BibEntry bibEntry, @Nullable JsonNode parentNode)

A Hayagriva parent describes what an entry is contained in and has no direct BibTeX equivalent.

private Optional<String>
formatPerson(JsonNode personNode)

Hayagriva person strings already use BibTeX's structured "Family, Given" form and are kept verbatim; the structured map form is converted to it.

private static Optional<String>
formattedText(@Nullable JsonNode node)

Reads a Hayagriva "formattable string": either a plain scalar or a map holding the actual string under value (next to short, verbatim, ...

public String
getDescription()

Implements abstract org.jabref.logic.importer.Importer.getDescription.

Returns the description of the import format.

public FileType
getFileType()

Implements abstract org.jabref.logic.importer.Importer.getFileType.

Returns the type of files that this importer can read

public String
getId()

Implements abstract org.jabref.logic.importer.Importer.getId.

Returns a slug, which identifies this importer.

public String
getName()

Implements abstract org.jabref.logic.importer.Importer.getName.

Returns the name of this import format.

public ParserResult
importDatabase(BufferedReader input)

Implements abstract org.jabref.logic.importer.Importer.importDatabase.

Parse the database in the source.

public boolean
isRecognizedFormat(BufferedReader input)

Overrides org.jabref.logic.importer.Importer.isRecognizedFormat.

Lightweight recognition for the BufferedReader overload, which must reset the reader afterwards.

public boolean
isRecognizedFormat(Reader input)

Overrides org.jabref.logic.importer.Importer.isRecognizedFormat.

Full recognition for the Reader overload, which has no reset requirement, so a complete (but untyped) YAML parse is safe here.

private Optional<String>
persons(@Nullable JsonNode node)

Persons may be a single scalar, a list of scalars, a map, or a list of maps.

private static Optional<String>
scalarText(@Nullable JsonNode node)

Returns the text of a scalar node (string, number, boolean); empty for containers and null.

private static void
private BibEntry
toBibEntry(String citationKey, JsonNode entryNode)

Inherited from org.jabref.logic.importer.Importer:
compareToequalsgetCharsetgetReadergetReaderhashCodeimportDatabaseimportDatabaseisRecognizedFormatisRecognizedFormatsupportsFileExtensiontoString

Field Detail

BOOK_PART_TYPESback to summary
private static final Set<EntryType> BOOK_PART_TYPES
BOOKTITLE_PARENT_TYPESback to summary
private static final Set<String> BOOKTITLE_PARENT_TYPES
JOURNAL_PARENT_TYPESback to summary
private static final Set<String> JOURNAL_PARENT_TYPES
MAPPERback to summary
private static final YAMLMapper MAPPER
MAX_LOOKAHEAD_CHARSback to summary
private static final int MAX_LOOKAHEAD_CHARS

Generous room for leading comments plus the first entry, whose type key conventionally appears within its first few lines.

RECOGNIZED_TYPESback to summary
private static final Set<String> RECOGNIZED_TYPES

All entry types of the Hayagriva specification (https://github.com/typst/hayagriva/blob/main/docs/file-format.md#entry-type); used for format recognition. Types without a BibTeX equivalent in TYPES_MAP are imported as StandardEntryType#Misc.

TYPE_LINE_PATTERNback to summary
private static final Pattern TYPE_LINE_PATTERN
TYPES_MAPback to summary
private static final Map<String, EntryType> TYPES_MAP

Constructor Detail

HayagrivaImporterback to summary
public HayagrivaImporter()

Method Detail

applyAffiliatedback to summary
private void applyAffiliated(BibEntry bibEntry, @Nullable JsonNode affiliatedNode)

affiliated lists further people by their role (director, translator, ...). Roles with a matching BibTeX field (e.g. translator) land there; the rest are kept as custom fields named after the role, so no contributor information is lost on import.

applyParentsback to summary
private void applyParents(BibEntry bibEntry, @Nullable JsonNode parentNode)

A Hayagriva parent describes what an entry is contained in and has no direct BibTeX equivalent. The parent's type picks the target field, mirroring the Hayagriva exporter layout where possible (journal is exported as a periodical parent, series as a book parent). A missing or unmapped parent type falls back by entry type: articles are most likely contained in a journal, everything else in some book-like container. The first parent providing a value for a field wins; nested parents are not descended into.

formatPersonback to summary
private Optional<String> formatPerson(JsonNode personNode)

Hayagriva person strings already use BibTeX's structured "Family, Given" form and are kept verbatim; the structured map form is converted to it.

formattedTextback to summary
private static Optional<String> formattedText(@Nullable JsonNode node)

Reads a Hayagriva "formattable string": either a plain scalar or a map holding the actual string under value (next to short, verbatim, ... which JabRef cannot represent).

getDescriptionback to summary
public String getDescription()

Implements abstract org.jabref.logic.importer.Importer.getDescription.

Doc from org.jabref.logic.importer.Importer.getDescription.

Returns the description of the import format.

The description should specify
what kind of entries from what sources and based on what specification it is able to import

by what criteria it isRecognizedFormat(BufferedReader) recognizes an import format

Annotations
@Override
getFileTypeback to summary
public FileType getFileType()

Implements abstract org.jabref.logic.importer.Importer.getFileType.

Doc from org.jabref.logic.importer.Importer.getFileType.

Returns the type of files that this importer can read

Annotations
@Override
getIdback to summary
public String getId()

Implements abstract org.jabref.logic.importer.Importer.getId.

Doc from org.jabref.logic.importer.Importer.getId.

Returns a slug, which identifies this importer. Used, for example, to identify the importer in CLI.

Typically, this name should be short, in English, and should not contain special characters like #, %, etc.

Annotations
@Override
getNameback to summary
public String getName()

Implements abstract org.jabref.logic.importer.Importer.getName.

Doc from org.jabref.logic.importer.Importer.getName.

Returns the name of this import format. Typically, this is a string that denotes file type or format. It can also be localized, like "XMP-annotated PDF".

Annotations
@Override
importDatabaseback to summary
public ParserResult importDatabase(BufferedReader input) throws IOException

Implements abstract org.jabref.logic.importer.Importer.importDatabase.

Doc from org.jabref.logic.importer.Importer.importDatabase.

Parse the database in the source.

This method can be called in two different contexts - either when importing in a specified format, or when importing in unknown format. In the latter case, JabRef cycles through all available import formats. No error messages or feedback is displayed from individual import formats in this case.

If importing in a specified format and an empty library is returned, JabRef reports that no entries were found.

If your format is binary-based (PDF, ZIP-based, or others), then you should not solely override this method. For binary formats do this:

  1. Throw UnsupportedOperationException in this method.
  2. Override the method Importer#importDatabase(Path). Example of this workaround is in: org.jabref.logic.importer.fileformat.pdf.PdfImporter.
Annotations
@Override
isRecognizedFormatback to summary
public boolean isRecognizedFormat(BufferedReader input) throws IOException

Overrides org.jabref.logic.importer.Importer.isRecognizedFormat.

Lightweight recognition for the BufferedReader overload, which must reset the reader afterwards. Instead of doing a full YAML parse (which could read past the marked read-ahead limit and make reset() throw), this scans the first MAX_LOOKAHEAD_CHARS characters for a type: line matching a known Hayagriva entry type. The block read never exceeds the marked limit, so reset() is always possible (a line-based read could exceed it on a single overlong line).

Annotations
@Override
isRecognizedFormatback to summary
public boolean isRecognizedFormat(Reader input) throws IOException

Overrides org.jabref.logic.importer.Importer.isRecognizedFormat.

Full recognition for the Reader overload, which has no reset requirement, so a complete (but untyped) YAML parse is safe here.

Annotations
@Override
personsback to summary
private Optional<String> persons(@Nullable JsonNode node)

Persons may be a single scalar, a list of scalars, a map, or a list of maps.

scalarTextback to summary
private static Optional<String> scalarText(@Nullable JsonNode node)

Returns the text of a scalar node (string, number, boolean); empty for containers and null.

setIfAbsentback to summary
private static void setIfAbsent(BibEntry entry, StandardField field, String value)
toBibEntryback to summary
private BibEntry toBibEntry(String citationKey, JsonNode entryNode)