KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-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: EjbqlOrderByVisitor.java,v 1.3 2004/09/17 08:25:02 joaninh Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.jonas_ejb.lib;
26
27
28 import java.util.ArrayList JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Stack JavaDoc;
32
33 import org.objectweb.jonas_ejb.deployment.ejbql.ASTEJBQL;
34 import org.objectweb.jonas_ejb.deployment.ejbql.ASTCmpPathExpression;
35 import org.objectweb.jonas_ejb.deployment.ejbql.ASTOrderByClause;
36 import org.objectweb.jonas_ejb.deployment.ejbql.ASTOrderByItem;
37 import org.objectweb.jonas_ejb.deployment.ejbql.ASTPath;
38 import org.objectweb.jonas_ejb.deployment.ejbql.SimpleNode;
39
40 import org.objectweb.medor.api.Field;
41 import org.objectweb.medor.query.api.OrderField;
42 import org.objectweb.medor.query.api.QueryTreeField;
43 import org.objectweb.medor.query.lib.BasicOrderField;
44
45 /**
46  * Implementation of a visitor that creates a list of org.objectweb.medor.query.api.OrderField
47  * to a given ORDER BY clause.
48  * Created on Aug 27, 2003
49  * @author Helene Joanin
50  */

51 public class EjbqlOrderByVisitor extends EjbqlAbstractVisitor {
52
53     Map JavaDoc fields;
54     ArrayList JavaDoc orderFields;
55
56     /**
57      * Constructor
58      * @param ejbql root of the lexical tree of the query
59      * @param fields Map with (name,QueryTreeField) pairs of all the variables appear in the query
60      */

61     public EjbqlOrderByVisitor(ASTEJBQL ejbql, Map JavaDoc _fields) throws Exception JavaDoc {
62         orderFields = new ArrayList JavaDoc();
63         fields = _fields;
64         visit(ejbql);
65     }
66
67     /**
68      * get the that was built from visiting the lexical tree
69      */

70     public OrderField[] getOrderFields() {
71         OrderField[] ofs = new OrderField[orderFields.size()];
72         Iterator JavaDoc itr = orderFields.iterator();
73         for (int i=0; itr.hasNext(); i++) {
74             ofs[i] = (OrderField) itr.next();
75         }
76         return ofs;
77     }
78
79
80     /**
81      * Visit child node.
82      * ORDER BY OrderByItem() (, OrderByItem() )*
83      */

84     public Object JavaDoc visit(ASTOrderByClause node, Object JavaDoc data) {
85         visit((SimpleNode) node, data);
86         return null;
87     }
88
89     /**
90      * Visit child node.
91      */

92     public Object JavaDoc visit(ASTOrderByItem node, Object JavaDoc data) {
93         visit((SimpleNode) node, data);
94         QueryTreeField qtf = (QueryTreeField) ((Stack JavaDoc) data).pop();
95         orderFields.add(new BasicOrderField(qtf, !node.asc));
96         return null;
97     }
98
99     /**
100      * Push corresponding MedorField to the stack.<br>
101      * cmp_path_expression ::= path
102      * was in initial BNF
103      * cmp_path_expression ::= {identification_variable | single_valued_cmr_path_expression}.cmp_field
104      */

105     public Object JavaDoc visit(ASTCmpPathExpression node, Object JavaDoc data) {
106         visit((SimpleNode) node, data);
107         try {
108             String JavaDoc path = (String JavaDoc) ((ASTPath) ((Stack JavaDoc) data).pop()).value;
109             // FIXME check type for cmp field
110
((Stack JavaDoc) data).push((Field) fields.get(path));
111         } catch (Exception JavaDoc e) {
112             throw new VisitorException(e);
113         }
114         return null;
115     }
116
117     /**
118      * Push the Node to the stack
119      */

120     public Object JavaDoc visit(ASTPath node, Object JavaDoc data) {
121         ((Stack JavaDoc) data).push(node);
122         return null;
123     }
124
125 }
126
Popular Tags