Search in sources :

Example 1 with VisitorException

use of org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException in project rdf4j by eclipse.

the class UpdateExprBuilder method visit.

@Override
public Modify visit(ASTDeleteWhere node, Object data) throws VisitorException {
    // Collect delete clause triples
    GraphPattern parentGP = graphPattern;
    graphPattern = new GraphPattern();
    // inherit scope & context
    graphPattern.setStatementPatternScope(parentGP.getStatementPatternScope());
    graphPattern.setContextVar(parentGP.getContextVar());
    for (int i = 0; i < node.jjtGetNumChildren(); i++) {
        node.jjtGetChild(i).jjtAccept(this, data);
    }
    TupleExpr whereExpr = graphPattern.buildTupleExpr();
    graphPattern = parentGP;
    TupleExpr deleteExpr = whereExpr.clone();
    // FIXME we should adapt the grammar so we can avoid doing this
    // post-processing.
    VarCollector collector = new VarCollector();
    deleteExpr.visit(collector);
    for (Var var : collector.getCollectedVars()) {
        if (var.isAnonymous() && !var.hasValue()) {
            throw new VisitorException("DELETE WHERE may not contain blank nodes");
        }
    }
    Modify modify = new Modify(deleteExpr, null, whereExpr);
    return modify;
}
Also used : Var(org.eclipse.rdf4j.query.algebra.Var) Modify(org.eclipse.rdf4j.query.algebra.Modify) ASTModify(org.eclipse.rdf4j.query.parser.sparql.ast.ASTModify) VisitorException(org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr)

Example 2 with VisitorException

use of org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException 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);
    }
}
Also used : ASTVar(org.eclipse.rdf4j.query.parser.sparql.ast.ASTVar) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) ASTProjectionElem(org.eclipse.rdf4j.query.parser.sparql.ast.ASTProjectionElem) VisitorException(org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException)

Example 3 with VisitorException

use of org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException 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 4 with VisitorException

use of org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException 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)

Example 5 with VisitorException

use of org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException in project rdf4j by eclipse.

the class PrefixDeclProcessor 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(ASTOperationContainer qc) throws MalformedQueryException {
    List<ASTPrefixDecl> prefixDeclList = qc.getPrefixDeclList();
    // Build a prefix --> IRI map
    Map<String, String> prefixMap = new LinkedHashMap<String, String>();
    for (ASTPrefixDecl prefixDecl : prefixDeclList) {
        String prefix = prefixDecl.getPrefix();
        String iri = prefixDecl.getIRI().getValue();
        if (prefixMap.containsKey(prefix)) {
            throw new MalformedQueryException("Multiple prefix declarations for prefix '" + prefix + "'");
        }
        prefixMap.put(prefix, iri);
    }
    // insert some default prefixes (if not explicitly defined in the query)
    final int defaultPrefixesAdded = insertDefaultPrefix(prefixMap, "rdf", RDF.NAMESPACE) + insertDefaultPrefix(prefixMap, "rdfs", RDFS.NAMESPACE) + insertDefaultPrefix(prefixMap, "sesame", SESAME.NAMESPACE) + insertDefaultPrefix(prefixMap, "owl", OWL.NAMESPACE) + insertDefaultPrefix(prefixMap, "xsd", XMLSchema.NAMESPACE) + insertDefaultPrefix(prefixMap, "fn", FN.NAMESPACE);
    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) {
        String prefixes = createPrefixesInSPARQLFormat(prefixMap);
        dataBlock.setAddedDefaultPrefixes(defaultPrefixesAdded);
        // TODO optimize string concat?
        dataBlock.setDataBlock(prefixes + dataBlock.getDataBlock());
    } else {
        QNameProcessor visitor = new QNameProcessor(prefixMap);
        try {
            qc.jjtAccept(visitor, null);
        } catch (VisitorException e) {
            throw new MalformedQueryException(e);
        }
    }
    return prefixMap;
}
Also used : ASTPrefixDecl(org.eclipse.rdf4j.query.parser.sparql.ast.ASTPrefixDecl) ASTDeleteData(org.eclipse.rdf4j.query.parser.sparql.ast.ASTDeleteData) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) ASTUnparsedQuadDataBlock(org.eclipse.rdf4j.query.parser.sparql.ast.ASTUnparsedQuadDataBlock) VisitorException(org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException) ASTInsertData(org.eclipse.rdf4j.query.parser.sparql.ast.ASTInsertData) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

VisitorException (org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException)8 MalformedQueryException (org.eclipse.rdf4j.query.MalformedQueryException)6 ASTInsertData (org.eclipse.rdf4j.query.parser.sparql.ast.ASTInsertData)3 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)2 Var (org.eclipse.rdf4j.query.algebra.Var)2 ASTDeleteData (org.eclipse.rdf4j.query.parser.sparql.ast.ASTDeleteData)2 ASTPrefixDecl (org.eclipse.rdf4j.query.parser.sparql.ast.ASTPrefixDecl)2 ASTUnparsedQuadDataBlock (org.eclipse.rdf4j.query.parser.sparql.ast.ASTUnparsedQuadDataBlock)2 URISyntaxException (java.net.URISyntaxException)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 ParsedIRI (org.eclipse.rdf4j.common.net.ParsedIRI)1 Dataset (org.eclipse.rdf4j.query.Dataset)1 Modify (org.eclipse.rdf4j.query.algebra.Modify)1 UpdateExpr (org.eclipse.rdf4j.query.algebra.UpdateExpr)1 ParsedUpdate (org.eclipse.rdf4j.query.parser.ParsedUpdate)1 ASTBaseDecl (org.eclipse.rdf4j.query.parser.sparql.ast.ASTBaseDecl)1 ASTDescribe (org.eclipse.rdf4j.query.parser.sparql.ast.ASTDescribe)1 ASTDescribeQuery (org.eclipse.rdf4j.query.parser.sparql.ast.ASTDescribeQuery)1 ASTModify (org.eclipse.rdf4j.query.parser.sparql.ast.ASTModify)1