KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > odmg > oql > OQLParser


1 // $ANTLR 2.7.5 (20050128): "oql-ojb.g" -> "OQLParser.java"$
2

3 /* Copyright 2003-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.ojb.odmg.oql;
19
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Vector JavaDoc;
24
25 import org.apache.ojb.broker.metadata.ClassDescriptor;
26 import org.apache.ojb.broker.metadata.MetadataManager;
27 import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
28 import org.apache.ojb.broker.query.Criteria;
29 import org.apache.ojb.broker.query.Query;
30 import org.apache.ojb.broker.query.QueryByCriteria;
31 import org.apache.ojb.broker.query.QueryFactory;
32 import org.apache.ojb.broker.util.ClassHelper;
33
34 import antlr.NoViableAltException;
35 import antlr.ParserSharedInputState;
36 import antlr.RecognitionException;
37 import antlr.Token;
38 import antlr.TokenBuffer;
39 import antlr.TokenStream;
40 import antlr.TokenStreamException;
41 import antlr.collections.impl.BitSet;
42
43 public class OQLParser extends antlr.LLkParser implements OQLLexerTokenTypes
44  {
45
46 protected OQLParser(TokenBuffer tokenBuf, int k) {
47   super(tokenBuf,k);
48   tokenNames = _tokenNames;
49 }
50
51 public OQLParser(TokenBuffer tokenBuf) {
52   this(tokenBuf,3);
53 }
54
55 protected OQLParser(TokenStream lexer, int k) {
56   super(lexer,k);
57   tokenNames = _tokenNames;
58 }
59
60 public OQLParser(TokenStream lexer) {
61   this(lexer,3);
62 }
63
64 public OQLParser(ParserSharedInputState state) {
65   super(state,3);
66   tokenNames = _tokenNames;
67 }
68
69     public final Query buildQuery() throws RecognitionException, TokenStreamException {
70         Query query = null;
71         
72         
73         try { // for error handling
74
query=selectQuery();
75             {
76             if ((LA(1)==TOK_SEMIC)) {
77                 match(TOK_SEMIC);
78             }
79             else if ((LA(1)==EOF)) {
80             }
81             else {
82                 throw new NoViableAltException(LT(1), getFilename());
83             }
84             
85             }
86         }
87         catch (RecognitionException ex) {
88             reportError(ex);
89             recover(ex,_tokenSet_0);
90         }
91         return query;
92     }
93     
94     public final QueryByCriteria selectQuery() throws RecognitionException, TokenStreamException {
95         QueryByCriteria query = null;
96         
97         
98         try { // for error handling
99

100             Class JavaDoc clazz = null;
101             Criteria criteria = new Criteria();
102             String JavaDoc[] projectionAttrs;
103             boolean distinct = false;
104             
105             match(LITERAL_select);
106             {
107             if ((LA(1)==LITERAL_distinct)) {
108                 match(LITERAL_distinct);
109                 
110                 distinct = true;
111                 
112             }
113             else if ((LA(1)==TOK_STAR||LA(1)==Identifier)) {
114             }
115             else {
116                 throw new NoViableAltException(LT(1), getFilename());
117             }
118             
119             }
120             projectionAttrs=projectionAttributes();
121             match(LITERAL_from);
122             clazz=fromClause();
123             {
124             if ((LA(1)==LITERAL_where)) {
125                 match(LITERAL_where);
126                 whereClause(criteria);
127             }
128             else if ((_tokenSet_1.member(LA(1)))) {
129             }
130             else {
131                 throw new NoViableAltException(LT(1), getFilename());
132             }
133             
134             }
135             
136             if (clazz != null)
137             {
138             if (projectionAttrs[0].indexOf('.') < 0)
139             {
140             query = QueryFactory.newQuery(clazz, criteria, distinct);
141             }
142             else
143             {
144             ClassDescriptor cld = MetadataManager.getInstance().getRepository().getDescriptorFor(clazz);
145             for (int i = 0; i < projectionAttrs.length; i++)
146             {
147             projectionAttrs[i] = projectionAttrs[i].substring(projectionAttrs[i].indexOf('.') + 1);
148             }
149             
150             ArrayList JavaDoc descs = cld.getAttributeDescriptorsForPath(projectionAttrs[0]);
151             int pathLen = descs.size();
152             
153             if ((pathLen > 0) && (descs.get(pathLen - 1) instanceof ObjectReferenceDescriptor))
154             {
155             ObjectReferenceDescriptor ord =
156             ((ObjectReferenceDescriptor) descs.get(pathLen - 1));
157             query = QueryFactory.newQuery(clazz, criteria, distinct);
158             query.setObjectProjectionAttribute(projectionAttrs[0],
159             ord.getItemClass());
160             }
161             else
162             {
163             query = QueryFactory.newReportQuery(clazz, projectionAttrs, criteria, distinct);
164             }
165             }
166             }
167             
168             {
169             if ((LA(1)==LITERAL_order)) {
170                 match(LITERAL_order);
171                 match(LITERAL_by);
172                 orderClause(query);
173             }
174             else if ((_tokenSet_2.member(LA(1)))) {
175             }
176             else {
177                 throw new NoViableAltException(LT(1), getFilename());
178             }
179             
180             }
181             {
182             if ((LA(1)==LITERAL_group)) {
183                 match(LITERAL_group);
184                 match(LITERAL_by);
185                 groupClause(query);
186             }
187             else if ((_tokenSet_3.member(LA(1)))) {
188             }
189             else {
190                 throw new NoViableAltException(LT(1), getFilename());
191             }
192             
193             }
194             {
195             if ((LA(1)==LITERAL_prefetch)) {
196                 match(LITERAL_prefetch);
197                 prefetchClause(query);
198             }
199             else if ((LA(1)==EOF||LA(1)==TOK_RPAREN||LA(1)==TOK_SEMIC)) {
200             }
201             else {
202                 throw new NoViableAltException(LT(1), getFilename());
203             }
204             
205             }
206         }
207         catch (RecognitionException ex) {
208             reportError(ex);
209             recover(ex,_tokenSet_4);
210         }
211         return query;
212     }
213     
214     public final String JavaDoc[] projectionAttributes() throws RecognitionException, TokenStreamException {
215         String JavaDoc[] projectionAttrs = null;
216         
217         Token id = null;
218         Token id1 = null;
219         
220         try { // for error handling
221

222             String JavaDoc first = null;
223             ArrayList JavaDoc list = null;
224             
225             {
226             if ((LA(1)==Identifier)) {
227                 id = LT(1);
228                 match(Identifier);
229                 {
230                 
231                 first = id.getText();
232                 
233                 }
234                 {
235                 _loop80:
236                 do {
237                     if ((LA(1)==TOK_COMMA)) {
238                         match(TOK_COMMA);
239                         id1 = LT(1);
240                         match(Identifier);
241                         {
242                         
243                         if (list == null)
244                         {
245                         list = new ArrayList JavaDoc();
246                         list.add(first);
247                         }
248                         list.add(id1.getText());
249                         
250                         }
251                     }
252                     else {
253                         break _loop80;
254                     }
255                     
256                 } while (true);
257                 }
258             }
259             else if ((LA(1)==TOK_STAR)) {
260                 match(TOK_STAR);
261             }
262             else {
263                 throw new NoViableAltException(LT(1), getFilename());
264             }
265             
266             }
267             
268             if (list == null)
269             {
270             projectionAttrs = new String JavaDoc[] {first};
271             }
272             else
273             {
274             projectionAttrs = (String JavaDoc[]) list.toArray(new String JavaDoc[list.size()]);
275             }
276             
277             
278         }
279         catch (RecognitionException ex) {
280             reportError(ex);
281             recover(ex,_tokenSet_5);
282         }
283         return projectionAttrs;
284     }
285     
286     public final Class JavaDoc fromClause() throws RecognitionException, TokenStreamException {
287         Class JavaDoc clazz = null;
288         
289         Token id = null;
290         
291         try { // for error handling
292
id = LT(1);
293             match(Identifier);
294             
295             try {
296             clazz = ClassHelper.getClass(id.getText());
297             } catch (Exception JavaDoc e) {
298             }
299             
300         }
301         catch (RecognitionException ex) {
302             reportError(ex);
303             recover(ex,_tokenSet_6);
304         }
305         return clazz;
306     }
307     
308     public final void whereClause(
309         Criteria criteria
310     ) throws RecognitionException, TokenStreamException {
311         
312         
313         try { // for error handling
314
orExpr(criteria);
315         }
316         catch (RecognitionException ex) {
317             reportError(ex);
318             recover(ex,_tokenSet_7);
319         }
320     }
321     
322     public final void orderClause(
323         QueryByCriteria query
324     ) throws RecognitionException, TokenStreamException {
325         
326         
327         try { // for error handling
328
sortCriterion(query);
329             {
330             _loop83:
331             do {
332                 if ((LA(1)==TOK_COMMA)) {
333                     match(TOK_COMMA);
334                     sortCriterion(query);
335                 }
336                 else {
337                     break _loop83;
338                 }
339                 
340             } while (true);
341             }
342         }
343         catch (RecognitionException ex) {
344             reportError(ex);
345             recover(ex,_tokenSet_2);
346         }
347     }
348     
349     public final void groupClause(
350         QueryByCriteria query
351     ) throws RecognitionException, TokenStreamException {
352         
353         
354         try { // for error handling
355
groupCriterion(query);
356             {
357             _loop89:
358             do {
359                 if ((LA(1)==TOK_COMMA)) {
360                     match(TOK_COMMA);
361                     groupCriterion(query);
362                 }
363                 else {
364                     break _loop89;
365                 }
366                 
367             } while (true);
368             }
369         }
370         catch (RecognitionException ex) {
371             reportError(ex);
372             recover(ex,_tokenSet_3);
373         }
374     }
375     
376     public final void prefetchClause(
377         QueryByCriteria query
378     ) throws RecognitionException, TokenStreamException {
379         
380         
381         try { // for error handling
382
prefetchCriterion(query);
383             {
384             _loop93:
385             do {
386                 if ((LA(1)==TOK_COMMA)) {
387                     match(TOK_COMMA);
388                     prefetchCriterion(query);
389                 }
390                 else {
391                     break _loop93;
392                 }
393                 
394             } while (true);
395             }
396         }
397         catch (RecognitionException ex) {
398             reportError(ex);
399             recover(ex,_tokenSet_4);
400         }
401     }
402     
403     public final Query existsQuery() throws RecognitionException, TokenStreamException {
404         Query query = null;
405         
406         
407         try { // for error handling
408

409             Class JavaDoc clazz = null;
410             Criteria criteria = new Criteria();
411             
412             match(LITERAL_exists);
413             projectionAttributes();
414             match(LITERAL_in);
415             clazz=fromClause();
416             {
417             if ((LA(1)==TOK_COLON)) {
418                 match(TOK_COLON);
419                 whereClause(criteria);
420             }
421             else if ((_tokenSet_7.member(LA(1)))) {
422             }
423             else {
424                 throw new NoViableAltException(LT(1), getFilename());
425             }
426             
427             }
428             
429             if (clazz != null) {
430             query = QueryFactory.newQuery(clazz, criteria);
431             }
432                     
433         }
434         catch (RecognitionException ex) {
435             reportError(ex);
436             recover(ex,_tokenSet_7);
437         }
438         return query;
439     }
440     
441     public final void orExpr(
442         Criteria criteria
443     ) throws RecognitionException, TokenStreamException {
444         
445         
446         try { // for error handling
447
andExpr(criteria);
448             {
449             _loop97:
450             do {
451                 if ((LA(1)==LITERAL_or) && (_tokenSet_8.member(LA(2))) && (_tokenSet_9.member(LA(3)))) {
452                     match(LITERAL_or);
453                     Criteria orCriteria = new Criteria();
454                     andExpr(orCriteria);
455                     criteria.addOrCriteria(orCriteria);
456                 }
457                 else {
458                     break _loop97;
459                 }
460                 
461             } while (true);
462             }
463         }
464         catch (RecognitionException ex) {
465             reportError(ex);
466             recover(ex,_tokenSet_7);
467         }
468     }
469     
470     public final void sortCriterion(
471         QueryByCriteria query
472     ) throws RecognitionException, TokenStreamException {
473         
474         Token id = null;
475         
476         try { // for error handling
477
boolean descending = false;
478             id = LT(1);
479             match(Identifier);
480             {
481             if ((LA(1)==LITERAL_asc||LA(1)==LITERAL_desc)) {
482                 {
483                 if ((LA(1)==LITERAL_asc)) {
484                     match(LITERAL_asc);
485                     descending = false;
486                 }
487                 else if ((LA(1)==LITERAL_desc)) {
488                     match(LITERAL_desc);
489                     descending = true;
490                 }
491                 else {
492                     throw new NoViableAltException(LT(1), getFilename());
493                 }
494                 
495                 }
496             }
497             else if ((_tokenSet_10.member(LA(1)))) {
498             }
499             else {
500                 throw new NoViableAltException(LT(1), getFilename());
501             }
502             
503             }
504             
505             if (descending) {
506             query.addOrderByDescending(id.getText());
507             } else {
508             query.addOrderByAscending(id.getText());
509             }
510             
511         }
512         catch (RecognitionException ex) {
513             reportError(ex);
514             recover(ex,_tokenSet_10);
515         }
516     }
517     
518     public final void groupCriterion(
519         QueryByCriteria query
520     ) throws RecognitionException, TokenStreamException {
521         
522         Token id = null;
523         
524         try { // for error handling
525
id = LT(1);
526             match(Identifier);
527             
528             query.addGroupBy(id.getText());
529             
530         }
531         catch (RecognitionException ex) {
532             reportError(ex);
533             recover(ex,_tokenSet_11);
534         }
535     }
536     
537     public final void prefetchCriterion(
538         QueryByCriteria query
539     ) throws RecognitionException, TokenStreamException {
540         
541         Token id = null;
542         
543         try { // for error handling
544
id = LT(1);
545             match(Identifier);
546             
547             query.addPrefetchedRelationship(id.getText());
548             
549         }
550         catch (RecognitionException ex) {
551             reportError(ex);
552             recover(ex,_tokenSet_12);
553         }
554     }
555     
556     public final void andExpr(
557         Criteria criteria
558     ) throws RecognitionException, TokenStreamException {
559         
560         
561         try { // for error handling
562
quantifierExpr(criteria);
563             {
564             _loop100:
565             do {
566                 if ((LA(1)==LITERAL_and) && (_tokenSet_8.member(LA(2))) && (_tokenSet_9.member(LA(3)))) {
567                     match(LITERAL_and);
568                     Criteria andCriteria = new Criteria();
569                     quantifierExpr(andCriteria);
570                     criteria.addAndCriteria(andCriteria);
571                 }
572                 else {
573                     break _loop100;
574                 }
575                 
576             } while (true);
577             }
578         }
579         catch (RecognitionException ex) {
580             reportError(ex);
581             recover(ex,_tokenSet_7);
582         }
583     }
584     
585     public final void quantifierExpr(
586         Criteria criteria
587     ) throws RecognitionException, TokenStreamException {
588         
589         
590         try { // for error handling
591
switch ( LA(1)) {
592             case TOK_LPAREN:
593             {
594                 match(TOK_LPAREN);
595                 orExpr(criteria);
596                 match(TOK_RPAREN);
597                 break;
598             }
599             case LITERAL_is_undefined:
600             case LITERAL_is_defined:
601             {
602                 undefinedExpr(criteria);
603                 break;
604             }
605             case LITERAL_exists:
606             case LITERAL_not:
607             {
608                 existsExpr(criteria);
609                 break;
610             }
611             default:
612                 if ((LA(1)==Identifier) && (_tokenSet_13.member(LA(2)))) {
613                     equalityExpr(criteria);
614                 }
615                 else if ((LA(1)==Identifier) && (LA(2)==LITERAL_not||LA(2)==LITERAL_like) && (_tokenSet_14.member(LA(3)))) {
616                     likeExpr(criteria);
617                 }
618                 else if ((LA(1)==Identifier) && (LA(2)==LITERAL_not||LA(2)==LITERAL_between) && (_tokenSet_15.member(LA(3)))) {
619                     betweenExpr(criteria);
620                 }
621                 else if ((LA(1)==Identifier) && (LA(2)==LITERAL_in||LA(2)==LITERAL_not) && (LA(3)==TOK_LPAREN||LA(3)==LITERAL_in||LA(3)==LITERAL_list)) {
622                     inExpr(criteria);
623                 }
624             else {
625                 throw new NoViableAltException(LT(1), getFilename());
626             }
627             }
628         }
629         catch (RecognitionException ex) {
630             reportError(ex);
631             recover(ex,_tokenSet_7);
632         }
633     }
634     
635     public final void equalityExpr(
636         Criteria criteria
637     ) throws RecognitionException, TokenStreamException {
638         
639         Token id = null;
640         
641         try { // for error handling
642

643                 Object JavaDoc value = null;
644             
645             id = LT(1);
646             match(Identifier);
647             {
648             {
649             switch ( LA(1)) {
650             case TOK_EQ:
651             {
652                 match(TOK_EQ);
653                 {
654                 if ((LA(1)==LITERAL_nil)) {
655                     match(LITERAL_nil);
656                     criteria.addIsNull(id.getText());
657                 }
658                 else if ((_tokenSet_16.member(LA(1)))) {
659                     value=literal();
660                     criteria.addEqualTo(id.getText(), value);
661                 }
662                 else {
663                     throw new NoViableAltException(LT(1), getFilename());
664                 }
665                 
666                 }
667                 break;
668             }
669             case TOK_NE:
670             {
671                 match(TOK_NE);
672                 {
673                 if ((LA(1)==LITERAL_nil)) {
674                     match(LITERAL_nil);
675                     criteria.addNotNull(id.getText());
676                 }
677                 else if ((_tokenSet_16.member(LA(1)))) {
678                     value=literal();
679                     criteria.addNotEqualTo(id.getText(), value);
680                 }
681                 else {
682                     throw new NoViableAltException(LT(1), getFilename());
683                 }
684                 
685                 }
686                 break;
687             }
688             case TOK_NE2:
689             {
690                 match(TOK_NE2);
691                 {
692                 if ((LA(1)==LITERAL_nil)) {
693                     match(LITERAL_nil);
694                     criteria.addNotNull(id.getText());
695                 }
696                 else if ((_tokenSet_16.member(LA(1)))) {
697                     value=literal();
698                     criteria.addNotEqualTo(id.getText(), value);
699                 }
700                 else {
701                     throw new NoViableAltException(LT(1), getFilename());
702                 }
703                 
704                 }
705                 break;
706             }
707             case TOK_LT:
708             {
709                 match(TOK_LT);
710                 value=literal();
711                 criteria.addLessThan(id.getText(), value);
712                 break;
713             }
714             case TOK_GT:
715             {
716                 match(TOK_GT);
717                 value=literal();
718                 criteria.addGreaterThan(id.getText(), value);
719                 break;
720             }
721             case TOK_LE:
722             {
723                 match(TOK_LE);
724                 value=literal();
725                 criteria.addLessOrEqualThan(id.getText(), value);
726                 break;
727             }
728             case TOK_GE:
729             {
730                 match(TOK_GE);
731                 value=literal();
732                 criteria.addGreaterOrEqualThan(id.getText(), value);
733                 break;
734             }
735             default:
736             {
737                 throw new NoViableAltException(LT(1), getFilename());
738             }
739             }
740             }
741             }
742         }
743         catch (RecognitionException ex) {
744             reportError(ex);
745             recover(ex,_tokenSet_7);
746         }
747     }
748     
749     public final void likeExpr(
750         Criteria criteria
751     ) throws RecognitionException, TokenStreamException {
752         
753         Token id = null;
754         
755         try { // for error handling
756

757             boolean negative = false;
758             Object JavaDoc value = null;
759             
760             id = LT(1);
761             match(Identifier);
762             {
763             if ((LA(1)==LITERAL_not)) {
764                 match(LITERAL_not);
765                 negative = true;
766             }
767             else if ((LA(1)==LITERAL_like)) {
768             }
769             else {
770                 throw new NoViableAltException(LT(1), getFilename());
771             }
772             
773             }
774             match(LITERAL_like);
775             value=literal();
776             
777             if (negative) {
778             criteria.addNotLike(id.getText(), value);
779             } else {
780             criteria.addLike(id.getText(), value);
781             }
782             
783         }
784         catch (RecognitionException ex) {
785             reportError(ex);
786             recover(ex,_tokenSet_7);
787         }
788     }
789     
790     public final void undefinedExpr(
791         Criteria criteria
792     ) throws RecognitionException,