KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > ejbql > BasicVisitor


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb.plugins.cmp.ejbql;
23
24 import org.jboss.ejb.plugins.cmp.jdbc.SQLUtil;
25
26 /**
27  * This a basic abstract syntax tree visitor. It simply converts the tree
28  * back into ejbql. This is useful for testing and extensions, as most
29  * extensions translate just a few elements of the tree.
30  *
31  * @author <a HREF="mailto:dain@daingroup.com">Dain Sundstrom</a>
32  * @version $Revision: 37459 $
33  */

34 public class BasicVisitor implements JBossQLParserVisitor
35 {
36    public Object JavaDoc visit(SimpleNode node, Object JavaDoc data)
37    {
38       return data;
39    }
40
41    public Object JavaDoc visit(ASTEJBQL node, Object JavaDoc data)
42    {
43       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
44       for(int i = 0; i < node.jjtGetNumChildren(); i++)
45       {
46          if(i > 0)
47          {
48             buf.append(' ');
49          }
50          node.jjtGetChild(i).jjtAccept(this, data);
51       }
52       return data;
53    }
54
55    public Object JavaDoc visit(ASTFrom node, Object JavaDoc data)
56    {
57       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
58       buf.append(SQLUtil.FROM);
59       for(int i = 0; i < node.jjtGetNumChildren(); i++)
60       {
61          if(i > 0)
62          {
63             buf.append(SQLUtil.COMMA);
64          }
65          node.jjtGetChild(i).jjtAccept(this, data);
66       }
67       return data;
68    }
69
70    public Object JavaDoc visit(ASTCollectionMemberDeclaration node, Object JavaDoc data)
71    {
72       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
73       buf.append(SQLUtil.IN).append('(');
74       node.jjtGetChild(0).jjtAccept(this, data);
75       buf.append(')').append(' ');
76       node.jjtGetChild(1).jjtAccept(this, data);
77       return data;
78    }
79
80    public Object JavaDoc visit(ASTRangeVariableDeclaration node, Object JavaDoc data)
81    {
82       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
83       node.jjtGetChild(0).jjtAccept(this, data);
84       buf.append(' ');
85       node.jjtGetChild(1).jjtAccept(this, data);
86       return data;
87    }
88
89    public Object JavaDoc visit(ASTSelect node, Object JavaDoc data)
90    {
91       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
92       buf.append(SQLUtil.SELECT);
93       node.jjtGetChild(0).jjtAccept(this, data);
94       return data;
95    }
96
97    public Object JavaDoc visit(ASTWhere node, Object JavaDoc data)
98    {
99       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
100       buf.append(SQLUtil.WHERE);
101       node.jjtGetChild(0).jjtAccept(this, data);
102       return data;
103    }
104
105    public Object JavaDoc visit(ASTOr node, Object JavaDoc data)
106    {
107       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
108       node.jjtGetChild(0).jjtAccept(this, data);
109       for(int i = 1; i < node.jjtGetNumChildren(); ++i)
110       {
111          buf.append(SQLUtil.OR);
112          node.jjtGetChild(i).jjtAccept(this, data);
113       }
114       return data;
115    }
116
117    public Object JavaDoc visit(ASTWhereConditionalTerm node, Object JavaDoc data)
118    {
119       for(int i = 0; i < node.jjtGetNumChildren(); ++i)
120       {
121          node.jjtGetChild(i).jjtAccept(this, data);
122       }
123       return data;
124    }
125
126    public Object JavaDoc visit(ASTAnd node, Object JavaDoc data)
127    {
128       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
129       node.jjtGetChild(0).jjtAccept(this, data);
130       for(int i = 1; i < node.jjtGetNumChildren(); i++)
131       {
132          buf.append(SQLUtil.AND);
133          node.jjtGetChild(i).jjtAccept(this, data);
134       }
135       return data;
136    }
137
138    public Object JavaDoc visit(ASTNot node, Object JavaDoc data)
139    {
140       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
141       buf.append(SQLUtil.NOT);
142       node.jjtGetChild(0).jjtAccept(this, data);
143       return data;
144    }
145
146    public Object JavaDoc visit(ASTConditionalParenthetical node, Object JavaDoc data)
147    {
148       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
149       buf.append('(');
150       node.jjtGetChild(0).jjtAccept(this, data);
151       buf.append(')');
152       return data;
153    }
154
155    public Object JavaDoc visit(ASTBetween node, Object JavaDoc data)
156    {
157       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
158       node.jjtGetChild(0).jjtAccept(this, data);
159       if(node.not)
160       {
161          buf.append(SQLUtil.NOT);
162       }
163       buf.append(SQLUtil.BETWEEN);
164       node.jjtGetChild(1).jjtAccept(this, data);
165       buf.append(SQLUtil.AND);
166       node.jjtGetChild(2).jjtAccept(this, data);
167       return data;
168    }
169
170    public Object JavaDoc visit(ASTIn node, Object JavaDoc data)
171    {
172       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
173       node.jjtGetChild(0).jjtAccept(this, data);
174       if(node.not)
175       {
176          buf.append(SQLUtil.NOT);
177       }
178       buf.append(SQLUtil.IN).append('(');
179       node.jjtGetChild(1).jjtAccept(this, data);
180       for(int i = 2; i < node.jjtGetNumChildren(); i++)
181       {
182          buf.append(SQLUtil.COMMA);
183          node.jjtGetChild(i).jjtAccept(this, data);
184       }
185       buf.append(')');
186       return data;
187    }
188
189    public Object JavaDoc visit(ASTLike node, Object JavaDoc data)
190    {
191       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
192       node.jjtGetChild(0).jjtAccept(this, data);
193       if(node.not)
194       {
195          buf.append(SQLUtil.NOT);
196       }
197       buf.append(SQLUtil.LIKE);
198       node.jjtGetChild(1).jjtAccept(this, data);
199       if(node.jjtGetNumChildren() == 3)
200       {
201          buf.append(SQLUtil.ESCAPE);
202          node.jjtGetChild(2).jjtAccept(this, data);
203       }
204       return data;
205    }
206
207
208    public Object JavaDoc visit(ASTNullComparison node, Object JavaDoc data)
209    {
210       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
211       node.jjtGetChild(0).jjtAccept(this, data);
212       buf.append(SQLUtil.IS);
213       if(node.not)
214       {
215          buf.append(SQLUtil.NOT);
216       }
217       buf.append(SQLUtil.NULL);
218       return data;
219    }
220
221    public Object JavaDoc visit(ASTIsEmpty node, Object JavaDoc data)
222    {
223       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
224       node.jjtGetChild(0).jjtAccept(this, data);
225       buf.append(SQLUtil.IS);
226       if(node.not)
227       {
228          buf.append(SQLUtil.NOT);
229       }
230       buf.append(SQLUtil.EMPTY);
231       return data;
232    }
233
234    public Object JavaDoc visit(ASTMemberOf node, Object JavaDoc data)
235    {
236       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
237       node.jjtGetChild(0).jjtAccept(this, data);
238       if(node.not)
239       {
240          buf.append(SQLUtil.NOT);
241       }
242       buf.append(SQLUtil.MEMBER_OF);
243       node.jjtGetChild(1).jjtAccept(this, data);
244       return data;
245    }
246
247    public Object JavaDoc visit(ASTStringComparison node, Object JavaDoc data)
248    {
249       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
250       node.jjtGetChild(0).jjtAccept(this, data);
251       buf.append(' ').append(node.opp).append(' ');
252       node.jjtGetChild(1).jjtAccept(this, data);
253       return data;
254    }
255
256    public Object JavaDoc visit(ASTBooleanComparison node, Object JavaDoc data)
257    {
258       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
259       node.jjtGetChild(0).jjtAccept(this, data);
260       if(node.jjtGetNumChildren() == 2)
261       {
262          buf.append(' ').append(node.opp).append(' ');
263          node.jjtGetChild(1).jjtAccept(this, data);
264       }
265       return data;
266    }
267
268    public Object JavaDoc visit(ASTDatetimeComparison node, Object JavaDoc data)
269    {
270       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
271       node.jjtGetChild(0).jjtAccept(this, data);
272       buf.append(' ').append(node.opp).append(' ');
273       node.jjtGetChild(1).jjtAccept(this, data);
274       return data;
275    }
276
277    public Object JavaDoc visit(ASTEntityComparison node, Object JavaDoc data)
278    {
279       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
280       node.jjtGetChild(0).jjtAccept(this, data);
281       buf.append(' ').append(node.opp).append(' ');
282       node.jjtGetChild(1).jjtAccept(this, data);
283       return data;
284    }
285
286    public Object JavaDoc visit(ASTValueClassComparison node, Object JavaDoc data)
287    {
288       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
289       node.jjtGetChild(0).jjtAccept(this, data);
290       buf.append(' ').append(node.opp).append(' ');
291       node.jjtGetChild(1).jjtAccept(this, data);
292       return data;
293    }
294
295    public Object JavaDoc visit(ASTArithmeticComparison node, Object JavaDoc data)
296    {
297       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
298       node.jjtGetChild(0).jjtAccept(this, data);
299       buf.append(' ').append(node.opp).append(' ');
300       node.jjtGetChild(1).jjtAccept(this, data);
301       return data;
302    }
303
304    public Object JavaDoc visit(ASTPlusMinus node, Object JavaDoc data)
305    {
306       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
307       for(int i = 0; i < node.jjtGetNumChildren(); i++)
308       {
309          if(i > 0)
310          {
311             buf.append(' ').append(node.opps.get(i - 1)).append(' ');
312          }
313          node.jjtGetChild(i).jjtAccept(this, data);
314       }
315       return data;
316    }
317
318    public Object JavaDoc visit(ASTMultDiv node, Object JavaDoc data)
319    {
320       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
321       for(int i = 0; i < node.jjtGetNumChildren(); i++)
322       {
323          if(i > 0)
324          {
325             buf.append(' ').append(node.opps.get(i - 1)).append(' ');
326          }
327          node.jjtGetChild(i).jjtAccept(this, data);
328       }
329       return data;
330    }
331
332    public Object JavaDoc visit(ASTNegation node, Object JavaDoc data)
333    {
334       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
335       buf.append('-');
336       node.jjtGetChild(0).jjtAccept(this, data);
337       return data;
338    }
339
340    public Object JavaDoc visit(ASTArithmeticParenthetical node, Object JavaDoc data)
341    {
342       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
343       buf.append('(');
344       node.jjtGetChild(0).jjtAccept(this, data);
345       buf.append(')');
346       return data;
347    }
348
349    public Object JavaDoc visit(ASTStringParenthetical node, Object JavaDoc data)
350    {
351       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
352       buf.append('(');
353       node.jjtGetChild(0).jjtAccept(this, data);
354       buf.append(')');
355       return data;
356    }
357
358    public Object JavaDoc visit(ASTConcat node, Object JavaDoc data)
359    {
360       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
361       buf.append(SQLUtil.CONCAT).append('(');
362       node.jjtGetChild(0).jjtAccept(this, data);
363       buf.append(SQLUtil.COMMA);
364       node.jjtGetChild(1).jjtAccept(this, data);
365       buf.append(')');
366       return data;
367    }
368
369    public Object JavaDoc visit(ASTSubstring node, Object JavaDoc data)
370    {
371       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
372       buf.append(SQLUtil.SUBSTRING).append('(');
373       node.jjtGetChild(0).jjtAccept(this, data);
374       buf.append(SQLUtil.COMMA);
375       node.jjtGetChild(1).jjtAccept(this, data);
376       buf.append(SQLUtil.COMMA);
377       node.jjtGetChild(2).jjtAccept(this, data);
378       buf.append(')');
379       return data;
380    }
381
382    public Object JavaDoc visit(ASTLCase node, Object JavaDoc data)
383    {
384       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
385       buf.append(SQLUtil.LCASE).append('(');
386       node.jjtGetChild(0).jjtAccept(this, data);
387       buf.append(')');
388       return data;
389    }
390
391    public Object JavaDoc visit(ASTUCase node, Object JavaDoc data)
392    {
393       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
394       buf.append(SQLUtil.UCASE).append('(');
395       node.jjtGetChild(0).jjtAccept(this, data);
396       buf.append(')');
397       return data;
398    }
399
400    public Object JavaDoc visit(ASTLength node, Object JavaDoc data)
401    {
402       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
403       buf.append(SQLUtil.LENGTH).append('(');
404       node.jjtGetChild(0).jjtAccept(this, data);
405       buf.append(')');
406       return data;
407    }
408
409    public Object JavaDoc visit(ASTLocate node, Object JavaDoc data)
410    {
411       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
412       buf.append(SQLUtil.LOCATE).append('(');
413       node.jjtGetChild(0).jjtAccept(this, data);
414       buf.append(SQLUtil.COMMA);
415       node.jjtGetChild(1).jjtAccept(this, data);
416       if(node.jjtGetNumChildren() == 3)
417       {
418          buf.append(SQLUtil.COMMA);
419          node.jjtGetChild(2).jjtAccept(this, data);
420       }
421       buf.append(')');
422       return data;
423    }
424
425    public Object JavaDoc visit(ASTAbs node, Object JavaDoc data)
426    {
427       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
428       buf.append(SQLUtil.ABS).append('(');
429       node.jjtGetChild(0).jjtAccept(this, data);
430       buf.append(')');
431       return data;
432    }
433
434    public Object JavaDoc visit(ASTSqrt node, Object JavaDoc data)
435    {
436       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
437       buf.append(SQLUtil.SQRT).append('(');
438       node.jjtGetChild(0).jjtAccept(this, data);
439       buf.append(')');
440       return data;
441    }
442
443    public Object JavaDoc visit(ASTCount node, Object JavaDoc data)
444    {
445       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
446       buf.append(SQLUtil.COUNT).append('(');
447       ASTPath path = (ASTPath)node.jjtGetChild(0);
448       path.children[0].jjtAccept(this, data);
449       buf.append(')');
450       return data;
451    }
452
453    public Object JavaDoc visit(ASTMax node, Object JavaDoc data)
454    {
455       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
456       buf.append(SQLUtil.MAX).append('(');
457       ASTPath path = (ASTPath)node.jjtGetChild(0);
458       path.children[0].jjtAccept(this, data);
459       buf.append(')');
460       return data;
461    }
462
463    public Object JavaDoc visit(ASTMin node, Object JavaDoc data)
464    {
465       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
466       buf.append(SQLUtil.MIN).append('(');
467       ASTPath path = (ASTPath)node.jjtGetChild(0);
468       path.children[0].jjtAccept(this, data);
469       buf.append(')');
470       return data;
471    }
472
473    public Object JavaDoc visit(ASTAvg node, Object JavaDoc data)
474    {
475       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
476       buf.append(SQLUtil.AVG).append('(');
477       ASTPath path = (ASTPath)node.jjtGetChild(0);
478       path.children[0].jjtAccept(this, data);
479       buf.append(')');
480       return data;
481    }
482
483    public Object JavaDoc visit(ASTSum node, Object JavaDoc data)
484    {
485       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
486       buf.append(SQLUtil.SUM).append('(');
487       ASTPath path = (ASTPath)node.jjtGetChild(0);
488       path.children[0].jjtAccept(this, data);
489       buf.append(')');
490       return data;
491    }
492
493    public Object JavaDoc visit(ASTOrderBy node, Object JavaDoc data)
494    {
495       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
496       for(int i = 0; i < node.jjtGetNumChildren(); i++)
497       {
498          if(i > 0)
499          {
500             buf.append(SQLUtil.COMMA);
501          }
502          node.jjtGetChild(i).jjtAccept(this, data);
503       }
504       return data;
505    }
506
507    public Object JavaDoc visit(ASTOrderByPath node, Object JavaDoc data)
508    {
509       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
510
511       node.jjtGetChild(0).jjtAccept(this, data);
512       if(node.ascending)
513       {
514          buf.append(SQLUtil.ASC);
515       }
516       else
517       {
518          buf.append(SQLUtil.DESC);
519       }
520       return data;
521    }
522
523    public Object JavaDoc visit(ASTPath node, Object JavaDoc data)
524    {
525       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
526       buf.append(node.getPath());
527       return data;
528    }
529
530    public Object JavaDoc visit(ASTIdentifier node, Object JavaDoc data)
531    {
532       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
533       buf.append(node.identifier);
534       return data;
535    }
536
537    public Object JavaDoc visit(ASTAbstractSchema node, Object JavaDoc data)
538    {
539       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
540       buf.append(node.abstractSchemaName);
541       return data;
542    }
543
544    public Object JavaDoc visit(ASTParameter node, Object JavaDoc data)
545    {
546       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
547       buf.append("?").append(node.number);
548       return data;
549    }
550
551    public Object JavaDoc visit(ASTExactNumericLiteral node, Object JavaDoc data)
552    {
553       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
554       buf.append(node.literal);
555       return data;
556    }
557
558    public Object JavaDoc visit(ASTApproximateNumericLiteral node, Object JavaDoc data)
559    {
560       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
561       buf.append(node.literal);
562       return data;
563    }
564
565    public Object JavaDoc visit(ASTStringLiteral node, Object JavaDoc data)
566    {
567       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
568       buf.append(node.value);
569       return data;
570    }
571
572    public Object JavaDoc visit(ASTBooleanLiteral node, Object JavaDoc data)
573    {
574       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
575       buf.append(node.value);
576       return data;
577    }
578
579    public Object JavaDoc visit(ASTLimitOffset node, Object JavaDoc data)
580    {
581       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
582       int child = 0;
583       if(node.hasOffset)
584       {
585          buf.append(SQLUtil.OFFSET);
586          node.jjtGetChild(child++).jjtAccept(this, data);
587       }
588       if(node.hasLimit)
589       {
590          buf.append(SQLUtil.LIMIT);
591          node.jjtGetChild(child).jjtAccept(this, data);
592       }
593       return data;
594    }
595
596    public Object JavaDoc visit(ASTMod node, Object JavaDoc data)
597    {
598       StringBuffer JavaDoc buf = (StringBuffer JavaDoc)data;
599       buf.append(SQLUtil.MOD).append('(');
600       node.jjtGetChild(0).jjtAccept(this, data);
601       buf.append(SQLUtil.COMMA);
602       node.jjtGetChild(1).jjtAccept(this, data);
603       buf.append(')');
604       return data;
605    }
606 }
607
Popular Tags