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 2 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 @@ -29,6 +29,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv

- It is possible again to use "current table sort order" for the order of entries when saving. [#9869](https://github.com/JabRef/jabref/issues/9869)
- Passwords can be stored in GNOME key ring. [#10274](https://github.com/JabRef/jabref/issues/10274)
- The ampersand checker skips verbatim fields (`file`, `url`, ...). [#10419](https://github.com/JabRef/jabref/pull/10419)
- We fixed an issue where groups based on an aux file could not be created due to an exception [#10350](https://github.com/JabRef/jabref/issues/10350)
- We fixed an issue where the JabRef browser extension could not communicate with JabRef under macOS due to missing files. You should use the `.pkg` for the first installation as it updates all necessary files for the extension [#10308](https://github.com/JabRef/jabref/issues/10308)
- We fixed an issue where the ISBN fetcher returned the entrytype `misc` for certain ISBN numbers [#10348](https://github.com/JabRef/jabref/issues/10348)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
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 \
Expand All @@ -24,6 +26,9 @@ public List<IntegrityMessage> check(BibEntry entry) {
List<IntegrityMessage> results = new ArrayList<>();

for (Map.Entry<Field, String> field : entry.getFieldMap().entrySet()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe even cooler to integrate the filteirng directly in to the getFieldMap call?
e.g. entry.getFieldMap().entrySet().stream().filter...
https://mkyong.com/java8/java-8-filter-a-map-examples/

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried my best. Needed to use Tuple2 for the HtmlChecker. dc82d44

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I double checked that all usages do not modify the list any more

if (field.getKey().getProperties().contains(FieldProperty.VERBATIM)) {
continue;
}
// counts the number of even \ occurrences preceding an &
long unescapedAmpersands = BACKSLASH_PRECEDED_AMPERSAND.matcher(field.getValue())
.results()
Expand Down
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));
}
}