forked from JabRef/jabref
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from Nuisigor/issue-02/undefined-strings
Issue 02/undefined strings
- Loading branch information
Showing
4 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/main/java/org/jabref/logic/integrity/UndefinedChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/test/java/org/jabref/logic/integrity/UndefinedCheckerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |