Source Code Cross Referenced for QueryParsingHelper.java in  » Database-ORM » db-ojb » org » apache » ojb » jdo » jdoql » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database ORM » db ojb » org.apache.ojb.jdo.jdoql 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.