KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > xquery > normalize > CanonizeVisitor


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  * Copyright (C) 2003 XQuark Group.
5  *
6  * This program 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 (at your option) any later version.
10  *
11  * This program 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 program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
19  * You can also get it at http://www.gnu.org/licenses/lgpl.html
20  *
21  * For more information on this software, see http://www.xquark.org.
22  */

23
24 package org.xquark.xquery.normalize;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Stack JavaDoc;
29
30 import org.xquark.xquery.parser.*;
31 import org.xquark.xquery.parser.util.Constants;
32 import org.xquark.xquery.typing.TypeVisitor;
33
34 public class CanonizeVisitor extends DefaultParserVisitor {
35
36     private static final String JavaDoc RCSRevision = "$Revision: 1.4 $";
37     private static final String JavaDoc RCSName = "$Name: $";
38
39     private Stack JavaDoc parents = null;
40     private Stack JavaDoc FLWRParents = null;
41     private XQueryExpression resultExpr = null;
42
43     private ArrayList JavaDoc QDB = null;
44     private XQueryExpression QMEM = null;
45     private int inReturn = 0;
46     private int inWhere = 0;
47     private Variable inVarDef = null;
48
49     protected TypeVisitor typeVisitor = null;
50
51     // public CanonizeVisitor()
52
public CanonizeVisitor(TypeVisitor typeVisitor) {
53         parents = new Stack JavaDoc();
54         FLWRParents = new Stack JavaDoc();
55         this.typeVisitor = typeVisitor;
56     }
57
58     public XQueryExpression getResult() {
59         return resultExpr;
60     }
61     public ArrayList JavaDoc getQDB() {
62         return QDB;
63     }
64     public XQueryExpression getQMEM() {
65         return QMEM;
66     }
67
68     // visitors
69

70     public void visit(Element arg) throws XQueryException {
71         resultExpr = null;
72         parents.push(arg);
73         ArrayList JavaDoc attributes = arg.getAttributes();
74         if (attributes != null) {
75             for (int i = 0; i < attributes.size(); i++) {
76                 XQueryExpression expri = (XQueryExpression) attributes.get(i);
77                 expri.accept(this);
78                 while (resultExpr != null) {
79                     attributes.set(i, resultExpr);
80                     expri = (XQueryExpression) attributes.get(i);
81                     resultExpr = null;
82                     expri.accept(this);
83                 }
84             }
85         }
86         ArrayList JavaDoc subExpressions = arg.getSubExpressions();
87         if (subExpressions != null) {
88             for (int i = 0; i < subExpressions.size(); i++) {
89                 XQueryExpression expri = (XQueryExpression) subExpressions.get(i);
90                 expri.accept(this);
91                 while (resultExpr != null) {
92                     subExpressions.set(i, resultExpr);
93                     expri = (XQueryExpression) subExpressions.get(i);
94                     resultExpr = null;
95                     expri.accept(this);
96                 }
97             }
98         }
99         parents.pop();
100     }
101
102     public void visit(FLWRExpression arg) throws XQueryException {
103         resultExpr = null;
104         int i, j, k;
105         // test if it has a parent FLWR
106
if (inReturn > 0 && !FLWRParents.isEmpty()) {
107             FLWRExpression parentFLWRExpr = (FLWRExpression) FLWRParents.peek();
108             // look at dependencies between variables! (LATER!!!)
109
// create new variable
110
Variable var = typeVisitor.getStaticContext().createVariable(Constants.OUTERLET_BINDINGTYPE, arg, Variable.CAN_CREATOR);
111             parentFLWRExpr.addVariable(var);
112             XQueryExpression retClause = parentFLWRExpr.getReturnClause();
113             if (retClause == arg)
114                 parentFLWRExpr.setReturnClause(var);
115             else {
116                 // substiture FLWR by var in it's parent
117
HashMap JavaDoc map = new HashMap JavaDoc();
118                 map.put(arg, var);
119                 retClause.substitute(map, typeVisitor);
120             }
121             var.accept(typeVisitor);
122             var.accept(this);
123             return;
124         }
125
126         parents.push(arg);
127         FLWRParents.push(arg);
128
129         XQueryExpression whereExpr = arg.getWhereClause();
130         if (whereExpr != null) {
131             inWhere++;
132             whereExpr.accept(this);
133             inWhere--;
134             whereExpr = arg.getWhereClause();
135         }
136
137         XQueryExpression returnExpr = arg.getReturnClause();
138         inReturn++;
139         returnExpr.accept(this);
140         inReturn--;
141         returnExpr = arg.getReturnClause();
142
143         //HintTree hinttree = arg.getHintTree();
144
//ArrayList orderby = arg.getOrderBy();
145
/*
146         for (i=0;i<orderby.size();i++) {
147             XQueryExpression orderbyi = (XQueryExpression)orderby.get(i);
148             inReturn++;
149             orderbyi.accept(this);
150             inReturn--;
151         }
152         */

153         ArrayList JavaDoc variables = arg.getVariables();
154         Variable tmpVar = null;
155         int varsize = variables.size();
156         for (i = 0; i < varsize; i++) {
157             tmpVar = (Variable) variables.get(i);
158             Variable tmpVarDef = inVarDef;
159             inVarDef = tmpVar;
160             tmpVar.accept(this);
161             inVarDef = tmpVarDef;
162         }
163
164         ArrayList JavaDoc qdb = new ArrayList JavaDoc();
165         ArrayList JavaDoc forvarlist = new ArrayList JavaDoc();
166         ArrayList JavaDoc letvarlist = new ArrayList JavaDoc();
167         // ArrayList usedvarlist = new ArrayList();
168
// get variables used in return clause
169
// put variables in different lists
170
for (i = 0; i < variables.size(); i++) {
171             tmpVar = (Variable) variables.get(i);
172             if (tmpVar.getBindingType() == Constants.OUTERLET_BINDINGTYPE)
173                 letvarlist.add(tmpVar);
174             else
175                 forvarlist.add(tmpVar);
176         }
177
178         // XQueryExpressionSequence tmpSeq = new XQueryExpressionSequence(usedvarlist,typeVisitor);
179
XQueryExpressionSequence tmpSeq = new XQueryExpressionSequence(forvarlist, arg.getParentModule());
180         tmpSeq.setParenthesis(true);
181         // change 23/01/2003
182
//FLWRExpression tmpFLWR = new FLWRExpression(forvarlist, orderby, whereExpr, tmpSeq, typeVisitor);
183
FLWRExpression tmpFLWR = new FLWRExpression(forvarlist, arg.getHintTree(), arg.getOrderBy(), whereExpr, tmpSeq, arg.getParentModule());
184         tmpFLWR.setParentModule(arg.getParentModule());
185         tmpVar = typeVisitor.getStaticContext().createVariable(Constants.LET_BINDINGTYPE, tmpFLWR, Variable.CAN_CREATOR);
186         qdb.add(tmpVar);
187         XQueryExpression qmem = returnExpr;
188
189         ArrayList JavaDoc allinnerqdb = new ArrayList JavaDoc();
190         for (i = 0; i < letvarlist.size(); i++) {
191             Variable letVarListi = (Variable) letvarlist.get(i);
192             FLWRExpression letVarListiExpr = (FLWRExpression) letVarListi.getExpression();
193             letVarListiExpr.accept(this);
194             ArrayList JavaDoc innerqdb = QDB;
195             XQueryExpression innerqmem = QMEM;
196             // put depends
197
ArrayList JavaDoc vars = letVarListiExpr.getVariables();
198             if (!vars.isEmpty()) {
199                 ArrayList JavaDoc dependids = new ArrayList JavaDoc();
200                 for (j = 0; j < vars.size(); j++) {
201                     Variable varj = (Variable) vars.get(j);
202                     if (varj.getBindingType() == Constants.FOR_BINDINGTYPE)
203                         dependids.add(varj);
204                 }
205                 if (letVarListiExpr.getReturnClause().getDependIDs() == null && !dependids.isEmpty())
206                     letVarListiExpr.getReturnClause().setDependIDs(dependids);
207             }
208
209             // add for variables from qdb into innerqmem
210
for (j = 0; j < innerqdb.size(); j++) {
211                 Variable innerQdbVari = (Variable) innerqdb.get(j);
212                 FLWRExpression innerQdbVariExpr = (FLWRExpression) innerQdbVari.getExpression();
213                 for (k = 0; k < qdb.size(); k++) {
214                     Variable qdbVari = (Variable) qdb.get(k);
215                     XQueryExpression qdbVariExpr = qdbVari.getExpression();
216                     tmpVar = typeVisitor.getStaticContext().createVariable(Constants.FOR_BINDINGTYPE, qdbVari, Variable.CAN_CREATOR);
217                     innerQdbVariExpr.insertVariable(tmpVar);
218                 }
219             }
220             // add innerqdb to allinnerqdb
221
allinnerqdb.addAll(innerqdb);
222             // replace in qmem variable by innerqmem
223
// handle special case when entire return expression must be replaced
224
if (qmem == letVarListi) {
225                 arg.setReturnClause(innerqmem);
226                 qmem = arg.getReturnClause();
227             } else {
228                 ArrayList JavaDoc parentList = letVarListi.getParentExpression();
229                 HashMap JavaDoc map = new HashMap JavaDoc();
230                 map.put(letVarListi, innerqmem);
231                 for (j = 0; j < parentList.size(); j++) {
232                     XQueryExpression parenti = (XQueryExpression) parentList.get(j);
233                     parenti.substitute(map, typeVisitor);
234                 }
235             }
236         }
237
238         // add allinnerqdb to qdb
239
qdb.addAll(allinnerqdb);
240         // put skolems
241
if (letvarlist.size() > 0) {
242             ArrayList JavaDoc tmpList = new ArrayList JavaDoc(forvarlist.size());
243             for (i = 0; i < forvarlist.size(); i++) {
244                 Variable var = (Variable) forvarlist.get(i);
245                 // change 27/03/2002
246
if (var.getBindingType() != Constants.LET_BINDINGTYPE)
247                     tmpList.add(var);
248             }
249             qmem.putSkolemIDs(tmpList);
250         }
251
252         // set return values
253
QDB = qdb;
254         QMEM = qmem;
255
256         FLWRParents.pop();
257         parents.pop();
258     }
259
260     public void visit(Variable arg) throws XQueryException {
261         resultExpr = null;
262         parents.push(arg);
263         if (inReturn > 0) {
264             if (inVarDef != null) {
265                 XQueryExpression expr = arg.getExpression();
266                 expr.accept(this);
267             }
268         }
269         parents.pop();
270     }
271
272     public void visit(XQueryBinaryOperatorExpression arg) throws XQueryException {
273         resultExpr = null;
274         parents.push(arg);
275         XQueryExpression tmpExpr = arg.getExpression1();
276         tmpExpr.accept(this);
277         while (resultExpr != null) {
278             arg.setExpression1(resultExpr);
279             tmpExpr = arg.getExpression1();
280             resultExpr = null;
281             tmpExpr.accept(this);
282         }
283         tmpExpr = arg.getExpression2();
284         tmpExpr.accept(this);
285         while (resultExpr != null) {
286             arg.setExpression2(resultExpr);
287             tmpExpr = arg.getExpression2();
288             resultExpr = null;
289             tmpExpr.accept(this);
290         }
291         parents.pop();
292     }
293     // public void visit(XQueryBooleanOperatorExpression arg) throws XQueryException;
294

295     public void visit(XQueryModule arg) throws XQueryException {
296         // TODO danger expressions must be of size 1
297
ArrayList JavaDoc expressions = arg.getExpressions();
298         if (expressions != null && expressions.size() == 1)
299             ((XQueryExpression)expressions.get(0)).accept(this);
300         else
301             resultExpr = null;
302     }
303
304     public void visit(XQueryExpression arg) throws XQueryException {
305         resultExpr = null;
306     }
307
308     public void visit(XQueryExpressionSequence arg) throws XQueryException {
309         resultExpr = null;
310         parents.push(arg);
311         ArrayList JavaDoc subExpressions = arg.getSubExpressions();
312         ArrayList JavaDoc newSubExpressions = new ArrayList JavaDoc();
313         XQueryExpression rexExpr = null;
314         if (subExpressions != null) {
315             for (int i = 0; i < subExpressions.size(); i++) {
316                 XQueryExpression expri = (XQueryExpression) subExpressions.get(i);
317                 expri.accept(this);
318                 while (resultExpr != null) {
319                     subExpressions.set(i, resultExpr);
320                     expri = (XQueryExpression) subExpressions.get(i);
321                     resultExpr = null;
322                     expri.accept(this);
323                 }
324             }
325         }
326         parents.pop();
327     }
328
329     public void visit(XQueryUnaryOperatorExpression arg) throws XQueryException {
330         resultExpr = null;
331         parents.push(arg);
332         XQueryExpression expr = arg.getExpression();
333         expr.accept(this);
334         while (resultExpr != null) {
335             arg.setExpression(resultExpr);
336             expr = arg.getExpression();
337             resultExpr = null;
338             expr.accept(this);
339         }
340         parents.pop();
341     }
342
343 }
344
Popular Tags