001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: EjbqlAbstractVisitor.java 5843 2004-12-03 07:48:57Z joaninh $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_ejb.lib;
025:
026: import org.objectweb.jonas_ejb.deployment.ejbql.ASTAbstractSchemaName;
027: import org.objectweb.jonas_ejb.deployment.ejbql.ASTAggregateSelectExpression;
028: import org.objectweb.jonas_ejb.deployment.ejbql.ASTArithmeticExpression;
029: import org.objectweb.jonas_ejb.deployment.ejbql.ASTArithmeticFactor;
030: import org.objectweb.jonas_ejb.deployment.ejbql.ASTArithmeticLiteral;
031: import org.objectweb.jonas_ejb.deployment.ejbql.ASTArithmeticTerm;
032: import org.objectweb.jonas_ejb.deployment.ejbql.ASTBetweenExpression;
033: import org.objectweb.jonas_ejb.deployment.ejbql.ASTBooleanExpression;
034: import org.objectweb.jonas_ejb.deployment.ejbql.ASTBooleanLiteral;
035: import org.objectweb.jonas_ejb.deployment.ejbql.ASTCmpPathExpression;
036: import org.objectweb.jonas_ejb.deployment.ejbql.ASTCollectionMemberDeclaration;
037: import org.objectweb.jonas_ejb.deployment.ejbql.ASTCollectionMemberExpression;
038: import org.objectweb.jonas_ejb.deployment.ejbql.ASTCollectionValuedPathExpression;
039: import org.objectweb.jonas_ejb.deployment.ejbql.ASTComparisonExpression;
040: import org.objectweb.jonas_ejb.deployment.ejbql.ASTConditionalExpression;
041: import org.objectweb.jonas_ejb.deployment.ejbql.ASTConditionalFactor;
042: import org.objectweb.jonas_ejb.deployment.ejbql.ASTConditionalTerm;
043: import org.objectweb.jonas_ejb.deployment.ejbql.ASTDatetimeExpression;
044: import org.objectweb.jonas_ejb.deployment.ejbql.ASTEJBQL;
045: import org.objectweb.jonas_ejb.deployment.ejbql.ASTEmptyCollectionComparisonExpression;
046: import org.objectweb.jonas_ejb.deployment.ejbql.ASTEntityBeanExpression;
047: import org.objectweb.jonas_ejb.deployment.ejbql.ASTFloatingPointLiteral;
048: import org.objectweb.jonas_ejb.deployment.ejbql.ASTFromClause;
049: import org.objectweb.jonas_ejb.deployment.ejbql.ASTFunctionsReturningNumerics;
050: import org.objectweb.jonas_ejb.deployment.ejbql.ASTFunctionsReturningStrings;
051: import org.objectweb.jonas_ejb.deployment.ejbql.ASTIdentificationVariable;
052: import org.objectweb.jonas_ejb.deployment.ejbql.ASTIdentifier;
053: import org.objectweb.jonas_ejb.deployment.ejbql.ASTInExpression;
054: import org.objectweb.jonas_ejb.deployment.ejbql.ASTInputParameter;
055: import org.objectweb.jonas_ejb.deployment.ejbql.ASTIntegerLiteral;
056: import org.objectweb.jonas_ejb.deployment.ejbql.ASTLikeExpression;
057: import org.objectweb.jonas_ejb.deployment.ejbql.ASTLimitClause;
058: import org.objectweb.jonas_ejb.deployment.ejbql.ASTLimitExpression;
059: import org.objectweb.jonas_ejb.deployment.ejbql.ASTLiteral;
060: import org.objectweb.jonas_ejb.deployment.ejbql.ASTNullComparisonExpression;
061: import org.objectweb.jonas_ejb.deployment.ejbql.ASTOrderByClause;
062: import org.objectweb.jonas_ejb.deployment.ejbql.ASTOrderByItem;
063: import org.objectweb.jonas_ejb.deployment.ejbql.ASTPath;
064: import org.objectweb.jonas_ejb.deployment.ejbql.ASTRangeVariableDeclaration;
065: import org.objectweb.jonas_ejb.deployment.ejbql.ASTSelectClause;
066: import org.objectweb.jonas_ejb.deployment.ejbql.ASTSelectExpression;
067: import org.objectweb.jonas_ejb.deployment.ejbql.ASTSingleValuedCmrPathExpression;
068: import org.objectweb.jonas_ejb.deployment.ejbql.ASTSingleValuedPathExpression;
069: import org.objectweb.jonas_ejb.deployment.ejbql.ASTStringExpression;
070: import org.objectweb.jonas_ejb.deployment.ejbql.ASTStringLiteral;
071: import org.objectweb.jonas_ejb.deployment.ejbql.ASTWhereClause;
072: import org.objectweb.jonas_ejb.deployment.ejbql.EJBQLVisitor;
073: import org.objectweb.jonas_ejb.deployment.ejbql.SimpleNode;
074: import org.objectweb.medor.query.api.QueryTree;
075: import org.objectweb.medor.query.jorm.lib.NavigatorNodeFactory;
076:
077: import java.util.ArrayList;
078: import java.util.Iterator;
079: import java.util.Stack;
080: import java.util.StringTokenizer;
081:
082: /**
083: * Base class with visitor utility class and default implementation of
084: * visit methods
085: * Created on Sep 12, 2002
086: * @author Christophe Ney [cney@batisseurs.com]: Initial developer
087: * @author Helene Joanin: Add the toString() method to IdValue.
088: * @author Helene Joanin: Add some utility methods as endsWith(), basePath().
089: * @author Helene Joanin: Take into account the ORDER BY clause.
090: * @author Helene Joanin: Take into account the EJBQL version 2.1 syntax.
091: */
092: public class EjbqlAbstractVisitor implements EJBQLVisitor {
093:
094: /**
095: * Values associated with each declared identifiers
096: */
097: protected class IdValue {
098: /**
099: * Paths used in the select and where clause starting with the corresponding id
100: */
101: private ArrayList paths = null;
102:
103: /**
104: * abstract schema name or collection values path
105: */
106: private String[] name = null;
107:
108: /**
109: * The query tree that corresponds to the id and the paths
110: */
111: private QueryTree qt = null;
112:
113: /**
114: * Create an IdVAlue for the given path
115: * @param pathName an abstract schema name or collection values path
116: */
117: public IdValue(String pathName) {
118: this ();
119: name = splitPath(pathName);
120: }
121:
122: /**
123: * Default constructor
124: */
125: public IdValue() {
126: paths = new ArrayList();
127: }
128:
129: /**
130: * @return the name
131: */
132: public String[] getName() {
133: return name;
134: }
135:
136: /**
137: * @param name the abstract schema name or collection values path
138: */
139: public void setName(String[] name) {
140: this .name = name;
141: }
142:
143: /**
144: * add a path (if it's not already declared)
145: * @param path the path to add
146: */
147: public void addPath(String path) {
148: if (!paths.contains(path)) {
149: paths.add(path);
150: }
151: }
152:
153: /**
154: * @param idx index of the path
155: * @return return the splited path of the specified position
156: */
157: public String[] getSplitedPath(int idx) {
158: return splitPath((String) paths.get(idx));
159: }
160:
161: /**
162: * @param idx index of the path
163: * @return return the merged path of the specified position
164: */
165: public String getMergedPath(int idx) {
166: return (String) paths.get(idx);
167: }
168:
169: /**
170: * @return the number of declared path
171: */
172: public int getDeclaredPathLength() {
173: return paths.size();
174: }
175:
176: /**
177: * @return the associated QueryTree
178: */
179: public QueryTree getQueryTree() {
180: return qt;
181: }
182:
183: /**
184: * @param qt QueryTree
185: */
186: public void setQueryTree(QueryTree qt) {
187: this .qt = qt;
188: }
189:
190: /**
191: * @return a string representation for debug
192: */
193: public String toString() {
194: StringBuffer sb = new StringBuffer();
195: sb = sb.append("(paths=[");
196: Iterator ip = paths.iterator();
197: while (ip.hasNext()) {
198: sb = sb.append((String) ip.next() + ",");
199: }
200: if (name == null) {
201: sb = sb.append("],name=null");
202: } else {
203: sb = sb.append("],name=");
204: for (int i = 0; i < name.length; i++) {
205: sb = sb.append(name[i] + ".");
206: }
207: }
208: if (qt == null) {
209: sb = sb.append(",qt=null)");
210: } else {
211: sb = sb.append(",qt!=null)");
212: }
213: return sb.toString();
214: }
215: }
216:
217: /**
218: * Runtime Exception used to wrap exceptions thrown in visit methods
219: */
220: protected class VisitorException extends RuntimeException {
221:
222: Exception nestedException;
223:
224: VisitorException(Exception nestedException) {
225: this .nestedException = nestedException;
226: }
227:
228: Exception getNestedException() {
229: return nestedException;
230: }
231: }
232:
233: /**
234: * split a dot separated path into tokens
235: * @param path the input path
236: * @return the splitted path
237: */
238: protected String[] splitPath(String path) {
239: StringTokenizer st = new StringTokenizer(path, ".");
240: String[] ret = new String[st.countTokens()];
241: for (int i = 0; i < ret.length; i++) {
242: ret[i] = st.nextToken();
243: }
244: return ret;
245: }
246:
247: /**
248: * @param path the input path
249: * @param begin the beginning index
250: * @param length the length
251: * @return the merge of the tokens (from the begin index to the (begin+length) index into a dot separated path
252: */
253: protected String mergePath(String[] path, int begin, int length) {
254: if (length == 0) {
255: return "";
256: }
257: StringBuffer ret = new StringBuffer();
258: for (int i = begin; i < (begin + length); i++) {
259: ret.append(path[i]);
260: ret.append('.');
261: }
262: ret.deleteCharAt(ret.length() - 1);
263: return ret.toString();
264: }
265:
266: /**
267: * @param path the input path
268: * @return the merge tokens into a dot separated path
269: */
270: protected String mergePath(String[] path) {
271: return (mergePath(path, 0, path.length));
272: }
273:
274: /**
275: * @param path the input path
276: * @param suffix the input suffix
277: * @return true if the last token of the path is equal to the suffix.
278: */
279: protected boolean endsWith(String[] path, String suffix) {
280: return (suffix.equals(path[path.length - 1]));
281: }
282:
283: /**
284: * merge the first tokens into a dot separated path.
285: * (The last token is left)
286: * @param path the input path
287: * @return the base path
288: */
289: protected String[] basePath(String[] path) {
290: String[] base = new String[path.length - 1];
291: for (int i = 0; i < path.length - 1; i++) {
292: base[i] = new String(path[i]);
293: }
294: return base;
295: }
296:
297: /**
298: * Visit method to call from constructor.
299: * Child node visitors get a <code>java.util.Stack</code> as data parameter.
300: * @param node the node to visit
301: * @return the stack
302: * @throws Exception any nested exception thrown from other visit method
303: */
304: public Object visit(SimpleNode node) throws Exception {
305: try {
306: return visit((SimpleNode) node, new Stack());
307: } catch (VisitorException e) {
308: throw e.getNestedException();
309: }
310: }
311:
312: /**
313: * Generic visit method that traverses all child nodes
314: * @param node the node to visit
315: * @param data the current stack
316: * @return the stack
317: */
318: public Object visit(SimpleNode node, Object data) {
319: return node.childrenAccept(this , data);
320: }
321:
322: /**
323: * null implementation of the visit method for the corresponding parameter type
324: * @param node the node to visit
325: * @param data the current stack
326: * @return null
327: */
328: public Object visit(ASTEJBQL node, Object data) {
329: return null;
330: }
331:
332: /**
333: * null implementation of the visit method for the corresponding parameter type
334: * @param node the node to visit
335: * @param data the current stack
336: * @return null
337: */
338: public Object visit(ASTFromClause node, Object data) {
339: return null;
340: }
341:
342: /**
343: * null implementation of the visit method for the corresponding parameter type
344: * @param node the node to visit
345: * @param data the current stack
346: * @return null
347: */
348: public Object visit(ASTCollectionMemberDeclaration node, Object data) {
349: return null;
350: }
351:
352: /**
353: * null implementation of the visit method for the corresponding parameter type
354: * @param node the node to visit
355: * @param data the current stack
356: * @return null
357: */
358: public Object visit(ASTRangeVariableDeclaration node, Object data) {
359: return null;
360: }
361:
362: /**
363: * null implementation of the visit method for the corresponding parameter type
364: * @param node the node to visit
365: * @param data the current stack
366: * @return null
367: */
368: public Object visit(ASTSingleValuedPathExpression node, Object data) {
369: return null;
370: }
371:
372: /**
373: * null implementation of the visit method for the corresponding parameter type
374: * @param node the node to visit
375: * @param data the current stack
376: * @return null
377: */
378: public Object visit(ASTCmpPathExpression node, Object data) {
379: return null;
380: }
381:
382: /**
383: * null implementation of the visit method for the corresponding parameter type
384: * @param node the node to visit
385: * @param data the current stack
386: * @return null
387: */
388: public Object visit(ASTSingleValuedCmrPathExpression node,
389: Object data) {
390: return null;
391: }
392:
393: /**
394: * null implementation of the visit method for the corresponding parameter type
395: * @param node the node to visit
396: * @param data the current stack
397: * @return null
398: */
399: public Object visit(ASTCollectionValuedPathExpression node,
400: Object data) {
401: return null;
402: }
403:
404: /**
405: * null implementation of the visit method for the corresponding parameter type
406: * @param node the node to visit
407: * @param data the current stack
408: * @return null
409: */
410: public Object visit(ASTSelectClause node, Object data) {
411: return null;
412: }
413:
414: /**
415: * null implementation of the visit method for the corresponding parameter type
416: * @param node the node to visit
417: * @param data the current stack
418: * @return null
419: */
420: public Object visit(ASTSelectExpression node, Object data) {
421: return null;
422: }
423:
424: /**
425: * null implementation of the visit method for the corresponding parameter type
426: * @param node the node to visit
427: * @param data the current stack
428: * @return null
429: */
430: public Object visit(ASTAggregateSelectExpression node, Object data) {
431: return null;
432: }
433:
434: /**
435: * null implementation of the visit method for the corresponding parameter type
436: * @param node the node to visit
437: * @param data the current stack
438: * @return null
439: */
440: public Object visit(ASTOrderByClause node, Object data) {
441: return null;
442: }
443:
444: /**
445: * null implementation of the visit method for the corresponding parameter type
446: * @param node the node to visit
447: * @param data the current stack
448: * @return null
449: */
450: public Object visit(ASTOrderByItem node, Object data) {
451: return null;
452: }
453:
454: /**
455: * null implementation of the visit method for the corresponding parameter type
456: * @param node the node to visit
457: * @param data the current stack
458: * @return null
459: */
460: public Object visit(ASTLimitClause node, Object data) {
461: return null;
462: }
463:
464: /**
465: * null implementation of the visit method for the corresponding parameter type
466: * @param node the node to visit
467: * @param data the current stack
468: * @return null
469: */
470: public Object visit(ASTLimitExpression node, Object data) {
471: return null;
472: }
473:
474: /**
475: * null implementation of the visit method for the corresponding parameter type
476: * @param node the node to visit
477: * @param data the current stack
478: * @return null
479: */
480: public Object visit(ASTWhereClause node, Object data) {
481: return null;
482: }
483:
484: /**
485: * null implementation of the visit method for the corresponding parameter type
486: * @param node the node to visit
487: * @param data the current stack
488: * @return null
489: */
490: public Object visit(ASTConditionalExpression node, Object data) {
491: return null;
492: }
493:
494: /**
495: * null implementation of the visit method for the corresponding parameter type
496: * @param node the node to visit
497: * @param data the current stack
498: * @return null
499: */
500: public Object visit(ASTConditionalTerm node, Object data) {
501: return null;
502: }
503:
504: /**
505: * null implementation of the visit method for the corresponding parameter type
506: * @param node the node to visit
507: * @param data the current stack
508: * @return null
509: */
510: public Object visit(ASTConditionalFactor node, Object data) {
511: return null;
512: }
513:
514: /**
515: * null implementation of the visit method for the corresponding parameter type
516: * @param node the node to visit
517: * @param data the current stack
518: * @return null
519: */
520: public Object visit(ASTBetweenExpression node, Object data) {
521: return null;
522: }
523:
524: /**
525: * null implementation of the visit method for the corresponding parameter type
526: * @param node the node to visit
527: * @param data the current stack
528: * @return null
529: */
530: public Object visit(ASTInExpression node, Object data) {
531: return null;
532: }
533:
534: /**
535: * null implementation of the visit method for the corresponding parameter type
536: * @param node the node to visit
537: * @param data the current stack
538: * @return null
539: */
540: public Object visit(ASTLikeExpression node, Object data) {
541: return null;
542: }
543:
544: /**
545: * null implementation of the visit method for the corresponding parameter type
546: * @param node the node to visit
547: * @param data the current stack
548: * @return null
549: */
550: public Object visit(ASTNullComparisonExpression node, Object data) {
551: return null;
552: }
553:
554: /**
555: * null implementation of the visit method for the corresponding parameter type
556: * @param node the node to visit
557: * @param data the current stack
558: * @return null
559: */
560: public Object visit(ASTEmptyCollectionComparisonExpression node,
561: Object data) {
562: return null;
563: }
564:
565: /**
566: * null implementation of the visit method for the corresponding parameter type
567: * @param node the node to visit
568: * @param data the current stack
569: * @return null
570: */
571: public Object visit(ASTCollectionMemberExpression node, Object data) {
572: return null;
573: }
574:
575: /**
576: * null implementation of the visit method for the corresponding parameter type
577: * @param node the node to visit
578: * @param data the current stack
579: * @return null
580: */
581: public Object visit(ASTComparisonExpression node, Object data) {
582: return null;
583: }
584:
585: /**
586: * null implementation of the visit method for the corresponding parameter type
587: * @param node the node to visit
588: * @param data the current stack
589: * @return null
590: */
591: public Object visit(ASTArithmeticExpression node, Object data) {
592: return null;
593: }
594:
595: /**
596: * null implementation of the visit method for the corresponding parameter type
597: * @param node the node to visit
598: * @param data the current stack
599: * @return null
600: */
601: public Object visit(ASTIntegerLiteral node, Object data) {
602: return null;
603: }
604:
605: /**
606: * null implementation of the visit method for the corresponding parameter type
607: * @param node the node to visit
608: * @param data the current stack
609: * @return null
610: */
611: public Object visit(ASTFloatingPointLiteral node, Object data) {
612: return null;
613: }
614:
615: /**
616: * null implementation of the visit method for the corresponding parameter type
617: * @param node the node to visit
618: * @param data the current stack
619: * @return null
620: */
621: public Object visit(ASTArithmeticTerm node, Object data) {
622: return null;
623: }
624:
625: /**
626: * null implementation of the visit method for the corresponding parameter type
627: * @param node the node to visit
628: * @param data the current stack
629: * @return null
630: */
631: public Object visit(ASTArithmeticFactor node, Object data) {
632: return null;
633: }
634:
635: /**
636: * null implementation of the visit method for the corresponding parameter type
637: * @param node the node to visit
638: * @param data the current stack
639: * @return null
640: */
641: public Object visit(ASTStringExpression node, Object data) {
642: return null;
643: }
644:
645: /**
646: * null implementation of the visit method for the corresponding parameter type
647: * @param node the node to visit
648: * @param data the current stack
649: * @return null
650: */
651: public Object visit(ASTDatetimeExpression node, Object data) {
652: return null;
653: }
654:
655: /**
656: * null implementation of the visit method for the corresponding parameter type
657: * @param node the node to visit
658: * @param data the current stack
659: * @return null
660: */
661: public Object visit(ASTBooleanExpression node, Object data) {
662: return null;
663: }
664:
665: /**
666: * null implementation of the visit method for the corresponding parameter type
667: * @param node the node to visit
668: * @param data the current stack
669: * @return null
670: */
671: public Object visit(ASTEntityBeanExpression node, Object data) {
672: return null;
673: }
674:
675: /**
676: * null implementation of the visit method for the corresponding parameter type
677: * @param node the node to visit
678: * @param data the current stack
679: * @return null
680: */
681: public Object visit(ASTFunctionsReturningStrings node, Object data) {
682: return null;
683: }
684:
685: /**
686: * null implementation of the visit method for the corresponding parameter type
687: * @param node the node to visit
688: * @param data the current stack
689: * @return null
690: */
691: public Object visit(ASTFunctionsReturningNumerics node, Object data) {
692: return null;
693: }
694:
695: /**
696: * null implementation of the visit method for the corresponding parameter type
697: * @param node the node to visit
698: * @param data the current stack
699: * @return null
700: */
701: public Object visit(ASTAbstractSchemaName node, Object data) {
702: return null;
703: }
704:
705: /**
706: * null implementation of the visit method for the corresponding parameter type
707: * @param node the node to visit
708: * @param data the current stack
709: * @return null
710: */
711: public Object visit(ASTIdentificationVariable node, Object data) {
712: return null;
713: }
714:
715: /**
716: * null implementation of the visit method for the corresponding parameter type
717: * @param node the node to visit
718: * @param data the current stack
719: * @return null
720: */
721: public Object visit(ASTIdentifier node, Object data) {
722: return null;
723: }
724:
725: /**
726: * null implementation of the visit method for the corresponding parameter type
727: * @param node the node to visit
728: * @param data the current stack
729: * @return null
730: */
731: public Object visit(ASTPath node, Object data) {
732: return null;
733: }
734:
735: /**
736: * null implementation of the visit method for the corresponding parameter type
737: * @param node the node to visit
738: * @param data the current stack
739: * @return null
740: */
741:
742: public Object visit(ASTLiteral node, Object data) {
743: return null;
744: }
745:
746: /**
747: * null implementation of the visit method for the corresponding parameter type
748: * @param node the node to visit
749: * @param data the current stack
750: * @return null
751: */
752: public Object visit(ASTStringLiteral node, Object data) {
753: return null;
754: }
755:
756: /**
757: * null implementation of the visit method for the corresponding parameter type
758: * @param node the node to visit
759: * @param data the current stack
760: * @return null
761: */
762: public Object visit(ASTArithmeticLiteral node, Object data) {
763: return null;
764: }
765:
766: /**
767: * null implementation of the visit method for the corresponding parameter type
768: * @param node the node to visit
769: * @param data the current stack
770: * @return null
771: */
772: public Object visit(ASTBooleanLiteral node, Object data) {
773: return null;
774: }
775:
776: /**
777: * null implementation of the visit method for the corresponding parameter type
778: * @param node the node to visit
779: * @param data the current stack
780: * @return null
781: */
782: public Object visit(ASTInputParameter node, Object data) {
783: return null;
784: }
785: }
|