KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > xquery > parser > FLWRExpression


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.xquery.parser;
24
25 import java.util.ArrayList JavaDoc;
26
27 import org.xquark.xquery.parser.hinttree.HintTree;
28 import org.xquark.xquery.parser.util.Constants;
29 import org.xquark.xquery.typing.TypeException;
30
31 public class FLWRExpression extends XQueryExpression implements Cloneable JavaDoc {
32
33     private static final String JavaDoc RCSRevision = "$Revision: 1.10 $";
34     private static final String JavaDoc RCSName = "$Name: $";
35
36     // cannot be null or empty
37
protected ArrayList JavaDoc variables = null;
38     protected XQueryExpression whereClause = null;
39     // cannot be null
40
protected XQueryExpression returnClause = null;
41     // can be null
42
protected ArrayList JavaDoc orderby = null;
43     protected ArrayList JavaDoc listWhereAdded = new ArrayList JavaDoc();
44     protected HintTree hinttree = null;
45     // #############################################################################
46
// VISITOR STUFF
47
// #############################################################################
48

49     public void accept(ParserVisitor visitor) throws XQueryException {
50         visitor.visit(this);
51     }
52
53     // #############################################################################
54
// CONSTRUCTOR STUFF
55
// #############################################################################
56

57     public FLWRExpression(ArrayList JavaDoc variables, XQueryExpression whereClause, XQueryExpression returnClause, XQueryModule parentModule) throws TypeException, XQueryException {
58         setVariables(variables);
59         setWhereClause(whereClause);
60         setReturnClause(returnClause,false);
61         setParentModule(parentModule);
62         if (parentModule != null && parentModule.getStaticContext().getTypeVisitor() != null)
63             accept(parentModule.getStaticContext().getTypeVisitor());
64     }
65
66     public FLWRExpression(ArrayList JavaDoc variables, ArrayList JavaDoc orderby, XQueryExpression whereClause, XQueryExpression returnClause, XQueryModule parentModule) throws TypeException, XQueryException {
67         setVariables(variables);
68         setOrderBy(orderby);
69         setWhereClause(whereClause);
70         setReturnClause(returnClause,false);
71         setParentModule(parentModule);
72         if (parentModule != null && parentModule.getStaticContext().getTypeVisitor() != null)
73             accept(parentModule.getStaticContext().getTypeVisitor());
74     }
75
76     public FLWRExpression(ArrayList JavaDoc variables, HintTree hintClause, XQueryExpression whereClause, ArrayList JavaDoc orderby, XQueryExpression returnClause, XQueryModule parentModule) throws TypeException, XQueryException {
77         setVariables(variables);
78         setHintTree(hintClause);
79         setWhereClause(whereClause);
80         setOrderBy(orderby);
81         setReturnClause(returnClause,false);
82         setParentModule(parentModule);
83         if (parentModule != null && parentModule.getStaticContext().getTypeVisitor() != null)
84             accept(parentModule.getStaticContext().getTypeVisitor());
85     }
86
87     public FLWRExpression(ArrayList JavaDoc variables, HintTree hintClause, ArrayList JavaDoc orderby, XQueryExpression whereClause, XQueryExpression returnClause, XQueryModule parentModule) throws TypeException, XQueryException {
88         setVariables(variables);
89         setHintTree(hintClause);
90         setOrderBy(orderby);
91         setWhereClause(whereClause);
92         setReturnClause(returnClause,false);
93         setParentModule(parentModule);
94         if (parentModule != null && parentModule.getStaticContext().getTypeVisitor() != null)
95             accept(parentModule.getStaticContext().getTypeVisitor());
96     }
97
98     // #############################################################################
99
// ATTRIBUTE ACCESS STUFF
100
// #############################################################################
101

102     public ArrayList JavaDoc getVariables() {
103         return variables;
104     }
105     public void setVariables(ArrayList JavaDoc variables) throws XQueryException {
106         // cannot be null or empty
107
if (variables == null || variables.isEmpty())
108             throw new XQueryException("variables of FLWRExpression cannot be null or empty");
109         this.variables = variables;
110         for (int i = 0; i < this.variables.size(); i++) {
111             Variable var = (Variable) this.variables.get(i);
112             var.setParentFLWRExpression(this);
113             var.setParentModule(parentModule);
114         }
115     }
116     public void insertVariable(Variable variable) {
117         // the existence of the same variable could be tested !!!
118
variable.setParentFLWRExpression(this);
119         variable.setParentModule(parentModule);
120         variables.add(0, variable);
121     }
122     public void addVariable(Variable variable) {
123         // the existence of the same variable could be tested !!!
124
variable.setParentFLWRExpression(this);
125         variable.setParentModule(parentModule);
126         variables.add(variable);
127     }
128     public void addVariables(ArrayList JavaDoc varlist) {
129         if (varlist == null || varlist.isEmpty())
130             return;
131         for (int i = 0; i < varlist.size(); i++)
132             addVariable((Variable) varlist.get(i));
133     }
134     public void addVariable(int index, Variable variable) {
135         // the existence of the same variable could be tested !!!
136
variable.setParentFLWRExpression(this);
137         variable.setParentModule(parentModule);
138         variables.add(index, variable);
139     }
140     public void addVariables(int index, ArrayList JavaDoc varlist) {
141         if (varlist == null || varlist.isEmpty())
142             return;
143         for (int i = 0; i < varlist.size(); i++)
144             addVariable(index+i, (Variable) varlist.get(i));
145     }
146
147     public ArrayList JavaDoc getOrderBy() {
148         return orderby;
149     }
150     public void setOrderBy(ArrayList JavaDoc orderby) {
151         this.orderby = orderby;
152     }
153
154     public void addOrderBy(XQueryExpression expr) {
155         // the existence of the same variable could be tested !!!
156
expr.setParentExpression(this);
157         expr.setParentModule(parentModule);
158         if (orderby == null)
159             orderby = new ArrayList JavaDoc(1);
160         orderby.add(expr);
161     }
162
163     public void addOrderBy(int index, XQueryExpression expr) {
164         // the existence of the same variable could be tested !!!
165
expr.setParentExpression(this);
166         expr.setParentModule(parentModule);
167         if (orderby == null)
168             orderby = new ArrayList JavaDoc(1);
169         orderby.add(index, expr);
170     }
171
172     public XQueryExpression getReturnClause() {
173         return returnClause;
174     }
175     public void setReturnClause(XQueryExpression returnClause) throws XQueryException {
176         setReturnClause(returnClause,true);
177     }
178     public void setReturnClause(XQueryExpression returnClause, boolean typing) throws XQueryException {
179         // cannot be null
180
if (returnClause == null)
181             throw new XQueryException("returnClause of FWLRExpression cannot be null");
182         this.returnClause = returnClause;
183         this.returnClause.setParentExpression(this);
184         if (typing && parentModule != null && parentModule.getStaticContext().getTypeVisitor() != null) {
185             qtype = null;
186             accept(parentModule.getStaticContext().getTypeVisitor());
187         }
188     }
189
190     public XQueryExpression getWhereClause() {
191         return whereClause;
192     }
193     public void setWhereClause(XQueryExpression whereClause) {
194         this.whereClause = whereClause;
195         if (this.whereClause != null) {
196             this.whereClause.setParentExpression(this);
197             this.whereClause.setParentModule(parentModule);
198         }
199     }
200
201     public int getVarPosition(Variable var) {
202         if (variables == null)
203             return -1;
204         return variables.indexOf(var);
205     }
206
207     public ArrayList JavaDoc getXTrees() {
208         return returnClause.getXTrees();
209     }
210
211     public HintTree getHintTree() {
212         return hinttree;
213     }
214
215     public void setHintTree(HintTree hinttree) {
216         this.hinttree = hinttree;
217     }
218
219     private int GetFirstForVariable(int index) {
220         for (int i = index; i < variables.size(); i++) {
221             if (((Variable) variables.get(i)).getBindingType() == Constants.FOR_BINDINGTYPE)
222                 return i;
223         }
224         return -1;
225     }
226
227     // #############################################################################
228
// CLONE STUFF
229
// #############################################################################
230

231     public void addParentExpression(XQueryExpression parentExpression) {
232         addParentExpression(parentExpression);
233
234         if (variables != null)
235             for (int i = 0; i < variables.size(); i++)
236                  ((Variable) variables.get(i)).addParentExpression(parentExpression);
237         if (whereClause != null)
238             whereClause.addParentExpression(parentExpression);
239         if (returnClause != null)
240             returnClause.addParentExpression(parentExpression);
241     }
242
243     // #############################################################################
244
// RESTRUCTURE STUFF
245
// #############################################################################
246
public void insertVariableAtPosition(Variable var, int position) {
247         variables.add(position, var);
248         var.setParentFLWRExpression(this);
249         var.setParentModule(parentModule);
250     }
251
252     public void addWhereClause(XQueryExpression addWhereClause) throws XQueryException {
253         if (addWhereClause == null)
254             return;
255         if (whereClause == null) {
256             listWhereAdded.add(addWhereClause);
257             setWhereClause(addWhereClause);
258             return;
259         }
260         if (!listWhereAdded.contains(addWhereClause)) {
261             listWhereAdded.add(addWhereClause);
262             whereClause.setParenthesis(true);
263             addWhereClause.setParenthesis(true);
264             setWhereClause(new BinOpANDExpression(whereClause, addWhereClause, parentModule));
265         }
266     }
267
268 }
269
Popular Tags