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

Bug#275 sparql comments not ignored #287

Merged
merged 2 commits into from
Nov 21, 2024
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 @@ -54,6 +54,8 @@ public class SparqlQueryParser implements QueryParser {
private ParamType currentParamType;
private StringBuilder currentWord;
private boolean inProjection;
private boolean inComment;
private boolean inUri;

public SparqlQueryParser(ParameterValueFactory parameterValueFactory) {
this.parameterValueFactory = parameterValueFactory;
Expand All @@ -67,45 +69,46 @@ private enum ParamType {
@Override
public SparqlQueryHolder parseQuery(String query) {
this.query = query;
this.queryParts = new ArrayList<>();
this.uniqueParams = new HashMap<>();
this.positionalCounter = 1;
this.parameters = new ArrayList<>();
this.inSQString = false;
// In double-quoted string
this.inDQString = false;
this.inParam = false;
this.lastParamEndIndex = 0;
this.paramStartIndex = 0;
this.currentParamType = null;
this.currentWord = new StringBuilder();
resetParser();
int i;
for (i = 0; i < query.length(); i++) {
final char c = query.charAt(i);
switch (c) {
case '#':
startComment(i, c);
break;
case '\'':
inSQString = !inSQString;
this.inSQString = inComment == inSQString; // ~ inComment ? inSQString : !inSQString
break;
case '"':
inDQString = !inDQString;
this.inDQString = inComment == inDQString; // ~ inComment ? inDQString : !inDQString
break;
case '$':
parameterStart(i, ParamType.POSITIONAL);
if (!inComment) {
parameterStart(i, ParamType.POSITIONAL);
}
break;
case '?':
queryVariableStart(i);
break;
case '<':
this.inUri = true;
if (inParam) {
parameterEnd(i); // Property path zero or one
} else {
parameterStart(i, ParamType.NAMED);
parameterEnd(i);
}
wordEnd();
break;
case '{':
this.inProjection = false; // Intentional fall-through
case '<':
case '>':
case ',':
this.inUri = false;
wordEnd();
break;
case '\n':
this.inComment = false; // Intentional fall-through
case '{':
// ~ inComment ? inProjection : !inProjection
this.inProjection = inComment && inProjection; // Intentional fall-through
case '\r':
case ',':
case ')':
case ' ':
case '.':
Expand Down Expand Up @@ -135,11 +138,49 @@ public SparqlQueryHolder parseQuery(String query) {
return new SparqlQueryHolder(query, queryParts, parameters);
}

private void resetParser() {
this.queryParts = new ArrayList<>();
this.uniqueParams = new HashMap<>();
this.positionalCounter = 1;
this.parameters = new ArrayList<>();
this.inSQString = false;
// In double-quoted string
this.inDQString = false;
this.inParam = false;
this.inUri = false;
this.inComment = false;
this.lastParamEndIndex = 0;
this.paramStartIndex = 0;
this.currentParamType = null;
this.currentWord = new StringBuilder();
}

private void startComment(int index, char c) {
if (!inUri) {
if (inParam && !inComment) {
parameterEnd(index);
}
this.inComment = true;
} else {
currentWord.append(c);
}
}

private void queryVariableStart(int i) {
if (!inComment) {
if (inParam) {
parameterEnd(i); // Property path zero or one
} else {
parameterStart(i, ParamType.NAMED);
}
}
}

private void parameterStart(int index, ParamType paramType) {
if (!inSQString && !inDQString) {
queryParts.add(query.substring(lastParamEndIndex, index));
paramStartIndex = index + 1;
inParam = true;
this.paramStartIndex = index + 1;
this.inParam = true;
this.currentParamType = paramType;
}
}
Expand Down Expand Up @@ -200,6 +241,6 @@ private void wordEnd() {
} else if (inProjection && SparqlConstants.WHERE.equalsIgnoreCase(currentWord.toString())) {
this.inProjection = false;
}
currentWord = new StringBuilder();
this.currentWord = new StringBuilder();
}
}
Loading
Loading