Search in sources :

Example 1 with ASTNamespaceDecl

use of org.eclipse.rdf4j.query.parser.serql.ast.ASTNamespaceDecl 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

HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 MalformedQueryException (org.eclipse.rdf4j.query.MalformedQueryException)1 ASTNamespaceDecl (org.eclipse.rdf4j.query.parser.serql.ast.ASTNamespaceDecl)1 VisitorException (org.eclipse.rdf4j.query.parser.serql.ast.VisitorException)1