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

Group all checker which only check the value of one field #2437

Merged
merged 2 commits into from
Jan 15, 2017
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
Original file line number Diff line number Diff line change
@@ -1,34 +1,17 @@
package net.sf.jabref.logic.integrity;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

import net.sf.jabref.logic.integrity.IntegrityCheck.Checker;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.model.entry.BibEntry;

public class AbbreviationChecker implements Checker {

private final String field;


public AbbreviationChecker(String field) {
this.field = field;
}
public class AbbreviationChecker implements ValueChecker {

@Override
public List<IntegrityMessage> check(BibEntry entry) {
Optional<String> value = entry.getField(field);
if (!value.isPresent()) {
return Collections.emptyList();
}

if (value.get().contains(".")) {
return Collections
.singletonList(new IntegrityMessage(Localization.lang("abbreviation detected"), entry, field));
public Optional<String> checkValue(String value) {
if (value.contains(".")) {
return Optional.of(Localization.lang("abbreviation detected"));
}

return Collections.emptyList();
return Optional.empty();
}
}
36 changes: 0 additions & 36 deletions src/main/java/net/sf/jabref/logic/integrity/AuthorNameChecker.java

This file was deleted.

This file was deleted.

22 changes: 5 additions & 17 deletions src/main/java/net/sf/jabref/logic/integrity/BooktitleChecker.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
package net.sf.jabref.logic.integrity;

import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Optional;

import net.sf.jabref.logic.integrity.IntegrityCheck.Checker;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.FieldName;

public class BooktitleChecker implements Checker {
public class BooktitleChecker implements ValueChecker {

@Override
public List<IntegrityMessage> check(BibEntry entry) {
String field = FieldName.BOOKTITLE;
Optional<String> value = entry.getField(field);
if (!value.isPresent()) {
return Collections.emptyList();
public Optional<String> checkValue(String value) {
if (value.toLowerCase(Locale.ENGLISH).endsWith("conference on")) {
return Optional.of(Localization.lang("booktitle ends with 'conference on'"));
}

if (value.get().toLowerCase(Locale.ENGLISH).endsWith("conference on")) {
return Collections.singletonList(
new IntegrityMessage(Localization.lang("booktitle ends with 'conference on'"), entry, field));
}

return Collections.emptyList();
return Optional.empty();
}
}
31 changes: 6 additions & 25 deletions src/main/java/net/sf/jabref/logic/integrity/BracketChecker.java
Original file line number Diff line number Diff line change
@@ -1,50 +1,31 @@
package net.sf.jabref.logic.integrity;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

import net.sf.jabref.logic.integrity.IntegrityCheck.Checker;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.model.entry.BibEntry;

public class BracketChecker implements Checker {

private final String field;


public BracketChecker(String field) {
this.field = field;
}
public class BracketChecker implements ValueChecker {

@Override
public List<IntegrityMessage> check(BibEntry entry) {
Optional<String> value = entry.getField(field);
if (!value.isPresent()) {
return Collections.emptyList();
}

public Optional<String> checkValue(String value) {
// metaphor: integer-based stack (push + / pop -)
int counter = 0;
for (char a : value.get().trim().toCharArray()) {
for (char a : value.trim().toCharArray()) {
if (a == '{') {
counter++;
} else if (a == '}') {
if (counter == 0) {
return Collections.singletonList(
new IntegrityMessage(Localization.lang("unexpected closing curly bracket"), entry, field));
return Optional.of(Localization.lang("unexpected closing curly bracket"));
} else {
counter--;
}
}
}

if (counter > 0) {
return Collections.singletonList(
new IntegrityMessage(Localization.lang("unexpected opening curly bracket"), entry, field));
return Optional.of(Localization.lang("unexpected opening curly bracket"));
}

return Collections.emptyList();
return Optional.empty();
}

}
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
package net.sf.jabref.logic.integrity;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

import net.sf.jabref.logic.integrity.IntegrityCheck.Checker;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.util.DOI;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.FieldName;

public class DOIValidityChecker implements Checker {
public class DOIValidityChecker implements ValueChecker {

@Override
public List<IntegrityMessage> check(BibEntry entry) {
final String field = FieldName.DOI;
return entry.getField(field)
.filter(d -> !DOI.isValid(d))
.map(d -> Collections.singletonList(new IntegrityMessage(Localization.lang("DOI %0 is invalid", d), entry, field)))
.orElse(Collections.emptyList());
public Optional<String> checkValue(String value) {
if (DOI.isValid(value)) {
return Optional.empty();
} else {
return Optional.of(Localization.lang("DOI %0 is invalid", value));
}
}
}
26 changes: 7 additions & 19 deletions src/main/java/net/sf/jabref/logic/integrity/EditionChecker.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
package net.sf.jabref.logic.integrity;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.regex.Pattern;

import net.sf.jabref.logic.integrity.IntegrityCheck.Checker;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.model.database.BibDatabaseContext;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.FieldName;

public class EditionChecker implements Checker {
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].+)$")
Expand All @@ -37,24 +32,17 @@ public EditionChecker(BibDatabaseContext bibDatabaseContext) {
* This should be an ordinal, and should have the first letter capitalized.
*/
@Override
public List<IntegrityMessage> check(BibEntry entry) {
Optional<String> value = entry.getField(FieldName.EDITION);
if (!value.isPresent()) {
return Collections.emptyList();
}

public Optional<String> checkValue(String value) {
//BibLaTeX
if (bibDatabaseContextEdition.isBiblatexMode() && !ONLY_NUMERALS_OR_LITERALS.test(value.get().trim())) {
return Collections.singletonList(new IntegrityMessage(
Localization.lang("should contain an integer or a literal"), entry, FieldName.EDITION));
if (bibDatabaseContextEdition.isBiblatexMode() && !ONLY_NUMERALS_OR_LITERALS.test(value.trim())) {
return Optional.of(Localization.lang("should contain an integer or a literal"));
}

//BibTeX
if (!bibDatabaseContextEdition.isBiblatexMode() && !FIRST_LETTER_CAPITALIZED.test(value.get().trim())) {
return Collections.singletonList(new IntegrityMessage(
Localization.lang("should have the first letter capitalized"), entry, FieldName.EDITION));
if (!bibDatabaseContextEdition.isBiblatexMode() && !FIRST_LETTER_CAPITALIZED.test(value.trim())) {
return Optional.of(Localization.lang("should have the first letter capitalized"));
}

return Collections.emptyList();
return Optional.empty();
}
}
29 changes: 29 additions & 0 deletions src/main/java/net/sf/jabref/logic/integrity/FieldChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package net.sf.jabref.logic.integrity;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import net.sf.jabref.logic.util.OptionalUtil;
import net.sf.jabref.model.entry.BibEntry;

public class FieldChecker implements IntegrityCheck.Checker {
protected final String field;
private final ValueChecker checker;

public FieldChecker(String field, ValueChecker checker) {
this.field = field;
this.checker = Objects.requireNonNull(checker);
}

@Override
public List<IntegrityMessage> check(BibEntry entry) {
Optional<String> value = entry.getField(field);
if (!value.isPresent()) {
return Collections.emptyList();
}

return OptionalUtil.toList(checker.checkValue(value.get()).map(message -> new IntegrityMessage(message, entry, field)));
}
}
51 changes: 51 additions & 0 deletions src/main/java/net/sf/jabref/logic/integrity/FieldCheckers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package net.sf.jabref.logic.integrity;

import java.util.List;
import java.util.stream.Collectors;

import net.sf.jabref.model.database.BibDatabaseContext;
import net.sf.jabref.model.entry.FieldName;
import net.sf.jabref.model.entry.InternalBibtexFields;
import net.sf.jabref.model.metadata.FileDirectoryPreferences;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

public class FieldCheckers {
static List<FieldChecker> getAll(BibDatabaseContext databaseContext, FileDirectoryPreferences fileDirectoryPreferences) {
return getAllMap(databaseContext, fileDirectoryPreferences)
.entries().stream()
.map(pair -> new FieldChecker(pair.getKey(), pair.getValue()))
.collect(Collectors.toList());
}

private static Multimap<String, ValueChecker> getAllMap(BibDatabaseContext databaseContext, FileDirectoryPreferences fileDirectoryPreferences) {
ArrayListMultimap<String, ValueChecker> fieldCheckers = ArrayListMultimap.create(50, 10);

for (String field : InternalBibtexFields.getJournalNameFields()) {
fieldCheckers.put(field, new AbbreviationChecker());
}
for (String field : InternalBibtexFields.getBookNameFields()) {
fieldCheckers.put(field, new AbbreviationChecker());
}
for (String field : InternalBibtexFields.getPersonNameFields()) {
fieldCheckers.put(field, new PersonNamesChecker());
}
fieldCheckers.put(FieldName.BOOKTITLE, new BooktitleChecker());
fieldCheckers.put(FieldName.TITLE, new BracketChecker());
fieldCheckers.put(FieldName.TITLE, new TitleChecker(databaseContext));
fieldCheckers.put(FieldName.DOI, new DOIValidityChecker());
fieldCheckers.put(FieldName.EDITION, new EditionChecker(databaseContext));
fieldCheckers.put(FieldName.FILE, new FileChecker(databaseContext, fileDirectoryPreferences));
fieldCheckers.put(FieldName.HOWPUBLISHED, new HowPublishedChecker(databaseContext));
fieldCheckers.put(FieldName.ISBN, new ISBNChecker());
fieldCheckers.put(FieldName.ISSN, new ISSNChecker());
fieldCheckers.put(FieldName.MONTH, new MonthChecker(databaseContext));
fieldCheckers.put(FieldName.NOTE, new NoteChecker(databaseContext));
fieldCheckers.put(FieldName.PAGES, new PagesChecker(databaseContext));
fieldCheckers.put(FieldName.URL, new UrlChecker());
fieldCheckers.put(FieldName.YEAR, new YearChecker());

return fieldCheckers;
}
}
Loading