01: /*
02: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2007.
03: *
04: * Licensed under the Aduna BSD-style license.
05: */
06: package org.openrdf.query.parser.serql;
07:
08: import java.io.BufferedReader;
09: import java.io.InputStreamReader;
10: import java.util.Map;
11:
12: import org.openrdf.model.impl.ValueFactoryImpl;
13: import org.openrdf.query.MalformedQueryException;
14: import org.openrdf.query.algebra.TupleExpr;
15: import org.openrdf.query.parser.ParsedGraphQuery;
16: import org.openrdf.query.parser.ParsedQuery;
17: import org.openrdf.query.parser.ParsedTupleQuery;
18: import org.openrdf.query.parser.QueryParser;
19: import org.openrdf.query.parser.serql.ast.ASTGraphQuery;
20: import org.openrdf.query.parser.serql.ast.ASTQuery;
21: import org.openrdf.query.parser.serql.ast.ASTQueryContainer;
22: import org.openrdf.query.parser.serql.ast.ASTTupleQuery;
23: import org.openrdf.query.parser.serql.ast.ParseException;
24: import org.openrdf.query.parser.serql.ast.SyntaxTreeBuilder;
25: import org.openrdf.query.parser.serql.ast.TokenMgrError;
26: import org.openrdf.query.parser.serql.ast.VisitorException;
27:
28: public class SeRQLParser implements QueryParser {
29:
30: public ParsedQuery parseQuery(String queryStr, String baseURI)
31: throws MalformedQueryException {
32: try {
33: ASTQueryContainer qc = SyntaxTreeBuilder
34: .parseQuery(queryStr);
35:
36: // Replace deprecates NULL nodes with semantically equivalent
37: // alternatives
38: NullProcessor.process(qc);
39:
40: StringEscapesProcessor.process(qc);
41: Map<String, String> namespaces = NamespaceDeclProcessor
42: .process(qc);
43: WildcardProjectionProcessor.process(qc);
44: qc.jjtAccept(new ProjectionAliasProcessor(), null);
45: qc.jjtAccept(new AnonymousVarGenerator(), null);
46:
47: // TODO: check use of unbound variables?
48:
49: TupleExpr tupleExpr = QueryModelBuilder.buildQueryModel(qc,
50: new ValueFactoryImpl());
51:
52: ASTQuery queryNode = qc.getQuery();
53: ParsedQuery query;
54: if (queryNode instanceof ASTTupleQuery) {
55: query = new ParsedTupleQuery(tupleExpr);
56: } else if (queryNode instanceof ASTGraphQuery) {
57: query = new ParsedGraphQuery(tupleExpr, namespaces);
58: } else {
59: throw new RuntimeException("Unexpected query type: "
60: + queryNode.getClass());
61: }
62:
63: return query;
64: } catch (ParseException e) {
65: throw new MalformedQueryException(e.getMessage(), e);
66: } catch (TokenMgrError e) {
67: throw new MalformedQueryException(e.getMessage(), e);
68: } catch (VisitorException e) {
69: throw new MalformedQueryException(e.getMessage(), e);
70: }
71: }
72:
73: public static void main(String[] args) throws java.io.IOException {
74: System.out.println("Your SeRQL query:");
75: BufferedReader in = new BufferedReader(new InputStreamReader(
76: System.in));
77:
78: StringBuilder buf = new StringBuilder();
79: String line = null;
80: while ((line = in.readLine()) != null) {
81: if (line.length() > 0) {
82: buf.append(' ').append(line).append('\n');
83: } else {
84: String queryStr = buf.toString().trim();
85: if (queryStr.length() > 0) {
86: try {
87: SeRQLParser parser = new SeRQLParser();
88: parser.parseQuery(queryStr, null);
89: } catch (Exception e) {
90: System.err.println(e.getMessage());
91: e.printStackTrace();
92: }
93: }
94: buf.setLength(0);
95: }
96: }
97: }
98: }
|