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

Fix 12354 title should not contain url #12431

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
15 changes: 15 additions & 0 deletions src/main/java/org/jabref/logic/integrity/BooktitleChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@

import java.util.Locale;
import java.util.Optional;
import java.util.regex.Pattern;

import org.jabref.logic.l10n.Localization;
import org.jabref.model.strings.StringUtil;

public class BooktitleChecker implements ValueChecker {

private static final Pattern FULL_URL_PATTERN = Pattern.compile(
"(https?://\\S+/\\S+|www\\.\\S+/\\S+)", Pattern.CASE_INSENSITIVE);

private static final Pattern DOMAIN_ONLY_PATTERN = Pattern.compile(
"(https?://\\S+|www\\.\\S+)(/|$)", Pattern.CASE_INSENSITIVE);

@Override
public Optional<String> checkValue(String value) {
if (StringUtil.isBlank(value)) {
Expand All @@ -18,6 +25,14 @@ public Optional<String> checkValue(String value) {
return Optional.of(Localization.lang("booktitle ends with 'conference on'"));
}

if (FULL_URL_PATTERN.matcher(value).find()) {
return Optional.of(Localization.lang("The title contains a URL"));
}

if (DOMAIN_ONLY_PATTERN.matcher(value).find()) {
Copy link
Member

Choose a reason for hiding this comment

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

why do you check this case? It seems unnecessary to me.

return Optional.empty();
}

return Optional.empty();
}
}
13 changes: 13 additions & 0 deletions src/main/java/org/jabref/logic/integrity/TitleChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public class TitleChecker implements ValueChecker {
private static final Pattern INSIDE_CURLY_BRAKETS = Pattern.compile("\\{[^}\\{]*\\}");
private static final Pattern DELIMITERS = Pattern.compile("\\.|\\!|\\?|\\;|\\:|\\[");
private static final Predicate<String> HAS_CAPITAL_LETTERS = Pattern.compile("[\\p{Lu}\\p{Lt}]").asPredicate();
private static final Pattern FULL_URL_PATTERN = Pattern.compile(
"(https?://\\S+/\\S+|www\\.\\S+/\\S+)", Pattern.CASE_INSENSITIVE);

private static final Pattern DOMAIN_ONLY_PATTERN = Pattern.compile(
"(https?://\\S+|www\\.\\S+)(/|$)", Pattern.CASE_INSENSITIVE);

private final BibDatabaseContext databaseContext;

Expand Down Expand Up @@ -40,6 +45,14 @@ public Optional<String> checkValue(String value) {
return Optional.empty();
}

if (FULL_URL_PATTERN.matcher(value).find()) {
return Optional.of(Localization.lang("The title contains a URL"));
}

if (DOMAIN_ONLY_PATTERN.matcher(value).find()) {
return Optional.empty();
}

String valueOnlySpacesWithinCurlyBraces = INSIDE_CURLY_BRAKETS.matcher(value).replaceAll("");

String[] splitTitle = DELIMITERS.split(valueOnlySpacesWithinCurlyBraces);
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2830,3 +2830,6 @@ Include=Include
Exclude=Exclude
Include\ or\ exclude\ cross-referenced\ entries=Include or exclude cross-referenced entries
Would\ you\ like\ to\ include\ cross-reference\ entries\ in\ the\ current\ operation?=Would you like to include cross-reference entries in the current operation?
The\ title\ contains\ a\ full\ URL\ which\ is\ forbidden=The title contains a URL
The\ book\ title\ contains\ a\ full\ URL\ which\ is\ forbidden=The title contains a URL
12 changes: 12 additions & 0 deletions src/test/java/org/jabref/logic/integrity/BooktitleCheckerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,16 @@ void booktitleDoesNotAcceptsIfItEndsWithConferenceOn() {
void booktitleIsBlank() {
assertEquals(Optional.empty(), checker.checkValue(" "));
}

@Test
void booktitleDoesNotAcceptFullURL() {
assertNotEquals(Optional.empty(), checker.checkValue("Proceedings of the https://example.com/conference"));
assertNotEquals(Optional.empty(), checker.checkValue("Workshop on www.example.com/session"));
}

@Test
void booktitleAcceptsDomainOnlyURL() {
assertEquals(Optional.empty(), checker.checkValue("Annual Meeting of www.example.com"));
assertEquals(Optional.empty(), checker.checkValue("Conference hosted at https://example.com"));
}
}
27 changes: 27 additions & 0 deletions src/test/java/org/jabref/logic/integrity/TitleCheckerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,33 @@ static Stream<Arguments> invalidTitle() {
Arguments.of("bibTexDoesNotAcceptsLeadingTranslatedTitleWithOriginal ", "[This is translated title] This is a title")
);
}

@ParameterizedTest(name = "{index}. Title: \"{1}\" {0}")
@MethodSource("invalidTitlesWithURLs")
void titleShouldRaiseWarningForFullURLs(String message, String title) {
assertNotEquals(Optional.empty(), checker.checkValue(title));
}

static Stream<Arguments> invalidTitlesWithURLs() {
return Stream.of(
Arguments.of("Title contains full URL with https", "Check this out: https://example.com/something"),
Arguments.of("Title contains full URL with http", "Visit http://example.com/path"),
Arguments.of("Title contains full URL without protocol", "Found at www.example.com/something")
);
}

@ParameterizedTest(name = "{index}. Title: \"{1}\" {0}")
@MethodSource("validTitlesWithDomainOnlyURLs")
void titleShouldAllowDomainOnlyURLs(String message, String title) {
assertEquals(Optional.empty(), checker.checkValue(title));
}

static Stream<Arguments> validTitlesWithDomainOnlyURLs() {
return Stream.of(
Arguments.of("Title contains a domain only URL", "Visit www.example.com"),
Arguments.of("Title contains a domain only HTTPS URL", "Check https://example.com")
);
}
}

@Nested
Expand Down
Loading