|
| 1 | +package net.sf.jabref.logic.integrity; |
| 2 | + |
| 3 | +import java.util.Collections; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Objects; |
| 6 | +import java.util.Optional; |
| 7 | +import java.util.function.Predicate; |
| 8 | +import java.util.regex.Pattern; |
| 9 | + |
| 10 | +import net.sf.jabref.BibDatabaseContext; |
| 11 | +import net.sf.jabref.logic.integrity.IntegrityCheck.Checker; |
| 12 | +import net.sf.jabref.logic.l10n.Localization; |
| 13 | +import net.sf.jabref.model.entry.BibEntry; |
| 14 | +import net.sf.jabref.model.entry.FieldName; |
| 15 | + |
| 16 | +public class EditionChecker implements Checker { |
| 17 | + |
| 18 | + private static final Predicate<String> FIRST_LETTER_CAPITALIZED = Pattern.compile("^[A-Z]").asPredicate(); |
| 19 | + private static final Predicate<String> ONLY_NUMERALS_OR_LITERALS = Pattern.compile("^([0-9]+|[^0-9]+)$") |
| 20 | + .asPredicate(); |
| 21 | + |
| 22 | + private final BibDatabaseContext bibDatabaseContextEdition; |
| 23 | + |
| 24 | + |
| 25 | + public EditionChecker(BibDatabaseContext bibDatabaseContext) { |
| 26 | + this.bibDatabaseContextEdition = Objects.requireNonNull(bibDatabaseContext); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Checks, if field contains only an integer or a literal (BibLaTeX mode) |
| 31 | + * Checks, if the first letter is capitalized (BibTeX mode) |
| 32 | + * BibLaTex: |
| 33 | + * The edition of a printed publication. This must be an integer, not an ordinal. |
| 34 | + * It is also possible to give the edition as a literal string, for example "Third, revised and expanded edition". |
| 35 | + * Official bibtex spec: |
| 36 | + * The edition of a book-for example, "Second". |
| 37 | + * This should be an ordinal, and should have the first letter capitalized. |
| 38 | + */ |
| 39 | + @Override |
| 40 | + public List<IntegrityMessage> check(BibEntry entry) { |
| 41 | + Optional<String> value = entry.getField(FieldName.EDITION); |
| 42 | + if (!value.isPresent()) { |
| 43 | + return Collections.emptyList(); |
| 44 | + } |
| 45 | + |
| 46 | + //BibLaTeX |
| 47 | + if (bibDatabaseContextEdition.isBiblatexMode() && !ONLY_NUMERALS_OR_LITERALS.test(value.get().trim())) { |
| 48 | + return Collections.singletonList(new IntegrityMessage( |
| 49 | + Localization.lang("should contain an integer or a literal"), entry, FieldName.EDITION)); |
| 50 | + } |
| 51 | + |
| 52 | + //BibTeX |
| 53 | + if (!bibDatabaseContextEdition.isBiblatexMode() && !FIRST_LETTER_CAPITALIZED.test(value.get().trim())) { |
| 54 | + return Collections.singletonList(new IntegrityMessage( |
| 55 | + Localization.lang("should have the first letter capitalized"), entry, FieldName.EDITION)); |
| 56 | + } |
| 57 | + |
| 58 | + return Collections.emptyList(); |
| 59 | + } |
| 60 | +} |
0 commit comments