Search in sources :

Example 1 with ASTPrefixDecl

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

Example 2 with ASTPrefixDecl

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

the class SPARQLParser method parseUpdate.

@Override
public ParsedUpdate parseUpdate(String updateStr, String baseURI) throws MalformedQueryException {
    try {
        ParsedUpdate update = new ParsedUpdate(updateStr);
        ASTUpdateSequence updateSequence = SyntaxTreeBuilder.parseUpdateSequence(updateStr);
        List<ASTUpdateContainer> updateOperations = updateSequence.getUpdateContainers();
        List<ASTPrefixDecl> sharedPrefixDeclarations = null;
        Node node = updateSequence.jjtGetChild(0);
        Set<String> globalUsedBNodeIds = new HashSet<String>();
        for (int i = 0; i < updateOperations.size(); i++) {
            ASTUpdateContainer uc = updateOperations.get(i);
            if (uc.jjtGetNumChildren() == 0 && i > 0 && i < updateOperations.size() - 1) {
                // empty update in the middle of the sequence
                throw new MalformedQueryException("empty update in sequence not allowed");
            }
            StringEscapesProcessor.process(uc);
            BaseDeclProcessor.process(uc, baseURI);
            WildcardProjectionProcessor.process(uc);
            if (uc.getBaseDecl() != null) {
                baseURI = uc.getBaseDecl().getIRI();
            }
            // do a special dance to handle prefix declarations in sequences: if
            // the current
            // operation has its own prefix declarations, use those. Otherwise,
            // try and use
            // prefix declarations from a previous operation in this sequence.
            List<ASTPrefixDecl> prefixDeclList = uc.getPrefixDeclList();
            if (prefixDeclList == null || prefixDeclList.size() == 0) {
                if (sharedPrefixDeclarations != null) {
                    for (ASTPrefixDecl prefixDecl : sharedPrefixDeclarations) {
                        uc.jjtAppendChild(prefixDecl);
                    }
                }
            } else {
                sharedPrefixDeclarations = prefixDeclList;
            }
            PrefixDeclProcessor.process(uc);
            Set<String> usedBNodeIds = BlankNodeVarProcessor.process(uc);
            if (uc.getUpdate() instanceof ASTInsertData || uc.getUpdate() instanceof ASTInsertData) {
                if (Collections.disjoint(usedBNodeIds, globalUsedBNodeIds)) {
                    globalUsedBNodeIds.addAll(usedBNodeIds);
                } else {
                    throw new MalformedQueryException("blank node identifier may not be shared across INSERT/DELETE DATA operations");
                }
            }
            UpdateExprBuilder updateExprBuilder = new UpdateExprBuilder(SimpleValueFactory.getInstance());
            ASTUpdate updateNode = uc.getUpdate();
            if (updateNode != null) {
                UpdateExpr updateExpr = (UpdateExpr) updateNode.jjtAccept(updateExprBuilder, null);
                // add individual update expression to ParsedUpdate sequence
                // container
                update.addUpdateExpr(updateExpr);
                // associate updateExpr with the correct dataset (if any)
                Dataset dataset = DatasetDeclProcessor.process(uc);
                update.map(updateExpr, dataset);
            }
        }
        return update;
    } 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 : Dataset(org.eclipse.rdf4j.query.Dataset) Node(org.eclipse.rdf4j.query.parser.sparql.ast.Node) TokenMgrError(org.eclipse.rdf4j.query.parser.sparql.ast.TokenMgrError) ASTUpdateSequence(org.eclipse.rdf4j.query.parser.sparql.ast.ASTUpdateSequence) ASTInsertData(org.eclipse.rdf4j.query.parser.sparql.ast.ASTInsertData) ASTPrefixDecl(org.eclipse.rdf4j.query.parser.sparql.ast.ASTPrefixDecl) ParsedUpdate(org.eclipse.rdf4j.query.parser.ParsedUpdate) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) ASTUpdate(org.eclipse.rdf4j.query.parser.sparql.ast.ASTUpdate) UpdateExpr(org.eclipse.rdf4j.query.algebra.UpdateExpr) ParseException(org.eclipse.rdf4j.query.parser.sparql.ast.ParseException) VisitorException(org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException) ASTUpdateContainer(org.eclipse.rdf4j.query.parser.sparql.ast.ASTUpdateContainer) HashSet(java.util.HashSet)

Aggregations

MalformedQueryException (org.eclipse.rdf4j.query.MalformedQueryException)2 ASTInsertData (org.eclipse.rdf4j.query.parser.sparql.ast.ASTInsertData)2 ASTPrefixDecl (org.eclipse.rdf4j.query.parser.sparql.ast.ASTPrefixDecl)2 VisitorException (org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException)2 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 Dataset (org.eclipse.rdf4j.query.Dataset)1 UpdateExpr (org.eclipse.rdf4j.query.algebra.UpdateExpr)1 ParsedUpdate (org.eclipse.rdf4j.query.parser.ParsedUpdate)1 ASTDeleteData (org.eclipse.rdf4j.query.parser.sparql.ast.ASTDeleteData)1 ASTUnparsedQuadDataBlock (org.eclipse.rdf4j.query.parser.sparql.ast.ASTUnparsedQuadDataBlock)1 ASTUpdate (org.eclipse.rdf4j.query.parser.sparql.ast.ASTUpdate)1 ASTUpdateContainer (org.eclipse.rdf4j.query.parser.sparql.ast.ASTUpdateContainer)1 ASTUpdateSequence (org.eclipse.rdf4j.query.parser.sparql.ast.ASTUpdateSequence)1 Node (org.eclipse.rdf4j.query.parser.sparql.ast.Node)1 ParseException (org.eclipse.rdf4j.query.parser.sparql.ast.ParseException)1 TokenMgrError (org.eclipse.rdf4j.query.parser.sparql.ast.TokenMgrError)1