KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > jdo > jdoql > QueryParsingHelper


1 package org.apache.ojb.jdo.jdoql;
2
3 /* Copyright 2003-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import java.io.StringReader JavaDoc;
19 import java.util.*;
20
21 import javax.jdo.JDOUserException;
22
23 import antlr.RecognitionException;
24 import antlr.TokenStreamException;
25
26 /**
27  * Helper class for building the actual query structure from a JDOQL query definition.
28  *
29  * @author <a HREF="mailto:tomdz@apache.org">Thomas Dudziak</a>
30  */

31 public class QueryParsingHelper
32 {
33     /**
34      * Parses the given parameter declaration string.
35      *
36      * @return The parameters indexed by their names
37      */

38     public Map parseParameters(String JavaDoc declaration) throws JDOUserException
39     {
40         JDOQLLexer lexer = new JDOQLLexer(new StringReader JavaDoc(declaration));
41         JDOQLParser parser = new JDOQLParser(lexer);
42         JDOQLTreeParser treeParser = new JDOQLTreeParser();
43         
44         parser.setASTNodeClass(ASTWithPositionInfo.class.getName());
45         try
46         {
47             parser.declareParameters();
48             return treeParser.declareParameters(parser.getAST());
49         }
50         catch (RecognitionException ex)
51         {
52             throw new JDOUserException("Error in parameter declaration at line "+ex.getLine()+", column "+ex.getColumn());
53         }
54         catch (TokenStreamException ex)
55         {
56             throw new JDOUserException("Could not parse the parameter declaration");
57         }
58     }
59
60     /**
61      * Parses the given declaration string.
62      *
63      * @return The variables indexed by their names
64      */

65     public Map parseVariables(String JavaDoc declaration) throws JDOUserException
66     {
67         JDOQLLexer lexer = new JDOQLLexer(new StringReader JavaDoc(declaration));
68         JDOQLParser parser = new JDOQLParser(lexer);
69         JDOQLTreeParser treeParser = new JDOQLTreeParser();
70         
71         parser.setASTNodeClass(ASTWithPositionInfo.class.getName());
72         try
73         {
74             parser.declareVariables();
75             return treeParser.declareVariables(parser.getAST());
76         }
77         catch (RecognitionException ex)
78         {
79             throw new JDOUserException("Error in variable declaration at line "+ex.getLine()+", column "+ex.getColumn());
80         }
81         catch (TokenStreamException ex)
82         {
83             throw new JDOUserException("Could not parse the variable declaration");
84         }
85     }
86
87     /**
88      * Parses the given imports declaration string.
89      *
90      * @return The imports list
91      */

92     public List parseImports(String JavaDoc declaration) throws JDOUserException
93     {
94         JDOQLLexer lexer = new JDOQLLexer(new StringReader JavaDoc(declaration));
95         JDOQLParser parser = new JDOQLParser(lexer);
96         JDOQLTreeParser treeParser = new JDOQLTreeParser();
97
98         parser.setASTNodeClass(ASTWithPositionInfo.class.getName());
99         try
100         {
101             parser.declareImports();
102             return treeParser.declareImports(parser.getAST());
103         }
104         catch (RecognitionException ex)
105         {
106             throw new JDOUserException("Error in import specification at line "+ex.getLine()+", column "+ex.getColumn());
107         }
108         catch (TokenStreamException ex)
109         {
110             throw new JDOUserException("Could not parse the import specification");
111         }
112     }
113
114     /**
115      * Parses the given orderings declaration string.
116      *
117      * @return The orderings list
118      */

119     public List parseOrderings(String JavaDoc declaration) throws JDOUserException
120     {
121         JDOQLLexer lexer = new JDOQLLexer(new StringReader JavaDoc(declaration));
122         JDOQLParser parser = new JDOQLParser(lexer);
123         JDOQLTreeParser treeParser = new JDOQLTreeParser();
124         
125         parser.setASTNodeClass(ASTWithPositionInfo.class.getName());
126         try
127         {
128             parser.setOrdering();
129             return treeParser.setOrdering(parser.getAST());
130         }
131         catch (RecognitionException ex)
132         {
133             throw new JDOUserException("Error in ordering specification at line "+ex.getLine()+", column "+ex.getColumn());
134         }
135         catch (TokenStreamException ex)
136         {
137             throw new JDOUserException("Could not parse the ordering specification");
138         }
139     }
140
141     /**
142      * Parses the given filter expression string.
143      *
144      * @return The filter expression
145      */

146     public Expression parseFilter(String JavaDoc expression) throws JDOUserException
147     {
148         JDOQLLexer lexer = new JDOQLLexer(new StringReader JavaDoc(expression));
149         JDOQLParser parser = new JDOQLParser(lexer);
150         JDOQLTreeParser treeParser = new JDOQLTreeParser();
151         
152         parser.setASTNodeClass(ASTWithPositionInfo.class.getName());
153         try
154         {
155             parser.expression();
156             return treeParser.expression(parser.getAST());
157         }
158         catch (RecognitionException ex)
159         {
160             throw new JDOUserException("Error in filter expression at line "+ex.getLine()+", column "+ex.getColumn());
161         }
162         catch (TokenStreamException ex)
163         {
164             throw new JDOUserException("Could not parse the filter expression");
165         }
166     }
167
168     public static void main(String JavaDoc[] args) throws JDOUserException
169     {
170         if (args.length < 2)
171         {
172             System.out.println("Usage:\n\njava "+QueryParsingHelper.class.getName()+" [type] [text]\n");
173             System.out.println("where type specifies what kind of text to parse");
174             System.out.println("(imports, parameters, variables, orderings, filter)");
175             System.out.println("and text gives the actual text to parse");
176             return;
177         }
178
179         QueryParsingHelper helper = new QueryParsingHelper();
180         
181         if ("imports".equals(args[0]))
182         {
183             List imports = helper.parseImports(args[1]);
184
185             for (int idx = 0; idx < imports.size(); idx++)
186             {
187                 System.out.println("("+idx+"): "+imports.get(idx).toString());
188             }
189         }
190         else if ("parameters".equals(args[0]))
191         {
192             Map params = helper.parseParameters(args[1]);
193
194             for (Iterator it = params.values().iterator(); it.hasNext();)
195             {
196                 System.out.println(it.toString());
197             }
198         }
199         else if ("variables".equals(args[0]))
200         {
201             Map vars = helper.parseVariables(args[1]);
202
203             for (Iterator it = vars.values().iterator(); it.hasNext();)
204             {
205                 System.out.println(it.toString());
206             }
207         }
208         else if ("orderings".equals(args[0]))
209         {
210             List orderings = helper.parseOrderings(args[1]);
211
212             for (int idx = 0; idx < orderings.size(); idx++)
213             {
214                 System.out.println("("+idx+"): "+orderings.get(idx).toString());
215             }
216         }
217         else if ("filter".equals(args[0]))
218         {
219             Expression filterExpr = helper.parseFilter(args[1]);
220
221             System.out.println(filterExpr.toString());
222         }
223         else
224         {
225             System.out.println("Unknown kind of text; allowed are: imports, parameters, variables, orderings, filter");
226         }
227     }
228 }
229
Popular Tags