Skip to content

Commit

Permalink
Merge pull request #11 from Nuisigor/issue-02/undefined-strings
Browse files Browse the repository at this point in the history
Issue 02/undefined strings
  • Loading branch information
getuliobr authored Dec 8, 2022
2 parents eeee059 + ad55d07 commit a35584d
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public IntegrityCheck(BibDatabaseContext bibDatabaseContext,
new BibStringChecker(),
new HTMLCharacterChecker(),
new EntryLinkChecker(bibDatabaseContext.getDatabase()),
new UndefinedChecker(bibDatabaseContext.getDatabase()),
new CitationKeyDeviationChecker(bibDatabaseContext, citationKeyPatternPreferences),
new CitationKeyDuplicationChecker(bibDatabaseContext.getDatabase())
));
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/org/jabref/logic/integrity/UndefinedChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.jabref.logic.integrity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jabref.logic.l10n.Localization;
import org.jabref.model.database.BibDatabase;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.Field;

/**
* Checks, if there is an undefined string # (FieldWriter.BIBTEX_STRING_START_END_SYMBOL)
*/
public class UndefinedChecker implements EntryChecker {

private static final Pattern INSIDE_BIBTEX_STRING_START_END_SYMBOL = Pattern.compile("\\#[^#\\#]*\\#");
private final BibDatabase database;

public UndefinedChecker(BibDatabase database) {
this.database = Objects.requireNonNull(database);
}

@Override
public List<IntegrityMessage> check(BibEntry entry) {
List<IntegrityMessage> result = new ArrayList<>();
for (Map.Entry<Field, String> field : entry.getFieldMap().entrySet()) {
String fieldValue = field.getValue();

List<String> allMatches = new ArrayList<String>();
Matcher matcher = INSIDE_BIBTEX_STRING_START_END_SYMBOL.matcher(fieldValue);
while (matcher.find()) {
allMatches.add(matcher.group());
}
for (String match : allMatches) {
String constantId = match.substring(1, match.length() - 1);

if (!database.getStringByName(constantId).isPresent()) {
result.add(new IntegrityMessage(Localization.lang("String %0 is undefined", match), entry, field.getKey()));
}
}
}
return result;
}
}
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1788,6 +1788,8 @@ Different\ customization,\ current\ settings\ will\ be\ overwritten=Different cu
Entry\ type\ %0\ is\ only\ defined\ for\ Biblatex\ but\ not\ for\ BibTeX=Entry type %0 is only defined for Biblatex but not for BibTeX
String\ %0\ is\ undefined=String %0 is undefined
Copied\ %0\ citations.=Copied %0 citations.
journal\ not\ found\ in\ abbreviation\ list=journal not found in abbreviation list
Expand Down
91 changes: 91 additions & 0 deletions src/test/java/org/jabref/logic/integrity/UndefinedCheckerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package org.jabref.logic.integrity;

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

import org.jabref.logic.l10n.Localization;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.database.BibDatabaseMode;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.BibtexString;
import org.jabref.model.entry.field.InternalField;
import org.jabref.model.entry.field.StandardField;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class UndefinedCheckerTest {
private UndefinedChecker checker;

@BeforeEach
public void setUp() {
BibDatabaseContext databaseContext = new BibDatabaseContext();
databaseContext.setMode(BibDatabaseMode.BIBTEX);

BibtexString str = new BibtexString("defined", "test");
databaseContext.getDatabase().addString(str);

checker = new UndefinedChecker(databaseContext.getDatabase());
}

@Test
void undefinedStringAtStart() {
BibEntry entry = new BibEntry().withField(InternalField.KEY_FIELD, "#undefined# string at start")
.withField(StandardField.AUTHOR, "Getulio")
.withField(StandardField.YEAR, "2022");

assertEquals(List.of(new IntegrityMessage(Localization.lang("String %0 is undefined", "#undefined#"), entry, InternalField.KEY_FIELD)), checker.check(entry));
}

@Test
void undefinedStringAtMiddle() {
BibEntry entry = new BibEntry().withField(InternalField.KEY_FIELD, "string #undefined# at middle")
.withField(StandardField.AUTHOR, "Igor")
.withField(StandardField.YEAR, "2022");

assertEquals(List.of(new IntegrityMessage(Localization.lang("String %0 is undefined", "#undefined#"), entry, InternalField.KEY_FIELD)), checker.check(entry));
}

@Test
void undefinedStringAtEnd() {
BibEntry entry = new BibEntry().withField(InternalField.KEY_FIELD, "string undefined at #end#")
.withField(StandardField.AUTHOR, "Gustavo")
.withField(StandardField.YEAR, "2022");

assertEquals(List.of(new IntegrityMessage(Localization.lang("String %0 is undefined", "#end#"), entry, InternalField.KEY_FIELD)), checker.check(entry));
}

@Test
void stringIsDefinedStart() {
BibEntry entry = new BibEntry().withField(StandardField.AUTHOR, "#defined# this string at start")
.withField(StandardField.TITLE, "The Title")
.withField(StandardField.YEAR, "2021");
assertEquals(Collections.emptyList(), checker.check(entry));
}

@Test
void stringIsDefinedMiddle() {
BibEntry entry = new BibEntry().withField(StandardField.AUTHOR, "this string is #defined# at the middle")
.withField(StandardField.TITLE, "The Title")
.withField(StandardField.YEAR, "2021");
assertEquals(Collections.emptyList(), checker.check(entry));
}

@Test
void stringIsDefinedEnd() {
BibEntry entry = new BibEntry().withField(StandardField.AUTHOR, "This string is #defined#")
.withField(StandardField.TITLE, "The Title")
.withField(StandardField.YEAR, "2021");
assertEquals(Collections.emptyList(), checker.check(entry));
}

@Test
void noString() {
BibEntry entry = new BibEntry().withField(StandardField.AUTHOR, "There is no string")
.withField(StandardField.TITLE, "The Title")
.withField(StandardField.YEAR, "2021");
assertEquals(Collections.emptyList(), checker.check(entry));
}
}

0 comments on commit a35584d

Please sign in to comment.