KickJava   Java API By Example, From Geeks To Geeks.

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


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
27 import org.xquark.xquery.parser.*;
28
29 public class AddLoopInfoVisitor extends DefaultParserVisitor {
30     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
31     private static final String JavaDoc RCSName = "$Name: $";
32     
33     private ArrayList JavaDoc loopIDs = new ArrayList JavaDoc(1);
34     private String JavaDoc varliststring = null;
35     private int varlistsize = 0;
36     
37     public AddLoopInfoVisitor(ArrayList JavaDoc varlist) {
38         if (varlist != null && !varlist.isEmpty()) {
39             this.varlistsize = varlist.size();
40             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
41             for (int i=0;i<varlist.size();i++) buf.append(((Variable)varlist.get(i)).toString());
42             this.varliststring = buf.toString();
43         }
44     }
45     
46     private void addLoopIDs(ArrayList JavaDoc list) {
47         if (list == null) return;
48         if (list.isEmpty()) return;
49         for (int i=0;i<list.size();i++) {
50             Variable var = (Variable)list.get(i);
51             if (!(loopIDs.contains(var)))
52                 loopIDs.add(var);
53         }
54     }
55         
56     // public void visit(AggregateFunctionCall arg) throws XQueryException;
57
public void visit(AttributeValuePair arg) throws XQueryException {
58         XQueryExpression attValue = arg.getAttributeValue();
59         attValue.accept(this);
60     }
61     // public void visit(BinOpANDExpression arg) throws XQueryException;
62
// public void visit(BinOpORExpression arg) throws XQueryException;
63
// public void visit(CastTreatExpression arg) throws XQueryException;
64
// public void visit(ContextDeclaration arg) throws XQueryException;
65
// public void visit(Dereference arg) throws XQueryException;
66

67     public void visit(Element arg) throws XQueryException {
68         ArrayList JavaDoc prevLoopIDs = (ArrayList JavaDoc)loopIDs.clone();
69         addLoopIDs(arg.getDependIDs());
70         addLoopIDs(arg.getSkolemIDs());
71         ArrayList JavaDoc attributes = arg.getAttributes();
72         if (attributes != null)
73             for (int i=0;i<attributes.size();i++)
74                 ((AttributeValuePair)attributes.get(i)).accept(this);
75         ArrayList JavaDoc subExpressions = arg.getSubExpressions();
76         if (subExpressions != null)
77             for (int i=0;i<subExpressions.size();i++)
78                 ((XQueryExpression)subExpressions.get(i)).accept(this);
79         loopIDs = prevLoopIDs;
80     }
81     
82     // public void visit(FLWRExpression arg) throws XQueryException;
83
// public void visit(FunctionCall arg) throws XQueryException;
84
// public void visit(FunctionDefinition arg) throws XQueryException;
85
// public void visit(InstanceOfExpression arg) throws XQueryException;
86
// public void visit(InternalFunctionCall arg) throws XQueryException;
87
public void visit(ITEExpression arg) throws XQueryException {
88         ArrayList JavaDoc prevLoopIDs = (ArrayList JavaDoc)loopIDs.clone();
89         addLoopIDs(arg.getDependIDs());
90         addLoopIDs(arg.getSkolemIDs());
91         arg.getIfExpression().accept(this);
92         arg.getThenExpression().accept(this);
93         arg.getElseExpression().accept(this);
94         loopIDs = prevLoopIDs;
95     }
96     // public void visit(LibraryFunctionCall arg) throws XQueryException;
97
// public void visit(ListOpAFTERExpression arg) throws XQueryException;
98
// public void visit(ListOpArithExpression arg) throws XQueryException;
99
// public void visit(ListOpBEFOREExpression arg) throws XQueryException;
100
// public void visit(ListOpCompExpression arg) throws XQueryException;
101
// public void visit(ListOpEXCEPTExpression arg) throws XQueryException;
102
// public void visit(ListOpINTERSECTExpression arg) throws XQueryException;
103
// public void visit(ListOpUNIONExpression arg) throws XQueryException;
104

105     public void visit(LocatedExpression arg) throws XQueryException {
106         ArrayList JavaDoc prevLoopIDs = (ArrayList JavaDoc)loopIDs.clone();
107         addLoopIDs(arg.getDependIDs());
108         addLoopIDs(arg.getSkolemIDs());
109         if (loopIDs.isEmpty())arg.setLoopIDs(varliststring);
110         else {
111             if (loopIDs.size() > this.varlistsize) arg.setLoopIDs(null);
112             else {
113                 StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
114                 for (int i=0;i<loopIDs.size();i++) buf.append(((Variable)loopIDs.get(i)).toString());
115                 arg.setLoopIDs(buf.toString());
116             }
117         }
118         loopIDs = prevLoopIDs;
119         // for sub path list
120
ArrayList JavaDoc steps = arg.getSteps();
121         XQueryExpression tmpExpr = ((Step)steps.get(0)).getExpression();
122         if (tmpExpr instanceof Variable) {
123             ((Variable)tmpExpr).addSubPath(arg); // CHANGE 29/05/2002
124
}
125     }
126     
127     // public void visit(NodeType arg) throws XQueryException;
128
// public void visit(Parameter arg) throws XQueryException;
129
// public void visit(PrimitiveFunctionCall arg) throws XQueryException;
130
// public void visit(QName arg) throws XQueryException;
131
// public void visit(QuantifiedExpression arg) throws XQueryException;
132
// public void visit(RangeExpression arg) throws XQueryException;
133
// public void visit(RootNodeFunctionCall arg) throws XQueryException;
134
// public void visit(SortedExpression arg) throws XQueryException;
135
// public void visit(Step arg) throws XQueryException;
136
// public void visit(TypeSwitchExpression arg) throws XQueryException;
137
// public void visit(UnOpMinusExpression arg) throws XQueryException;
138
// public void visit(Value arg) throws XQueryException;
139
// public void visit(ValueBoolean arg) throws XQueryException;
140
// public void visit(ValueFloat arg) throws XQueryException;
141
// public void visit(ValueInteger arg) throws XQueryException;
142
// public void visit(ValueString arg) throws XQueryException;
143
// public void visit(ValueText arg) throws XQueryException;
144

145     public void visit(Variable arg) throws XQueryException {
146         ArrayList JavaDoc prevLoopIDs = (ArrayList JavaDoc)loopIDs.clone();
147         addLoopIDs(arg.getDependIDs());
148         addLoopIDs(arg.getSkolemIDs());
149         if (loopIDs.isEmpty()) arg.setLoopIDs(varliststring);
150         else {
151             if (loopIDs.size() > this.varlistsize) arg.setLoopIDs(null);
152             else {
153                 StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
154                 for (int i=0;i<loopIDs.size();i++) buf.append(((Variable)loopIDs.get(i)).toString());
155                 arg.setLoopIDs(buf.toString());
156             }
157         }
158         loopIDs = prevLoopIDs;
159         // for sub path list
160
arg.addSubPath(arg);
161     }
162     
163     // public void visit(XQueryBinaryOperatorExpression arg) throws XQueryException;
164
// public void visit(XQueryBooleanOperatorExpression arg) throws XQueryException;
165

166     public void visit(XQueryExpressionSequence arg) throws XQueryException {
167         ArrayList JavaDoc prevLoopIDs = (ArrayList JavaDoc)loopIDs.clone();
168         addLoopIDs(arg.getDependIDs());
169         addLoopIDs(arg.getSkolemIDs());
170         ArrayList JavaDoc subExpressions = arg.getSubExpressions();
171         if (subExpressions != null)
172             for (int i=0;i<subExpressions.size();i++)
173                 ((XQueryExpression)subExpressions.get(i)).accept(this);
174         loopIDs = prevLoopIDs;
175     }
176     
177     // public void visit(XQueryFile arg) throws XQueryException;
178
// public void visit(XQueryListBinaryOperatorExpression arg) throws XQueryException;
179
// public void visit(XQueryListUnaryOperatorExpression arg) throws XQueryException;
180
// public void visit(XQueryUnaryOperatorExpression arg) throws XQueryException;
181
// public void visit(XQueryUnit arg) throws XQueryException;
182
// public void visit(XQueryVoid arg) throws XQueryException;
183

184     // primitive functions
185

186 }
187
Popular Tags