001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.query.parser.sparql;
007:
008: import java.io.BufferedReader;
009: import java.io.InputStreamReader;
010: import java.util.Map;
011:
012: import org.openrdf.model.impl.ValueFactoryImpl;
013: import org.openrdf.query.Dataset;
014: import org.openrdf.query.MalformedQueryException;
015: import org.openrdf.query.algebra.TupleExpr;
016: import org.openrdf.query.parser.ParsedBooleanQuery;
017: import org.openrdf.query.parser.ParsedGraphQuery;
018: import org.openrdf.query.parser.ParsedQuery;
019: import org.openrdf.query.parser.ParsedTupleQuery;
020: import org.openrdf.query.parser.QueryParser;
021: import org.openrdf.query.parser.sparql.ast.ASTAskQuery;
022: import org.openrdf.query.parser.sparql.ast.ASTConstructQuery;
023: import org.openrdf.query.parser.sparql.ast.ASTDescribeQuery;
024: import org.openrdf.query.parser.sparql.ast.ASTQuery;
025: import org.openrdf.query.parser.sparql.ast.ASTQueryContainer;
026: import org.openrdf.query.parser.sparql.ast.ASTSelectQuery;
027: import org.openrdf.query.parser.sparql.ast.ParseException;
028: import org.openrdf.query.parser.sparql.ast.SyntaxTreeBuilder;
029: import org.openrdf.query.parser.sparql.ast.TokenMgrError;
030: import org.openrdf.query.parser.sparql.ast.VisitorException;
031:
032: public class SPARQLParser implements QueryParser {
033:
034: public ParsedQuery parseQuery(String queryStr, String baseURI)
035: throws MalformedQueryException {
036: try {
037: ASTQueryContainer qc = SyntaxTreeBuilder
038: .parseQuery(queryStr);
039: StringEscapesProcessor.process(qc);
040: BaseDeclProcessor.process(qc, baseURI);
041: Map<String, String> prefixes = PrefixDeclProcessor
042: .process(qc);
043: WildcardProjectionProcessor.process(qc);
044: BlankNodeVarProcessor.process(qc);
045: TupleExpr tupleExpr = buildQueryModel(qc);
046:
047: ParsedQuery query;
048:
049: ASTQuery queryNode = qc.getQuery();
050: if (queryNode instanceof ASTSelectQuery) {
051: query = new ParsedTupleQuery(tupleExpr);
052: } else if (queryNode instanceof ASTConstructQuery) {
053: query = new ParsedGraphQuery(tupleExpr, prefixes);
054: } else if (queryNode instanceof ASTAskQuery) {
055: query = new ParsedBooleanQuery(tupleExpr);
056: } else if (queryNode instanceof ASTDescribeQuery) {
057: query = new ParsedGraphQuery(tupleExpr, prefixes);
058: } else {
059: throw new RuntimeException("Unexpected query type: "
060: + queryNode.getClass());
061: }
062:
063: // Handle dataset declaration
064: Dataset dataset = DatasetDeclProcessor.process(qc);
065: if (dataset != null) {
066: query.setDataset(dataset);
067: }
068:
069: return query;
070: } catch (ParseException e) {
071: throw new MalformedQueryException(e.getMessage(), e);
072: } catch (TokenMgrError e) {
073: throw new MalformedQueryException(e.getMessage(), e);
074: }
075: }
076:
077: private TupleExpr buildQueryModel(ASTQueryContainer qc)
078: throws MalformedQueryException {
079: TupleExprBuilder tupleExprBuilder = new TupleExprBuilder(
080: new ValueFactoryImpl());
081: try {
082: return (TupleExpr) qc.jjtAccept(tupleExprBuilder, null);
083: } catch (VisitorException e) {
084: throw new MalformedQueryException(e.getMessage(), e);
085: }
086: }
087:
088: public static void main(String[] args) throws java.io.IOException {
089: System.out.println("Your SPARQL query:");
090:
091: BufferedReader in = new BufferedReader(new InputStreamReader(
092: System.in));
093:
094: StringBuilder buf = new StringBuilder();
095: String line = null;
096: while ((line = in.readLine()) != null) {
097: if (line.length() > 0) {
098: buf.append(' ').append(line).append('\n');
099: } else {
100: String queryStr = buf.toString().trim();
101: if (queryStr.length() > 0) {
102: try {
103: SPARQLParser parser = new SPARQLParser();
104: parser.parseQuery(queryStr, null);
105: } catch (Exception e) {
106: System.err.println(e.getMessage());
107: e.printStackTrace();
108: }
109: }
110: buf.setLength(0);
111: }
112: }
113: }
114: }
|