Skip to content

Commit

Permalink
Honored comments and added test
Browse files Browse the repository at this point in the history
  • Loading branch information
oscargus committed Apr 18, 2016
1 parent 6d01383 commit b2c2d6b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
33 changes: 17 additions & 16 deletions src/main/java/net/sf/jabref/logic/integrity/IntegrityCheck.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
package net.sf.jabref.logic.integrity;

import net.sf.jabref.BibDatabaseContext;
import net.sf.jabref.Globals;
import net.sf.jabref.bibtex.FieldProperties;
import net.sf.jabref.bibtex.InternalBibtexFields;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.util.io.FileUtil;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.FileField;
import net.sf.jabref.model.entry.ParsedFileField;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import net.sf.jabref.BibDatabaseContext;
import net.sf.jabref.Globals;
import net.sf.jabref.bibtex.FieldProperties;
import net.sf.jabref.bibtex.InternalBibtexFields;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.util.io.FileUtil;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.FileField;
import net.sf.jabref.model.entry.ParsedFileField;

public class IntegrityCheck {

private final BibDatabaseContext bibDatabaseContext;
Expand Down Expand Up @@ -60,7 +61,7 @@ private List<IntegrityMessage> checkBibtexEntry(BibEntry entry) {
result.addAll(new TypeChecker().check(entry));
result.addAll(new AbbreviationChecker("journal").check(entry));
result.addAll(new AbbreviationChecker("booktitle").check(entry));
result.addAll(new HashChecker().check(entry));
result.addAll(new BibStringChecker().check(entry));

return result;
}
Expand Down Expand Up @@ -326,7 +327,7 @@ public List<IntegrityMessage> check(BibEntry entry) {
}
}

private static class HashChecker implements Checker {
private static class BibStringChecker implements Checker {

// Detect # if it doesn't have a \ in front of it or if it starts the string
private static final Pattern UNESCAPED_HASH = Pattern.compile("(?<!\\\\)#|^#");
Expand All @@ -338,16 +339,16 @@ private static class HashChecker implements Checker {
@Override
public List<IntegrityMessage> check(BibEntry entry) {
List<IntegrityMessage> results = new ArrayList<>();
for (String fieldName : entry.getFieldNames()) {
String fieldValue = entry.getField(fieldName);
Matcher hashMatcher = UNESCAPED_HASH.matcher(fieldValue);
for (Map.Entry<String, String> field : entry.getFieldMap().entrySet()) {
Matcher hashMatcher = UNESCAPED_HASH.matcher(field.getValue());
int hashCount = 0;
while (hashMatcher.find()) {
hashCount++;
}
if ((hashCount % 2) == 1) {
results.add(
new IntegrityMessage(Localization.lang("odd number of unescaped '#'"), entry, fieldName));
new IntegrityMessage(Localization.lang("odd number of unescaped '#'"), entry,
field.getKey()));
}
}
return results;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/net/sf/jabref/model/entry/BibEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -597,4 +597,9 @@ public List<String> getSeparatedKeywords() {
public Collection<String> getFieldValues() {
return fields.values();
}

public Map<String, String> getFieldMap() {
// TODO Auto-generated method stub
return fields;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ public void testPageNumbersChecks() {
assertCorrect(createContext("pages", "7+,41--43,73"));
}

@Test
public void testBibStringChecks() {
assertCorrect(createContext("title", "Not a single hash mark"));
assertCorrect(createContext("month", "#jan#"));
assertCorrect(createContext("author", "#einstein# and #newton#"));
assertWrong(createContext("month", "#jan"));
assertWrong(createContext("author", "#einstein# #amp; #newton#"));
}

private BibDatabaseContext createContext(String field, String value, String type) {
BibEntry entry = new BibEntry();
entry.setField(field, value);
Expand Down

0 comments on commit b2c2d6b

Please sign in to comment.