01: /*
02: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2006.
03: *
04: * Licensed under the Aduna BSD-style license.
05: */
06: package org.openrdf.query.parser.sparql;
07:
08: import java.util.LinkedHashSet;
09: import java.util.Set;
10:
11: import org.openrdf.query.MalformedQueryException;
12: import org.openrdf.query.parser.sparql.ast.ASTDescribe;
13: import org.openrdf.query.parser.sparql.ast.ASTDescribeQuery;
14: import org.openrdf.query.parser.sparql.ast.ASTQuery;
15: import org.openrdf.query.parser.sparql.ast.ASTQueryContainer;
16: import org.openrdf.query.parser.sparql.ast.ASTSelect;
17: import org.openrdf.query.parser.sparql.ast.ASTSelectQuery;
18: import org.openrdf.query.parser.sparql.ast.ASTVar;
19: import org.openrdf.query.parser.sparql.ast.Node;
20: import org.openrdf.query.parser.sparql.ast.SyntaxTreeBuilderTreeConstants;
21: import org.openrdf.query.parser.sparql.ast.VisitorException;
22:
23: /**
24: * Processes 'wildcard' projections, making them explicit by adding the
25: * appropriate variable nodes to them.
26: *
27: * @author arjohn
28: */
29: class WildcardProjectionProcessor extends ASTVisitorBase {
30:
31: public static void process(ASTQueryContainer qc)
32: throws MalformedQueryException {
33: ASTQuery queryNode = qc.getQuery();
34:
35: if (queryNode instanceof ASTSelectQuery) {
36: ASTSelect selectNode = ((ASTSelectQuery) queryNode)
37: .getSelect();
38:
39: if (selectNode.isWildcard()) {
40: addQueryVars(qc, selectNode);
41: selectNode.setWildcard(false);
42: }
43: } else if (queryNode instanceof ASTDescribeQuery) {
44: ASTDescribe describeNode = ((ASTDescribeQuery) queryNode)
45: .getDescribe();
46:
47: if (describeNode.isWildcard()) {
48: addQueryVars(qc, describeNode);
49: describeNode.setWildcard(false);
50: }
51: }
52: }
53:
54: private static void addQueryVars(ASTQueryContainer qc,
55: Node wildcardNode) throws MalformedQueryException {
56: QueryVariableCollector visitor = new QueryVariableCollector();
57: try {
58: // Collect variable names from query
59: qc.jjtAccept(visitor, null);
60:
61: // Adds ASTVar nodes to the wildcard node
62: for (String varName : visitor.getVariableNames()) {
63: ASTVar varNode = new ASTVar(
64: SyntaxTreeBuilderTreeConstants.JJTVAR);
65: varNode.setName(varName);
66: wildcardNode.jjtAppendChild(varNode);
67: varNode.jjtSetParent(wildcardNode);
68: }
69: } catch (VisitorException e) {
70: throw new MalformedQueryException(e);
71: }
72: }
73:
74: /*------------------------------------*
75: * Inner class QueryVariableCollector *
76: *------------------------------------*/
77:
78: private static class QueryVariableCollector extends ASTVisitorBase {
79:
80: private Set<String> variableNames = new LinkedHashSet<String>();
81:
82: public Set<String> getVariableNames() {
83: return variableNames;
84: }
85:
86: @Override
87: public Object visit(ASTVar node, Object data)
88: throws VisitorException {
89: if (!node.isAnonymous()) {
90: variableNames.add(node.getName());
91: }
92: return super.visit(node, data);
93: }
94: }
95: }
|