001: package org.apache.ojb.jdo.jdoql;
002:
003: /* Copyright 2003-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import java.io.StringReader;
019: import java.util.*;
020:
021: import javax.jdo.JDOUserException;
022:
023: import antlr.RecognitionException;
024: import antlr.TokenStreamException;
025:
026: /**
027: * Helper class for building the actual query structure from a JDOQL query definition.
028: *
029: * @author <a href="mailto:tomdz@apache.org">Thomas Dudziak</a>
030: */
031: public class QueryParsingHelper {
032: /**
033: * Parses the given parameter declaration string.
034: *
035: * @return The parameters indexed by their names
036: */
037: public Map parseParameters(String declaration)
038: throws JDOUserException {
039: JDOQLLexer lexer = new JDOQLLexer(new StringReader(declaration));
040: JDOQLParser parser = new JDOQLParser(lexer);
041: JDOQLTreeParser treeParser = new JDOQLTreeParser();
042:
043: parser.setASTNodeClass(ASTWithPositionInfo.class.getName());
044: try {
045: parser.declareParameters();
046: return treeParser.declareParameters(parser.getAST());
047: } catch (RecognitionException ex) {
048: throw new JDOUserException(
049: "Error in parameter declaration at line "
050: + ex.getLine() + ", column "
051: + ex.getColumn());
052: } catch (TokenStreamException ex) {
053: throw new JDOUserException(
054: "Could not parse the parameter declaration");
055: }
056: }
057:
058: /**
059: * Parses the given declaration string.
060: *
061: * @return The variables indexed by their names
062: */
063: public Map parseVariables(String declaration)
064: throws JDOUserException {
065: JDOQLLexer lexer = new JDOQLLexer(new StringReader(declaration));
066: JDOQLParser parser = new JDOQLParser(lexer);
067: JDOQLTreeParser treeParser = new JDOQLTreeParser();
068:
069: parser.setASTNodeClass(ASTWithPositionInfo.class.getName());
070: try {
071: parser.declareVariables();
072: return treeParser.declareVariables(parser.getAST());
073: } catch (RecognitionException ex) {
074: throw new JDOUserException(
075: "Error in variable declaration at line "
076: + ex.getLine() + ", column "
077: + ex.getColumn());
078: } catch (TokenStreamException ex) {
079: throw new JDOUserException(
080: "Could not parse the variable declaration");
081: }
082: }
083:
084: /**
085: * Parses the given imports declaration string.
086: *
087: * @return The imports list
088: */
089: public List parseImports(String declaration)
090: throws JDOUserException {
091: JDOQLLexer lexer = new JDOQLLexer(new StringReader(declaration));
092: JDOQLParser parser = new JDOQLParser(lexer);
093: JDOQLTreeParser treeParser = new JDOQLTreeParser();
094:
095: parser.setASTNodeClass(ASTWithPositionInfo.class.getName());
096: try {
097: parser.declareImports();
098: return treeParser.declareImports(parser.getAST());
099: } catch (RecognitionException ex) {
100: throw new JDOUserException(
101: "Error in import specification at line "
102: + ex.getLine() + ", column "
103: + ex.getColumn());
104: } catch (TokenStreamException ex) {
105: throw new JDOUserException(
106: "Could not parse the import specification");
107: }
108: }
109:
110: /**
111: * Parses the given orderings declaration string.
112: *
113: * @return The orderings list
114: */
115: public List parseOrderings(String declaration)
116: throws JDOUserException {
117: JDOQLLexer lexer = new JDOQLLexer(new StringReader(declaration));
118: JDOQLParser parser = new JDOQLParser(lexer);
119: JDOQLTreeParser treeParser = new JDOQLTreeParser();
120:
121: parser.setASTNodeClass(ASTWithPositionInfo.class.getName());
122: try {
123: parser.setOrdering();
124: return treeParser.setOrdering(parser.getAST());
125: } catch (RecognitionException ex) {
126: throw new JDOUserException(
127: "Error in ordering specification at line "
128: + ex.getLine() + ", column "
129: + ex.getColumn());
130: } catch (TokenStreamException ex) {
131: throw new JDOUserException(
132: "Could not parse the ordering specification");
133: }
134: }
135:
136: /**
137: * Parses the given filter expression string.
138: *
139: * @return The filter expression
140: */
141: public Expression parseFilter(String expression)
142: throws JDOUserException {
143: JDOQLLexer lexer = new JDOQLLexer(new StringReader(expression));
144: JDOQLParser parser = new JDOQLParser(lexer);
145: JDOQLTreeParser treeParser = new JDOQLTreeParser();
146:
147: parser.setASTNodeClass(ASTWithPositionInfo.class.getName());
148: try {
149: parser.expression();
150: return treeParser.expression(parser.getAST());
151: } catch (RecognitionException ex) {
152: throw new JDOUserException(
153: "Error in filter expression at line "
154: + ex.getLine() + ", column "
155: + ex.getColumn());
156: } catch (TokenStreamException ex) {
157: throw new JDOUserException(
158: "Could not parse the filter expression");
159: }
160: }
161:
162: public static void main(String[] args) throws JDOUserException {
163: if (args.length < 2) {
164: System.out.println("Usage:\n\njava "
165: + QueryParsingHelper.class.getName()
166: + " [type] [text]\n");
167: System.out
168: .println("where type specifies what kind of text to parse");
169: System.out
170: .println("(imports, parameters, variables, orderings, filter)");
171: System.out
172: .println("and text gives the actual text to parse");
173: return;
174: }
175:
176: QueryParsingHelper helper = new QueryParsingHelper();
177:
178: if ("imports".equals(args[0])) {
179: List imports = helper.parseImports(args[1]);
180:
181: for (int idx = 0; idx < imports.size(); idx++) {
182: System.out.println("(" + idx + "): "
183: + imports.get(idx).toString());
184: }
185: } else if ("parameters".equals(args[0])) {
186: Map params = helper.parseParameters(args[1]);
187:
188: for (Iterator it = params.values().iterator(); it.hasNext();) {
189: System.out.println(it.toString());
190: }
191: } else if ("variables".equals(args[0])) {
192: Map vars = helper.parseVariables(args[1]);
193:
194: for (Iterator it = vars.values().iterator(); it.hasNext();) {
195: System.out.println(it.toString());
196: }
197: } else if ("orderings".equals(args[0])) {
198: List orderings = helper.parseOrderings(args[1]);
199:
200: for (int idx = 0; idx < orderings.size(); idx++) {
201: System.out.println("(" + idx + "): "
202: + orderings.get(idx).toString());
203: }
204: } else if ("filter".equals(args[0])) {
205: Expression filterExpr = helper.parseFilter(args[1]);
206:
207: System.out.println(filterExpr.toString());
208: } else {
209: System.out
210: .println("Unknown kind of text; allowed are: imports, parameters, variables, orderings, filter");
211: }
212: }
213: }
|