KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
26
27 import org.xquark.extractor.xfunctions.XFunction;
28
29 /**
30  * This visitor changes the relation name in AttributeExpressions by the substitution
31  * one if it matches one of the provided relation names. It blocks on RenameRelation
32  * nodes since it is not necessary to rename references to table that can not be seen.
33  * This visitor may be used when using a temporary table to rename attribute references
34  * in query using the temporary table.
35  */

36 public class RenameAttributeExpressionVisitor extends DefaultCompleteVisitor {
37
38     private Collection JavaDoc _relations;
39     private RenameRelation _substitution;
40     private boolean _blockOnRenameItems = false;
41     private boolean _exclude = false;
42     
43     public RenameAttributeExpressionVisitor() {}
44     
45     public RenameAttributeExpressionVisitor(Collection JavaDoc renameRelations, RenameRelation substitution) {
46         reinit(renameRelations, substitution);
47     }
48     
49     public void reinit(Collection JavaDoc renameRelations, RenameRelation substitution,
50             boolean blockOnRenameItems, boolean exclude) {
51         _relations = renameRelations;
52         _substitution = substitution;
53         _blockOnRenameItems = blockOnRenameItems;
54         _exclude = exclude;
55     }
56     public void reinit(Collection JavaDoc renameRelations, RenameRelation substitution) {
57         reinit(renameRelations, substitution, false, false);
58     }
59     
60     public void reinit(RenameRelation substitution, boolean blockOnRenameItems) {
61         reinit(null, substitution, blockOnRenameItems, false);
62     }
63     
64     public void reinit(RenameRelation substitution) {
65         reinit(substitution, false);
66     }
67     
68     public Expression getVisitedExpression() {
69         return expr;
70     }
71     
72     public void visit(AttributeExpression arg) {
73         if (_relations == null || (_relations.contains(arg.getTableInstance()) == !_exclude))
74             arg.setTableInstance(_substitution);
75         expr = arg;
76     }
77     
78     public void visit(RenameItem arg) {
79         if (_blockOnRenameItems) {
80             AttributeExpression attExpr = new AttributeExpression(_substitution, arg.getName());
81             attExpr.setUnderlyingExpr(arg.getOperand());
82             arg._father.replaceChild(arg, attExpr);
83             expr = attExpr;
84         }
85         else {
86             super.visit((UnaryAtomicOp)arg);
87             expr = arg;
88         }
89     }
90     
91     public void visit(RenameRelation arg) { expr = arg;/* block */}
92
93     public void visit(BinaryAtomicOp arg){
94         super.visit(arg);
95         expr = arg;
96     }
97     
98     public void visit(UnaryAtomicOp arg) {
99         super.visit(arg);
100         expr = arg;
101     }
102     
103     public void visit(XFunction arg) {
104         super.visit(arg);
105         expr = arg;
106     }
107 }
108
Popular Tags