SparqlQueryParser implements QueryParser { @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; this.inDQString = false; this.inParam = false; this.lastParamEndIndex = 0; this.paramStartIndex = 0; this.currentParamType = null; int i; for (i = 0; i < query.length(); i++) { final char c = query.charAt(i); switch (c) { case '\'': inSQString = !inSQString; break; case '"': inDQString = !inDQString; break; case '$': parameterStart(i, ParamType.POSITIONAL); break; case '?': if (inParam) { parameterEnd(i); } else { parameterStart(i, ParamType.NAMED); } break; case '<': case '>': case ',': case '\n': case ')': case ' ': case '.': case ';': case '{': case '}': case '[': case ']': case '+': case '*': case '/': case '|': if (inParam) { parameterEnd(i); } break; default: break; } } if (inParam) { parameterEnd(i); } else { queryParts.add(query.substring(lastParamEndIndex)); } return new SparqlQueryHolder(query, queryParts, parameters); } SparqlQueryParser(ParameterValueFactory parameterValueFactory); @Override SparqlQueryHolder parseQuery(String query);  }
@Test public void testParseSimpleQueryWithoutParameters() { final String query = "SELECT * WHERE { <http: final QueryHolder holder = queryParser.parseQuery(query); assertTrue(holder.getParameters().isEmpty()); assertEquals(query, holder.assembleQuery()); assertEquals(query, holder.getQuery()); }
@Test public void testParseQueryWithSingleParameter() { final String query = "SELECT ?x WHERE { ?x <http: final QueryHolder holder = queryParser.parseQuery(query); assertEquals(1, holder.getParameters().size()); assertTrue(holder.getParameters().contains(new QueryParameter<>("x", valueFactory))); }
@Test public void testParseQueryWithThreeParametersInSingleBGP() { final String query = "SELECT ?x ?y ?z WHERE { ?x ?y ?z . }"; final QueryHolder holder = queryParser.parseQuery(query); assertEquals(3, holder.getParameters().size()); assertTrue(holder.getParameters().contains(new QueryParameter<>("x", valueFactory))); assertTrue(holder.getParameters().contains(new QueryParameter<>("y", valueFactory))); assertTrue(holder.getParameters().contains(new QueryParameter<>("z", valueFactory))); }
@Test public void testParseQueryWithMultiplePatternsAndParameters() { final String query = "PREFIX foaf: <http: "SELECT ?craft\n" + "{\n" + "?craft foaf:name \"Apollo 7\" .\n" + "?craft foaf:homepage ?homepage\n" + "}"; final QueryHolder holder = queryParser.parseQuery(query); assertEquals(2, holder.getParameters().size()); assertTrue(holder.getParameters().contains(new QueryParameter<>("craft", valueFactory))); assertTrue(holder.getParameters().contains(new QueryParameter<>("homepage", valueFactory))); }
@Test public void testParseQueryWithFilterAndRegex() { final String query = "PREFIX dc: <http: "SELECT ?title\n" + "WHERE { ?x dc:title ?title\n" + " FILTER regex(?title, \"^SPARQL\") \n" + " }"; final QueryHolder holder = queryParser.parseQuery(query); assertEquals(2, holder.getParameters().size()); assertTrue(holder.getParameters().contains(new QueryParameter<>("title", valueFactory))); assertTrue(holder.getParameters().contains(new QueryParameter<>("x", valueFactory))); }
@Test public void testParseQueryWithFilterAndMultipleParameters() { final String query = "PREFIX dc: <http: "PREFIX ns: <http: "SELECT ?title ?price\n" + "WHERE { ?x ns:price ?price .\n" + " FILTER (?price < 30.5)\n" + " ?x dc:title ?title . }"; final QueryHolder holder = queryParser.parseQuery(query); assertEquals(3, holder.getParameters().size()); assertTrue(holder.getParameters().contains(new QueryParameter<>("title", valueFactory))); assertTrue(holder.getParameters().contains(new QueryParameter<>("price", valueFactory))); assertTrue(holder.getParameters().contains(new QueryParameter<>("x", valueFactory))); }
@Test public void testParseConstructQuery() { final String query = "PREFIX foaf: <http: "PREFIX org: <http: "\n" + "CONSTRUCT { ?x foaf:name ?name }\n" + "WHERE { ?x org:employeeName ?name }"; final QueryHolder holder = queryParser.parseQuery(query); assertEquals(2, holder.getParameters().size()); assertTrue(holder.getParameters().contains(new QueryParameter<>("x", valueFactory))); assertTrue(holder.getParameters().contains(new QueryParameter<>("name", valueFactory))); }
@Test public void parserThrowsExceptionWhenParameterNameIsMissing() { final String query = "SELECT ?x ?y ?z WHERE { ?x ?y ? . }"; assertThrows(QueryParserException.class, () -> queryParser.parseQuery(query)); }
@Test public void testParseQueryWithNumberedPositionalParams() { final String query = "PREFIX foaf: <http: "SELECT ?craft\n" + "{\n" + "?craft foaf:name $1 .\n" + "?craft foaf:homepage $2\n" + "}"; final QueryHolder holder = queryParser.parseQuery(query); assertEquals(3, holder.getParameters().size()); assertTrue(holder.getParameters().contains(new QueryParameter<>("craft", valueFactory))); assertTrue(holder.getParameters().contains(new QueryParameter<>(1, valueFactory))); assertTrue(holder.getParameters().contains(new QueryParameter<>(2, valueFactory))); }
@Test public void testParseQueryWithUnnumberedPositionalParams() { final String query = "PREFIX foaf: <http: "SELECT ?craft\n" + "{\n" + "?craft foaf:name $ .\n" + "?craft foaf:homepage $\n" + "}"; final QueryHolder holder = queryParser.parseQuery(query); assertEquals(3, holder.getParameters().size()); assertTrue(holder.getParameters().contains(new QueryParameter<>("craft", valueFactory))); assertTrue(holder.getParameters().contains(new QueryParameter<>(1, valueFactory))); assertTrue(holder.getParameters().contains(new QueryParameter<>(2, valueFactory))); }
@Test public void testParseQueryWithMixedNumberedAndUnnumberedPositionalParams() { final String query = "PREFIX foaf: <http: "SELECT ?craft\n" + "{\n" + "?craft foaf:name $1 .\n" + "?craft foaf:homepage $\n" + "}"; final QueryHolder holder = queryParser.parseQuery(query); assertEquals(3, holder.getParameters().size()); assertTrue(holder.getParameters().contains(new QueryParameter<>("craft", valueFactory))); assertTrue(holder.getParameters().contains(new QueryParameter<>(1, valueFactory))); assertTrue(holder.getParameters().contains(new QueryParameter<>(2, valueFactory))); }
@Test public void parsingQueryWithUsedParameterPositionThrowsException() { final String query = "PREFIX foaf: <http: "SELECT ?craft\n" + "{\n" + "?craft foaf:name $1 .\n" + "?craft foaf:homepage $1\n" + "}"; assertThrows(QueryParserException.class, () -> queryParser.parseQuery(query)); }
@Test public void parsingQueryWithIncorrectlyMixedNumberedAndUnnumberedPositionsThrowsException() { final String query = "PREFIX foaf: <http: "SELECT ?craft\n" + "{\n" + "?craft foaf:name $1 .\n" + "?craft foaf:homepage $\n" + "?craft rdf:label $2\n" + "}"; assertThrows(QueryParserException.class, () -> queryParser.parseQuery(query)); }
@Test public void parsingQueryWithPositionalParameterNotNumberThrowsException() { final String query = "PREFIX foaf: <http: "SELECT ?craft\n" + "{\n" + "?craft foaf:name $1 .\n" + "?craft foaf:homepage $notanumber\n" + "}"; assertThrows(QueryParserException.class, () -> queryParser.parseQuery(query)); }
@Test public void parsesQueryWithOrderByDescendingAtEnd() { final String query = "SELECT ?x WHERE { ?x a <http: final QueryHolder holder = queryParser.parseQuery(query); assertEquals(1, holder.getParameters().size()); assertEquals(query, holder.assembleQuery()); }
@Test public void parsesQueryWrittenInCondensedString() { final String query = "SELECT ?x WHERE { ?x a ?y. } ORDER BY DESC(?y)"; final QueryHolder holder = queryParser.parseQuery(query); assertEquals(2, holder.getParameters().size()); assertTrue(holder.getParameters().contains(new QueryParameter<>("x", valueFactory))); assertTrue(holder.getParameters().contains(new QueryParameter<>("y", valueFactory))); assertEquals(query, holder.assembleQuery()); }
@Test public void parsesQueryWithOrderByAtEnd() { final String query = "SELECT ?x WHERE { ?x a <http: final QueryHolder holder = queryParser.parseQuery(query); assertEquals(1, holder.getParameters().size()); assertEquals(query, holder.assembleQuery()); }
@Test public void parsesQueryWithVariableWithoutTrailingSpace() { final String query = "SELECT ?x WHERE {?x a ?type}"; final QueryHolder holder = queryParser.parseQuery(query); assertEquals(2, holder.getParameters().size()); assertNotNull(holder.getParameter("x")); assertNotNull(holder.getParameter("type")); }
@Test public void parsesQueryWithStringLiteralInSingleQuotes() { final String query = "SELECT ?x WHERE {?x a ?type; ?x rdfs:label 'label'@en.}"; final QueryHolder holder = queryParser.parseQuery(query); assertNotNull(holder.getParameter("x")); assertNotNull(holder.getParameter("type")); }
@Test public void parsesQueryWithPropertyPathOneOrMore() { final String query = "SELECT ?x WHERE {?x ?hasPart+ ?part . }"; final QueryHolder holder = queryParser.parseQuery(query); assertNotNull(holder.getParameter("x")); assertNotNull(holder.getParameter("hasPart")); assertNotNull(holder.getParameter("part")); }
@Test public void parsesQueryWithPropertyPathZeroOrMore() { final String query = "SELECT ?x WHERE {?x ?hasPart* ?part . }"; final QueryHolder holder = queryParser.parseQuery(query); assertNotNull(holder.getParameter("x")); assertNotNull(holder.getParameter("hasPart")); assertNotNull(holder.getParameter("part")); }
@Test public void parsesQueryWithPropertyPathZeroOrOne() { final String query = "SELECT ?x WHERE {?x ?hasPart? ?part . }"; final QueryHolder holder = queryParser.parseQuery(query); assertNotNull(holder.getParameter("x")); assertNotNull(holder.getParameter("hasPart")); assertNotNull(holder.getParameter("part")); }
@Test public void parsesQueryWithPropertyPathSequence() { final String query = "SELECT ?x WHERE {?x ?hasPart/?hasQuestion ?part . }"; final QueryHolder holder = queryParser.parseQuery(query); assertNotNull(holder.getParameter("x")); assertNotNull(holder.getParameter("hasPart")); assertNotNull(holder.getParameter("hasQuestion")); assertNotNull(holder.getParameter("part")); }
@Test public void parsesQueryWithPropertyPathAlternative() { final String query = "SELECT ?x WHERE {?x ?hasPart|?hasQuestion ?part . }"; final QueryHolder holder = queryParser.parseQuery(query); assertNotNull(holder.getParameter("x")); assertNotNull(holder.getParameter("hasPart")); assertNotNull(holder.getParameter("hasQuestion")); assertNotNull(holder.getParameter("part")); }