KickJava   Java API By Example, From Geeks To Geeks.

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


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.mediator.decomposer;
25
26 import java.util.ArrayList JavaDoc;
27
28 import org.xquark.mediator.algebra.AlgFLWR;
29 import org.xquark.mediator.algebra.Algebra;
30 import org.xquark.xquery.parser.*;
31
32 public class GetUsedVariablesVisitor extends DefaultParserVisitor {
33     
34     private static final String JavaDoc RCSRevision = "$Revision: 1.7 $";
35     private static final String JavaDoc RCSName = "Name";
36     
37     //private ArrayList wherevariables = null;
38
private ArrayList JavaDoc variables = null;
39     private ArrayList JavaDoc declaredvariables = null;
40     //private boolean stopWhenFound = false;
41
//private boolean hasFound = false;
42
//private boolean isflwr = false;
43

44     public GetUsedVariablesVisitor() {
45         variables = new ArrayList JavaDoc() ;
46         declaredvariables = new ArrayList JavaDoc() ;
47     }
48         
49     public ArrayList JavaDoc getVariables() { return variables; }
50     //public ArrayList getWhereVariables() { return wherevariables; }
51
//public boolean hasFound() { return hasFound; }
52

53 // public void reset(boolean isflwr) {
54
// this.isflwr = isflwr;
55
// if (variables != null) variables.clear();
56
// declaredvariables.clear();
57
// }
58
public void reset() {
59         if (variables != null) variables.clear();
60         declaredvariables.clear();
61     }
62     
63     // special
64

65     public void visit(Algebra arg) throws XQueryException {
66         arg.getExpression().accept(this);
67         ArrayList JavaDoc children = arg.getChildren();
68         if (children != null)
69             for (int i=0;i<children.size();i++) {
70                 //if (stopWhenFound && hasFound) return;
71
visit((Algebra)children.get(i));
72             }
73         if (arg.isQDB()) {
74             AlgFLWR tmpdepmem = (AlgFLWR)arg;
75             if (tmpdepmem.getDependencyList() != null)
76                 for (int i=0;i<tmpdepmem.getDependencyList().size();i++) {
77                     //if (stopWhenFound && hasFound) return;
78
visit((AlgFLWR)tmpdepmem.getDependencyList().get(i));
79                 }
80         }
81     }
82
83     // public void visit(AggregateFunctionCall arg) throws XQueryException
84
// public void visit(AttributeValuePair arg) throws XQueryException
85
// public void visit(BinOpANDExpression arg) throws XQueryException
86
// public void visit(BinOpORExpression arg) throws XQueryException
87
// public void visit(CastTreatExpression arg) throws XQueryException
88
// public void visit(ContextDeclaration arg) throws XQueryException
89
// public void visit(Dereference arg) throws XQueryException
90
public void visit(Element arg) throws XQueryException {
91         //if (stopWhenFound && hasFound) return;
92
if (arg.getStartTag() != null) {
93             arg.getStartTag().accept(this);
94         }
95         if (arg.getAttributes() != null) {
96             for (int i = 0; i < arg.getAttributes().size(); i++) {
97                 //if (stopWhenFound && hasFound) return;
98
((AttributeValuePair)arg.getAttributes().get(i)).accept(this);
99             }
100         }
101         if (arg.getSubExpressions() != null) {
102             for (int j = 0; j < arg.getSubExpressions().size(); j++) {
103                 //if (stopWhenFound && hasFound) return;
104
((XQueryExpression)arg.getSubExpressions().get(j)).accept(this);
105             }
106         }
107         //if (stopWhenFound && hasFound) return;
108
}
109     
110     public void visit(FLWRExpression arg) throws XQueryException {
111         for (int i = 0; i < arg.getVariables().size(); i++) {
112             //if (stopWhenFound && hasFound) return;
113
Variable vari = (Variable)arg.getVariables().get(i);
114             declaredvariables.add(vari);
115             vari.getExpression().accept(this);
116         }
117         //if (stopWhenFound && hasFound) return;
118
if (arg.getWhereClause() != null)
119 // if (isflwr) {
120
// GetUsedVariablesVisitor visitor = new GetUsedVariablesVisitor();
121
// visitor.reset();
122
// arg.getWhereClause().accept(visitor);
123
// wherevariables = visitor.getVariables();
124
// }
125
// else
126
arg.getWhereClause().accept(this);
127         //arg.getReturnClause().accept(this);
128
}
129     
130     public void visit(FunctionCall arg) throws XQueryException {
131         if (arg.getArguments() != null) {
132             for (int i = 0; i < arg.getArguments().size(); i++) {
133                 //if (stopWhenFound && hasFound) return;
134
((XQueryExpression)arg.getArguments().get(i)).accept(this);
135             }
136         }
137     }
138     
139     // public void visit(FunctionDefinition arg) throws XQueryException
140
// public void visit(InstanceOfExpression arg) throws XQueryException
141
// public void visit(InternalFunctionCall arg) throws XQueryException
142

143     public void visit(ITEExpression arg) throws XQueryException {
144         //if (stopWhenFound && hasFound) return;
145
arg.getIfExpression().accept(this);
146         //if (stopWhenFound && hasFound) return;
147
arg.getElseExpression().accept(this);
148         //if (stopWhenFound && hasFound) return;
149
arg.getThenExpression().accept(this);
150     }
151     
152     // public void visit(LibraryFunctionCall arg) throws XQueryException
153
// public void visit(ListOpAFTERExpression arg) throws XQueryException
154
// public void visit(ListOpArithExpression arg) throws XQueryException
155
// public void visit(ListOpBEFOREExpression arg) throws XQueryException
156
// public void visit(ListOpCompExpression arg) throws XQueryException
157
// public void visit(ListOpEXCEPTExpression arg) throws XQueryException
158
// public void visit(ListOpINTERSECTExpression arg) throws XQueryException
159
// public void visit(ListOpUNIONExpression arg) throws XQueryException
160
public void visit(LocatedExpression arg) throws XQueryException {
161         for (int i = 0; i < arg.getSteps().size(); i++) {
162             //if (stopWhenFound && hasFound) return;
163
((Step)arg.getSteps().get(i)).accept(this);
164         }
165     }
166     
167     // public void visit(NodeType arg) throws XQueryException
168
// public void visit(Parameter arg) throws XQueryException
169
// public void visit(PrimitiveFunctionCall arg) throws XQueryException
170
// public void visit(QName arg) throws XQueryException
171
public void visit(QuantifiedExpression arg) throws XQueryException {
172         /*
173         declaredvariables.add(arg.getVariable());
174         arg.getVariable().getExpression().accept(this);
175         arg.getConstraintExpresson().accept(this);
176         */

177     }
178     // public void visit(RangeExpression arg) throws XQueryException
179
// public void visit(RootNodeFunctionCall arg) throws XQueryException
180
// public void visit(SortedExpression arg) throws XQueryException
181
// public void visit(Step arg) throws XQueryException
182
// public void visit(TypeSwitchExpression arg) throws XQueryException
183
// public void visit(UnOpMinusExpression arg) throws XQueryException
184
// public void visit(Value arg) throws XQueryException
185
// public void visit(ValueBoolean arg) throws XQueryException
186
// public void visit(ValueFloat arg) throws XQueryException
187
// public void visit(ValueInteger arg) throws XQueryException
188
// public void visit(ValueString arg) throws XQueryException
189
public void visit(Variable arg) throws XQueryException {
190         if (!declaredvariables.contains(arg)) {
191             //if (stopWhenFound) hasFound = true;
192
if (!variables.contains(arg)) variables.add(arg);
193         }
194     }
195     
196     public void visit(XQueryBinaryOperatorExpression arg) throws XQueryException {
197         if (arg.getExpression1() != null) {
198             //if (stopWhenFound && hasFound) return;
199
arg.getExpression1().accept(this);
200         }
201         if (arg.getExpression2() != null) {
202             //if (stopWhenFound && hasFound) return;
203
arg.getExpression2().accept(this);
204         }
205     }
206     
207     // public void visit(XQueryBooleanOperatorExpression arg) throws XQueryException
208

209     //public void visit(XQueryExpression arg) throws XQueryException {}
210

211     
212     public void visit(XQueryExpressionSequence arg) throws XQueryException {
213         for (int i = 0; i < arg.getSubExpressions().size(); i++) {
214             //if (stopWhenFound && hasFound) return;
215
((XQueryExpression)arg.getSubExpressions().get(i)).accept(this);
216         }
217     }
218     
219     // public void visit(XQueryListBinaryOperatorExpression arg) throws XQueryException
220
// public void visit(XQueryListUnaryOperatorExpression arg) throws XQueryException
221
public void visit(XQueryUnaryOperatorExpression arg) throws XQueryException {
222         if (arg.getExpression() != null) {
223             //if (stopWhenFound && hasFound) return;
224
arg.getExpression().accept(this);
225         }
226     }
227         
228     public void visit(XQueryModule arg) throws XQueryException {
229         for (int i = 0 ; i < arg.getExpressions().size() ; i ++) {
230             //if (stopWhenFound && hasFound) return;
231
XQueryExpression expressioni = (XQueryExpression) arg.getExpressions().get(i) ;
232             expressioni.accept(this) ;
233         }
234     }
235     
236     // public void visit(XQueryVoid arg) throws XQueryException;
237

238 }
239
Popular Tags