KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > support > ejb > ejbqlc > EJBQLAST


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
26 /*
27  * EJBQLAST.java
28  *
29  * Created on November 12, 2001
30  */

31
32 package com.sun.jdo.spi.persistence.support.ejb.ejbqlc;
33
34 import persistence.antlr.Token;
35 import persistence.antlr.CommonAST;
36 import persistence.antlr.collections.AST;
37
38 /**
39  * An instance of this class represents a node of the intermediate
40  * representation (AST) used by the query compiler. It stores per node:
41  * <ul>
42  * <li> token type info
43  * <li> token text
44  * <li> line info
45  * <li> column info
46  * <li> type info the semantic analysis calculates the type of an expression
47  * and adds this info to each node.
48  * </ul>
49  *
50  * @author Michael Bouschen
51  */

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

62     /** The line info */
63     protected int line = 0;
64
65     /** The column info */
66     protected int column = 0;
67
68     /** The type info */
69     protected transient Object JavaDoc typeInfo;
70
71     /** No args constructor. */
72     public EJBQLAST() {}
73
74     /** Constructor taking token type, text and type info. */
75     public EJBQLAST(int type, String JavaDoc text, Object JavaDoc typeInfo)
76     {
77         initialize(type, text, typeInfo);
78     }
79
80     /** Copy constructor. */
81     public EJBQLAST(EJBQLAST ast)
82     {
83         initialize(ast);
84     }
85     
86     /** */
87     public void initialize(Token t)
88     {
89         setType(t.getType());
90         setText(t.getText());
91         setLine(t.getLine());
92         setColumn(t.getColumn());
93     }
94
95     /** */
96     public void initialize(int type, String JavaDoc text, Object JavaDoc typeInfo)
97     {
98         setType(type);
99         setText(text);
100         setTypeInfo(typeInfo);
101     }
102
103     /** */
104     public void initialize(AST _ast)
105     {
106         EJBQLAST ast = (EJBQLAST)_ast;
107         setType(ast.getType());
108         setText(ast.getText());
109         setLine(ast.getLine());
110         setColumn(ast.getColumn());
111         setTypeInfo(ast.getTypeInfo());
112     }
113     
114     /** */
115     public void setLine(int line)
116     {
117         this.line = line;
118     }
119
120     /** */
121     public int getLine()
122     {
123         return line;
124     }
125
126     /** */
127     public void setColumn(int column)
128     {
129         this.column = column;
130     }
131
132     /** */
133     public int getColumn()
134     {
135         return column;
136     }
137
138     /** */
139     public void setTypeInfo(Object JavaDoc typeInfo)
140     {
141         this.typeInfo = typeInfo;
142     }
143
144     /** */
145     public Object JavaDoc getTypeInfo()
146     {
147         return typeInfo;
148     }
149
150     /**
151      * Returns a string representation of this EJBQLAST w/o child ast nodes.
152      * @return a string representation of the object.
153      */

154     public String JavaDoc toString()
155     {
156         Object JavaDoc typeInfo = getTypeInfo();
157         StringBuffer JavaDoc repr = new StringBuffer JavaDoc();
158         // token text
159
repr.append((getText() == null ? "null" : getText())); //NOI18N
160
repr.append(" ["); //NOI18N
161
// token type
162
repr.append(getType());
163         // line/column info
164
repr.append(", ("); //NOI18N
165
repr.append(getLine() + "/" + getColumn()); //NOI18N
166
repr.append(")"); //NOI18N
167
// type info
168
repr.append(", "); //NOI18N
169
repr.append(typeInfo);
170         repr.append("]"); //NOI18N
171
return repr.toString();
172     }
173
174     /**
175      * Returns a full string representation of this JQLAST.
176      * The returned string starts with the specified title string,
177      * followed by the string representation of this ast,
178      * followed by the string representation of the child ast nodes of this ast.
179      * The method dumps each ast node on a separate line.
180      * Child ast nodes are indented.
181      * The method calls toString to dump a single node w/o children.
182      * @return string representation of this ast including children.
183      */

184     public String JavaDoc getTreeRepr(String JavaDoc title)
185     {
186         return title + this.getTreeRepr(0);
187     }
188
189     /** Helper method for getTreeRepr. */
190     private String JavaDoc getTreeRepr(int level)
191     {
192         StringBuffer JavaDoc repr = new StringBuffer JavaDoc();
193         // current node
194
repr.append(SEPARATOR);
195         repr.append(getIndent(level));
196         repr.append(this.toString());
197         // handle children
198
for (EJBQLAST node = (EJBQLAST)this.getFirstChild();
199              node != null;
200              node = (EJBQLAST)node.getNextSibling()) {
201             repr.append(node.getTreeRepr(level+1));
202         }
203         return repr.toString();
204     }
205     
206     /** Returns the indent specified by level. */
207     private String JavaDoc getIndent(int level)
208     {
209         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
210         for (int i = 0; i < level; i++) {
211             buf.append(INDENT);
212         }
213         return buf.toString();
214     }
215
216     /**
217      * Creates and returns a copy of this object.
218      * The returned EJBQLAST shares the same state as this object, meaning
219      * the fields type, text, line, column, and typeInfo have the same values.
220      * But it is not bound to any tree structure, thus the child is null
221      * and the sibling is null.
222      * @return a clone of this instance.
223      */

224     protected Object JavaDoc clone()
225         throws CloneNotSupportedException JavaDoc
226     {
227         EJBQLAST clone = (EJBQLAST)super.clone();
228         clone.setFirstChild(null);
229         clone.setNextSibling(null);
230         return clone;
231     }
232     
233 }
234
235
Popular Tags