use of org.eclipse.rdf4j.query.MalformedQueryException in project rdf4j by eclipse.
the class TupleExprBuilder method visit.
@Override
public TupleExpr visit(ASTConstructQuery node, Object data) throws VisitorException {
// Start with building the graph pattern
graphPattern = new GraphPattern();
node.getWhereClause().jjtAccept(this, null);
TupleExpr tupleExpr = graphPattern.buildTupleExpr();
// Apply grouping
ASTGroupClause groupNode = node.getGroupClause();
if (groupNode != null) {
tupleExpr = (TupleExpr) groupNode.jjtAccept(this, tupleExpr);
}
Group group = null;
if (tupleExpr instanceof Group) {
group = (Group) tupleExpr;
} else {
// create a new implicit group. Note that this group will only
// actually
// be used in the query model if the query has HAVING or ORDER BY
// clause
group = new Group(tupleExpr);
}
// Apply HAVING group filter condition
tupleExpr = processHavingClause(node.getHavingClause(), tupleExpr, group);
// process bindings clause
ASTBindingsClause bindingsClause = node.getBindingsClause();
if (bindingsClause != null) {
tupleExpr = new Join((BindingSetAssignment) bindingsClause.jjtAccept(this, null), tupleExpr);
}
// Apply result ordering
tupleExpr = processOrderClause(node.getOrderClause(), tupleExpr, null);
// Process construct clause
ASTConstruct constructNode = node.getConstruct();
if (!constructNode.isWildcard()) {
tupleExpr = (TupleExpr) constructNode.jjtAccept(this, tupleExpr);
} else {
// create construct clause from graph pattern.
ConstructorBuilder cb = new ConstructorBuilder();
// possible future use.
try {
tupleExpr = cb.buildConstructor(tupleExpr, false, false);
} catch (MalformedQueryException e) {
throw new VisitorException(e.getMessage());
}
}
// process limit and offset clauses
ASTLimit limitNode = node.getLimit();
long limit = -1L;
if (limitNode != null) {
limit = (Long) limitNode.jjtAccept(this, null);
}
ASTOffset offsetNode = node.getOffset();
long offset = -1;
if (offsetNode != null) {
offset = (Long) offsetNode.jjtAccept(this, null);
}
if (offset >= 1 || limit >= 0) {
tupleExpr = new Slice(tupleExpr, offset, limit);
}
return tupleExpr;
}
use of org.eclipse.rdf4j.query.MalformedQueryException in project rdf4j by eclipse.
the class SPARQLParser method parseQuery.
@Override
public ParsedQuery parseQuery(String queryStr, String baseURI) throws MalformedQueryException {
try {
ASTQueryContainer qc = SyntaxTreeBuilder.parseQuery(queryStr);
StringEscapesProcessor.process(qc);
BaseDeclProcessor.process(qc, baseURI);
Map<String, String> prefixes = PrefixDeclProcessor.process(qc);
WildcardProjectionProcessor.process(qc);
BlankNodeVarProcessor.process(qc);
if (qc.containsQuery()) {
// handle query operation
TupleExpr tupleExpr = buildQueryModel(qc);
ParsedQuery query;
ASTQuery queryNode = qc.getQuery();
if (queryNode instanceof ASTSelectQuery) {
query = new ParsedTupleQuery(queryStr, tupleExpr);
} else if (queryNode instanceof ASTConstructQuery) {
query = new ParsedGraphQuery(queryStr, tupleExpr, prefixes);
} else if (queryNode instanceof ASTAskQuery) {
query = new ParsedBooleanQuery(queryStr, tupleExpr);
} else if (queryNode instanceof ASTDescribeQuery) {
query = new ParsedDescribeQuery(queryStr, tupleExpr, prefixes);
} else {
throw new RuntimeException("Unexpected query type: " + queryNode.getClass());
}
// Handle dataset declaration
Dataset dataset = DatasetDeclProcessor.process(qc);
if (dataset != null) {
query.setDataset(dataset);
}
return query;
} else {
throw new IncompatibleOperationException("supplied string is not a query operation");
}
} catch (ParseException e) {
throw new MalformedQueryException(e.getMessage(), e);
} catch (TokenMgrError e) {
throw new MalformedQueryException(e.getMessage(), e);
}
}
use of org.eclipse.rdf4j.query.MalformedQueryException in project rdf4j by eclipse.
the class DatasetDeclProcessor method process.
/**
* Extracts a SPARQL {@link Dataset} from an ASTQueryContainer, if one is contained. Returns null
* otherwise.
*
* @param qc
* The query model to resolve relative URIs in.
* @throws MalformedQueryException
* If DatasetClause does not contain a valid URI.
*/
public static Dataset process(ASTOperationContainer qc) throws MalformedQueryException {
SimpleDataset dataset = null;
ASTOperation op = qc.getOperation();
if (op != null) {
List<ASTDatasetClause> datasetClauses = op.getDatasetClauseList();
if (!datasetClauses.isEmpty()) {
dataset = new SimpleDataset();
for (ASTDatasetClause dc : datasetClauses) {
ASTIRI astIri = dc.jjtGetChild(ASTIRI.class);
try {
IRI uri = SESAME.NIL;
if (astIri != null) {
uri = SimpleValueFactory.getInstance().createIRI(astIri.getValue());
}
boolean withClause = false;
if (op instanceof ASTModify) {
if (dc.equals(((ASTModify) op).getWithClause())) {
withClause = true;
dataset.setDefaultInsertGraph(uri);
dataset.addDefaultRemoveGraph(uri);
}
}
// clauses.
if (!withClause || datasetClauses.size() == 1) {
if (dc.isNamed()) {
dataset.addNamedGraph(uri);
} else {
dataset.addDefaultGraph(uri);
}
}
} catch (IllegalArgumentException e) {
throw new MalformedQueryException(e.getMessage(), e);
}
}
}
}
return dataset;
}
use of org.eclipse.rdf4j.query.MalformedQueryException in project rdf4j by eclipse.
the class WildcardProjectionProcessor method addQueryVars.
private static void addQueryVars(ASTWhereClause queryBody, Node wildcardNode) throws MalformedQueryException {
QueryVariableCollector visitor = new QueryVariableCollector();
try {
// Collect variable names from query
queryBody.jjtAccept(visitor, null);
// Adds ASTVar nodes to the ASTProjectionElem nodes and to the parent
for (String varName : visitor.getVariableNames()) {
ASTVar varNode = new ASTVar(SyntaxTreeBuilderTreeConstants.JJTVAR);
ASTProjectionElem projectionElemNode = new ASTProjectionElem(SyntaxTreeBuilderTreeConstants.JJTPROJECTIONELEM);
varNode.setName(varName);
projectionElemNode.jjtAppendChild(varNode);
varNode.jjtSetParent(projectionElemNode);
wildcardNode.jjtAppendChild(projectionElemNode);
projectionElemNode.jjtSetParent(wildcardNode);
}
} catch (VisitorException e) {
throw new MalformedQueryException(e);
}
}
use of org.eclipse.rdf4j.query.MalformedQueryException in project rdf4j by eclipse.
the class RDF4JProtocolSession method getStatements.
/*---------------------------*
* Get/add/remove statements *
*---------------------------*/
public void getStatements(Resource subj, IRI pred, Value obj, boolean includeInferred, RDFHandler handler, Resource... contexts) throws IOException, RDFHandlerException, RepositoryException, UnauthorizedException, QueryInterruptedException {
checkRepositoryURL();
try {
String transactionURL = getTransactionURL();
final boolean useTransaction = transactionURL != null;
String baseLocation = useTransaction ? transactionURL : Protocol.getStatementsLocation(getQueryURL());
URIBuilder url = new URIBuilder(baseLocation);
if (subj != null) {
url.setParameter(Protocol.SUBJECT_PARAM_NAME, Protocol.encodeValue(subj));
}
if (pred != null) {
url.setParameter(Protocol.PREDICATE_PARAM_NAME, Protocol.encodeValue(pred));
}
if (obj != null) {
url.setParameter(Protocol.OBJECT_PARAM_NAME, Protocol.encodeValue(obj));
}
for (String encodedContext : Protocol.encodeContexts(contexts)) {
url.addParameter(Protocol.CONTEXT_PARAM_NAME, encodedContext);
}
url.setParameter(Protocol.INCLUDE_INFERRED_PARAM_NAME, Boolean.toString(includeInferred));
if (useTransaction) {
url.setParameter(Protocol.ACTION_PARAM_NAME, Action.GET.toString());
}
HttpRequestBase method = useTransaction ? new HttpPut(url.build()) : new HttpGet(url.build());
try {
getRDF(method, handler, true);
} catch (MalformedQueryException e) {
logger.warn("Server reported unexpected malfored query error", e);
throw new RepositoryException(e.getMessage(), e);
} finally {
method.reset();
}
} catch (URISyntaxException e) {
throw new AssertionError(e);
}
pingTransaction();
}
Aggregations