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

Exclude check for ampersand (&) at verbatim fields #10419

Merged
merged 7 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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 @@ -24,6 +24,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv

- The export formats `listrefs`, `tablerefs`, `tablerefsabsbib`, now use the ISO date format in the footer [#10383](https://github.com/JabRef/jabref/pull/10383).
- When searching for an identifier in the "Web search", the title of the search window is now "Identifier-based Web Search". [#10391](https://github.com/JabRef/jabref/pull/10391)
- The ampersand checker now skips verbatim fields (`file`, `url`, ...). [#10419](https://github.com/JabRef/jabref/pull/10419)

### Fixed

Expand Down
38 changes: 23 additions & 15 deletions src/main/java/org/jabref/logic/integrity/AmpersandChecker.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,49 @@
package org.jabref.logic.integrity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
Siedlerchr marked this conversation as resolved.
Show resolved Hide resolved
import java.util.regex.MatchResult;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import javafx.util.Pair;

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

import com.google.common.base.CharMatcher;

/**
* Checks if the BibEntry contains unescaped ampersands.
* This is done in nonverbatim fields. Similar to {@link HTMLCharacterChecker}
*/
public class AmpersandChecker implements EntryChecker {
// matches for an & preceded by any number of \
private static final Pattern BACKSLASH_PRECEDED_AMPERSAND = Pattern.compile("\\\\*&");

@Override
public List<IntegrityMessage> check(BibEntry entry) {
List<IntegrityMessage> results = new ArrayList<>();

for (Map.Entry<Field, String> field : entry.getFieldMap().entrySet()) {
// counts the number of even \ occurrences preceding an &
long unescapedAmpersands = BACKSLASH_PRECEDED_AMPERSAND.matcher(field.getValue())
.results()
.map(MatchResult::group)
.filter(m -> CharMatcher.is('\\').countIn(m) % 2 == 0)
.count();
return entry.getFieldMap().entrySet().stream()
.filter(field -> !field.getKey().getProperties().contains(FieldProperty.VERBATIM))
// We use "flatMap" instead of filtering later, because we assume there won't be that much error messages - and construction of Stream.empty() is faster than construction of a new Tuple2 (including lifting long to Long)
.flatMap(AmpersandChecker::getUnescapedAmpersandsWithCount)
.map(pair -> new IntegrityMessage(Localization.lang("Found %0 unescaped '&'", pair.getValue()), entry, pair.getKey()))
.toList();
}

if (unescapedAmpersands > 0) {
results.add(new IntegrityMessage(Localization.lang("Found %0 unescaped '&'", unescapedAmpersands), entry, field.getKey()));
// note: when changing the message - also do so in tests
}
private static Stream<Pair<Field, Long>> getUnescapedAmpersandsWithCount(Map.Entry<Field, String> entry) {
// counts the number of even \ occurrences preceding an &
long unescapedAmpersands = BACKSLASH_PRECEDED_AMPERSAND.matcher(entry.getValue())
.results()
.map(MatchResult::group)
.filter(m -> CharMatcher.is('\\').countIn(m) % 2 == 0)
.count();
if (unescapedAmpersands == 0) {
return Stream.empty();
}
return results;
return Stream.of(new Pair<>(entry.getKey(), unescapedAmpersands));
}
}
22 changes: 5 additions & 17 deletions src/main/java/org/jabref/logic/integrity/HTMLCharacterChecker.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package org.jabref.logic.integrity;

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

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

/**
Expand All @@ -20,18 +16,10 @@ public class HTMLCharacterChecker implements EntryChecker {

@Override
public List<IntegrityMessage> check(BibEntry entry) {
List<IntegrityMessage> results = new ArrayList<>();
for (Map.Entry<Field, String> field : entry.getFieldMap().entrySet()) {
if (field.getKey().getProperties().contains(FieldProperty.VERBATIM)) {
continue;
}

Matcher characterMatcher = HTML_CHARACTER_PATTERN.matcher(field.getValue());
if (characterMatcher.find()) {
results.add(
new IntegrityMessage(Localization.lang("HTML encoded character found"), entry, field.getKey()));
}
}
return results;
return entry.getFieldMap().entrySet().stream()
.filter(field -> !field.getKey().getProperties().contains(FieldProperty.VERBATIM))
.filter(field -> HTML_CHARACTER_PATTERN.matcher(field.getValue()).find())
.map(field -> new IntegrityMessage(Localization.lang("HTML encoded character found"), entry, field.getKey()))
.toList();
}
}
14 changes: 14 additions & 0 deletions src/test/java/org/jabref/logic/integrity/AmpersandCheckerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,18 @@ void entryWithMultipleEscapedAndUnescapedAmpersands() {
entry.setField(StandardField.AFTERWORD, "May the force be with you & live long \\\\& prosper \\& to infinity \\\\\\& beyond & assemble \\\\\\\\& excelsior!");
assertEquals(List.of(new IntegrityMessage("Found 4 unescaped '&'", entry, StandardField.AFTERWORD)), checker.check(entry));
}

static Stream<Arguments> entryWithVerabitmFieldsNotCausingMessages() {
return Stream.of(
Arguments.of(StandardField.FILE, "one & another.pdf"),
Arguments.of(StandardField.URL, "https://example.org?key=value&key2=value2")
);
}

@ParameterizedTest
@MethodSource
void entryWithVerabitmFieldsNotCausingMessages(Field field, String value) {
entry.setField(field, value);
assertEquals(List.of(), checker.check(entry));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;

import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.StandardField;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down Expand Up @@ -59,4 +64,18 @@ void journalDoesNotAcceptHTMLEncodedCharacters() {
entry.setField(StandardField.JOURNAL, "&Auml;rling Str&ouml;m for &#8211; &#x2031;");
assertEquals(List.of(new IntegrityMessage("HTML encoded character found", entry, StandardField.JOURNAL)), checker.check(entry));
}

static Stream<Arguments> entryWithVerabitmFieldsNotCausingMessages() {
return Stream.of(
Arguments.of(StandardField.FILE, "one &amp; another.pdf"),
Arguments.of(StandardField.URL, "https://example.org?key=value&key2=value2")
);
}

@ParameterizedTest
@MethodSource
void entryWithVerabitmFieldsNotCausingMessages(Field field, String value) {
entry.setField(field, value);
assertEquals(List.of(), checker.check(entry));
}
}
Loading