KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ejb > lib > EjbqlLimitVisitor


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: EjbqlLimitVisitor.java,v 1.1 2004/12/03 07:48:57 joaninh Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.jonas_ejb.lib;
26
27 import java.util.Stack JavaDoc;
28
29 import org.objectweb.jonas_ejb.deployment.ejbql.ASTEJBQL;
30 import org.objectweb.jonas_ejb.deployment.ejbql.ASTInputParameter;
31 import org.objectweb.jonas_ejb.deployment.ejbql.ASTIntegerLiteral;
32 import org.objectweb.jonas_ejb.deployment.ejbql.ASTLimitClause;
33 import org.objectweb.jonas_ejb.deployment.ejbql.ASTLimitExpression;
34 import org.objectweb.jonas_ejb.deployment.ejbql.ParseException;
35 import org.objectweb.jonas_ejb.deployment.ejbql.SimpleNode;
36
37 /**
38  * Implementation of a visitor that creates the limiter ranges corresponding to
39  * the LIMIT clause
40  * @author Cyrille Blot : Initial developer
41  * @author Helene Joanin
42  */

43 public class EjbqlLimitVisitor extends EjbqlAbstractVisitor {
44
45     /**
46      * Types of the parameters list of the finder/select method
47      */

48     private Class JavaDoc[] paramTypes;
49
50     /**
51      * Limiter ranges
52      */

53     private EjbqlLimiterRange[] ranges = new EjbqlLimiterRange[0];
54
55     /**
56      * Constructor
57      * @param ejbql root of the lexical tree of the query
58      * @param paramTypes Types of the parameters list of the finder/select
59      * method
60      * @throws Exception in error case
61      */

62     public EjbqlLimitVisitor(ASTEJBQL ejbql, Class JavaDoc[] paramTypes) throws Exception JavaDoc {
63         this.paramTypes = paramTypes;
64         visit(ejbql);
65     }
66
67     /**
68      * @return returns the limiter ranges of the LIMIT clause.
69      * May be 0 element if no LIMIT clause, 1 or 2 elements otherwise.
70      */

71     public EjbqlLimiterRange[] getLimiterRanges() {
72         return ranges;
73     }
74
75     /**
76      * Visit child node. LIMIT LimitExpression() ( , LimitExpression())?
77      * @param node sub-root of the lexical tree
78      * @param data stack
79      * @return returns null
80      */

81     public Object JavaDoc visit(ASTLimitClause node, Object JavaDoc data) {
82         visit((SimpleNode) node, data);
83         Stack JavaDoc s = (Stack JavaDoc) data;
84         ranges = new EjbqlLimiterRange[s.size()];
85         if (s.size() > 1) {
86             // s = [row, nbr]
87
ranges[1] = (EjbqlLimiterRange) s.pop();
88         }
89         // s = [row]
90
ranges[0] = (EjbqlLimiterRange) s.pop();
91         return null;
92     }
93
94     /**
95      * Visit child node LimitExpression().
96      * @param node sub-root of the lexical tree
97      * @param data stack
98      * @return returns null
99      */

100     public Object JavaDoc visit(ASTLimitExpression node, Object JavaDoc data) {
101         visit((SimpleNode) node, data);
102         return null;
103     }
104
105     /**
106      * Visit child nodes literal ::= integer_literal Push the corresponding
107      * EjbqlLimiterRange to the stack
108      * @param node sub-root of the lexical tree
109      * @param data stack
110      * @return returns null
111      */

112     public Object JavaDoc visit(ASTIntegerLiteral node, Object JavaDoc data) {
113         ((Stack JavaDoc) data).push(new EjbqlLimiterRange(EjbqlLimiterRange.KIND_LITERAL, ((Long JavaDoc) node.value).intValue()));
114         return null;
115     }
116
117     /**
118      * Node with value set to parameter index (1..n) string. Push the
119      * corresponding EjbqlLimiterRange to the stack
120      * @param node sub-root of the lexical tree
121      * @param data stack
122      * @return returns null
123      */

124     public Object JavaDoc visit(ASTInputParameter node, Object JavaDoc data) {
125         try {
126             int pIndex = ((Integer JavaDoc) node.value).intValue() - 1;
127             if (pIndex >= paramTypes.length) {
128                 throw new ParseException("Parameter ?" + (pIndex + 1) + " is out of range (max=" + paramTypes.length
129                         + ")");
130             }
131             // TODO : check if the associated parameter type is compatible to
132
// integer
133
((Stack JavaDoc) data).push(new EjbqlLimiterRange(EjbqlLimiterRange.KIND_PARAMETER, pIndex));
134             return null;
135         } catch (ParseException e) {
136             throw new VisitorException(e);
137         }
138     }
139
140 }
Popular Tags