KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mediator > DOMUtils > FillExprWithTupleVisitor


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.mediator.DOMUtils;
24
25 import java.util.ArrayList JavaDoc;
26
27 import org.xquark.mediator.algebra.AlgFLWR;
28 import org.xquark.mediator.algebra.Algebra;
29 import org.xquark.schema.SchemaManager;
30 import org.xquark.xpath.datamodel.TypedNode;
31 import org.xquark.xquery.parser.*;
32 import org.xquark.xquery.typing.*;
33
34
35 public class FillExprWithTupleVisitor extends DefaultParserVisitor {
36     // **********************************************************************
37
// * VERSIONING
38
// **********************************************************************
39
private static final String JavaDoc RCSRevision = "$Revision: 1.10 $";
40     private static final String JavaDoc RCSName = "$Name: $";
41     // **********************************************************************
42
// * CLASS VARIABLES
43
// **********************************************************************
44
//private Tuple tuple = null;
45
private ArrayList JavaDoc tuples = null;
46     private XQueryExpression resultExpr = null;
47     private SchemaManager schemamanager = null;
48     private TypeVisitor typevisitor = null;
49     // **********************************************************************
50
// * INITIALIZATION
51
// **********************************************************************
52
/**
53      *
54      */

55     public FillExprWithTupleVisitor(SchemaManager schemamanager, TypeVisitor typevisitor, Tuple tuple) {
56         this.tuples = new ArrayList JavaDoc(1);
57         tuples.add(tuple);
58         this.schemamanager = schemamanager;
59         this.typevisitor = typevisitor;
60     }
61
62     public FillExprWithTupleVisitor(SchemaManager schemamanager, TypeVisitor typevisitor, ArrayList JavaDoc tuples) {
63         this.tuples = tuples;
64         this.schemamanager = schemamanager;
65         this.typevisitor = typevisitor;
66     }
67
68     public XQueryExpression getResultExpr() { return resultExpr; }
69
70     // **********************************************************************
71
// * METHODS
72
// **********************************************************************
73
// special
74

75     public void visit(Algebra arg) throws XQueryException {
76         arg.getExpression().accept(this);
77         if (resultExpr != null)
78             arg.setExpression(resultExpr);
79         ArrayList JavaDoc children = arg.getChildren();
80         if (children != null)
81             for (int i = 0; i < children.size(); i++)
82                 visit((Algebra) children.get(i));
83         if (arg.isQDB()) {
84             AlgFLWR tmpdepmem = (AlgFLWR) arg;
85             if (tmpdepmem.getDependencyList() != null)
86                 for (int i = 0; i < tmpdepmem.getDependencyList().size(); i++)
87                     visit((AlgFLWR) tmpdepmem.getDependencyList().get(i));
88         }
89         arg.getVarsDependingOn().clear();
90         resultExpr = null;
91     }
92
93     // **********************************************************************
94
// * VISITATION
95
// **********************************************************************
96
public void visit(XQueryExpression arg) throws XQueryException {
97         resultExpr = null;
98     }
99
100     public void visit(FunctionCall arg) throws XQueryException {
101         ArrayList JavaDoc arguments = arg.getArguments();
102         if (arguments != null) {
103             for (int i = 0; i < arguments.size(); i++) {
104                 XQueryExpression argi = (XQueryExpression) arguments.get(i);
105                 argi.accept(this);
106                 if (resultExpr != null)
107                     arguments.set(i, resultExpr);
108             }
109         }
110         resultExpr = null;
111     }
112
113     public void visit(FLWRExpression arg) throws XQueryException {
114         for (int i = 0; i < arg.getVariables().size(); i++) {
115             Variable vari = (Variable) arg.getVariables().get(i);
116             vari.getExpression().accept(this);
117             if (resultExpr != null)
118                 vari.setExpression(resultExpr,false);
119         }
120         if (arg.getWhereClause() != null) {
121             arg.getWhereClause().accept(this);
122             if (resultExpr != null)
123                 arg.setWhereClause(resultExpr);
124         }
125 // arg.getReturnClause().accept(this);
126
// if (resultExpr != null)
127
// arg.setReturnClause(resultExpr);
128
resultExpr = null;
129     }
130
131     public void visit(QuantifiedExpression arg) throws XQueryException {
132         for (int i = 0; i < arg.getVariables().size(); i++) {
133             Variable vari = (Variable) arg.getVariables().get(i);
134             vari.getExpression().accept(this);
135             if (resultExpr != null)
136                 vari.setExpression(resultExpr,false);
137         }
138         arg.getConstraintExpresson().accept(this);
139         if (resultExpr != null)
140             arg.setConstraintExpresson(resultExpr);
141         resultExpr = null;
142     }
143
144     public void visit(XQueryBinaryOperatorExpression arg) throws XQueryException {
145         arg.getExpression1().accept(this);
146         if (resultExpr != null)
147             arg.setExpression1(resultExpr);
148         arg.getExpression2().accept(this);
149         if (resultExpr != null)
150             arg.setExpression2(resultExpr);
151         resultExpr = null;
152     }
153
154     public void visit(LocatedExpression arg) throws XQueryException {
155         resultExpr = null;
156         XQueryExpression step0expr = arg.getExpression();
157         if (step0expr instanceof Variable) {
158
159             EvaluationVisitor visitor = null;
160             for (int j = 0; j < tuples.size(); j++) {
161
162                 if (j == 0)
163                     visitor = new EvaluationVisitor(this.schemamanager, (Tuple) tuples.get(j));
164                 else
165                     visitor.reset((Tuple) tuples.get(j));
166
167                 arg.accept(visitor);
168                 ArrayList JavaDoc tmplist = visitor.getResNodes();
169                 if (tmplist != null && !tmplist.isEmpty()) {
170                     if (tmplist.size() == 1) {
171                         TypedNode tmpnode = (TypedNode) tmplist.get(0);
172                         Object JavaDoc strvalue = tmpnode.getTypedValue();
173                         if (strvalue == null) {
174                             strvalue = DOMUtils.getTextLeaf(tmpnode);
175                         }
176                         if (strvalue == null)
177                             throw new XQueryException("Unexpected value encountered while replacing variable");
178                         resultExpr = DOMUtils.getXQueryExpression(strvalue);
179                         if (resultExpr == null)
180                             throw new XQueryException("Unexpected type encountered while replacing variable");
181                     } else {
182                         ArrayList JavaDoc seqlist = new ArrayList JavaDoc();
183                         for (int i = 0; i < tmplist.size(); i++) {
184                             TypedNode tmpnode = (TypedNode) tmplist.get(i);
185                             Object JavaDoc strvalue = tmpnode.getTypedValue();
186                             if (strvalue == null) {
187                                 strvalue = DOMUtils.getTextLeaf(tmpnode);
188                             }
189                             if (strvalue == null)
190                                 throw new XQueryException("Unexpected value encountered while replacing variable");
191                             XQueryExpression tmpExpr = DOMUtils.getXQueryExpression(strvalue);
192                             if (tmpExpr == null)
193                                 throw new XQueryException("Unexpected type encountered while replacing variable");
194                             seqlist.add(tmpExpr);
195                         }
196                         resultExpr = new XQueryExpressionSequence(seqlist, arg.getParentModule());
197                         resultExpr.setParenthesis(true);
198                     }
199                     break;
200                 }
201             }
202         }
203     }
204
205     public void visit(Variable arg) throws XQueryException {
206         for (int j = 0; j < tuples.size(); j++) {
207             ArrayList JavaDoc tmplist = ((Tuple) tuples.get(j)).getPath(arg.getStringValue());
208             if (tmplist != null) {
209                 if (tmplist.size() == 1) {
210                     TypedNode tmpnode = (TypedNode) tmplist.get(0);
211                     
212                     Object JavaDoc strvalue = tmpnode.getTypedValue();
213                     if (strvalue == null) {
214                         strvalue = DOMUtils.getTextLeaf(tmpnode);
215                     }
216                     if (strvalue == null)
217                         throw new XQueryException("Unexpected value encountered while replacing variable");
218                     resultExpr = DOMUtils.getXQueryExpression(strvalue);
219                     /*
220                     if (arg.getQType().isNumeric()) resultExpr = new ValueDecimal(strvalue,null);
221                     else if (arg.getQType().isString()) resultExpr = new ValueString(strvalue,null);
222                     else if (arg.getQType().isDate()) resultExpr = new ValueString(strvalue,null);
223                     else throw new XQueryException("Unexpected type encountered while replacing variable" );
224                     */

225                     if (resultExpr == null)
226                         throw new XQueryException("Unexpected type encountered while replacing variable");
227                 } else {
228                     throw new XQueryException("Replacing variable by more than one value -> not implemented yet");
229                 }
230                 break;
231             }
232         }
233     }
234
235 }
236
Popular Tags