use of org.eclipse.rdf4j.query.MalformedQueryException in project rdf4j by eclipse.
the class SPARQLConnection method getContextIDs.
public RepositoryResult<Resource> getContextIDs() throws RepositoryException {
TupleQueryResult iter = null;
RepositoryResult<Resource> result = null;
boolean allGood = false;
try {
TupleQuery query = prepareTupleQuery(SPARQL, NAMEDGRAPHS, "");
iter = query.evaluate();
result = new RepositoryResult<Resource>(new ExceptionConvertingIteration<Resource, RepositoryException>(new ConvertingIteration<BindingSet, Resource, QueryEvaluationException>(iter) {
@Override
protected Resource convert(BindingSet bindings) throws QueryEvaluationException {
return (Resource) bindings.getValue("_");
}
}) {
@Override
protected RepositoryException convert(Exception e) {
return new RepositoryException(e);
}
});
allGood = true;
return result;
} catch (MalformedQueryException e) {
throw new RepositoryException(e);
} catch (QueryEvaluationException e) {
throw new RepositoryException(e);
} finally {
if (!allGood) {
try {
if (result != null) {
result.close();
}
} finally {
if (iter != null) {
iter.close();
}
}
}
}
}
use of org.eclipse.rdf4j.query.MalformedQueryException in project rdf4j by eclipse.
the class SPARQLConnection method getStatementsQuadMode.
private RepositoryResult<Statement> getStatementsQuadMode(Resource subj, IRI pred, Value obj, boolean includeInferred, Resource... contexts) throws MalformedQueryException, RepositoryException, QueryEvaluationException {
TupleQueryResult qRes = null;
RepositoryResult<Statement> result = null;
boolean allGood = false;
try {
TupleQuery tupleQuery = prepareTupleQuery(SPARQL, EVERYTHING_WITH_GRAPH);
setBindings(tupleQuery, subj, pred, obj, contexts);
tupleQuery.setIncludeInferred(includeInferred);
qRes = tupleQuery.evaluate();
result = new RepositoryResult<Statement>(new ExceptionConvertingIteration<Statement, RepositoryException>(toStatementIteration(qRes, subj, pred, obj)) {
@Override
protected RepositoryException convert(Exception e) {
return new RepositoryException(e);
}
});
allGood = true;
return result;
} finally {
if (!allGood) {
try {
if (result != null) {
result.close();
}
} finally {
if (qRes != null) {
qRes.close();
}
}
}
}
}
use of org.eclipse.rdf4j.query.MalformedQueryException in project rdf4j by eclipse.
the class HTTPTupleQuery method evaluate.
public void evaluate(TupleQueryResultHandler handler) throws QueryEvaluationException, TupleQueryResultHandlerException {
SPARQLProtocolSession client = getHttpClient();
try {
conn.flushTransactionState(Protocol.Action.QUERY);
client.sendTupleQuery(queryLanguage, queryString, baseURI, dataset, includeInferred, getMaxExecutionTime(), handler, getBindingsArray());
} catch (IOException e) {
throw new HTTPQueryEvaluationException(e.getMessage(), e);
} catch (RepositoryException e) {
throw new HTTPQueryEvaluationException(e.getMessage(), e);
} catch (MalformedQueryException e) {
throw new HTTPQueryEvaluationException(e.getMessage(), e);
}
}
use of org.eclipse.rdf4j.query.MalformedQueryException in project rdf4j by eclipse.
the class HTTPGraphQuery method evaluate.
/*
* public GraphQueryResult evaluate() throws QueryEvaluationException { HTTPClient client =
* httpCon.getRepository().getHTTPClient(); try { return client.sendGraphQuery(queryLanguage, queryString,
* baseURI, dataset, includeInferred, maxQueryTime, getBindingsArray()); } catch (IOException e) { throw
* new HTTPQueryEvaluationException(e.getMessage(), e); } catch (RepositoryException e) { throw new
* HTTPQueryEvaluationException(e.getMessage(), e); } catch (MalformedQueryException e) { throw new
* HTTPQueryEvaluationException(e.getMessage(), e); } }
*/
public void evaluate(RDFHandler handler) throws QueryEvaluationException, RDFHandlerException {
SPARQLProtocolSession client = getHttpClient();
try {
conn.flushTransactionState(Protocol.Action.QUERY);
client.sendGraphQuery(queryLanguage, queryString, baseURI, dataset, includeInferred, getMaxExecutionTime(), handler, getBindingsArray());
} catch (IOException e) {
throw new HTTPQueryEvaluationException(e.getMessage(), e);
} catch (RepositoryException e) {
throw new HTTPQueryEvaluationException(e.getMessage(), e);
} catch (MalformedQueryException e) {
throw new HTTPQueryEvaluationException(e.getMessage(), e);
}
}
use of org.eclipse.rdf4j.query.MalformedQueryException in project rdf4j by eclipse.
the class NamespaceDeclProcessor method process.
/**
* Processes prefix declarations in queries. This method collects all prefixes that are declared in the
* supplied query, verifies that prefixes are not redefined and replaces any {@link ASTQName} nodes in the
* query with equivalent {@link ASTIRI} nodes.
*
* @param qc
* The query that needs to be processed.
* @return A map containing the prefixes that are declared in the query (key) and the namespace they map
* to (value).
* @throws MalformedQueryException
* If the query contains redefined prefixes or qnames that use undefined prefixes.
*/
public static Map<String, String> process(ASTQueryContainer qc) throws MalformedQueryException {
List<ASTNamespaceDecl> nsDeclList = qc.getNamespaceDeclList();
// Build a prefix --> URI map
Map<String, String> nsMap = new LinkedHashMap<String, String>();
for (ASTNamespaceDecl nsDecl : nsDeclList) {
String prefix = nsDecl.getPrefix();
String uri = nsDecl.getURI().getValue();
if (nsMap.containsKey(prefix)) {
// Prefix already defined
if (nsMap.get(prefix).equals(uri)) {
// duplicate, ignore
} else {
throw new MalformedQueryException("Multiple namespace declarations for prefix '" + prefix + "'");
}
} else {
nsMap.put(prefix, uri);
}
}
// Use default namespace prefixes when not defined explicitly
if (!nsMap.containsKey("rdf")) {
nsMap.put("rdf", RDF.NAMESPACE);
}
if (!nsMap.containsKey("rdfs")) {
nsMap.put("rdfs", RDFS.NAMESPACE);
}
if (!nsMap.containsKey("xsd")) {
nsMap.put("xsd", XMLSchema.NAMESPACE);
}
if (!nsMap.containsKey("owl")) {
nsMap.put("owl", OWL.NAMESPACE);
}
if (!nsMap.containsKey("sesame")) {
nsMap.put("sesame", SESAME.NAMESPACE);
}
// For backwards compatibility:
Map<String, String> extendedNsMap = new HashMap<String, String>(nsMap);
if (!extendedNsMap.containsKey("serql")) {
extendedNsMap.put("serql", SESAME.NAMESPACE);
}
// Replace all qnames with URIs
QNameProcessor visitor = new QNameProcessor(extendedNsMap);
try {
qc.jjtAccept(visitor, null);
} catch (VisitorException e) {
throw new MalformedQueryException(e.getMessage(), e);
}
return nsMap;
}
Aggregations