KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
27
28 import org.xquark.xpath.Axis;
29 import org.xquark.xquery.parser.*;
30 import org.xquark.xquery.typing.TypeVisitor;
31
32 public class SubstituteVisitor extends DefaultParserVisitor {
33     private static final String JavaDoc RCSRevision = "$Revision: 1.4 $";
34     private static final String JavaDoc RCSName = "$Name: $";
35     
36     private HashMap JavaDoc map = null;
37     private boolean inReturn = false;
38     private TypeVisitor typeVisitor = null;
39     
40     public SubstituteVisitor(HashMap JavaDoc map,TypeVisitor typeVisitor) {
41         setMap(map);
42         this.typeVisitor = typeVisitor;
43     }
44     
45     public void setMap(HashMap JavaDoc map) { this.map = map; }
46     
47     // public void visit(AggregateFunctionCall arg) throws XQueryException
48

49     public void visit(AttributeValuePair arg) throws XQueryException {
50         XQueryExpression expr = null;
51         XQueryExpression valueExpr = arg.getAttributeValue();
52         if ((expr = (XQueryExpression)map.get(valueExpr)) != null) {
53             ArrayList JavaDoc parenti = valueExpr.getParentExpression();
54             int indexi = parenti.indexOf(arg);
55             if (indexi != -1) parenti.remove(indexi);
56             try {
57                 XQueryExpression exprClone = (XQueryExpression)expr.clone();
58                 arg.setAttributeValue(exprClone);
59             }
60             catch (CloneNotSupportedException JavaDoc e) { throw new XQueryException("Could not clone expression : " + expr); }
61         }
62         else valueExpr.accept(this);
63      }
64     
65     // public void visit(BinOpANDExpression arg) throws XQueryException;
66
// public void visit(BinOpORExpression arg) throws XQueryException;
67
// public void visit(CastTreatExpression arg) throws XQueryException;
68
// public void visit(ContextDeclaration arg) throws XQueryException;
69
// public void visit(Dereference arg) throws XQueryException;
70

71     public void visit(Element arg) throws XQueryException {
72         XQueryExpression expr = null;
73         expr = arg.getStartTag();
74         if ((expr = (XQueryExpression)map.get(expr)) != null)
75             arg.setStartTag(expr);
76         else
77             arg.getStartTag().accept(this);
78         ArrayList JavaDoc attributes = arg.getAttributes();
79         if (attributes != null) {
80             for (int i=0;i<attributes.size();i++) {
81                 AttributeValuePair subi = (AttributeValuePair)attributes.get(i);
82                 subi.accept(this);
83             }
84         }
85         ArrayList JavaDoc subExpressions = arg.getSubExpressions();
86         if (subExpressions != null) {
87             for (int i=0;i<subExpressions.size();i++) {
88                 XQueryExpression subi = (XQueryExpression)subExpressions.get(i);
89                 if ((expr = (XQueryExpression)map.get(subi)) != null) {
90                      subExpressions.remove(i);
91                     ArrayList JavaDoc parenti = subi.getParentExpression();
92                     int indexi = parenti.indexOf(arg);
93                     if (indexi != -1) parenti.remove(indexi);
94                     try {
95                         XQueryExpression exprClone = (XQueryExpression)expr.clone();
96                         arg.addSubExpressions(i,exprClone);
97                     }
98                     catch (CloneNotSupportedException JavaDoc e) { throw new XQueryException("Could not clone expression : " + expr); }
99                 }
100                 else subi.accept(this);
101             }
102         }
103     }
104     
105     public void visit(FLWRExpression arg) throws XQueryException {
106         ArrayList JavaDoc variables = arg.getVariables();
107         XQueryExpression expr = null;
108         for (int i=0;i<variables.size();i++) {
109             Variable vari = (Variable)variables.get(i);
110             XQueryExpression variExpr = vari.getExpression();
111             if ((expr = (XQueryExpression)map.get(variExpr)) != null) {
112                 ArrayList JavaDoc parenti = variExpr.getParentExpression();
113                 int indexi = parenti.indexOf(arg);
114                 if (indexi != -1) parenti.remove(indexi);
115                 try {
116                     XQueryExpression exprClone = (XQueryExpression)expr.clone();
117                     vari.setExpression(exprClone, false);
118                 }
119                 catch (CloneNotSupportedException JavaDoc e) { throw new XQueryException("Could not clone expression : " + expr); }
120             }
121             else variExpr.accept(this);
122         }
123         ArrayList JavaDoc orderbys = arg.getOrderBy();
124         if (orderbys != null) {
125             for (int i=0;i<orderbys.size();i++) {
126                 XQueryExpression expri = (XQueryExpression)orderbys.get(i);
127                if ((expr = (XQueryExpression)map.get(expri)) != null) {
128                    ArrayList JavaDoc parenti = expri.getParentExpression();
129                    int indexi = parenti.indexOf(arg);
130                    if (indexi != -1) parenti.remove(indexi);
131                    try {
132                        XQueryExpression exprClone = (XQueryExpression)expr.clone();
133                        orderbys.set(i,exprClone);
134                    }
135                    catch (CloneNotSupportedException JavaDoc e) { throw new XQueryException("Could not clone expression : " + expr); }
136                }
137                else expri.accept(this);
138            }
139         }
140         XQueryExpression whereClause = arg.getWhereClause();
141         if (whereClause != null){
142             if ((expr = (XQueryExpression)map.get(whereClause)) != null) {
143                 try {
144                     XQueryExpression exprClone = (XQueryExpression)expr.clone();
145                     arg.setWhereClause(exprClone);
146                 }
147                 catch (CloneNotSupportedException JavaDoc e) { throw new XQueryException("Could not clone expression : " + expr); }
148              }
149             else whereClause.accept(this);
150         }
151         XQueryExpression returnClause = arg.getReturnClause();
152         if ((expr = (XQueryExpression)map.get(returnClause)) != null) {
153             try {
154                 XQueryExpression exprClone = (XQueryExpression)expr.clone();
155                 arg.setReturnClause(exprClone);
156             }
157             catch (CloneNotSupportedException JavaDoc e) { throw new XQueryException("Could not clone expression : " + expr); }
158         }
159         else returnClause.accept(this);
160     }
161     
162     public void visit(FunctionCall arg) throws XQueryException {
163         ArrayList JavaDoc arguments = arg.getArguments();
164         if (arguments == null) return;
165         XQueryExpression expr = null;
166         for (int i=0;i<arguments.size();i++) {
167             XQueryExpression argi = (XQueryExpression)arguments.get(i);
168             if ((expr = (XQueryExpression)map.get(argi)) != null) {
169                 arguments.remove(i);
170                 ArrayList JavaDoc parenti = argi.getParentExpression();
171                 int indexi = parenti.indexOf(arg);
172                 if (indexi != -1) parenti.remove(indexi);
173                 try {
174                     XQueryExpression exprClone = (XQueryExpression)expr.clone();
175                     arg.addArguments(i,exprClone);
176                 }
177                 catch (CloneNotSupportedException JavaDoc e) { throw new XQueryException("Could not clone expression : " + expr); }
178             }
179             else argi.accept(this);
180         }
181     }
182     // public void visit(FunctionDefinition arg) throws XQueryException;
183
// public void visit(InstanceOfExpression arg) throws XQueryException;
184
// public void visit(InternalFunctionCall arg) throws XQueryException;
185
// public void visit(ITEExpression arg) throws XQueryException;
186
// public void visit(LibraryFunctionCall arg) throws XQueryException;
187
// public void visit(ListOpAFTERExpression arg) throws XQueryException;
188
// public void visit(ListOpArithExpression arg) throws XQueryException;
189
// public void visit(ListOpBEFOREExpression arg) throws XQueryException;
190
// public void visit(ListOpCompExpression arg) throws XQueryException;
191
// public void visit(ListOpEXCEPTExpression arg) throws XQueryException;
192
// public void visit(ListOpINTERSECTExpression arg) throws XQueryException;
193
// public void visit(ListOpUNIONExpression arg) throws XQueryException;
194
public void visit(LocatedExpression arg) throws XQueryException {
195         ArrayList JavaDoc steps = arg.getSteps();
196         XQueryExpression expr = null;
197         boolean change = false;
198         for (int i=0;i<steps.size();i++) {
199             Step stepi = (Step)steps.get(i);
200             XQueryExpression stepExpr = stepi.getExpression();
201             if ((expr = (XQueryExpression)map.get(stepExpr)) != null) {
202                 // if (expr instanceof LocatedExpression) {
203
steps.remove(i);
204                 if (stepi.getPredicates() != null) {
205                     ArrayList JavaDoc addSteps = expr.getSteps();
206                     Step lastStep = (Step)addSteps.get(addSteps.size()-1);
207                     lastStep.addPredicates(stepi.getPredicates());
208                 }
209                 if (expr instanceof LocatedExpression) {
210                     try {
211                         LocatedExpression locClone = (LocatedExpression)expr.clone();
212                         arg.addStepsAt(i,locClone.getSteps());
213                     }
214                     catch (CloneNotSupportedException JavaDoc e) { throw new XQueryException("Could not clone expression : " + expr); }
215                 } else {
216                     try {
217                         ArrayList JavaDoc list = new ArrayList JavaDoc(1);
218                         list.add(new Step(false,Axis.NONE,(XQueryExpression)expr.clone(),null,arg.getParentModule()));
219                         arg.addStepsAt(i,list);
220                     }
221                     catch (CloneNotSupportedException JavaDoc e) { throw new XQueryException("Could not clone expression : " + expr); }
222                 }
223                 change = true;
224                 
225                 /*
226                 }
227                 else if (expr instanceof FLWRExpression) {
228                     if (((FLWRExpression)expr).getReturnClause() instanceof LocatedExpression) {
229                         typeVisitor.setForce(true);
230                         LocatedExpression tmpLoc = (LocatedExpression)((FLWRExpression)expr).getReturnClause();
231                         steps.remove(i);
232                         if (stepi.getPredicates() != null) {
233                                 ArrayList addSteps = tmpLoc.getSteps();
234                                 Step lastStep = (Step)addSteps.get(addSteps.size()-1);
235                                 lastStep.addPredicates(stepi.getPredicates());
236                         }
237                         tmpLoc.addSteps(steps);
238                         tmpLoc.accept(typeVisitor);
239                         HashMap tmpMap = new HashMap(1);
240                         tmpMap.put(arg,expr);
241                         for (int j=0;j<arg.getParentExpression().size();j++) ((XQueryExpression)arg.getParentExpression().get(j)).substitute(tmpMap,typeVisitor);
242                         typeVisitor.setForce(false);
243                         return;
244                     }
245                     else if (((FLWRExpression)expr).getReturnClause() instanceof Variable) {
246                         typeVisitor.setForce(true);
247                         Variable tmpVar = (Variable)((FLWRExpression)expr).getReturnClause();
248                         Step tmpStep = new Step(-1,-1,tmpVar,null,typeVisitor);
249                         ArrayList tmpSteps = new ArrayList(1);
250                         tmpSteps.add(tmpStep);
251                         steps.remove(i);
252                         LocatedExpression tmpLoc = null;
253                         if (stepi.getPredicates() != null) {
254                                 Step tmpPredStep = new Step(-1,-1,tmpVar,stepi.getPredicates(),typeVisitor);
255                                 ArrayList tmpPredSteps = new ArrayList(1);
256                                 tmpPredSteps.add(tmpPredStep);
257                                 tmpLoc = new LocatedExpression(tmpPredSteps,typeVisitor);
258                                 ((FLWRExpression)expr).addWhereClause(tmpLoc);
259                         }
260                         tmpLoc = new LocatedExpression(tmpSteps,typeVisitor);
261                         tmpLoc.addSteps(steps);
262                         tmpLoc.accept(typeVisitor);
263                         ((FLWRExpression)expr).setReturnClause(tmpLoc);
264                         ((FLWRExpression)expr).accept(typeVisitor);
265                         HashMap tmpMap = new HashMap(1);
266                         arg.getSteps().add(i,stepi);
267                         tmpMap.put(arg,expr);
268                         for (int j=0;j<arg.getParentExpression().size();j++) ((XQueryExpression)arg.getParentExpression().get(j)).substitute(tmpMap,typeVisitor);
269                         typeVisitor.setForce(false);
270                         return;
271                     }
272                 }
273                 else {
274                         throw new Exception("Impossible substitution : " + stepExpr + " ( -> " + expr + " ) in " + arg + " , please rewrite your request");
275                 }
276                  */

277             }
278             stepi.accept(this);
279         }
280         if (change) arg.accept(this.typeVisitor);
281     }
282     // public void visit(NodeType arg) throws XQueryException;
283
// public void visit(Parameter arg) throws XQueryException;
284
// public void visit(PrimitiveFunctionCall arg) throws XQueryException;
285
// public void visit(QName arg) throws XQueryException;
286
public void visit(QuantifiedExpression arg) throws XQueryException {
287         ArrayList JavaDoc variables = arg.getVariables();
288         XQueryExpression expr = null;
289         for (int i=0;i<variables.size();i++) {
290             Variable vari = (Variable)variables.get(i);
291             XQueryExpression variExpr = vari.getExpression();
292             if ((expr = (XQueryExpression)map.get(variExpr)) != null) {
293                 ArrayList JavaDoc parenti = variExpr.getParentExpression();
294                 int indexi = parenti.indexOf(arg);
295                 if (indexi != -1) parenti.remove(indexi);
296                 try {
297                     XQueryExpression exprClone = (XQueryExpression)expr.clone();
298                     vari.setExpression(exprClone, false);
299                 }
300                 catch (CloneNotSupportedException JavaDoc e) { throw new XQueryException("Could not clone expression : " + expr); }
301             }
302             else variExpr.accept(this);
303         }
304         XQueryExpression tmpExpr = arg.getExpression();
305         if ((expr = (XQueryExpression)map.get(tmpExpr)) != null) {
306             try {
307                 XQueryExpression exprClone = (XQueryExpression)expr.clone();
308                 arg.setExpression(exprClone);
309             }
310             catch (CloneNotSupportedException JavaDoc e) { throw new XQueryException("Could not clone expression : " + expr); }
311         }
312         else tmpExpr.accept(this);
313     }
314     // public void visit(RangeExpression arg) throws XQueryException;
315
// public void visit(RootNodeFunctionCall arg) throws XQueryException;
316
// public void visit(SortedExpression arg) throws XQueryException;
317

318     public void visit(Step arg) throws XQueryException {
319         XQueryExpression stepExpr = arg.getExpression();
320         XQueryExpression expr = null;
321         if ((expr = (XQueryExpression)map.get(stepExpr)) != null && !(expr instanceof LocatedExpression)) {
322             ArrayList JavaDoc parenti = stepExpr.getParentExpression();
323             int indexi = parenti.indexOf(arg);
324             if (indexi != -1) parenti.remove(indexi);
325             try {
326                 XQueryExpression exprClone = (XQueryExpression)expr.clone();
327                 arg.setExpression(exprClone);
328             }
329             catch (CloneNotSupportedException JavaDoc e) { throw new XQueryException("Could not clone expression : " + expr); }
330         }
331         else {
332             stepExpr.accept(this);
333             ArrayList JavaDoc predicates = arg.getPredicates();
334             expr = null;
335             if (predicates != null)
336                 for (int i=0;i<predicates.size();i++) {
337                     XQueryExpression subi = (XQueryExpression)predicates.get(i);
338                     if ((expr = (XQueryExpression)map.get(subi)) != null) {
339                         predicates.remove(i);
340                         ArrayList JavaDoc parenti = subi.getParentExpression();
341                         int indexi = parenti.indexOf(arg);
342                         if (indexi != -1) parenti.remove(indexi);
343                         try {
344                             XQueryExpression exprClone = (XQueryExpression)expr.clone();
345                             arg.addPredicate(i,exprClone);
346                         }
347                         catch (CloneNotSupportedException JavaDoc e) { throw new XQueryException("Could not clone expression : " + expr); }
348                     }
349                     else subi.accept(this);
350                 }
351         }
352     }
353     
354     // public void visit(TypeSwitchExpression arg) throws XQueryException;
355
// public void visit(UnOpMinusExpression arg) throws XQueryException;
356
// public void visit(Value arg) throws XQueryException;
357
// public void visit(ValueBoolean arg) throws XQueryException;
358
// public void visit(ValueFloat arg) throws XQueryException;
359
// public void visit(ValueInteger arg) throws XQueryException;
360
// public void visit(ValueString arg) throws XQueryException;
361
// public void visit(ValueText arg) throws XQueryException;
362

363     public void visit(Variable arg) throws XQueryException {
364         XQueryExpression expr = null;
365         XQueryExpression varExpr = arg.getExpression();
366         if (varExpr == null) return;
367         if ((expr = (XQueryExpression)map.get(varExpr)) != null) {
368             try {
369                 XQueryExpression exprClone = (XQueryExpression)expr.clone();
370                 arg.setExpression(exprClone, false);
371             }
372             catch (CloneNotSupportedException JavaDoc e) { throw new XQueryException("Could not clone expression : " + expr); }
373         }
374         // else varExpr.accept(this);
375
}
376     
377     public void visit(XQueryBinaryOperatorExpression arg) throws XQueryException {
378         XQueryExpression expr = null;
379         XQueryExpression tmpExpr = arg.getExpression1();
380         if ((expr = (XQueryExpression)map.get(tmpExpr)) != null) {
381             try {
382                 XQueryExpression exprClone = (XQueryExpression)expr.clone();
383                 arg.setExpression1(exprClone);
384             }
385             catch (CloneNotSupportedException JavaDoc e) { throw new XQueryException("Could not clone expression : " + expr); }
386         }
387         else tmpExpr.accept(this);
388         tmpExpr = arg.getExpression2();
389         if ((expr = (XQueryExpression)map.get(tmpExpr)) != null) {
390             try {
391                 XQueryExpression exprClone = (XQueryExpression)expr.clone();
392                 arg.setExpression2(exprClone);
393             }
394             catch (CloneNotSupportedException JavaDoc e) { throw new XQueryException("Could not clone expression : " + expr); }
395         }
396         else tmpExpr.accept(this);
397     }
398     
399     // public void visit(XQueryBooleanOperatorExpression arg) throws XQueryException;
400
public void visit(XQueryExpression arg) throws XQueryException {
401     }
402     
403     public void visit(XQueryExpressionSequence arg) throws XQueryException {
404         ArrayList JavaDoc subExpressions = arg.getSubExpressions();
405         XQueryExpression expr = null;
406         for (int i=0;i<subExpressions.size();i++) {
407             XQueryExpression subi = (XQueryExpression)subExpressions.get(i);
408             if ((expr = (XQueryExpression)map.get(subi)) != null) {
409                 subExpressions.remove(i);
410                 ArrayList JavaDoc parenti = subi.getParentExpression();
411                 int indexi = parenti.indexOf(arg);
412                 if (indexi != -1) parenti.remove(indexi);
413                 try {
414                     XQueryExpression exprClone = (XQueryExpression)expr.clone();
415                     arg.addSubExpressions(i,exprClone);
416                 }
417                 catch (CloneNotSupportedException JavaDoc e) { throw new XQueryException("Could not clone expression : " + expr); }
418             }
419             else subi.accept(this);
420         }
421     }
422     
423     // public void visit(XQueryFile arg) throws XQueryException;
424
// public void visit(XQueryListBinaryOperatorExpression arg) throws XQueryException;
425
// public void visit(XQueryListUnaryOperatorExpression arg) throws XQueryException;
426

427     public void visit(XQueryUnaryOperatorExpression arg) throws XQueryException {
428         XQueryExpression expr = null;
429         XQueryExpression tmpExpr = arg.getExpression();
430         if ((expr = (XQueryExpression)map.get(tmpExpr)) != null) {
431             try {
432                 XQueryExpression exprClone = (XQueryExpression)expr.clone();
433                 arg.setExpression(exprClone);
434             }
435             catch (CloneNotSupportedException JavaDoc e) { throw new XQueryException("Could not clone expression : " + expr); }
436         }
437         else tmpExpr.accept(this);
438     }
439     
440     // public void visit(XQueryUnit arg) throws XQueryException;
441
// public void visit(XQueryVoid arg) throws XQueryException;
442

443     // primitive functions
444

445     // public void visit(FunctionATTRIBUTE arg) throws XQueryException ;
446
// public void visit(FunctionAVG arg) throws XQueryException ;
447
// public void visit(FunctionCOLLECTION arg) throws XQueryException;
448
// public void visit(FunctionCONTAINS arg) throws XQueryException;
449
// public void visit(FunctionCOUNT arg) throws XQueryException;
450
// public void visit(FunctionCURRENT_DATETIME arg) throws XQueryException
451
// public void visit(FunctionDATA arg) throws XQueryException;
452
// public void visit(FunctionDISTINCT_VALUES arg) throws XQueryException;
453
// public void visit(FunctionDOCUMENT arg) throws XQueryException;
454
// public void visit(FunctionEMPTY arg) throws XQueryException;
455
// public void visit(FunctionFILTER arg) throws XQueryException;
456
// public void visit(FunctionMAX arg) throws XQueryException;
457
// public void visit(FunctionMIN arg) throws XQueryException;
458
// public void visit(FunctionNAME arg) throws XQueryException;
459
// public void visit(FunctionNOT arg) throws XQueryException;
460
// public void visit(FunctionNUMBER arg) throws XQueryException;
461
// public void visit(FunctionPART arg) throws XQueryException;
462
// public void visit(FunctionPOSITION arg) throws XQueryException;
463
// public void visit(FunctionSHALLOW arg) throws XQueryException;
464
// public void visit(FunctionSTRING arg) throws XQueryException;
465
// public void visit(FunctionSTRINGVALUE arg) throws XQueryException;
466
// public void visit(FunctionSUM arg) throws XQueryException;
467
// public void visit(FunctionTEXT arg) throws XQueryException;
468
}
469
Popular Tags