Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 {

Expand All @@ -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;
Expand Down Expand Up @@ -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"));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link

Choose a reason for hiding this comment

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

The comment merely restates what the code does and contains a spelling error ('concidered'). Comments should provide additional information not obvious from the code.

assertTrue(viewModel.queryValidationStatus().validProperty().getValue());
}

@Test
Expand All @@ -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());
}

Expand Down
2 changes: 1 addition & 1 deletion jablib/src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading