KickJava   Java API By Example, From Geeks To Geeks.

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


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.normalize;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Set JavaDoc;
28
29 import org.apache.commons.collections.set.ListOrderedSet;
30 import org.xquark.xquery.parser.*;
31
32 public class GetBoundVariablesVisitor extends DefaultParserVisitor {
33
34     private static final String JavaDoc RCSRevision = "$Revision: 1.4 $";
35     private static final String JavaDoc RCSName = "$Name: $";
36
37     private Set JavaDoc variables = null;
38     private Set JavaDoc IDs = null;
39     private boolean doVariables = true;
40     private boolean doIDs = true;
41     // private boolean isQDB = false;
42

43     // public GetBoundVariablesVisitor() {
44
// variables = new ArrayList() ;
45
// IDs = new ArrayList() ;
46
// }
47

48     public GetBoundVariablesVisitor(boolean doVariables, boolean doIDs) {
49         this.doVariables = doVariables;
50         if (doVariables)
51             variables = ListOrderedSet.decorate(new HashSet JavaDoc());
52         this.doIDs = doIDs;
53         if (doIDs)
54             IDs = ListOrderedSet.decorate(new HashSet JavaDoc());
55     }
56
57     public ArrayList JavaDoc getVariables() {
58         ArrayList JavaDoc all = new ArrayList JavaDoc();
59         if (doVariables && variables != null)
60             all.addAll(variables);
61         if (doIDs && IDs != null)
62             all.addAll(IDs);
63         return all;
64     }
65
66     public ArrayList JavaDoc getAllVariables() {
67         ArrayList JavaDoc all = new ArrayList JavaDoc();
68         if (variables != null) all.addAll(variables);
69         if (IDs != null) all.addAll(IDs);
70         return all;
71     }
72
73     public ArrayList JavaDoc getOnlyVariables() {
74         ArrayList JavaDoc ret = new ArrayList JavaDoc(variables);
75         return ret;
76     }
77
78     public ArrayList JavaDoc getIDsVariables() {
79         ArrayList JavaDoc ret = new ArrayList JavaDoc(IDs);
80         return ret;
81     }
82
83     public void reset(boolean doIDs, boolean doVariables) {
84         this.doIDs = doIDs;
85         this.doVariables = doVariables;
86     }
87
88     public void reset() {
89         if (doVariables)
90             variables.clear();
91         if (doIDs)
92             IDs.clear();
93     }
94
95     // public void isQDB(boolean isQDB) {
96
// this.isQDB = isQDB;
97
// }
98

99     // public void visit(AggregateFunctionCall arg) throws XQueryException
100
// public void visit(AttributeValuePair arg) throws XQueryException
101
// public void visit(BinOpANDExpression arg) throws XQueryException
102
// public void visit(BinOpORExpression arg) throws XQueryException
103
// public void visit(CastTreatExpression arg) throws XQueryException
104
// public void visit(ContextDeclaration arg) throws XQueryException
105
// public void visit(Dereference arg) throws XQueryException
106
public void visit(Element arg) throws XQueryException {
107         if (doIDs) {
108             if (arg.getSkolemIDs() != null) IDs.addAll(arg.getSkolemIDs());
109             if (arg.getDependIDs() != null) IDs.addAll(arg.getDependIDs());
110         }
111         if (arg.getStartTag() != null) {
112             arg.getStartTag().accept(this);
113         }
114         if (arg.getAttributes() != null) {
115             for (int i = 0; i < arg.getAttributes().size(); i++) {
116                 ((AttributeValuePair) arg.getAttributes().get(i)).accept(this);
117             }
118         }
119         if (arg.getSubExpressions() != null) {
120             for (int j = 0; j < arg.getSubExpressions().size(); j++) {
121                 ((XQueryExpression) arg.getSubExpressions().get(j)).accept(this);
122             }
123         }
124     }
125
126     // public void visit(FLWRExpression arg) throws XQueryException {
127
// if (!isQDB) return;
128
// if (doIDs) {
129
// ArrayListUtil.add(IDs, arg.getSkolemIDs());
130
// ArrayListUtil.add(IDs, arg.getDependIDs());
131
// }
132
// for (int i=0;i<arg.getVariables().size();i++) {
133
// XQueryExpression expr = ((Variable)arg.getVariables().get(i)).getExpression();
134
// expr.accept(this);
135
// }
136
// if (arg.getOrderBy() != null) {
137
// for (int i=0;i<arg.getOrderBy().size();i++) {
138
// XQueryExpression expr = (XQueryExpression)arg.getOrderBy().get(i);
139
// if (expr instanceof Variable) {
140
// if (doVariables) ArrayListUtil.add(variables, (Variable)expr);
141
// } else if (expr instanceof LocatedExpression && ((LocatedExpression)expr).getExpression() instanceof Variable) {
142
// if (doVariables) ArrayListUtil.add(variables, (Variable)((LocatedExpression)expr).getExpression());
143
// } else
144
// expr.accept(this);
145
// }
146
// }
147
// XQueryExpression expr = arg.getReturnClause();
148
// expr.accept(this);
149
// }
150

151     public void visit(FunctionCall arg) throws XQueryException {
152         if (doIDs) {
153             if (arg.getSkolemIDs() != null) IDs.addAll(arg.getSkolemIDs());
154             if (arg.getDependIDs() != null) IDs.addAll(arg.getDependIDs());
155         }
156         if (arg.getArguments() != null) {
157             for (int i = 0; i < arg.getArguments().size(); i++) {
158                 ((XQueryExpression) arg.getArguments().get(i)).accept(this);
159             }
160         }
161     }
162
163     // public void visit(FunctionDefinition arg) throws XQueryException
164
// public void visit(InstanceOfExpression arg) throws XQueryException
165
// public void visit(InternalFunctionCall arg) throws XQueryException
166

167     public void visit(ITEExpression arg) throws XQueryException {
168         if (doIDs) {
169             if (arg.getSkolemIDs() != null) IDs.addAll(arg.getSkolemIDs());
170             if (arg.getDependIDs() != null) IDs.addAll(arg.getDependIDs());
171         }
172         arg.getIfExpression().accept(this);
173         arg.getElseExpression().accept(this);
174         arg.getThenExpression().accept(this);
175     }
176
177     // public void visit(LibraryFunctionCall arg) throws XQueryException
178
// public void visit(ListOpAFTERExpression arg) throws XQueryException
179
// public void visit(ListOpArithExpression arg) throws XQueryException
180
// public void visit(ListOpBEFOREExpression arg) throws XQueryException
181
// public void visit(ListOpCompExpression arg) throws XQueryException
182
// public void visit(ListOpEXCEPTExpression arg) throws XQueryException
183
// public void visit(ListOpINTERSECTExpression arg) throws XQueryException
184
// public void visit(ListOpUNIONExpression arg) throws XQueryException
185
public void visit(LocatedExpression arg) throws XQueryException {
186         if (doIDs) {
187             if (arg.getSkolemIDs() != null) IDs.addAll(arg.getSkolemIDs());
188             if (arg.getDependIDs() != null) IDs.addAll(arg.getDependIDs());
189         }
190         for (int i = 0; i < arg.getSteps().size(); i++) {
191             ((Step) arg.getSteps().get(i)).accept(this);
192         }
193     }
194
195     // public void visit(NodeType arg) throws XQueryException
196
// public void visit(Parameter arg) throws XQueryException
197
// public void visit(PrimitiveFunctionCall arg) throws XQueryException
198
// public void visit(QName arg) throws XQueryException
199
// public void visit(QuantifiedExpression arg) throws XQueryException
200
// public void visit(RangeExpression arg) throws XQueryException
201
// public void visit(RootNodeFunctionCall arg) throws XQueryException
202
// public void visit(SortedExpression arg) throws XQueryException
203
// public void visit(TypeSwitchExpression arg) throws XQueryException
204
// public void visit(UnOpMinusExpression arg) throws XQueryException
205
// public void visit(Value arg) throws XQueryException
206
// public void visit(ValueBoolean arg) throws XQueryException
207
// public void visit(ValueFloat arg) throws XQueryException
208
// public void visit(ValueInteger arg) throws XQueryException
209
// public void visit(ValueString arg) throws XQueryException
210
public void visit(Variable arg) throws XQueryException {
211         if (doIDs) {
212             if (arg.getSkolemIDs() != null) IDs.addAll(arg.getSkolemIDs());
213             if (arg.getDependIDs() != null) IDs.addAll(arg.getDependIDs());
214         }
215         //if (doVariables && !isQDB) ArrayListUtil.add(variables, arg);
216
if (doVariables)
217             variables.add(arg);
218     }
219
220     public void visit(XQueryBinaryOperatorExpression arg) throws XQueryException {
221         if (doIDs) {
222             if (arg.getSkolemIDs() != null) IDs.addAll(arg.getSkolemIDs());
223             if (arg.getDependIDs() != null) IDs.addAll(arg.getDependIDs());
224         }
225         if (arg.getExpression1() != null) {
226             arg.getExpression1().accept(this);
227         }
228         if (arg.getExpression2() != null) {
229             arg.getExpression2().accept(this);
230         }
231     }
232
233     // public void visit(XQueryBooleanOperatorExpression arg) throws XQueryException
234

235     public void visit(XQueryExpression arg) throws XQueryException {
236         if (doIDs) {
237             if (arg.getSkolemIDs() != null) IDs.addAll(arg.getSkolemIDs());
238             if (arg.getDependIDs() != null) IDs.addAll(arg.getDependIDs());
239         }
240     }
241
242     public void visit(XQueryExpressionSequence arg) throws XQueryException {
243         if (doIDs) {
244             if (arg.getSkolemIDs() != null) IDs.addAll(arg.getSkolemIDs());
245             if (arg.getDependIDs() != null) IDs.addAll(arg.getDependIDs());
246         }
247         for (int i = 0; i < arg.getSubExpressions().size(); i++) {
248             ((XQueryExpression) arg.getSubExpressions().get(i)).accept(this);
249         }
250     }
251
252     // public void visit(XQueryListBinaryOperatorExpression arg) throws XQueryException
253
// public void visit(XQueryListUnaryOperatorExpression arg) throws XQueryException
254
public void visit(XQueryUnaryOperatorExpression arg) throws XQueryException {
255         if (doIDs) {
256             if (arg.getSkolemIDs() != null) IDs.addAll(arg.getSkolemIDs());
257             if (arg.getDependIDs() != null) IDs.addAll(arg.getDependIDs());
258         }
259         if (arg.getExpression() != null) {
260             arg.getExpression().accept(this);
261         }
262     }
263
264     public void visit(XQueryModule arg) throws XQueryException {
265         for (int i = 0; i < arg.getExpressions().size(); i++) {
266             XQueryExpression expressioni = (XQueryExpression) arg.getExpressions().get(i);
267             expressioni.accept(this);
268         }
269     }
270
271     // public void visit(XQueryVoid arg) throws XQueryException;
272

273 }
274
Popular Tags