[Mulgara-svn] r599 - branches/mgr-61-sparql/src/jar/sparql/java/org/mulgara/sparql
pag at mulgara.org
pag at mulgara.org
Tue Dec 4 21:41:44 UTC 2007
Author: pag
Date: 2007-12-04 15:41:42 -0600 (Tue, 04 Dec 2007)
New Revision: 599
Added:
branches/mgr-61-sparql/src/jar/sparql/java/org/mulgara/sparql/SparqlInterpreter.java
Log:
Adding an interpreter class to convert strings into CSTs and then ASTs
Added: branches/mgr-61-sparql/src/jar/sparql/java/org/mulgara/sparql/SparqlInterpreter.java
===================================================================
--- branches/mgr-61-sparql/src/jar/sparql/java/org/mulgara/sparql/SparqlInterpreter.java 2007-12-04 21:40:59 UTC (rev 598)
+++ branches/mgr-61-sparql/src/jar/sparql/java/org/mulgara/sparql/SparqlInterpreter.java 2007-12-04 21:41:42 UTC (rev 599)
@@ -0,0 +1,86 @@
+/**
+ * The contents of this file are subject to the Open Software License
+ * Version 3.0 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ * http://www.opensource.org/licenses/osl-3.0.txt
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
+ * the License for the specific language governing rights and limitations
+ * under the License.
+ */
+package org.mulgara.sparql;
+
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.mulgara.parser.Interpreter;
+import org.mulgara.parser.MulgaraLexerException;
+import org.mulgara.parser.MulgaraParserException;
+import org.mulgara.query.Query;
+import org.mulgara.query.operation.Command;
+import org.mulgara.sparql.compiler.ExpressionParser;
+import org.mulgara.sparql.compiler.ExpressionScanner;
+import org.mulgara.sparql.cst.SelectQuery;
+import org.mulgara.sparql.cst.SparqlQuery;
+
+import beaver.Parser;
+
+/**
+ * Interpreter for SPARQL queries.
+ *
+ * @created December 4, 2007
+ * @author Paul Gearon
+ * @copyright © 2007 <a href="mailto:pgearon at users.sourceforge.net">Paul Gearon</a>
+ * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
+ */
+public class SparqlInterpreter implements Interpreter {
+
+ /**
+ * @see org.mulgara.parser.Interpreter#parseCommand(java.lang.String)
+ */
+ public Command parseCommand(String command) throws MulgaraParserException, MulgaraLexerException, IllegalArgumentException, IOException {
+ ExpressionParser parser = new ExpressionParser();
+ ExpressionScanner input = new ExpressionScanner(new StringReader(command));
+ SparqlQuery cmdNode;
+ try {
+ cmdNode = (SparqlQuery)parser.parse(input);
+ } catch (Parser.Exception e) {
+ throw new MulgaraParserException("Error parsing: " + e.getMessage(), e);
+ }
+ // double-dispatch into this command so that it invokes the correct method in this class
+ return cmdNode.accept(this);
+ }
+
+ /**
+ * Only handles a single query
+ * @see org.mulgara.parser.Interpreter#parseCommands(java.lang.String)
+ */
+ public List<Command> parseCommands(String command) throws MulgaraParserException, MulgaraLexerException, IOException, IllegalArgumentException {
+ List<Command> result = new ArrayList<Command>();
+ result.add(parseCommand(command));
+ return result;
+ }
+
+ /**
+ * Parses a query. Throws an exception if the string results in some other type of command.
+ * @see org.mulgara.parser.Interpreter#parseQuery(java.lang.String)
+ */
+ public Query parseQuery(String queryString) throws IOException, MulgaraLexerException, MulgaraParserException {
+ Command cmd = parseCommand(queryString);
+ if (!cmd.isAnswerable()) throw new MulgaraParserException("Not a valid query [" + cmd.getClass().getSimpleName() + "]: " + queryString);
+ return (Query)cmd;
+ }
+
+ public Command visit(SelectQuery select) {
+ // TODO:
+ return null;
+ }
+
+// TODO
+// public void visit(DescribeQuery describe) {
+//
+// }
+}
More information about the Mulgara-svn
mailing list