KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > hql > ast > SqlASTFactory


1 // $Id: SqlASTFactory.java,v 1.35 2005/07/12 20:27:29 steveebersole Exp $
2
package org.hibernate.hql.ast;
3
4 import antlr.ASTFactory;
5 import antlr.Token;
6 import antlr.collections.AST;
7 import org.hibernate.hql.antlr.HqlSqlTokenTypes;
8 import org.hibernate.hql.ast.tree.AggregateNode;
9 import org.hibernate.hql.ast.tree.ArithmeticNode;
10 import org.hibernate.hql.ast.tree.Case2Node;
11 import org.hibernate.hql.ast.tree.CaseNode;
12 import org.hibernate.hql.ast.tree.CollectionFunction;
13 import org.hibernate.hql.ast.tree.ConstructorNode;
14 import org.hibernate.hql.ast.tree.CountNode;
15 import org.hibernate.hql.ast.tree.DeleteStatement;
16 import org.hibernate.hql.ast.tree.DotNode;
17 import org.hibernate.hql.ast.tree.FromClause;
18 import org.hibernate.hql.ast.tree.FromElement;
19 import org.hibernate.hql.ast.tree.IdentNode;
20 import org.hibernate.hql.ast.tree.ImpliedFromElement;
21 import org.hibernate.hql.ast.tree.IndexNode;
22 import org.hibernate.hql.ast.tree.InitializeableNode;
23 import org.hibernate.hql.ast.tree.InsertStatement;
24 import org.hibernate.hql.ast.tree.IntoClause;
25 import org.hibernate.hql.ast.tree.LiteralNode;
26 import org.hibernate.hql.ast.tree.MethodNode;
27 import org.hibernate.hql.ast.tree.OrderByClause;
28 import org.hibernate.hql.ast.tree.ParameterNode;
29 import org.hibernate.hql.ast.tree.QueryNode;
30 import org.hibernate.hql.ast.tree.SelectClause;
31 import org.hibernate.hql.ast.tree.SelectExpressionImpl;
32 import org.hibernate.hql.ast.tree.SqlFragment;
33 import org.hibernate.hql.ast.tree.SqlNode;
34 import org.hibernate.hql.ast.tree.UnaryNode;
35 import org.hibernate.hql.ast.tree.UpdateStatement;
36
37 import java.lang.reflect.Constructor JavaDoc;
38
39 /**
40  * Custom AST factory the intermediate tree that causes ANTLR to create specialized
41  * AST nodes, given the AST node type (from HqlSqlTokenTypes). HqlSqlWalker registers
42  * this factory with itself when it is initialized.
43  * <br>User: josh
44  * <br>Date: Nov 22, 2003
45  * <br>Time: 3:34:28 PM
46  */

47 public class SqlASTFactory extends ASTFactory implements HqlSqlTokenTypes {
48     private HqlSqlWalker walker;
49
50     /**
51      * Create factory with a specific mapping from token type
52      * to Java AST node type. Your subclasses of ASTFactory
53      * can override and reuse the map stuff.
54      */

55     public SqlASTFactory(HqlSqlWalker walker) {
56         super();
57         this.walker = walker;
58     }
59
60     /**
61      * Returns the class for a given token type (a.k.a. AST node type).
62      *
63      * @param tokenType The token type.
64      * @return Class - The AST node class to instantiate.
65      */

66     public Class JavaDoc getASTNodeType(int tokenType) {
67         switch ( tokenType ) {
68             case SELECT:
69             case QUERY:
70                 return QueryNode.class;
71             case UPDATE:
72                 return UpdateStatement.class;
73             case DELETE:
74                 return DeleteStatement.class;
75             case INSERT:
76                 return InsertStatement.class;
77             case INTO:
78                 return IntoClause.class;
79             case FROM:
80                 return FromClause.class;
81             case FROM_FRAGMENT:
82                 return FromElement.class;
83             case IMPLIED_FROM:
84                 return ImpliedFromElement.class;
85             case DOT:
86                 return DotNode.class;
87             case INDEX_OP:
88                 return IndexNode.class;
89                 // Alias references and identifiers use the same node class.
90
case ALIAS_REF:
91             case IDENT:
92                 return IdentNode.class;
93             case SQL_TOKEN:
94                 return SqlFragment.class;
95             case METHOD_CALL:
96                 return MethodNode.class;
97             case ELEMENTS:
98             case INDICES:
99                 return CollectionFunction.class;
100             case SELECT_CLAUSE:
101                 return SelectClause.class;
102             case SELECT_EXPR:
103                 return SelectExpressionImpl.class;
104             case AGGREGATE:
105                 return AggregateNode.class;
106             case COUNT:
107                 return CountNode.class;
108             case CONSTRUCTOR:
109                 return ConstructorNode.class;
110             case NUM_INT:
111             case NUM_FLOAT:
112             case NUM_LONG:
113             case NUM_DOUBLE:
114             case QUOTED_STRING:
115             case TRUE:
116             case FALSE:
117                 return LiteralNode.class;
118             case ORDER:
119                 return OrderByClause.class;
120             case PLUS:
121             case MINUS:
122             case STAR:
123             case DIV:
124                 return ArithmeticNode.class;
125             case UNARY_MINUS:
126             case UNARY_PLUS:
127                 return UnaryNode.class;
128             case CASE2:
129                 return Case2Node.class;
130             case CASE:
131                 return CaseNode.class;
132             case PARAM:
133             case NAMED_PARAM:
134                 return ParameterNode.class;
135             default:
136                 return SqlNode.class;
137         } // switch
138
}
139
140     protected AST createUsingCtor(Token token, String JavaDoc className) {
141         Class JavaDoc c;
142         AST t;
143         try {
144             c = Class.forName( className );
145             Class JavaDoc[] tokenArgType = new Class JavaDoc[]{antlr.Token.class};
146             Constructor JavaDoc ctor = c.getConstructor( tokenArgType );
147             if ( ctor != null ) {
148                 t = ( AST ) ctor.newInstance( new Object JavaDoc[]{token} ); // make a new one
149
initializeSqlNode( t );
150             }
151             else {
152                 // just do the regular thing if you can't find the ctor
153
// Your AST must have default ctor to use this.
154
t = create( c );
155             }
156         }
157         catch ( Exception JavaDoc e ) {
158             throw new IllegalArgumentException JavaDoc( "Invalid class or can't make instance, " + className );
159         }
160         return t;
161     }
162
163     private void initializeSqlNode(AST t) {
164         // Initialize SQL nodes here.
165
if ( t instanceof InitializeableNode ) {
166             InitializeableNode initializeableNode = ( InitializeableNode ) t;
167             initializeableNode.initialize( walker );
168         }
169     }
170
171     /**
172      * Actually instantiate the AST node.
173      *
174      * @param c The class to instantiate.
175      * @return The instantiated and initialized node.
176      */

177     protected AST create(Class JavaDoc c) {
178         AST t;
179         try {
180             t = ( AST ) c.newInstance(); // make a new one
181
initializeSqlNode( t );
182         }
183         catch ( Exception JavaDoc e ) {
184             error( "Can't create AST Node " + c.getName() );
185             return null;
186         }
187         return t;
188     }
189
190 }
191
Popular Tags