Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edition should not start capitalize letter #6149

Merged
merged 13 commits into from
Mar 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 13 additions & 15 deletions src/main/java/org/jabref/logic/integrity/EditionChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,24 @@ public class EditionChecker implements ValueChecker {

private static final Predicate<String> FIRST_LETTER_CAPITALIZED = Pattern.compile("^[A-Z]").asPredicate();
private static final Predicate<String> ONLY_NUMERALS_OR_LITERALS = Pattern.compile("^([0-9]+|[^0-9].+)$")
.asPredicate();
.asPredicate();
private static final Predicate<String> 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<String> checkValue(String value) {
Expand All @@ -53,17 +49,19 @@ public Optional<String> 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]);
fabgio marked this conversation as resolved.
Show resolved Hide resolved
}
}
28 changes: 28 additions & 0 deletions src/test/java/org/jabref/logic/integrity/EditionCheckerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.jabref.logic.integrity;

import org.jabref.model.database.BibDatabaseContext;

fabgio marked this conversation as resolved.
Show resolved Hide resolved
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));
}

fabgio marked this conversation as resolved.
Show resolved Hide resolved
@Test
void isFirstCharacterNotANumber() {
boolean allowIntegerEdition = false;
var bibDatabaseContextEdition = new BibDatabaseContext();
var editionChecker = new EditionChecker(bibDatabaseContextEdition, allowIntegerEdition);
var stringWithLetter = "HelloWorld";
assertFalse(editionChecker.isFirstCharDigit(stringWithLetter));
}
}