KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > support > sqlstore > query > jqlc > JQLAST


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * JQLAST.java
26  *
27  * Created on March 8, 2000
28  */

29
30 package com.sun.jdo.spi.persistence.support.sqlstore.query.jqlc;
31
32 import persistence.antlr.Token;
33 import persistence.antlr.CommonAST;
34 import persistence.antlr.collections.AST;
35 import persistence.antlr.ASTFactory;
36
37 import com.sun.jdo.spi.persistence.support.sqlstore.query.util.type.Type;
38 import com.sun.jdo.spi.persistence.support.sqlstore.RetrieveDesc;
39
40 /**
41  * This class represents a node in the intermediate representation (AST)
42  * used by the query compiler.
43  * It provides
44  * - line info
45  * - column info
46  * - type info (object of class util.type.Type): the semantic analysis calculates
47  * the type of an expression and adds this info to each node.
48  * - RetrieveDesc info
49  * - value: this allows to add an arbitrary value to a node.
50  * This is used in compile time calulation of constant expression.
51  * @author Michael Bouschen
52  * @version 0.1
53  */

54 public class JQLAST
55     extends CommonAST
56 {
57     /** */
58     private static char SEPARATOR = '\n';
59
60     /** */
61     private static String JavaDoc INDENT = " "; //NOI18N
62

63     protected int line = 0;
64     protected int column = 0;
65     protected Type jqlType;
66     protected RetrieveDesc rd;
67     protected Object JavaDoc value;
68
69     public JQLAST()
70     {
71     }
72
73     public JQLAST(int type, String JavaDoc text, Type jqlType)
74     {
75         initialize(type, text, jqlType);
76     }
77
78     public JQLAST(int type, String JavaDoc text, Type jqlType, Object JavaDoc value)
79     {
80         initialize(type, text, jqlType, value);
81     }
82
83     public JQLAST(Token t)
84     {
85         initialize(t);
86     }
87     
88     public JQLAST(JQLAST ast)
89     {
90         initialize(ast);
91     }
92     
93     public void initialize(int type)
94     {
95         setType(type);
96     }
97     
98     public void initialize(int type, String JavaDoc text)
99     {
100         setType(type);
101         setText(text);
102     }
103
104     public void initialize(Token t)
105     {
106         setType(t.getType());
107         setText(t.getText());
108         setLine(t.getLine());
109         setColumn(t.getColumn());
110     }
111
112     public void initialize(int type, String JavaDoc text, Type jqlType)
113     {
114         setType(type);
115         setText(text);
116         setJQLType(jqlType);
117     }
118
119     public void initialize(int type, String JavaDoc text, Type jqlType, Object JavaDoc value)
120     {
121         setType(type);
122         setText(text);
123         setJQLType(jqlType);
124         setValue(value);
125     }
126
127     public void initialize(AST ast)
128     {
129         initialize((JQLAST)ast);
130     }
131
132     public void initialize(JQLAST ast)
133     {
134         setType(ast.getType());
135         setText(ast.getText());
136         setLine(ast.getLine());
137         setColumn(ast.getColumn());
138         setJQLType(ast.getJQLType());
139         setValue(ast.getValue());
140         setRetrieveDesc(ast.getRetrieveDesc());
141         setValue(ast.getValue());
142     }
143     
144     public void setLine(int line)
145     {
146         this.line = line;
147     }
148
149     public int getLine()
150     {
151         return line;
152     }
153
154     public void setColumn(int column)
155     {
156         this.column = column;
157     }
158
159     public int getColumn()
160     {
161         return column;
162     }
163
164     public void setJQLType(Type jqlType)
165     {
166         this.jqlType = jqlType;
167     }
168
169     public Type getJQLType()
170     {
171         return jqlType;
172     }
173
174     public void setRetrieveDesc(RetrieveDesc rd)
175     {
176         this.rd = rd;
177     }
178
179     public RetrieveDesc getRetrieveDesc()
180     {
181         return rd;
182     }
183
184     public void setValue(Object JavaDoc value)
185     {
186         this.value = value;
187     }
188
189     public Object JavaDoc getValue()
190     {
191         return value;
192     }
193
194     /**
195      * Returns a string representation of this JQLAST w/o child nodes.
196      * @return a string representation of the object.
197      */

198     public String JavaDoc toString()
199     {
200         StringBuffer JavaDoc repr = new StringBuffer JavaDoc();
201         Object JavaDoc jqlType = getJQLType();
202         RetrieveDesc rd = getRetrieveDesc();
203         // token text
204
repr.append((getText() == null ? "null" : getText())); //NOI18N
205
repr.append(" ["); //NOI18N
206
// token type
207
repr.append(getType());
208         // line/column info
209
repr.append(", ("); //NOI18N
210
repr.append(getLine() + "/" + getColumn()); //NOI18N
211
repr.append(")"); //NOI18N
212
// type info
213
repr.append(", "); //NOI18N
214
repr.append(jqlType);
215         // RetrieveDesc info
216
repr.append(", "); //NOI18N
217
repr.append(getRetrieveDescRepr(rd));
218         repr.append("]"); //NOI18N
219
return repr.toString();
220     }
221
222     /**
223      * Returns a full string representation of this JQLAST.
224      * The returned string starts with the specified title string,
225      * followed by the string representation of this ast,
226      * followed by the string representation of the child ast nodes of this ast.
227      * The method dumps each ast node on a separate line.
228      * Child ast nodes are indented.
229      * The method calls toString to dump a single node w/o children.
230      * @return string representation of this ast including children.
231      */

232     public String JavaDoc getTreeRepr(String JavaDoc title)
233     {
234         return title + this.getTreeRepr(0);
235     }
236
237     /** Helper method for getTreeRepr. */
238     private String JavaDoc getTreeRepr(int level)
239     {
240         StringBuffer JavaDoc repr = new StringBuffer JavaDoc();
241         // current node
242
repr.append(SEPARATOR);
243         repr.append(getIndent(level));
244         repr.append(this.toString());
245         // handle children
246
for (JQLAST node = (JQLAST)this.getFirstChild();
247              node != null;
248              node = (JQLAST)node.getNextSibling()) {
249             repr.append(node.getTreeRepr(level+1));
250         }
251         return repr.toString();
252     }
253     
254     /** Returns a string representation of the spceified RetrieveDesc. */
255     public static String JavaDoc getRetrieveDescRepr(RetrieveDesc rd)
256     {
257         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
258         buf.append("RD:"); //NOI18N
259
if (rd == null)
260         {
261             buf.append("null"); //NOI18N
262
}
263         else
264         {
265             String JavaDoc pcClassName = rd.getPersistenceCapableClass().toString();
266             if (pcClassName.startsWith("class ")) //NOI18N
267
buf.append(pcClassName.substring(6));
268             else
269                 buf.append(pcClassName);
270             buf.append("@"); //NOI18N
271
buf.append(System.identityHashCode(rd));
272         }
273         return buf.toString();
274     }
275
276     /** Returns the indent specified by level. */
277     private String JavaDoc getIndent(int level)
278     {
279         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
280         for (int i = 0; i < level; i++) {
281             buf.append(INDENT);
282         }
283         return buf.toString();
284     }
285
286     /**
287      * Factory to create and connect JQLAST nodes.
288      */

289     public static class Factory
290         extends ASTFactory
291     {
292         /** The singleton Factory instance. */
293         private static Factory factory = new Factory();
294         
295         /**
296          * Get an instance of Factory.
297          * @return an instance of Factory
298          */

299         public static Factory getInstance()
300         {
301             return factory;
302         }
303         
304         /** */
305         protected Factory()
306         {
307             this.theASTNodeTypeClass = JQLAST.class;
308             this.theASTNodeType = this.theASTNodeTypeClass.getName();
309         }
310         
311         /** */
312         public AST create()
313         {
314             return new JQLAST();
315         }
316     }
317 }
318
319
Popular Tags