/** * Returns an <tt>SelectQuery</tt> for the algebraic expression and optional dataset * specified. * * @param expression * the algebraic expression for the query * @param dataset * the dataset optionally associated to the query * @return the corresponding <tt>SelectQuery</tt> object * @throws ParseException * in case the supplied algebraic expression does not denote a valid SPARQL SELECT * query */ public static SelectQuery from(final TupleExpr expression, @Nullable final Dataset dataset) throws ParseException { Preconditions.checkNotNull(expression); try { // Sesame rendering facilities are definitely broken, so we use our own final String string = new SPARQLRenderer(null, true).render(expression, dataset); SelectQuery query = CACHE.getIfPresent(string); if (query == null) { query = new SelectQuery(string, expression, dataset); CACHE.put(string, query); } return query; } catch (final Exception ex) { throw new ParseException(expression.toString(), "The supplied algebraic expression does not denote a valid SPARQL query", ex); } }
/** * Returns a <tt>SelectQuery</tt> for the specified SPARQL SELECT query string. * * @param string * the query string, in SPARQL and without relative URIs * @return the corresponding <tt>SelectQuery</tt> * @throws ParseException * in case the string does not denote a valid SPARQL SELECT query */ public static SelectQuery from(final String string) throws ParseException { Preconditions.checkNotNull(string); SelectQuery query = CACHE.getIfPresent(string); if (query == null) { final ParsedTupleQuery parsedQuery; try { parsedQuery = QueryParserUtil.parseTupleQuery(QueryLanguage.SPARQL, string, null); } catch (final IllegalArgumentException ex) { throw new ParseException(string, "SPARQL query not in SELECT form", ex); } catch (final MalformedQueryException ex) { throw new ParseException(string, "Invalid SPARQL query: " + ex.getMessage(), ex); } query = new SelectQuery(string, parsedQuery.getTupleExpr(), parsedQuery.getDataset()); CACHE.put(string, query); } return query; }