KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > xquery > parser > CloneVisitor


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.parser;
24
25 import java.util.*;
26
27 import org.xquark.xquery.metadata.StaticContext;
28 import org.xquark.xquery.parser.primitivefunctions.fnfunctions.FunctionCOLLECTION;
29
30 public class CloneVisitor extends DefaultParserVisitor {
31
32     private static final String JavaDoc RCSRevision = "$Revision: 1.34 $";
33     private static final String JavaDoc RCSName = "$Name: $";
34
35     private HashMap mapvars = new HashMap();
36     //private boolean cloneNoneExistent = false;
37
//private boolean cloneBindings = true;
38
private XQueryExpression newobj = null;
39     private Stack objectStack = new Stack();
40     private boolean isBinding = false;
41     private boolean renameVars = true;
42     private StaticContext sc = null;
43     
44     public CloneVisitor(StaticContext sc) {
45         this.sc = sc;
46     }
47     public CloneVisitor(StaticContext sc, boolean renameVars) {
48         this.sc = sc;
49         this.renameVars = renameVars;
50     }
51
52     public XQueryExpression getClone() {
53         return (XQueryExpression) objectStack.peek();
54     }
55
56     private Object JavaDoc pop() {
57         Object JavaDoc obj = objectStack.pop();
58         if (!objectStack.isEmpty())
59             newobj = (XQueryExpression) objectStack.peek();
60         return obj;
61     }
62
63     private void push(XQueryExpression expr) {
64         newobj = expr;
65         objectStack.push(newobj);
66     }
67
68     // visit methods
69

70     public void visit(AggregateFunctionCall arg) throws XQueryException {
71         visit((PrimitiveFunctionCall) arg);
72     }
73
74     public void visit(AttributeValuePair arg) throws XQueryException {
75         visit((XQueryBinaryOperatorExpression) arg);
76     }
77
78     public void visit(BinOpANDExpression arg) throws XQueryException {
79         visit((XQueryBooleanOperatorExpression) arg);
80     }
81
82     public void visit(BinOpORExpression arg) throws XQueryException {
83         visit((XQueryBooleanOperatorExpression) arg);
84     }
85
86     public void visit(CaseClauseExpression arg) throws XQueryException {
87         visit((XQueryExpression) arg); //.accept(this);
88
SequenceType tmpSequenceTypeExpression = arg.getSequenceType();
89         if (tmpSequenceTypeExpression != null) {
90             tmpSequenceTypeExpression.accept(this);
91             tmpSequenceTypeExpression = (SequenceType) pop();
92             ((CaseClauseExpression) newobj).setSequenceType(tmpSequenceTypeExpression);
93         }
94         Variable tmpVariable = arg.getVariable();
95         if (tmpVariable != null) {
96             isBinding = true;
97             tmpVariable.accept(this);
98             tmpVariable = (Variable) pop();
99             ((CaseClauseExpression) newobj).setVariable(tmpVariable);
100         }
101         XQueryExpression tmpXQueryExpression = arg.getReturnExpression();
102         if (tmpXQueryExpression != null) {
103             tmpXQueryExpression.accept(this);
104             tmpXQueryExpression = (XQueryExpression) pop();
105             ((CaseClauseExpression) newobj).setReturnExpression(tmpXQueryExpression);
106         }
107     }
108
109     public void visit(CastTreatExpression arg) throws XQueryException {
110         visit((XQueryUnaryOperatorExpression) arg); //.accept(this);
111
SequenceType tmpSequenceTypeExpression = arg.getSequenceType();
112         if (tmpSequenceTypeExpression != null) {
113             tmpSequenceTypeExpression.accept(this);
114             tmpSequenceTypeExpression = (SequenceType) pop();
115             ((CastTreatExpression) newobj).setSequenceType(tmpSequenceTypeExpression);
116         }
117         ((CastTreatExpression) newobj).setOperator(arg.getOperator());
118     }
119
120     public void visit(CData arg) throws XQueryException {
121         visit((XQueryExpression) arg); //.accept(this);
122
if (arg.getCData() != null)
123              ((CData) newobj).setCData(new String JavaDoc(arg.getCData()));
124     }
125
126     public void visit(Element arg) throws XQueryException {
127         visit((XQueryExpression) arg); //.accept(this);
128
XQueryExpression tmpXQueryExpression = arg.getStartTag();
129         if (tmpXQueryExpression != null) {
130             tmpXQueryExpression.accept(this);
131             tmpXQueryExpression = (XQueryExpression) pop();
132             ((Element) newobj).setStartTag(tmpXQueryExpression);
133         }
134         ((Element) newobj).setLocalPrefixes(arg.getLocalPrefixes());
135         ((Element) newobj).setAttributes(totalClone(arg.getAttributes()));
136         ((Element) newobj).setSubExpressions(totalClone(arg.getSubExpressions()));
137     }
138
139     public void visit(FLWRExpression arg) throws XQueryException {
140         visit((XQueryExpression) arg); //.accept(this);
141
((FLWRExpression) newobj).setVariables(totalClone(arg.getVariables(), true));
142         XQueryExpression tmpXQueryExpression = arg.getWhereClause();
143         if (tmpXQueryExpression != null) {
144             tmpXQueryExpression.accept(this);
145             tmpXQueryExpression = (XQueryExpression) pop();
146             ((FLWRExpression) newobj).setWhereClause(tmpXQueryExpression);
147         }
148         tmpXQueryExpression = arg.getReturnClause();
149         if (tmpXQueryExpression != null) {
150             tmpXQueryExpression.accept(this);
151             tmpXQueryExpression = (XQueryExpression) pop();
152             ((FLWRExpression) newobj).setReturnClause(tmpXQueryExpression, false);
153         }
154         ((FLWRExpression) newobj).setOrderBy(totalClone(arg.getOrderBy()));
155     }
156
157     public void visit(FunctionCOLLECTION arg) throws XQueryException {
158         visit((PrimitiveFunctionCall) arg); //.accept(this);
159
((FunctionCOLLECTION) newobj).setArguments(totalClone(arg.getArguments()));
160     }
161     public void visit(FunctionCall arg) throws XQueryException {
162         visit((XQueryExpression) arg); //.accept(this);
163
QName tmpQName = arg.getFuncName();
164         if (tmpQName != null) {
165             tmpQName.accept(this);
166             tmpQName = (QName) pop();
167             ((FunctionCall) newobj).setFuncName(tmpQName);
168         }
169         ((FunctionCall) newobj).setArguments(totalClone(arg.getArguments()));
170     }
171
172     public void visit(FunctionDeclaration arg) throws XQueryException {
173         visit((XQueryExpression) arg); //.accept(this);
174
QName tmpQName = arg.getFuncName();
175         if (tmpQName != null) {
176             tmpQName.accept(this);
177             tmpQName = (QName) pop();
178             ((FunctionDeclaration) newobj).setFuncName(tmpQName);
179         }
180         ((FunctionDeclaration) newobj).setParameters(totalClone(arg.getParameters()));
181         SequenceType tmpSequenceTypeExpression = arg.getReturnType();
182         if (tmpSequenceTypeExpression != null) {
183             tmpSequenceTypeExpression.accept(this);
184             tmpSequenceTypeExpression = (SequenceType) pop();
185             ((FunctionDeclaration) newobj).setReturnType(tmpSequenceTypeExpression);
186         }
187         ((FunctionDeclaration) newobj).setExpressions(totalClone(arg.getExpressions()));
188     }
189
190     public void visit(InstanceOfExpression arg) throws XQueryException {
191         visit((XQueryUnaryOperatorExpression) arg); //.accept(this);
192
SequenceType tmpSequenceTypeExpression = arg.getSequenceType();
193         if (tmpSequenceTypeExpression != null) {
194             tmpSequenceTypeExpression.accept(this);
195             tmpSequenceTypeExpression = (SequenceType) pop();
196             ((InstanceOfExpression) newobj).setSequenceType(tmpSequenceTypeExpression);
197         }
198     }
199
200     public void visit(InternalFunctionCall arg) throws XQueryException {
201         visit((FunctionCall) arg);
202     }
203
204     public void visit(ITEExpression arg) throws XQueryException {
205         XQueryExpression ifXQueryExpression = arg.getIfExpression();
206         ifXQueryExpression.accept(this);
207         ifXQueryExpression = (XQueryExpression) pop();
208         XQueryExpression thenXQueryExpression = arg.getThenExpression();
209         thenXQueryExpression.accept(this);
210         thenXQueryExpression = (XQueryExpression) pop();
211         XQueryExpression elseXQueryExpression = arg.getElseExpression();
212         elseXQueryExpression.accept(this);
213         elseXQueryExpression = (XQueryExpression) pop();
214         visit((XQueryExpression) arg); //.accept(this);
215
((ITEExpression) newobj).setIfExpression(ifXQueryExpression);
216         ((ITEExpression) newobj).setThenExpression(thenXQueryExpression);
217         ((ITEExpression) newobj).setElseExpression(elseXQueryExpression);
218     }
219
220     public void visit(ItemType arg) throws XQueryException {
221         visit((XQueryExpression) arg); //.accept(this);
222
XQueryExpression tmpXQueryExpression = arg.getExpression();
223         if (tmpXQueryExpression != null) {
224             tmpXQueryExpression.accept(this);
225             tmpXQueryExpression = (XQueryExpression) pop();
226             ((ItemType) newobj).setExpression(tmpXQueryExpression);
227         }
228     }
229
230     public void visit(LibraryFunctionCall arg) throws XQueryException {
231         visit((FunctionCall) arg);
232     }
233
234     public void visit(ListOpAFTERExpression arg) throws XQueryException {
235         visit((XQueryListBinaryOperatorExpression) arg);
236     }
237
238     public void visit(ListOpArithExpression arg) throws XQueryException {
239         visit((XQueryListBinaryOperatorExpression) arg); //.accept(this);
240
((ListOpArithExpression) newobj).setOperator(arg.getOperator());
241     }
242
243     public void visit(ListOpBEFOREExpression arg) throws XQueryException {
244         visit((XQueryListBinaryOperatorExpression) arg);
245     }
246
247     public void visit(ListOpCompExpression arg) throws XQueryException {
248         visit((XQueryListBinaryOperatorExpression) arg); //.accept(this);
249
((ListOpCompExpression) newobj).setOperator(arg.getOperator());
250     }
251
252     public void visit(ListOpEXCEPTExpression arg) throws XQueryException {
253         visit((XQueryListBinaryOperatorExpression) arg);
254     }
255
256     public void visit(ListOpINTERSECTExpression arg) throws XQueryException {
257         visit((XQueryListBinaryOperatorExpression) arg);
258     }
259
260     public void visit(ListOpUNIONExpression arg) throws XQueryException {
261         visit((XQueryListBinaryOperatorExpression) arg);
262     }
263
264     public void visit(LocatedExpression arg) throws XQueryException {
265         visit((XQueryExpression) arg); //.accept(this);
266
if (arg.getSteps() != null)
267              ((LocatedExpression) newobj).setSteps(totalClone(arg.getSteps()));
268         ((LocatedExpression) newobj).setInPredicate(arg.getInPredicate());
269         ((LocatedExpression) newobj).setNodeAccessor(arg.getNodeAccessor());
270     }
271
272     public void visit(NodeTest arg) throws XQueryException {
273         visit((XQueryExpression) arg); //.accept(this);
274
((NodeTest) newobj).setKind(arg.getKind());
275         XQueryExpression tmpXQueryExpression = arg.getArg();
276         if (tmpXQueryExpression != null) {
277             tmpXQueryExpression.accept(this);
278             tmpXQueryExpression = (XQueryExpression) pop();
279             ((NodeTest) newobj).setArg(tmpXQueryExpression);
280         }
281     }
282
283     public void visit(PrimitiveFunctionCall arg) throws XQueryException {
284         visit((FunctionCall) arg);
285     }
286
287     public void visit(QName arg) throws XQueryException {
288         visit((XQueryExpression) arg);
289         if (arg.getNameSpace() != null)
290              ((QName) newobj).setNameSpace(arg.getNameSpace());
291         if (arg.getPrefixName() != null)
292              ((QName) newobj).setPrefixName(arg.getPrefixName());
293         if (arg.getLocalName() != null)
294              ((QName) newobj).setLocalName(arg.getLocalName());
295     }
296
297     public void visit(QuantifiedExpression arg) throws XQueryException {
298         visit((XQueryExpression) arg);
299         ((QuantifiedExpression) newobj).setVariables(totalClone(arg.getVariables(), true));
300         XQueryExpression tmpXQueryExpression = arg.getExpression();
301         if (tmpXQueryExpression != null) {
302             tmpXQueryExpression.accept(this);
303             tmpXQueryExpression = (XQueryExpression) pop();
304             ((XQueryUnaryOperatorExpression) newobj).setExpression(tmpXQueryExpression);
305         }
306         ((QuantifiedExpression) newobj).setKind(arg.getKind());
307     }
308
309     public void visit(RangeExpression arg) throws XQueryException {
310         visit((XQueryListBinaryOperatorExpression) arg);
311     }
312
313     public void visit(InputFunctionCall arg) throws XQueryException {
314         visit((PrimitiveFunctionCall) arg);
315     }
316
317     public void visit(SequenceType arg) throws XQueryException {
318         visit((XQueryExpression) arg);
319         ItemType tmpItemType = arg.getItemType();
320         if (tmpItemType != null) {
321             tmpItemType.accept(this);
322             tmpItemType = (ItemType) pop();
323             ((SequenceType) newobj).setItemType(tmpItemType);
324         }
325         ((SequenceType) newobj).setOccurrence(arg.getOccurrence());
326     }
327
328     public void visit(SortedExpression arg) throws XQueryException {
329         visit((XQueryListUnaryOperatorExpression) arg);
330          ((SortedExpression) newobj).setStable(arg.getStable());
331         if (arg.getSortClauses() != null)
332              ((SortedExpression) newobj).setSortClauses(totalClone(arg.getSortClauses()));
333     }
334
335     public void visit(Step arg) throws XQueryException {
336         visit((XQueryUnaryOperatorExpression) arg);
337         if (arg.getPredicates() != null)
338              ((Step) newobj).setPredicates(totalClone(arg.getPredicates()));
339         ((Step) newobj).setHasSeparator(arg.hasSeparator());
340         ((Step) newobj).setAxis(arg.getAxis());
341     }
342
343     public void visit(TypeSwitchExpression arg) throws XQueryException {
344         visit((XQueryBinaryOperatorExpression) arg);
345         if (arg.getCases() != null)
346              ((TypeSwitchExpression) newobj).setCases(totalCloneHashMap(arg.getCases()));
347         Variable tmpVariable = arg.getDefaultVariable();
348         if (tmpVariable != null) {
349             isBinding = true;
350             tmpVariable.accept(this);
351             tmpVariable = (Variable) pop();
352             ((TypeSwitchExpression) newobj).setDefaultVariable(tmpVariable);
353         }
354     }
355
356     public void visit(UnOpMinusExpression arg) throws XQueryException {
357         visit((XQueryUnaryOperatorExpression) arg);
358     }
359
360     public void visit(ValidateExpression arg) throws XQueryException {
361         visit((XQueryUnaryOperatorExpression) arg);
362         SchemaContextPath scp = arg.getSchemaContext();
363         if (scp != null) {
364             scp.accept(this);
365             scp = (SchemaContextPath) pop();
366             ((ValidateExpression) newobj).setSchemaContext(scp);
367         }
368         ((ValidateExpression) newobj).setScopeType(arg.getScopeType());
369         ((ValidateExpression) newobj).setValidationType(arg.getValidationType());
370     }
371
372     public void visit(Value arg) throws XQueryException {
373         visit((XQueryExpression) arg);
374         if (arg.getValue() != null)
375              ((Value) newobj).setValue(new String JavaDoc(arg.getValue()));
376     }
377
378     public void visit(ValueBoolean arg) throws XQueryException {
379         visit((Value) arg);
380     }
381
382     public void visit(ValueDecimal arg) throws XQueryException {
383         visit((Value) arg);
384     }
385
386     public void visit(ValueDouble arg) throws XQueryException {
387         visit((Value) arg);
388     }
389
390     public void visit(ValueFloat arg) throws XQueryException {
391         visit((Value) arg);
392     }
393
394     public void visit(ValueInteger arg) throws XQueryException {
395         visit((Value) arg);
396     }
397
398     public void visit(ValueString arg) throws XQueryException {
399         visit((Value) arg);
400     }
401
402     public void visit(ValueText arg) throws XQueryException {
403         visit((Value) arg);
404     }
405
406     public void visit(Variable arg) throws XQueryException {
407         Variable tmpVar = null;
408         //if (mapvars != null)
409
tmpVar = (Variable) mapvars.get(arg);
410         if (tmpVar != null) {
411             push(tmpVar);
412             return;
413         } else if (!isBinding /*&& !cloneNoneExistent*/) {
414             push(arg);
415             return;
416         }
417
418         visit((XQueryExpression) arg);
419         isBinding = false;
420         // W3C WD 16 August 2002
421
SequenceType tmpSequenceTypeExpression = arg.getTypeDeclaration();
422         if (tmpSequenceTypeExpression != null) {
423             tmpSequenceTypeExpression.accept(this);
424             tmpSequenceTypeExpression = (SequenceType) pop();
425             ((Variable) newobj).setTypeDeclaration(tmpSequenceTypeExpression);
426         }
427         // REMOVED: change the variable name!!!
428
// change LARS 23/02/04
429
if (renameVars) {
430             ((Variable) newobj).setVarName(sc.getVarName(arg.getParentModule()));
431             ((Variable) newobj).setCreator(Variable.CLONE_CREATOR);
432         }
433
434         XQueryExpression tmpXQueryExpression = arg.getExpression();
435         if (tmpXQueryExpression != null) {
436             tmpXQueryExpression.accept(this);
437             tmpXQueryExpression = (XQueryExpression) pop();
438             ((Variable) newobj).setExpression(tmpXQueryExpression, false);
439         }
440
441         //OUf, le chef a dit qu'au niveau du mediateur, AlgebraExpression est toujours a nul !
442
//Donc pas de clonage recursif
443
//NON ---> newobj.setAlgebraExpression((getAlgebraExpression () == null) ? null : (Expression) getAlgebraExpression ().clone (mapvars));
444
((Variable) newobj).setBindingType(arg.getBindingType());
445         mapvars.put(arg, newobj);
446     }
447
448     public void visit(XMLComment arg) throws XQueryException {
449         visit((XQueryUnaryOperatorExpression) arg);
450          ((XMLComment) newobj).setIsComputed(arg.isComputed());
451     }
452
453     public void visit(XMLProcessingInstruction arg) throws XQueryException {
454         visit((XQueryBinaryOperatorExpression) arg);
455         ((XMLProcessingInstruction) newobj).setIsComputed(arg.isComputed());
456     }
457
458     public void visit(XQueryBinaryOperatorExpression arg) throws XQueryException {
459         visit((XQueryExpression) arg);
460         XQueryExpression tmpXQueryExpression = arg.getExpression1();
461         if (tmpXQueryExpression != null) {
462             tmpXQueryExpression.accept(this);
463             tmpXQueryExpression = (XQueryExpression) pop();
464             ((XQueryBinaryOperatorExpression) newobj).setExpression1(tmpXQueryExpression);
465         }
466         tmpXQueryExpression = arg.getExpression2();
467         if (tmpXQueryExpression != null) {
468             tmpXQueryExpression.accept(this);
469             tmpXQueryExpression = (XQueryExpression) pop();
470             ((XQueryBinaryOperatorExpression) newobj).setExpression2(tmpXQueryExpression);
471         }
472     }
473
474     public void visit(XQueryBooleanOperatorExpression arg) throws XQueryException {
475         visit((XQueryBinaryOperatorExpression) arg);
476     }
477
478     public void visit(XQueryExpression arg) throws XQueryException {
479         newobj = arg.shallowClone();
480         newobj.setParenthesis(arg.getParenthesis());
481         newobj.setContext(arg.getContext());
482         newobj.setOrder(arg.getOrder());
483
484         newobj.setQType(arg.getQType());
485         newobj.setXTrees(arg.getXTrees());
486         newobj.setDependIDs(arg.getDependIDs());
487         newobj.setParentModule(arg.getParentModule());
488         newobj.setSkolemIDs(arg.getSkolemIDs());
489         newobj.setRoot(arg.getRoot());
490         newobj.setLoopIDs(arg.getLoopIDs());
491         newobj.setSourceNames(arg.getSourceNames());
492         newobj.setUrls(arg.getUrls());
493
494         newobj.resetStringValue();
495         push(newobj);
496     }
497
498     public void visit(XQueryExpressionSequence arg) throws XQueryException {
499         visit((XQueryExpression) arg);
500         if (arg.getSubExpressions() != null)
501              ((XQueryExpressionSequence) newobj).setSubExpressions(totalClone(arg.getSubExpressions()));
502     }
503
504     public void visit(XQueryListBinaryOperatorExpression arg) throws XQueryException {
505         visit((XQueryBinaryOperatorExpression) arg);
506     }
507
508     public void visit(XQueryListUnaryOperatorExpression arg) throws XQueryException {
509         visit((XQueryUnaryOperatorExpression) arg);
510     }
511
512     public void visit(XQueryUnaryOperatorExpression arg) throws XQueryException {
513         visit((XQueryExpression) arg);
514         XQueryExpression tmpXQueryExpression = arg.getExpression();
515         if (tmpXQueryExpression != null) {
516             tmpXQueryExpression.accept(this);
517             tmpXQueryExpression = (XQueryExpression) pop();
518             ((XQueryUnaryOperatorExpression) newobj).setExpression(tmpXQueryExpression);
519         }
520     }
521
522     public void visit(XQueryVoid arg) throws XQueryException {
523         visit((XQueryExpression) arg);
524     }
525
526     // primitive functions
527

528     // utilities
529
private ArrayList totalClone(ArrayList origlist) throws XQueryException {
530         return totalClone(origlist, false);
531     }
532     private ArrayList totalClone(ArrayList origlist, boolean isBinding) throws XQueryException {
533         if (origlist == null)
534             return null;
535         ArrayList newlist = new ArrayList(origlist.size());
536         for (int i = 0; i < origlist.size(); i++) {
537             this.isBinding = isBinding;
538             ((XQueryExpression) origlist.get(i)).accept(this);
539             newlist.add(pop());
540         }
541         return newlist;
542     }
543
544     private HashMap totalCloneHashMap(HashMap origmap) throws XQueryException {
545         if (origmap == null)
546             return null;
547         HashMap newmap = new HashMap(origmap.size());
548         Set keys = origmap.keySet();
549         Iterator it = keys.iterator();
550         while (it.hasNext()) {
551             XQueryExpression objkey = (XQueryExpression) it.next();
552             objkey.accept(this);
553             XQueryExpression clonekey = (XQueryExpression) pop();
554
555             XQueryExpression value = (XQueryExpression) origmap.get(objkey);
556             XQueryExpression clonevalue = null;
557             if (value != null) {
558                 value.accept(this);
559                 clonevalue = (XQueryExpression) pop();
560             }
561             newmap.put(clonekey, clonevalue);
562         }
563         return newmap;
564     }
565
566 }
567
Popular Tags