KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > extractor > algebra > RemoveProjectReplaceVisitor


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.extractor.algebra;
24
25 import java.util.*;
26
27 import org.xquark.extractor.common.Debug;
28 import org.xquark.extractor.common.SqlWrapperException;
29 import org.xquark.extractor.xfunctions.AfCast;
30 import org.xquark.extractor.xfunctions.XFunction;
31 import org.xquark.extractor.xfunctions.XfCast;
32
33 public final class RemoveProjectReplaceVisitor extends DefaultAlgebraVisitor {
34     private static final String JavaDoc RCSRevision = "$Revision: 1.4 $";
35     private static final String JavaDoc RCSName = "$Name: $";
36
37     public RemoveProjectReplaceVisitor() {
38     }
39
40     public RemoveProjectReplaceVisitor(Map map) {
41         setMap(map);
42     }
43
44     private Map _map;
45
46     /**
47     Access method for the _map property.
48     
49     @return the current value of the _map property
50      */

51     public Map getMap() {
52         return _map;
53     }
54
55     /**
56     Sets the value of the _map property.
57     
58     @param aMapping the new value of the _map property
59      */

60     public void setMap(Map aMap) {
61         _map = aMap;
62     }
63
64     public void updateTableInstance(RenameRelation tableInstance) {
65         //Trace.enter(this, "updateTableInstance", tableInstance);
66

67         HashMap newMap = new HashMap();
68         Set keySet = _map.keySet();
69         Iterator iter = keySet.iterator();
70         AttributeExpression item = null;
71         Object JavaDoc value = null;
72         while (iter.hasNext()) {
73             item = (AttributeExpression) iter.next();
74             value = _map.get(item);
75
76             item.setTableInstance(tableInstance);
77             newMap.put(item, value);
78         }
79         setMap(newMap);
80
81         //Trace.exit(this, "updateTableInstance", tableInstance);
82
}
83
84     public void visit(Expression arg) throws SqlWrapperException {
85         //Trace.enter(this,"visit(Expression arg)", arg);
86
Debug.assertTrue(false, "NYI!! for " + arg.pprint());
87         //Trace.exit(this,"visit(Expression arg)", arg);
88
}
89
90     public void visit(Attribute arg) throws SqlWrapperException {
91         //Trace.enter(this,"visit(Attribute arg)", arg);
92
Debug.assertTrue(false, "exceptional case:" + arg.toString());
93         //Trace.exit(this,"visit(Attribute arg)", arg);
94
}
95
96     public void visit(AttributeExpression arg) throws SqlWrapperException {
97         //Trace.enter(this,"visit(AttributeExpression arg)", arg);
98

99         Expression replacer = (Expression) _map.get(arg);
100         if (null != replacer) {
101             arg.getFather().replaceChild(arg, replacer);
102             //Trace.message(this,"visit(AttributeExpression arg)",arg.pprint()+" replaced by " + replacer.pprint());
103
} else {
104             Object JavaDoc tableInstance = _map.get(arg.getTableInstance());
105             if (null == tableInstance) {
106             } else if (tableInstance instanceof NullPointer) {
107                 arg.setTableInstance(null);
108             } else {
109                 arg.setTableInstance((RenameRelation) tableInstance);
110             }
111         }
112
113         replacer = (Expression) _map.get(arg.getUnderlyinExpr());
114         if (null != replacer) {
115             arg.getFather().replaceChild(arg, replacer);
116             //Trace.message(this,"visit(AttributeExpression arg)",arg.pprint()+" replaced by " + replacer.pprint());
117
}
118
119         //Trace.exit(this,"visit(AttributeExpression arg)", arg);
120
}
121
122     // public void visit(BinaryOperator arg) throws SqlWrapperException {
123
// Trace.enter(this,"visit(BinaryOperator arg)", arg);
124
// Trace.exit(this,"visit(BinaryOperator arg)", arg);
125
// }
126

127     public void visit(BinaryAtomicOp arg) throws SqlWrapperException {
128         //Trace.enter(this,"visit(BinaryAtomicOp arg)", arg);
129
arg.getLeftOperand().accept(this);
130         arg.getRightOperand().accept(this);
131         //Trace.exit(this,"visit(BinaryAtomicOp arg)", arg);
132
}
133
134     public void visit(BinaryAlgebra arg) throws SqlWrapperException {
135         //Trace.enter(this,"visit(BinaryAlgebra arg)", arg);
136
/* implemention for Merg, Union, Difference and except, but not for BinOpOuterJoin */
137         arg.getLeftOperand().accept(this);
138         arg.getRightOperand().accept(this);
139         //Trace.exit(this,"visit(BinaryAlgebra arg)", arg);
140
}
141
142     public void visit(BinOpOuterJoin arg) throws SqlWrapperException {
143         //Trace.enter(this,"visit(BinOpOuterJoin arg)", arg);
144

145         List list = arg.getPredicateList();
146         Debug.assertTrue(null != list, "null!=list");
147         Expression expr = null;
148         for (int i = 0; i < list.size(); i++) {
149             expr = (Expression) list.get(i);
150             expr.accept(this);
151         }
152
153         arg.getLeftOperand().accept(this);
154         arg.getRightOperand().accept(this);
155
156         //Trace.exit(this,"visit(BinOpOuterJoin arg)", arg);
157
}
158
159     public void visit(FunAggregate arg) throws SqlWrapperException {
160         //Trace.enter(this,"visit(FunAggregate arg)", arg);
161
arg.getOperand().accept(this);
162         //Trace.exit(this,"visit(FunAggregate arg)", arg);
163
}
164
165     // public void visit(IfThenElse arg) throws SqlWrapperException {
166
// Trace.enter(this,"visit(IfThenElse arg)", arg);
167
// Trace.exit(this,"visit(IfThenElse arg)", arg);
168
// }
169

170     public void visit(Join arg) throws SqlWrapperException {
171         //Trace.enter(this,"visit(Join arg)", arg);
172

173         List list = arg.getPredicateList();
174         Debug.assertTrue(null != list, "null!=list");
175         Expression expr = null;
176         for (int i = 0; i < list.size(); i++) {
177             expr = (Expression) list.get(i);
178             expr.accept(this);
179         }
180
181         list = arg.getOperands();
182         Debug.assertTrue(null != list, "null!=list");
183         expr = null;
184         for (int i = 0; i < list.size(); i++) {
185             expr = (Expression) list.get(i);
186             expr.accept(this);
187         }
188
189         //Trace.exit(this,"visit(Join arg)", arg);
190
}
191
192     public void visit(Literal arg) throws SqlWrapperException {
193         //Trace.enter(this,"visit(Literal arg)", arg);
194
//Trace.exit(this,"visit(Literal arg)", arg);
195
}
196
197     public void visit(RenameRelation arg) throws SqlWrapperException {
198         //Trace.enter(this,"visit(RenameRelation arg)", arg);
199
arg.getOperand().accept(this);
200         //Trace.exit(this,"visit(RenameRelation arg)", arg);
201
}
202
203     public void visit(Table arg) throws SqlWrapperException {
204         //Trace.enter(this,"visit(Table arg)", arg);
205
//Trace.exit(this,"visit(Table arg)", arg);
206
}
207
208     // public void visit(UnaryOperator arg) throws SqlWrapperException {
209
// Trace.enter(this,"visit(UnaryOperator arg)", arg);
210
// arg.getOperand().accept(this);
211
// Trace.exit(this,"visit(UnaryOperator arg)", arg);
212
// }
213

214     // public void visit(UnaryAlgebra arg) throws SqlWrapperException {
215
// Trace.enter(this,"visit(UnaryAlgebra arg)", arg);
216
// Trace.exit(this,"visit(UnaryAlgebra arg)", arg);
217
// }
218

219     public void visit(UnaryAtomicOp arg) throws SqlWrapperException {
220         //Trace.enter(this,"visit(UnaryAtomicOp arg)", arg);
221
arg.getOperand().accept(this);
222         //Trace.exit(this,"visit(UnaryAtomicOp arg)", arg);
223
}
224
225     // public void visit(UnOpAggregate arg) throws SqlWrapperException {
226
// Trace.enter(this,"visit(UnaryAtomicOp arg)", arg);
227
// Trace.exit(this,"visit(UnaryAtomicOp arg)", arg);
228
// }
229

230     // public void visit(UnOpExists arg) throws SqlWrapperException {
231
// Trace.enter(this,"visit(UnOpExists arg)", arg);
232
// arg.getOperand().accept(this);
233
// Trace.exit(this,"visit(UnOpExists arg)", arg);
234
// }
235

236     // public void visit(UnOpGroup arg) throws SqlWrapperException {
237
// Trace.enter(this,"visit(UnOpGroup arg)", arg);
238
// Trace.exit(this,"visit(UnOpGroup arg)", arg);
239
// }
240

241     // public void visit(UnOpMinus arg) throws SqlWrapperException {
242
// Trace.enter(this,"visit(UnOpMinus arg)", arg);
243
// Trace.exit(this,"visit(UnOpMinus arg)", arg);
244
// }
245

246     // public void visit(UnOpNot arg) throws SqlWrapperException {
247
// Trace.enter(this,"visit(UnOpNot arg)", arg);
248
// Trace.exit(this,"visit(UnOpNot arg)", arg);
249
// }
250

251     public void visit(UnOpProject arg) throws SqlWrapperException {
252         //Trace.enter(this,"visit(UnOpProject arg)", arg);
253

254         List list = arg.getItemList();
255         Debug.assertTrue(null != list, "null!=list");
256         Expression expr = null;
257         for (int i = 0; i < list.size(); i++) {
258             expr = (Expression) list.get(i);
259             expr.accept(this);
260         }
261
262         arg.getOperand().accept(this);
263
264         //Trace.exit(this,"visit(UnOpProject arg)", arg);
265
}
266
267     public void visit(UnOpRestrict arg) throws SqlWrapperException {
268         //Trace.enter(this,"visit(UnOpRestrict arg)", arg);
269
List list = arg.getPredicateList();
270         Debug.assertTrue(null != list, "null!=list");
271         Expression expr = null;
272         for (int i = 0; i < list.size(); i++) {
273             expr = (Expression) list.get(i);
274             expr.accept(this);
275         }
276
277         arg.getOperand().accept(this);
278
279         //Trace.exit(this,"visit(UnOpRestrict arg)", arg);
280
}
281
282     public void visit(UnOpSort arg) throws SqlWrapperException {
283         //Trace.enter(this,"visit(UnOpSort arg)", arg);
284

285         List list = arg.getSortSpecificationList();
286         Debug.assertTrue(null != list, "null!=list");
287         SortSpecification spec = null;
288         Expression expr = null;
289         for (int i = 0; i < list.size(); i++) {
290             spec = (SortSpecification) list.get(i);
291             expr = spec.getSortExpression();
292             expr.accept(this);
293         }
294
295         arg.getOperand().accept(this);
296
297         //Trace.exit(this,"visit(UnOpSort arg)", arg);
298
}
299
300     public void visit(AfCast arg) throws SqlWrapperException {
301         //Trace.enter(this,"visit(AfCast arg)", arg);
302
arg.getExpression().accept(this);
303         //Trace.exit(this,"visit(AfCast arg)", arg);
304
}
305
306     public void visit(XfCast arg) throws SqlWrapperException {
307         //Trace.enter(this,"visit(XfCast arg)", arg);
308
arg.getExpression().accept(this);
309         //Trace.exit(this,"visit(XfCast arg)", arg);
310
}
311
312     public void visit(XFunction arg) throws SqlWrapperException {
313         //Trace.enter(this,"visit(XFunction arg)", arg);
314

315         if (arg.getArgumentNumber() > 0) {
316             List argList = arg.getArguments();
317             Object JavaDoc argument = null;
318             for (int i = 0; i < argList.size(); i++) {
319                 argument = argList.get(i);
320                 if (argument instanceof Expression) {
321                     ((Expression) argument).accept(this);
322                 }
323             }
324         }
325
326         //Trace.exit(this,"visit(XFunction arg)", arg);
327
}
328
329     public String JavaDoc printMap() {
330         //Trace.enter(this, "pprint()");
331

332         String JavaDoc newLine = System.getProperty("line.separator");
333         StringBuffer JavaDoc retVal = new StringBuffer JavaDoc();
334         retVal.append(newLine);
335         retVal.append("<================ RemoveProjectReplaceVisitor ===========>");
336         retVal.append(newLine);
337         Object JavaDoc keyList[] = _map.keySet().toArray();
338         Expression key = null;
339         Object JavaDoc value = null;
340
341         for (int i = 0; i < keyList.length; i++) {
342             key = (Expression) keyList[i];
343             retVal.append(Integer.toString(i));
344             retVal.append("\t");
345
346             retVal.append(key.pprint());
347             retVal.append("\t");
348
349             value = _map.get(key);
350             if (value instanceof Expression) {
351                 retVal.append(((Expression) value).pprint());
352             } else {
353                 retVal.append(value.toString());
354             }
355             retVal.append(newLine);
356         }
357
358         retVal.append("</================ RemoveProjectReplaceVisitor ===========>");
359
360         //Trace.exit(this, "pprint()");
361
return retVal.toString();
362     }
363
364 }
365
Popular Tags