diff --git a/jabgui/src/main/java/org/jabref/gui/importer/fetcher/WebSearchPaneViewModel.java b/jabgui/src/main/java/org/jabref/gui/importer/fetcher/WebSearchPaneViewModel.java index e0406752482..118a0d6a6dd 100644 --- a/jabgui/src/main/java/org/jabref/gui/importer/fetcher/WebSearchPaneViewModel.java +++ b/jabgui/src/main/java/org/jabref/gui/importer/fetcher/WebSearchPaneViewModel.java @@ -22,6 +22,7 @@ import org.jabref.logic.importer.WebFetchers; import org.jabref.logic.l10n.Localization; import org.jabref.logic.util.BackgroundTask; +import org.jabref.model.search.query.SearchQuery; import org.jabref.model.strings.StringUtil; import org.jabref.model.util.OptionalUtil; @@ -30,12 +31,9 @@ import de.saxsys.mvvmfx.utils.validation.ValidationMessage; import de.saxsys.mvvmfx.utils.validation.ValidationStatus; import de.saxsys.mvvmfx.utils.validation.Validator; -import org.apache.lucene.queryparser.flexible.core.QueryNodeParseException; -import org.apache.lucene.queryparser.flexible.core.parser.SyntaxParser; -import org.apache.lucene.queryparser.flexible.standard.parser.ParseException; -import org.apache.lucene.queryparser.flexible.standard.parser.StandardSyntaxParser; - -import static org.jabref.logic.importer.fetcher.transformers.AbstractQueryTransformer.NO_EXPLICIT_FIELD; +import org.antlr.v4.runtime.RecognitionException; +import org.antlr.v4.runtime.Token; +import org.antlr.v4.runtime.misc.ParseCancellationException; public class WebSearchPaneViewModel { @@ -47,7 +45,6 @@ public class WebSearchPaneViewModel { private final StateManager stateManager; private final Validator searchQueryValidator; - private final SyntaxParser parser = new StandardSyntaxParser(); public WebSearchPaneViewModel(GuiPreferences preferences, DialogService dialogService, StateManager stateManager) { this.dialogService = dialogService; @@ -85,18 +82,23 @@ public WebSearchPaneViewModel(GuiPreferences preferences, DialogService dialogSe } try { - parser.parse(queryText, NO_EXPLICIT_FIELD); + // The result is ignored because we just check for validity + SearchQuery.getStartContext(queryText); return null; - } catch (ParseException e) { - String element = e.currentToken.image; - int position = e.currentToken.beginColumn; - if (element == null) { - return ValidationMessage.error(Localization.lang("Invalid query. Check position %0.", position)); - } else { - return ValidationMessage.error(Localization.lang("Invalid query element '%0' at position %1", element, position)); + } catch (ParseCancellationException e) { + // RecognitionException can point out the exact error + if (e.getCause() instanceof RecognitionException recEx) { + Token offendingToken = recEx.getOffendingToken(); + + // The character position is 0-based, so we add 1 for user-friendliness. + int line = offendingToken.getLine(); + int charPositionInLine = offendingToken.getCharPositionInLine() + 1; + + return ValidationMessage.error(Localization.lang("Invalid query element '%0' at position %1", line, charPositionInLine)); } - } catch (QueryNodeParseException e) { - return ValidationMessage.error(""); + + // Fallback for other failing reasons + return ValidationMessage.error(Localization.lang("Invalid query")); } }); } diff --git a/jabgui/src/test/java/org/jabref/gui/importer/fetcher/WebSearchPaneViewModelTest.java b/jabgui/src/test/java/org/jabref/gui/importer/fetcher/WebSearchPaneViewModelTest.java index dd8dec30616..1d7f94d11d1 100644 --- a/jabgui/src/test/java/org/jabref/gui/importer/fetcher/WebSearchPaneViewModelTest.java +++ b/jabgui/src/test/java/org/jabref/gui/importer/fetcher/WebSearchPaneViewModelTest.java @@ -71,9 +71,10 @@ void canExtractDOIFromQueryText() { } @Test - void queryConsistingOfInvalidDOIIsInvalid() { + void queryConsistingOfInvalidDOIIsValid() { viewModel.queryProperty().setValue("101.1007/JHEP02(2023)082"); - assertFalse(viewModel.queryValidationStatus().validProperty().getValue()); + // There is currently no interpretation of nearly-valid identifiers, therefore, this is concidered as "regular" search term + assertTrue(viewModel.queryValidationStatus().validProperty().getValue()); } @Test @@ -90,7 +91,7 @@ void canExtractISBNFromQueryText() { @Test void queryConsistingOfArXivIdIsValid() { - viewModel.queryProperty().setValue("arXiv:2110.02957"); + viewModel.queryProperty().setValue("arXiv=2110.02957"); assertTrue(viewModel.queryValidationStatus().validProperty().getValue()); } diff --git a/jablib/src/main/resources/l10n/JabRef_en.properties b/jablib/src/main/resources/l10n/JabRef_en.properties index b6a23a166a6..511eb8565fe 100644 --- a/jablib/src/main/resources/l10n/JabRef_en.properties +++ b/jablib/src/main/resources/l10n/JabRef_en.properties @@ -1001,7 +1001,7 @@ You\ must\ restart\ JabRef\ for\ this\ to\ come\ into\ effect.=You must restart Could\ not\ find\ fetcher\ '%0'=Could not find fetcher '%0' Running\ query\ '%0'\ with\ fetcher\ '%1'.=Running query '%0' with fetcher '%1'. -Invalid\ query.\ Check\ position\ %0.=Invalid query. Check position %0. +Invalid\ query=Invalid query Invalid\ query\ element\ '%0'\ at\ position\ %1=Invalid query element '%0' at position %1 Move\ file=Move file