Search in sources :

Example 21 with MalformedQueryException

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();
                }
            }
        }
    }
}
Also used : BindingSet(org.eclipse.rdf4j.query.BindingSet) ExceptionConvertingIteration(org.eclipse.rdf4j.common.iteration.ExceptionConvertingIteration) Resource(org.eclipse.rdf4j.model.Resource) TupleQuery(org.eclipse.rdf4j.query.TupleQuery) SPARQLTupleQuery(org.eclipse.rdf4j.repository.sparql.query.SPARQLTupleQuery) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) UpdateExecutionException(org.eclipse.rdf4j.query.UpdateExecutionException) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) UnknownTransactionStateException(org.eclipse.rdf4j.repository.UnknownTransactionStateException) UnsupportedQueryLanguageException(org.eclipse.rdf4j.query.UnsupportedQueryLanguageException) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) IOException(java.io.IOException) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) TupleQueryResult(org.eclipse.rdf4j.query.TupleQueryResult)

Example 22 with MalformedQueryException

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();
                }
            }
        }
    }
}
Also used : Statement(org.eclipse.rdf4j.model.Statement) ExceptionConvertingIteration(org.eclipse.rdf4j.common.iteration.ExceptionConvertingIteration) TupleQuery(org.eclipse.rdf4j.query.TupleQuery) SPARQLTupleQuery(org.eclipse.rdf4j.repository.sparql.query.SPARQLTupleQuery) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) TupleQueryResult(org.eclipse.rdf4j.query.TupleQueryResult) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) UpdateExecutionException(org.eclipse.rdf4j.query.UpdateExecutionException) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) UnknownTransactionStateException(org.eclipse.rdf4j.repository.UnknownTransactionStateException) UnsupportedQueryLanguageException(org.eclipse.rdf4j.query.UnsupportedQueryLanguageException) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) IOException(java.io.IOException)

Example 23 with MalformedQueryException

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);
    }
}
Also used : SPARQLProtocolSession(org.eclipse.rdf4j.http.client.SPARQLProtocolSession) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) IOException(java.io.IOException)

Example 24 with MalformedQueryException

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);
    }
}
Also used : SPARQLProtocolSession(org.eclipse.rdf4j.http.client.SPARQLProtocolSession) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) IOException(java.io.IOException)

Example 25 with MalformedQueryException

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;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ASTNamespaceDecl(org.eclipse.rdf4j.query.parser.serql.ast.ASTNamespaceDecl) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) VisitorException(org.eclipse.rdf4j.query.parser.serql.ast.VisitorException) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

MalformedQueryException (org.eclipse.rdf4j.query.MalformedQueryException)33 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)20 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)9 IOException (java.io.IOException)6 TupleQuery (org.eclipse.rdf4j.query.TupleQuery)5 TupleQueryResult (org.eclipse.rdf4j.query.TupleQueryResult)5 VisitorException (org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException)5 HttpGet (org.apache.http.client.methods.HttpGet)4 QueryInterruptedException (org.eclipse.rdf4j.query.QueryInterruptedException)4 UnsupportedQueryLanguageException (org.eclipse.rdf4j.query.UnsupportedQueryLanguageException)4 UpdateExecutionException (org.eclipse.rdf4j.query.UpdateExecutionException)4 ExceptionConvertingIteration (org.eclipse.rdf4j.common.iteration.ExceptionConvertingIteration)3 SPARQLProtocolSession (org.eclipse.rdf4j.http.client.SPARQLProtocolSession)3 Binding (org.eclipse.rdf4j.query.Binding)3 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)3 ParsedUpdate (org.eclipse.rdf4j.query.parser.ParsedUpdate)3 ASTInsertData (org.eclipse.rdf4j.query.parser.sparql.ast.ASTInsertData)3 UnknownTransactionStateException (org.eclipse.rdf4j.repository.UnknownTransactionStateException)3 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)3 RDFParseException (org.eclipse.rdf4j.rio.RDFParseException)3