KickJava   Java API By Example, From Geeks To Geeks.

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


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.Map JavaDoc;
26
27 import org.xquark.extractor.common.SqlWrapperException;
28
29 /**
30  * This visitor browse an algebra tree to replace leaf expressions (not operators)
31  * by others following a provided map. Attribute expressions are also modified to
32  * point to new relations if their relations are changed.
33  */

34 public class TopDownReplacer extends DefaultCompleteVisitor {
35
36     private static final String JavaDoc RCSRevision = "$Revision: 1.4 $";
37     private static final String JavaDoc RCSName = "$Name: $";
38
39     Map JavaDoc _replaceMap = null;
40
41     public TopDownReplacer() {
42     }
43
44     public TopDownReplacer(Map JavaDoc replaceMap) {
45         _replaceMap = replaceMap;
46     }
47
48     public void setReplaceMap(Map JavaDoc replaceMap) {
49         _replaceMap = replaceMap;
50     }
51
52     public Map JavaDoc getReplaceMap() {
53         return _replaceMap;
54     }
55
56     public void visit(Expression arg) {
57         //Trace.enter(this, "visit(Expression arg)", arg);
58
Expression replacer = (Expression) _replaceMap.get(arg);
59         /* try to find replacer for this Expression */
60         if (null != replacer) {
61             arg.getFather().replaceChild(arg, replacer);
62             //Trace.message(this,"visit(AttributeExpression arg)", arg.pprint()+" replaced by " + replacer.pprint());
63
}
64         //Trace.exit(this, "visit(Expression arg)", arg);
65
}
66
67     public void visit(AttributeExpression arg) throws SqlWrapperException {
68         //Trace.enter(this,"visit(AttributeExpression arg)", arg);
69

70         Expression replacer = (Expression) _replaceMap.get(arg);
71         /* try to find replacer for this AttributeExpression */
72         if (null != replacer) {
73             arg.getFather().replaceChild(arg, replacer);
74             //Trace.message(this,"visit(AttributeExpression arg)", arg.pprint()+" replaced by " + replacer.pprint());
75
} else {
76             /* try to find replacer for the table instance of this AttributeExpression */
77             Object JavaDoc ti = arg.getTableInstance();
78             Object JavaDoc tableInstance = null;
79             if (null != ti) {
80                 tableInstance = _replaceMap.get(ti);
81             }
82
83             if (null == tableInstance) {
84             } else if (tableInstance instanceof NullPointer) {
85                 arg.setTableInstance(null);
86             } else {
87                 arg.setTableInstance((RenameRelation) tableInstance);
88             }
89         }
90         //Trace.exit(this,"visit(AttributeExpression arg)", arg);
91
}
92
93     public void visit(SortSpecification arg) throws SqlWrapperException {
94         //Trace.enter(this,"visit(SortSpecification arg)", arg);
95

96         AttributeExpression sortExpr = (AttributeExpression) arg.getSortExpression();
97         sortExpr.accept(this);
98         Expression uderlyingExpr = sortExpr.getUnderlyinExpr();
99         if (uderlyingExpr instanceof AttributeExpression) {
100             Expression replacer = (Expression) _replaceMap.get(uderlyingExpr);
101             /* try to find replacer for this uderlyingExpr */
102             if (null != replacer) {
103                 sortExpr.setUnderlyingExpr(replacer);
104             }
105         }
106         //Trace.exit(this,"visit(SortSpecification arg)", arg);
107
}
108 }
109
Popular Tags