KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mediator > decomposer > GetVariablePathsVisitor


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.mediator.decomposer;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 import org.xquark.xquery.parser.*;
30 import org.xquark.xquery.parser.util.Constants;
31
32 public class GetVariablePathsVisitor extends DefaultParserVisitor {
33     // **********************************************************************
34
// * VERSIONING
35
// **********************************************************************
36
private static final String JavaDoc RCSRevision = "$Revision: 1.8 $";
37     private static final String JavaDoc RCSName = "$Name: $";
38     // **********************************************************************
39
// * CLASS VARIABLES
40
// **********************************************************************
41
private HashMap JavaDoc varpaths = null;
42     //private HashMap varjoinpaths = new HashMap();
43
private ArrayList JavaDoc pathstrlist = null;
44     private boolean eliminateDuplicates = false;
45     private ArrayList JavaDoc allDefinedVars = null;
46     
47     private ArrayList JavaDoc currentlist = null;
48     private boolean isrootqdb = false;
49     private boolean isqdb = false;
50     private boolean ismonosource = false;
51     private boolean inWhere = false;
52     private Variable defvar = null;
53     //private HashMap aliasmap = new HashMap();
54
//private HashMap dependvarmap = new HashMap();
55
private ArrayList JavaDoc onlyvariables = null;
56     private GetUsedVariablesVisitor guvv = new GetUsedVariablesVisitor();
57     
58     /*
59     private final Integer IN_DEFINITION = new Integer(1);
60     private final Integer IN_FLWR = new Integer(2);
61     private final Integer IN_WHERE = new Integer(3);
62     private final Integer IN_PREDICATE = new Integer(4);
63     private final Integer IN_FUNCTION = new Integer(5);
64      */

65     // **********************************************************************
66
// * INITIALIZATION
67
// **********************************************************************
68
/**
69      *
70      */

71
72     //public GetVariablePathsVisitor() { varpaths = new HashMap(); pathstrlist = new ArrayList(); }
73

74     public GetVariablePathsVisitor(boolean eliminateDuplicates) {
75         this.eliminateDuplicates = eliminateDuplicates;
76         varpaths = new HashMap JavaDoc();
77         pathstrlist = new ArrayList JavaDoc();
78     }
79
80     public GetVariablePathsVisitor(boolean eliminateDuplicates, HashMap JavaDoc varpaths, ArrayList JavaDoc pathstrlist) {
81         this.eliminateDuplicates = eliminateDuplicates;
82         this.varpaths = varpaths;
83         this.pathstrlist = pathstrlist;
84     }
85
86     public ArrayList JavaDoc getPathStrList() {
87         return pathstrlist;
88     }
89     public HashMap JavaDoc getVarPaths() {
90         return varpaths;
91     }
92     public ArrayList JavaDoc getVarPaths(Variable var) {
93         return (ArrayList JavaDoc) varpaths.get(var);
94     }
95     public ArrayList JavaDoc getPaths() {
96         ArrayList JavaDoc retlist = new ArrayList JavaDoc();
97         for (Iterator JavaDoc it = varpaths.keySet().iterator(); it.hasNext();)
98             retlist.addAll((ArrayList JavaDoc) varpaths.get(it.next()));
99         if (retlist.isEmpty())
100             return null;
101         return retlist;
102     }
103
104     public ArrayList JavaDoc getPaths(ArrayList JavaDoc variables) {
105         if (variables == null) return null;
106         ArrayList JavaDoc retlist = new ArrayList JavaDoc();
107         for (Iterator JavaDoc it = varpaths.keySet().iterator(); it.hasNext();) {
108             Object JavaDoc tmpvar = it.next();
109             if (variables.contains(tmpvar))
110                 retlist.addAll((ArrayList JavaDoc) varpaths.get(tmpvar));
111         }
112         if (retlist.isEmpty())
113             return null;
114         return retlist;
115     }
116
117 // public HashMap getAliasMap() {
118
// return this.aliasmap;
119
// }
120

121     public void reset() {
122         varpaths.clear();
123         pathstrlist.clear();
124 // aliasmap.clear();
125
// dependvarmap.clear();
126
}
127
128     public void resetPaths() {
129         varpaths.clear();
130         pathstrlist.clear();
131         onlyvariables = null;
132     }
133
134     public void setRootQDB(boolean isrootqdb) {
135         this.isrootqdb = isrootqdb;
136     }
137     public void setQDB(boolean isqdb) {
138         this.isqdb = isqdb;
139     }
140     public void setMonoSource(boolean ismonosource) {
141         this.ismonosource = ismonosource;
142     }
143
144     public void setVariables(ArrayList JavaDoc variables) {
145         this.onlyvariables = variables;
146     }
147     public void setCurrentList(ArrayList JavaDoc currentlist) {
148         this.currentlist = currentlist;
149     }
150     private void addVarList(ArrayList JavaDoc addlist) {
151         if (this.allDefinedVars == null)
152             this.allDefinedVars = new ArrayList JavaDoc();
153         this.allDefinedVars.addAll(addlist);
154     }
155     private void addVar(Variable var) {
156         if (this.allDefinedVars == null)
157             this.allDefinedVars = new ArrayList JavaDoc();
158         this.allDefinedVars.add(var);
159     }
160     private void removeVarList(ArrayList JavaDoc removelist) {
161         if (this.allDefinedVars != null)
162             this.allDefinedVars.removeAll(removelist);
163     }
164     public void resetCurrentVarList() {
165         this.currentlist = null;
166     }
167 // private void addAliasMap(Variable defvar, Variable var, XQueryExpression expr) {
168
// //dependvarmap.put(defvar, var);
169
// aliasmap.put(defvar, expr);
170
// }
171

172     private void addVarPath(Variable var, XQueryExpression expr) {
173         if (onlyvariables != null && !onlyvariables.contains(var))
174             return;
175         if (allDefinedVars != null) {
176             if (!allDefinedVars.contains(var))
177                 return;
178         }
179         ArrayList JavaDoc tmplist = (ArrayList JavaDoc) varpaths.get(var);
180         if (tmplist == null) {
181             tmplist = new ArrayList JavaDoc();
182             tmplist.add(expr);
183             pathstrlist.add(expr.getStringValue());
184             varpaths.put(var, tmplist);
185         } else if (!pathstrlist.contains(expr.getStringValue()) || !eliminateDuplicates) {
186             tmplist.add(expr);
187             pathstrlist.add(expr.getStringValue());
188         }
189     }
190
191     // **********************************************************************
192
// * VISITATION
193
// **********************************************************************
194
// public void visit(AttributeValuePair arg) throws XQueryException
195
// public void visit(BinOpANDExpression arg) throws XQueryException
196
// public void visit(BinOpORExpression arg) throws XQueryException
197
// public void visit(CastTreatExpression arg) throws XQueryException
198
// public void visit(ContextDeclaration arg) throws XQueryException
199
// public void visit(Dereference arg) throws XQueryException
200
public void visit(Element arg) throws XQueryException {
201         ArrayList JavaDoc al = arg.getAttributes();
202         if (al != null)
203             for (int i = 0; i < al.size(); i++)
204                  ((AttributeValuePair) al.get(i)).accept(this);
205         al = arg.getSubExpressions();
206         if (al != null)
207             for (int i = 0; i < al.size(); i++)
208                  ((XQueryExpression) al.get(i)).accept(this);
209     }
210     public void visit(FLWRExpression arg) throws XQueryException {
211         boolean isqdb = this.isqdb;
212         boolean isrootqdb = this.isrootqdb;
213         this.isqdb = false;
214         this.isrootqdb = false;
215         //ArrayList prevlist = varlist;
216
//varlist = arg.getVariables();
217
for (int i = 0; i < arg.getVariables().size(); i++) {
218             Variable vari = (Variable) arg.getVariables().get(i);
219 // if (varlist != null && !varlist.contains(vari))
220
// continue;
221
addVar(vari);
222             XQueryExpression expr = vari.getExpression();
223 // if (expr instanceof RootNodeFunctionCall || (expr instanceof LocatedExpression && ((LocatedExpression)expr).getStep(0).getExpression() instanceof RootNodeFunctionCall))
224
// if (expr instanceof Variable)
225
// this.addAliasMap(vari, (Variable) expr, expr);
226
// else if (expr instanceof LocatedExpression && ((LocatedExpression) expr).getStep(0).getExpression() instanceof Variable)
227
// this.addAliasMap(vari, (Variable) ((LocatedExpression) expr).getStep(0).getExpression(), expr);
228
expr.accept(this);
229         }
230         if (!isqdb) {
231             ArrayList JavaDoc prevlist = allDefinedVars;
232             allDefinedVars = null;
233             arg.getReturnClause().accept(this);
234             allDefinedVars = prevlist;
235         }
236         XQueryExpression qi = arg.getWhereClause();
237         if (qi != null) {
238 // if (!isqdb)
239
// this.removeVarList(arg.getVariables());
240
// guvv.reset();
241
// qi.accept(guvv);
242
// ArrayList reslist = guvv.getVariables();
243
// // change 18/06/2003
244
// if (reslist != null && !reslist.isEmpty()) {
245
//if (reslist != null && reslist.size() > 1) {
246
boolean prevInWhere = inWhere;
247                 inWhere = true;
248                 qi.accept(this);
249                 inWhere = prevInWhere;
250 // }
251
// if (!isqdb)
252
// this.addVarList(arg.getVariables());
253
}
254         ArrayList JavaDoc al = arg.getOrderBy();
255         if (al != null)
256             for (int i = 0; i < al.size(); i++) {
257                 XQueryExpression expri = (XQueryExpression) al.get(i);
258                 if (expri.getOrder(0) != Constants.PLACE_ORDER)
259                     expri.accept(this);
260             }
261         this.isrootqdb = isrootqdb;
262         this.isqdb = isqdb;
263     }
264
265     public void visit(FunctionCall arg) throws XQueryException {
266         // change 18/06/2003
267
// if (inWhere) {
268
// guvv.reset();
269
// arg.accept(guvv);
270
// ArrayList reslist = guvv.getVariables();
271
// if (reslist == null || reslist.size() < 2)
272
// return;
273
// }
274
ArrayList JavaDoc al = arg.getArguments();
275         if (al != null)
276             for (int i = 0; i < al.size(); i++) {
277                 ((XQueryExpression) al.get(i)).accept(this);
278             }
279     }
280
281     // public void visit(FunctionDefinition arg) throws XQueryException
282
// public void visit(InstanceOfExpression arg) throws XQueryException
283
// public void visit(InternalFunctionCall arg) throws XQueryException
284
// public void visit(ITEExpression arg) throws XQueryException
285
// public void visit(LibraryFunctionCall arg) throws XQueryException
286
// public void visit(ListOpAFTERExpression arg) throws XQueryException
287
// public void visit(ListOpArithExpression arg) throws XQueryException
288
// public void visit(ListOpBEFOREExpression arg) throws XQueryException
289

290     // public void visit(ListOpCompExpression arg) throws XQueryException {
291
// GetVariablePathsVisitor visitor = new GetVariablePathsVisitor(true);
292
// arg.getExpression1().accept(visitor);
293
// arg.getExpression2().accept(visitor);
294
// HashMap map = visitor.getVarPaths();
295
// int num = 0;
296
// boolean addvars = false;
297
// if (varlist != null) for (int i=0;i<varlist.size();i++) if (map.keySet().contains(varlist.get(i))) num++;
298
// if (num > 1 || (null !=onlyvariables && !onlyvariables.isEmpty())) addvars = true;
299
// for (Iterator it = map.keySet().iterator();it.hasNext();) {
300
// Variable var = (Variable)it.next();
301
// if (addvars || (varlist != null && !varlist.contains(var))) {
302
// ArrayList list = (ArrayList)map.get(var);
303
// for (int i=0;i<list.size();i++) this.addVarPath(var,(XQueryExpression)list.get(i));
304
// }
305
// }
306
// }
307
public void visit(ListOpCompExpression arg) throws XQueryException {
308         // change 18/06/2003
309
// if (inWhere) {
310
// guvv.reset();
311
// arg.accept(guvv);
312
// ArrayList reslist = guvv.getVariables();
313
// if (reslist == null || reslist.size() < 2)
314
// return;
315
// }
316
arg.getExpression1().accept(this);
317         arg.getExpression2().accept(this);
318     }
319
320     // public void visit(ListOpEXCEPTExpression arg) throws XQueryException
321
// public void visit(ListOpINTERSECTExpression arg) throws XQueryException
322
// public void visit(ListOpUNIONExpression arg) throws XQueryException
323
public void visit(LocatedExpression arg) throws XQueryException {
324         if (arg.getExpression() instanceof Variable)
325             addVarPath((Variable) arg.getExpression(), arg);
326         for (int i = 0; i < arg.getSteps().size(); i++)
327             arg.getStepNum(i).accept(this);
328     }
329
330     // public void visit(NodeType arg) throws XQueryException
331
// public void visit(Parameter arg) throws XQueryException
332
// public void visit(PrimitiveFunctionCall arg) throws XQueryException
333
// public void visit (QName arg) throws XQueryException
334
// public void visit(QuantifiedExpression arg) throws XQueryException
335
// public void visit(RangeExpression arg) throws XQueryException
336
// public void visit(RootNodeFunctionCall arg) throws XQueryException
337
// public void visit(SortedExpression arg) throws XQueryException
338
public void visit(Step arg) throws XQueryException {
339         ArrayList JavaDoc predicates = arg.getPredicates();
340         if (predicates == null)
341             return;
342         for (int i = 0; i < predicates.size(); i++)
343              ((XQueryExpression) predicates.get(i)).accept(this);
344     }
345     // public void visit(TypeSwitchExpression arg) throws XQueryException
346
// public void visit(UnOpMinusExpression arg) throws XQueryException
347
// public void visit(Value arg) throws XQueryException
348
// public void visit(ValueBoolean arg) throws XQueryException
349
// public void visit(ValueFloat arg) throws XQueryException
350
// public void visit(ValueInteger arg) throws XQueryException
351
// public void visit(ValueString arg) throws XQueryException
352
// public void visit(ValueText arg) throws XQueryException
353
public void visit(Variable arg) throws XQueryException {
354         addVarPath(arg, arg);
355     }
356
357     public void visit(XQueryBinaryOperatorExpression arg) throws XQueryException {
358         arg.getExpression1().accept(this);
359         arg.getExpression2().accept(this);
360     }
361
362     // public void visit(XQueryBooleanOperatorExpression arg) throws XQueryException
363
// public void visit (XQueryExpression arg) throws XQueryException
364
public void visit(XQueryExpressionSequence arg) throws XQueryException {
365         ArrayList JavaDoc al = arg.getSubExpressions();
366         if (al != null)
367             for (int i = 0; i < al.size(); i++)
368                  ((XQueryExpression) al.get(i)).accept(this);
369     }
370
371     // public void visit(XQueryListBinaryOperatorExpression arg) throws XQueryException
372
// public void visit(XQueryListUnaryOperatorExpression arg) throws XQueryException
373

374     public void visit(XQueryUnaryOperatorExpression arg) throws XQueryException {
375         arg.getExpression().accept(this);
376     }
377 }
378
Popular Tags