Verified Owner
Top Fields Constructors Methods
org.jabref.logic.citationkeypattern

public Class CitationKeyGenerator

extends BracketedPattern
Class Inheritance
Imports
java.util.Arrays, .List, .Objects, .Optional, java.util.function.Function, java.util.regex.PatternSyntaxException, org.jabref.logic.util.strings.StringUtil, .Transliteration, org.jabref.model.FieldChange, org.jabref.model.database.BibDatabase, .BibDatabaseContext, org.jabref.model.entry.BibEntry, org.jabref.model.entry.types.EntryType, com.google.common.annotations.VisibleForTesting, com.google.common.base.CharMatcher, org.jspecify.annotations.NonNull, org.slf4j.Logger, .LoggerFactory

Field Summary

Modifier and TypeField and Description
public static final String
APPENDIX_CHARACTERS

All single characters that we can use for extending a key to make it unique.

private final CitationKeyPatternPreferences
private final AbstractCitationKeyPatterns
private final BibDatabase
public static final List<Character>
DISALLOWED_CHARACTERS

Source of disallowed characters: https://tex.stackexchange.com/a/408548/9075 These characters are disallowed in BibTeX keys.

private final Character
private static final Logger
private final String

Constructor Summary

AccessConstructor and Description
public
CitationKeyGenerator(BibDatabaseContext bibDatabaseContext, CitationKeyPatternPreferences citationKeyPatternPreferences)

public
private
CitationKeyGenerator(AbstractCitationKeyPatterns citeKeyPattern, BibDatabase database, CitationKeyPatternPreferences citationKeyPatternPreferences, Character keywordSeparator)

Method Summary

Modifier and TypeMethod and Description
private String

Returns:

a key, if needed, with an appended letter
appendLettersToKey
(String
the new key
key
,
String
the old key
oldKey
)

A letter will be appended to the key based on the user's preferences, either always or to prevent duplicated keys.

public static String
cleanKey(String key, String unwantedCharacters)

private String
private Function<String, String>

Returns:

a cleaned citation key for the given BibEntry
expandBracketContent
(BibEntry
the BibEntry that a citation key is generated for
entry
)

A helper method to create a Function that takes a single bracketed expression, expands it, and cleans the key.

public Optional<FieldChange>

Returns:

the change to the key (or an empty optional if the key was not changed)
generateAndSetKey
(BibEntry
the entry to generate the key for
entry
)

Generates a citation key for the given entry, and sets the key.

public String

Returns:

a citation key based on the user's preferences
generateKey
(@NonNull BibEntry entry)

Generate a citation key for the given BibEntry.

private static String

Returns:

The String to append.
getAppendix
(int
The appendix number.
number
)

Computes an appendix to a citation key that could make it unique.

private static int
normalizeSuperscriptOrSubscript(int codePoint)

Maps Unicode superscript and subscript digits to their ASCII counterparts (e.g., ¹ → 1, ₂ → 2).

pack-priv static String
removeUnwantedCharacters(String key, String unwantedCharacters)

public static String

Returns:

the key with illegal and unwanted characters removed
removeUnwantedCharactersWithKeepDiacritics
(String
the citation key to sanitize
key
,
String unwantedCharacters)

Removes characters, unwanted characters that are illegal in citation keys preserving Unicode (e.g., ²) and diacritical characters.

private String

Returns:

the citation key where matches to the regex are replaced
replaceWithRegex
(String
the citation key
key
)

Using preferences, replace matches to the provided regex with a string.

public String
Inherited from org.jabref.logic.citationkeypattern.BracketedPattern:
allAuthorsapplyModifiersauthAuthEaauthEtalauthIniNauthorsAlphaauthorsAlphaLNIauthShortcamelizeSignificantWordsInTitleexpandexpandexpandexpandBracketContentexpandBracketsexpandBracketsfirstPagegenerateInstitutionKeygetCamelizedTitlegetCamelizedTitle_NgetFieldValuegetTitleWordsjoinAuthorsOnLastNamelastAuthorlastPageoneAuthorPlusInitialspagePrefixparseFieldAndModifiersremoveSmallWordstoString

Field Detail

APPENDIX_CHARACTERSback to summary
public static final String APPENDIX_CHARACTERS

All single characters that we can use for extending a key to make it unique.

citationKeyPatternPreferencesback to summary
private final CitationKeyPatternPreferences citationKeyPatternPreferences
citeKeyPatternback to summary
private final AbstractCitationKeyPatterns citeKeyPattern
databaseback to summary
private final BibDatabase database
DISALLOWED_CHARACTERSback to summary
public static final List<Character> DISALLOWED_CHARACTERS

Source of disallowed characters: https://tex.stackexchange.com/a/408548/9075 These characters are disallowed in BibTeX keys.

keywordSeparatorback to summary
private final Character keywordSeparator
LOGGERback to summary
private static final Logger LOGGER

Hides org.jabref.logic.citationkeypattern.BracketedPattern.LOGGER.

unwantedCharactersback to summary
private final String unwantedCharacters

Constructor Detail

CitationKeyGeneratorback to summary
public CitationKeyGenerator(BibDatabaseContext bibDatabaseContext, CitationKeyPatternPreferences citationKeyPatternPreferences)
CitationKeyGeneratorback to summary
public CitationKeyGenerator(@NonNull AbstractCitationKeyPatterns citeKeyPattern, @NonNull BibDatabase database, @NonNull CitationKeyPatternPreferences citationKeyPatternPreferences)
CitationKeyGeneratorback to summary
private CitationKeyGenerator(AbstractCitationKeyPatterns citeKeyPattern, BibDatabase database, CitationKeyPatternPreferences citationKeyPatternPreferences, Character keywordSeparator)

Method Detail

appendLettersToKeyback to summary
private String appendLettersToKey(String key, String oldKey)

A letter will be appended to the key based on the user's preferences, either always or to prevent duplicated keys.

Parameters
key:String

the new key

oldKey:String

the old key

Returns:String

a key, if needed, with an appended letter

cleanKeyback to summary
public static String cleanKey(String key, String unwantedCharacters)
createCitationKeyFromPatternback to summary
private String createCitationKeyFromPattern(BibEntry entry)
expandBracketContentback to summary
private Function<String, String> expandBracketContent(BibEntry entry)

A helper method to create a Function that takes a single bracketed expression, expands it, and cleans the key.

Parameters
entry:BibEntry

the BibEntry that a citation key is generated for

Returns:Function<String, String>

a cleaned citation key for the given BibEntry

generateAndSetKeyback to summary
public Optional<FieldChange> generateAndSetKey(BibEntry entry)

Generates a citation key for the given entry, and sets the key.

Parameters
entry:BibEntry

the entry to generate the key for

Returns:Optional<FieldChange>

the change to the key (or an empty optional if the key was not changed)

generateKeyback to summary
public String generateKey(@NonNull BibEntry entry)

Generate a citation key for the given BibEntry.

Parameters
entry:@NonNull BibEntry

a BibEntry

Returns:String

a citation key based on the user's preferences

getAppendixback to summary
private static String getAppendix(int number)

Computes an appendix to a citation key that could make it unique. We use a-z for numbers 0-25, and then aa-az, ba-bz, etc.

Parameters
number:int

The appendix number.

Returns:String

The String to append.

normalizeSuperscriptOrSubscriptback to summary
private static int normalizeSuperscriptOrSubscript(int codePoint)

Maps Unicode superscript and subscript digits to their ASCII counterparts (e.g., ¹ → 1, ₂ → 2). Other characters in the Superscripts and Subscripts block (e.g., ⁿ ⁱ ⁺ ⁻ ⁽ ⁾) are stripped by returning -1. All other code points are returned unchanged.

removeUnwantedCharactersback to summary
pack-priv static String removeUnwantedCharacters(String key, String unwantedCharacters)
Annotations
@VisibleForTesting
removeUnwantedCharactersWithKeepDiacriticsback to summary
public static String removeUnwantedCharactersWithKeepDiacritics(String key, String unwantedCharacters)

Removes characters, unwanted characters that are illegal in citation keys preserving Unicode (e.g., ²) and diacritical characters.

Parameters
key:String

the citation key to sanitize

unwantedCharacters:String

- pass through org.jabref.logic.citationkeypattern.CitationKeyPatternPreferences#getUnwantedCharacters()

Returns:String

the key with illegal and unwanted characters removed

replaceWithRegexback to summary
private String replaceWithRegex(String key)

Using preferences, replace matches to the provided regex with a string.

Parameters
key:String

the citation key

Returns:String

the citation key where matches to the regex are replaced

transliterateIfNeededback to summary
public String transliterateIfNeeded(String key)