KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  * Copyright (C) 2003 XQuark Group.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
19  * You can also get it at http://www.gnu.org/licenses/lgpl.html
20  *
21  * For more information on this software, see http://www.xquark.org.
22  */

23
24 package org.xquark.xquery.parser;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Set JavaDoc;
29
30 import org.xquark.schema.SchemaManager;
31 import org.xquark.util.NamespaceContextStack;
32 import org.xquark.xquery.normalize.PutSkolemIDsVisitor;
33 import org.xquark.xquery.normalize.SubstituteVisitor;
34 import org.xquark.xquery.parser.util.Constants;
35 import org.xquark.xquery.typing.QType;
36 import org.xquark.xquery.typing.TypeException;
37 import org.xquark.xquery.typing.TypeVisitor;
38
39 public abstract class XQueryExpression implements Cloneable JavaDoc {
40     private static final String JavaDoc RCSRevision = "$Revision: 1.51 $";
41     private static final String JavaDoc RCSName = "$Name: $";
42
43     // for printing only
44
protected boolean parenthesis = false;
45     protected boolean context = false;
46     // for ordering
47
protected int[] order = null;
48     // for typing
49
protected ArrayList JavaDoc parentExpression = null;
50     // protected XQueryExpression parentExpression = null;
51
protected QType qtype = null; // org.xquark.xml.typing.QType
52
protected ArrayList JavaDoc xtrees = null;
53
54     // for reconstruction
55
protected ArrayList JavaDoc skolemIDs = null;
56     protected ArrayList JavaDoc dependIDs = null;
57     protected boolean root = false;
58     protected boolean loop = false;
59
60     //protected Set loopIDs = null;
61
protected String JavaDoc loopIDs = null;
62
63     // for referencing
64
protected XQueryModule parentModule = null;
65
66     // for decomposer : vector of sources matching the path
67
protected Set JavaDoc sourceNames = null;
68     protected Set JavaDoc urls = null;
69
70     // to avoid building multiple string representations
71
protected String JavaDoc stringValue = null;
72     //boolean modified = false;
73
// #############################################################################
74
// VISITOR STUFF
75
// #############################################################################
76

77     public void accept(ParserVisitor visitor) throws XQueryException {
78         visitor.visit(this);
79     }
80
81     // ******************************************
82
// FOR ACCESS TO PRIVATE/PROTECTED ATTRIBUTES
83
// ******************************************
84

85     // parentExpression
86
public ArrayList JavaDoc getParentExpression() {
87         return parentExpression;
88     }
89     public void setParentExpression(XQueryExpression expression) {
90         if (expression == null)
91             return;
92         if (parentExpression == null)
93             parentExpression = new ArrayList JavaDoc(1);
94         else
95             parentExpression.clear();
96         parentExpression.add(expression);
97     }
98     public void setParentExpressions(ArrayList JavaDoc parentExpression) {
99         this.parentExpression = parentExpression;
100     }
101
102     // qtype
103
public QType getQType() {
104         return qtype;
105     }
106     public void setQType(QType qtype) {
107         this.qtype = qtype;
108     }
109
110     // qtype
111
public ArrayList JavaDoc getXTrees() {
112         return xtrees;
113     }
114     public void setXTrees(ArrayList JavaDoc xtrees) {
115         this.xtrees = xtrees;
116     }
117
118     //parentUnit
119
public XQueryModule getParentModule() {
120         return parentModule;
121     }
122     public void setParentModule(XQueryModule parentModule) {
123         if (parentModule == null)
124             return;
125         this.parentModule = parentModule;
126     }
127
128     // reconstruction
129
public ArrayList JavaDoc getSkolemIDs() {
130         return skolemIDs;
131     }
132     public void setSkolemIDs(ArrayList JavaDoc skolemIDs) {
133         if (this.skolemIDs == null)
134             this.skolemIDs = skolemIDs;
135         else if (skolemIDs != null) {
136             for (int i = 0; i < skolemIDs.size(); i++) {
137                 Variable tmpVar = (Variable) skolemIDs.get(i);
138                 if (!(this.skolemIDs.contains(tmpVar)))
139                     this.skolemIDs.add(0, tmpVar);
140             }
141         }
142     }
143
144     public ArrayList JavaDoc getDependIDs() {
145         return dependIDs;
146     }
147     public void setDependIDs(ArrayList JavaDoc dependIDs) {
148         this.dependIDs = dependIDs;
149     }
150
151     public boolean getRoot() {
152         return root;
153     }
154
155     // public void setRoot(boolean root,boolean force) {
156
// if (force) this.root = root;
157
// else setRoot(root);
158
// }
159

160     public void setRoot(boolean root) {
161         if ((skolemIDs == null || skolemIDs.isEmpty()) && (dependIDs == null || dependIDs.isEmpty()))
162             this.root = root;
163         if (root && dependIDs != null && !dependIDs.isEmpty()) {
164             this.loop = true;
165             //dependIDs = null;
166
}
167     }
168
169     public boolean getLoop() {
170         return loop;
171     }
172     // public void setRoot(boolean loop) { this.loop = loop; }
173

174     //public Set getLoopIDs() { return loopIDs; }
175
//public void setLoopIDs(Set loopIDs) { this.loopIDs = loopIDs; }
176
public String JavaDoc getLoopIDs() {
177         return loopIDs;
178     }
179     public void setLoopIDs(String JavaDoc loopIDs) {
180         this.loopIDs = loopIDs;
181     }
182
183     // parenthesis
184
public boolean getParenthesis() {
185         return parenthesis;
186     }
187     public void setParenthesis(boolean parenthesis) {
188         this.parenthesis = parenthesis;
189     }
190
191     // order
192
public int[] getOrder() {
193         return order;
194     }
195     public int getOrder(int index) {
196         if (order == null)
197             return Constants.NOTHING;
198         return order[index];
199     }
200     public void setOrder(int[] order) {
201         this.order = order;
202     }
203     public void setOrder(int order) {
204         if (this.order == null) {
205             this.order = new int[2];
206             this.order[1] = Constants.NOTHING;
207         }
208         this.order[0] = order;
209     }
210     // context
211
public boolean getContext() {
212         return context;
213     }
214     public void setContext(boolean context) {
215         this.context = context;
216     }
217
218     // name if exist
219
public String JavaDoc getName() {
220         return null;
221     }
222
223     // sequenceType if exist
224
public void setTypeDeclaration(SequenceType typeDeclaration) {
225         return;
226     }
227
228     // typeName if exist
229
public void setTypeName(QName atomicType) {
230         return;
231     }
232
233     // expression if exist
234
// public void setExpression(XQueryExpression expression) throws TypeException, XQueryException {
235
// return;
236
// }
237

238     // bindingType if exist
239
public void setBindingType(int bindingtype) throws XQueryException {
240         return;
241     }
242
243     // add 27/03/2003
244
public XQueryExpression erasePart(XQueryExpression expr) throws XQueryException {
245         if (this == expr)
246             return null;
247         return this;
248     }
249
250     // schemaManager
251
public SchemaManager getSchemaManager() {
252         if (parentModule == null)
253             return null;
254         return parentModule.getSchemaManager();
255     }
256
257     public boolean equals(XQueryExpression expr) {
258         /** @todo : It is ugly, but we need equals()!*/
259         if (this == expr) {
260             return true;
261         }
262         String JavaDoc thisString = this.toString();
263         String JavaDoc exprString = expr.toString();
264         if (thisString.equals(exprString)) {
265             return true;
266         }
267         return false;
268     }
269
270     public ArrayList JavaDoc getSteps() {
271         return null;
272     }
273
274     // public void isModified() {
275
// modified = true;
276
// }
277

278     public String JavaDoc toString(boolean indent, boolean doNodeAccessor, boolean doPrefix) {
279 // PrintVisitor pv = new PrintVisitor((parentModule == null) ? null : parentModule.getDeclarations());
280
// pv.reset(indent, false, doNodeAccessor);
281
PrintVisitor pv = new PrintVisitor();
282         pv.reset(indent, false, doNodeAccessor, doPrefix);
283         try {
284             this.accept(pv);
285         } catch (XQueryException qe) {
286         }
287         return pv.getString();
288     }
289     public String JavaDoc toString() {
290         return toString(false, false, true);
291     }
292
293     public String JavaDoc getStringValue() {
294         return getStringValue(false, true);
295     }
296     public String JavaDoc getStringValue(boolean doNodeAccessor, boolean doPrefix) {
297         if (/*modified ||*/
298             doNodeAccessor ||
299             stringValue == null) {
300             stringValue = toString(false, doNodeAccessor, doPrefix);
301             //modified = false;
302
}
303         return stringValue;
304     }
305     
306     // TODO make this better
307
// special to wrote query for SQLWrapper
308
public String JavaDoc getStringValue(NamespaceContextStack context) {
309         PrintVisitor pv = new PrintVisitor();
310         pv.reset(false, false, true, true);
311         pv.setNamespaceContextStack(context);
312         try {
313             this.accept(pv);
314         } catch (XQueryException qe) {
315         }
316         return pv.getString();
317     }
318
319     public void resetStringValue() {
320         stringValue = null;
321     }
322
323     // #############################################################################
324
// CLONE STUFF
325
// #############################################################################
326

327     public void addParentExpression(XQueryExpression expression) {
328         if (expression == null)
329             return;
330         if (parentExpression == null)
331             parentExpression = new ArrayList JavaDoc(1);
332         parentExpression.add(expression);
333     }
334
335     public XQueryExpression shallowClone() {
336         try {
337             return (XQueryExpression) super.clone();
338         } catch (CloneNotSupportedException JavaDoc e) {
339         }
340         return null;
341     }
342
343     public Object JavaDoc clone(boolean renameVars) throws CloneNotSupportedException JavaDoc {
344         try {
345             CloneVisitor cloneVisitor = new CloneVisitor(parentModule.getStaticContext(), renameVars);
346             this.accept(cloneVisitor);
347             return cloneVisitor.getClone();
348         } catch (XQueryException xqe) {
349             throw new CloneNotSupportedException JavaDoc("Could not clone : " + this);
350         }
351     }
352
353     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
354         try {
355             CloneVisitor cloneVisitor = new CloneVisitor(parentModule.getStaticContext(), true);
356             this.accept(cloneVisitor);
357             return cloneVisitor.getClone();
358         } catch (XQueryException xqe) {
359             throw new CloneNotSupportedException JavaDoc("Could not clone : " + this);
360         }
361     }
362
363     public void setSourceNames(Set JavaDoc sources) {
364         this.sourceNames = sources;
365     }
366
367     public Set JavaDoc getSourceNames() {
368         return sourceNames;
369     }
370
371     public void setUrls(Set JavaDoc urls) {
372         this.urls = urls;
373     }
374
375     public Set JavaDoc getUrls() {
376         return urls;
377     }
378
379     public boolean startsWithVariable() {
380         return false;
381     }
382
383     // #############################################################################
384
// RESTRUCTURE STUFF
385
// #############################################################################
386

387     public void substitute(HashMap JavaDoc map, TypeVisitor typeVisitor) throws XQueryException {
388         SubstituteVisitor substituteVisitor = new SubstituteVisitor(map, typeVisitor);
389         accept(substituteVisitor);
390     }
391
392     public void putSkolemIDs(ArrayList JavaDoc ids) throws XQueryException {
393         PutSkolemIDsVisitor putSkolemIDsVisitor = new PutSkolemIDsVisitor(ids);
394         accept(putSkolemIDsVisitor);
395     }
396
397 }
398
Popular Tags