Search in sources :

Example 26 with MalformedQueryException

use of org.eclipse.rdf4j.query.MalformedQueryException in project rdf4j by eclipse.

the class SeRQLParser method parseQuery.

public ParsedQuery parseQuery(String queryStr, String baseURI) throws MalformedQueryException {
    try {
        ASTQueryContainer qc = SyntaxTreeBuilder.parseQuery(queryStr);
        // Replace deprecated NULL nodes with semantically equivalent
        // alternatives
        NullProcessor.process(qc);
        StringEscapesProcessor.process(qc);
        Map<String, String> namespaces = NamespaceDeclProcessor.process(qc);
        ProjectionProcessor.process(qc);
        qc.jjtAccept(new ProjectionAliasProcessor(), null);
        qc.jjtAccept(new AnonymousVarGenerator(), null);
        // TODO: check use of unbound variables?
        TupleExpr tupleExpr = QueryModelBuilder.buildQueryModel(qc, SimpleValueFactory.getInstance());
        ASTQuery queryNode = qc.getQuery();
        ParsedQuery query;
        if (queryNode instanceof ASTTupleQuery) {
            query = new ParsedTupleQuery(tupleExpr);
        } else if (queryNode instanceof ASTGraphQuery) {
            query = new ParsedGraphQuery(tupleExpr, namespaces);
        } else {
            throw new RuntimeException("Unexpected query type: " + queryNode.getClass());
        }
        return query;
    } catch (ParseException e) {
        throw new MalformedQueryException(e.getMessage(), e);
    } catch (TokenMgrError e) {
        throw new MalformedQueryException(e.getMessage(), e);
    } catch (VisitorException e) {
        throw new MalformedQueryException(e.getMessage(), e);
    }
}
Also used : ParsedQuery(org.eclipse.rdf4j.query.parser.ParsedQuery) ParsedGraphQuery(org.eclipse.rdf4j.query.parser.ParsedGraphQuery) ASTQueryContainer(org.eclipse.rdf4j.query.parser.serql.ast.ASTQueryContainer) TokenMgrError(org.eclipse.rdf4j.query.parser.serql.ast.TokenMgrError) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) ASTTupleQuery(org.eclipse.rdf4j.query.parser.serql.ast.ASTTupleQuery) ASTQuery(org.eclipse.rdf4j.query.parser.serql.ast.ASTQuery) ASTGraphQuery(org.eclipse.rdf4j.query.parser.serql.ast.ASTGraphQuery) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) ParseException(org.eclipse.rdf4j.query.parser.serql.ast.ParseException) VisitorException(org.eclipse.rdf4j.query.parser.serql.ast.VisitorException) ParsedTupleQuery(org.eclipse.rdf4j.query.parser.ParsedTupleQuery)

Example 27 with MalformedQueryException

use of org.eclipse.rdf4j.query.MalformedQueryException in project rdf4j by eclipse.

the class SPARQLParserTest method testInsertDataLineNumberReporting.

@Test
public void testInsertDataLineNumberReporting() throws Exception {
    String insertDataString = "INSERT DATA {\n incorrect reference }";
    try {
        ParsedUpdate u = parser.parseUpdate(insertDataString, null);
        fail("should have resulted in parse exception");
    } catch (MalformedQueryException e) {
        assertTrue(e.getMessage().contains("line 2,"));
    }
}
Also used : ParsedUpdate(org.eclipse.rdf4j.query.parser.ParsedUpdate) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) Test(org.junit.Test)

Example 28 with MalformedQueryException

use of org.eclipse.rdf4j.query.MalformedQueryException in project rdf4j by eclipse.

the class SPARQLParserTest method testDeleteDataLineNumberReporting.

@Test
public void testDeleteDataLineNumberReporting() throws Exception {
    String deleteDataString = "DELETE DATA {\n incorrect reference }";
    try {
        ParsedUpdate u = parser.parseUpdate(deleteDataString, null);
        fail("should have resulted in parse exception");
    } catch (MalformedQueryException e) {
        assertTrue(e.getMessage().contains("line 2,"));
    }
}
Also used : ParsedUpdate(org.eclipse.rdf4j.query.parser.ParsedUpdate) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) Test(org.junit.Test)

Example 29 with MalformedQueryException

use of org.eclipse.rdf4j.query.MalformedQueryException in project rdf4j by eclipse.

the class BlankNodeVarProcessor method process.

public static Set<String> process(ASTOperationContainer qc) throws MalformedQueryException {
    try {
        BlankNodeToVarConverter converter = new BlankNodeToVarConverter();
        qc.jjtAccept(converter, null);
        return converter.getUsedBNodeIDs();
    } catch (VisitorException e) {
        throw new MalformedQueryException(e);
    }
}
Also used : MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) VisitorException(org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException)

Example 30 with MalformedQueryException

use of org.eclipse.rdf4j.query.MalformedQueryException in project rdf4j by eclipse.

the class BaseDeclProcessor method process.

/**
 * Resolves relative URIs in the supplied query model using either the specified <tt>externalBaseURI</tt>
 * or, if this parameter is <tt>null</tt>, the base URI specified in the query model itself.
 *
 * @param qc
 *        The query model to resolve relative URIs in.
 * @param externalBaseURI
 *        The external base URI to use for resolving relative URIs, or <tt>null</tt> if the base URI that
 *        is specified in the query model should be used.
 * @throws IllegalArgumentException
 *         If an external base URI is specified that is not an absolute URI.
 * @throws MalformedQueryException
 *         If the base URI specified in the query model is not an absolute URI.
 */
public static void process(ASTOperationContainer qc, String externalBaseURI) throws MalformedQueryException {
    ParsedIRI parsedBaseURI = null;
    // Use the query model's own base URI, if available
    ASTBaseDecl baseDecl = qc.getBaseDecl();
    if (baseDecl != null) {
        try {
            parsedBaseURI = new ParsedIRI(baseDecl.getIRI());
        } catch (URISyntaxException e) {
            throw new MalformedQueryException(e);
        }
        if (!parsedBaseURI.isAbsolute()) {
            throw new MalformedQueryException("BASE IRI is not an absolute IRI: " + externalBaseURI);
        }
    } else if (externalBaseURI != null) {
        // Use external base URI if the query doesn't contain one itself
        try {
            parsedBaseURI = new ParsedIRI(externalBaseURI);
        } catch (URISyntaxException e) {
            throw new MalformedQueryException(e);
        }
        if (!parsedBaseURI.isAbsolute()) {
            throw new IllegalArgumentException("Supplied base URI is not an absolute IRI: " + externalBaseURI);
        }
    } else {
    // FIXME: use the "Default Base URI"?
    }
    if (parsedBaseURI != null) {
        ASTUnparsedQuadDataBlock dataBlock = null;
        if (qc.getOperation() instanceof ASTInsertData) {
            ASTInsertData insertData = (ASTInsertData) qc.getOperation();
            dataBlock = insertData.jjtGetChild(ASTUnparsedQuadDataBlock.class);
        } else if (qc.getOperation() instanceof ASTDeleteData) {
            ASTDeleteData deleteData = (ASTDeleteData) qc.getOperation();
            dataBlock = deleteData.jjtGetChild(ASTUnparsedQuadDataBlock.class);
        }
        if (dataBlock != null) {
            final String baseURIDeclaration = "BASE <" + parsedBaseURI + "> \n";
            dataBlock.setDataBlock(baseURIDeclaration + dataBlock.getDataBlock());
        } else {
            RelativeIRIResolver visitor = new RelativeIRIResolver(parsedBaseURI);
            try {
                qc.jjtAccept(visitor, null);
            } catch (VisitorException e) {
                throw new MalformedQueryException(e);
            }
        }
    }
}
Also used : ASTBaseDecl(org.eclipse.rdf4j.query.parser.sparql.ast.ASTBaseDecl) ASTDeleteData(org.eclipse.rdf4j.query.parser.sparql.ast.ASTDeleteData) ParsedIRI(org.eclipse.rdf4j.common.net.ParsedIRI) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) ASTUnparsedQuadDataBlock(org.eclipse.rdf4j.query.parser.sparql.ast.ASTUnparsedQuadDataBlock) URISyntaxException(java.net.URISyntaxException) VisitorException(org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException) ASTInsertData(org.eclipse.rdf4j.query.parser.sparql.ast.ASTInsertData)

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