diff --git a/CHANGELOG.md b/CHANGELOG.md index 149ea8d8bc7..8a28d0813a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve ### Added - We added support for searching ShortScience for an entry through the user's browser. [#6018](https://github.com/JabRef/jabref/pull/6018) +- We updated EditionChecker to permit edition to start with a number. [#6144](https://github.com/JabRef/jabref/issues/6144) - We added tooltips for most fields in the entry editor containing a short description. [#5847](https://github.com/JabRef/jabref/issues/5847) ### Changed diff --git a/src/main/java/org/jabref/logic/integrity/EditionChecker.java b/src/main/java/org/jabref/logic/integrity/EditionChecker.java index fe5a0faabe3..d7a46603c0b 100644 --- a/src/main/java/org/jabref/logic/integrity/EditionChecker.java +++ b/src/main/java/org/jabref/logic/integrity/EditionChecker.java @@ -13,28 +13,24 @@ public class EditionChecker implements ValueChecker { private static final Predicate FIRST_LETTER_CAPITALIZED = Pattern.compile("^[A-Z]").asPredicate(); private static final Predicate ONLY_NUMERALS_OR_LITERALS = Pattern.compile("^([0-9]+|[^0-9].+)$") - .asPredicate(); + .asPredicate(); private static final Predicate ONLY_NUMERALS = Pattern.compile("[0-9]+").asPredicate(); private static final String FIRST_EDITION = "1"; private final BibDatabaseContext bibDatabaseContextEdition; private final boolean allowIntegerEdition; - public EditionChecker(BibDatabaseContext bibDatabaseContext, boolean allowIntegerEdition) { this.bibDatabaseContextEdition = Objects.requireNonNull(bibDatabaseContext); this.allowIntegerEdition = allowIntegerEdition; } /** - * Checks, if field contains only an integer or a literal (biblatex mode) - * Checks, if the first letter is capitalized (BibTeX mode) - * biblatex package documentation: - * The edition of a printed publication. This must be an integer, not an ordinal. - * It is also possible to give the edition as a literal string, for example "Third, revised and expanded edition". - * Official BibTeX specification: - * The edition of a book-for example, "Second". - * This should be an ordinal, and should have the first letter capitalized. + * Checks, if field contains only an integer or a literal (biblatex mode) Checks, if the first letter is capitalized + * (BibTeX mode) biblatex package documentation: The edition of a printed publication. This must be an integer, not + * an ordinal. It is also possible to give the edition as a literal string, for example "Third, revised and expanded + * edition". Official BibTeX specification: The edition of a book-for example, "Second". This should be an ordinal, + * and should have the first letter capitalized. */ @Override public Optional checkValue(String value) { @@ -53,17 +49,19 @@ public Optional checkValue(String value) { //BibTeX if (!bibDatabaseContextEdition.isBiblatexMode()) { - if (!allowIntegerEdition) { - if (!FIRST_LETTER_CAPITALIZED.test(value.trim())) { - return Optional.of(Localization.lang("should have the first letter capitalized")); - } + if (!isFirstCharDigit(value) && (!allowIntegerEdition) && !FIRST_LETTER_CAPITALIZED.test(value.trim())) { + return Optional.of(Localization.lang("should have the first letter capitalized")); } else { if (!ONLY_NUMERALS.test(value.trim()) && !FIRST_LETTER_CAPITALIZED.test(value.trim())) { return Optional.of(Localization.lang("should have the first letter capitalized")); } } } - return Optional.empty(); } + + boolean isFirstCharDigit(String input) { + char[] array = input.toCharArray(); + return Character.isDigit(array[0]); + } } diff --git a/src/test/java/org/jabref/logic/integrity/EditionCheckerTest.java b/src/test/java/org/jabref/logic/integrity/EditionCheckerTest.java new file mode 100644 index 00000000000..afb6c1e172e --- /dev/null +++ b/src/test/java/org/jabref/logic/integrity/EditionCheckerTest.java @@ -0,0 +1,28 @@ +package org.jabref.logic.integrity; + +import org.jabref.model.database.BibDatabaseContext; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class EditionCheckerTest { + @Test + void isFirstCharacterANumber() { + boolean allowIntegerEdition = false; + var bibDatabaseContextEdition = new BibDatabaseContext(); + var editionChecker = new EditionChecker(bibDatabaseContextEdition, allowIntegerEdition); + var stringWithNumber = "0HelloWorld"; + assertTrue(editionChecker.isFirstCharDigit(stringWithNumber)); + } + + @Test + void isFirstCharacterNotANumber() { + boolean allowIntegerEdition = false; + var bibDatabaseContextEdition = new BibDatabaseContext(); + var editionChecker = new EditionChecker(bibDatabaseContextEdition, allowIntegerEdition); + var stringWithLetter = "HelloWorld"; + assertFalse(editionChecker.isFirstCharDigit(stringWithLetter)); + } +}