KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  * Copyright (C) 2004 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
28 import org.xquark.xquery.parser.util.Constants;
29 import org.xquark.xquery.typing.TypeException;
30
31 public class Variable extends XQueryExpression implements Cloneable JavaDoc {
32     private static final String JavaDoc RCSRevision = "$Revision: 1.32 $";
33     private static final String JavaDoc RCSName = "$Name: $";
34
35     protected SequenceType typeDeclaration = null;
36     // cannot be null or empty string
37
protected QName varName = null;
38     protected XQueryExpression expression = null;
39     // cannot be -1
40
protected int bindingtype = -1;
41     // paths beginning with variable
42
protected ArrayList JavaDoc xpaths = null;
43     protected FLWRExpression parentFLWR = null;
44     protected Variable positionVar = null;
45     protected byte creator = 0;
46     protected boolean isDummy = false;
47
48     public static final byte PARSER_CREATOR = 0;
49     public static final byte NORM_CREATOR = 1;
50     public static final byte RES_CREATOR = 2;
51     public static final byte CAN_CREATOR = 3;
52     public static final byte CLONE_CREATOR = 4;
53     public static final byte DEPMAN_CREATOR = 5;
54
55     // #############################################################################
56
// VISITOR STUFF
57
// #############################################################################
58

59     public void accept(ParserVisitor visitor) throws XQueryException {
60         visitor.visit(this);
61     }
62
63     // #############################################################################
64
// CONSTRUCTOR STUFF
65
// #############################################################################
66

67     public Variable(QName varName, XQueryModule parentModule) throws TypeException, XQueryException {
68         setVarName(varName);
69         setParentModule(parentModule);
70         // if (parentModule != null && parentModule.getStaticContext().getTypeVisitor() != null)
71
// accept(parentModule.getStaticContext().getTypeVisitor());
72
}
73
74     // #############################################################################
75
// ATTRIBUTES ACCESS STUFF
76
// #############################################################################
77

78     public String JavaDoc toString() {
79         return toString(false, false, false);
80     }
81
82     public String JavaDoc getStringValue() {
83         return getStringValue(false, false);
84     }
85
86     public FLWRExpression getParentFLWRExpression() {
87         return parentFLWR;
88     }
89
90     public void setParentFLWRExpression(FLWRExpression parentFLWR) {
91         this.parentFLWR = parentFLWR;
92     }
93
94     public void setParentExpression(XQueryExpression expression) {
95         if (expression == null)
96             return;
97         if (parentExpression == null)
98             parentExpression = new ArrayList JavaDoc(1);
99         // else parentExpression.clear();
100
if (!parentExpression.contains(expression))
101             parentExpression.add(expression);
102     }
103
104     public void setExpression(XQueryExpression expression, boolean reType) throws TypeException, XQueryException {
105         if (expression != this.expression) {
106             this.expression = expression;
107             if (this.expression != null) {
108                 expression.setParentExpression(this);
109             }
110             if (reType && parentModule != null && parentModule.getStaticContext().getTypeVisitor() != null)
111                 accept(parentModule.getStaticContext().getTypeVisitor());
112         }
113     }
114
115     // public void setExpression(XQueryExpression expression) throws XQueryException {
116
// this.expression = expression;
117
// if (this.expression != null) {
118
// expression.setParentExpression(this);
119
// expression.setParentModule(parentModule);
120
// }
121
// }
122

123     public void setBindingType(int bindingtype) throws XQueryException {
124         if (bindingtype == -1 || bindingtype > Constants.MAX_BINDINGTYPE)
125             throw new XQueryException("bindingtype of Variable has incorrect value " + bindingtype);
126         this.bindingtype = bindingtype;
127     }
128
129     public Variable getPositionVar() {
130         return positionVar;
131     }
132
133     public XQueryExpression getExpression() {
134         return expression;
135     }
136     public int getBindingType() {
137         return bindingtype;
138     }
139
140     public boolean isInput() {
141         if (expression == null)
142             return false;
143         if (expression instanceof InputFunctionCall || (expression instanceof LocatedExpression && ((LocatedExpression) expression).getExpression() instanceof InputFunctionCall))
144             return true;
145         return false;
146     }
147
148     public ArrayList JavaDoc getSubPathList() {
149         return xpaths;
150     }
151
152     // attention if a variable is present this method only returns the variable
153
// attention this method erases the NodeTypes found at the end of a path
154
// DANGER do not use this method !!!!
155
public ArrayList JavaDoc getUniqueSubPathList() {
156         if (xpaths == null)
157             return null;
158         ArrayList JavaDoc tmpList = null;
159         if (xpaths.contains(this)) {
160             tmpList = new ArrayList JavaDoc(1);
161             tmpList.add(this);
162         } else {
163             tmpList = new ArrayList JavaDoc();
164             for (int i = 0; i < xpaths.size(); i++) {
165                 XQueryExpression tmpExpr = (XQueryExpression) xpaths.get(i);
166                 if (tmpExpr instanceof LocatedExpression) {
167                     LocatedExpression tmpLoc = (LocatedExpression) tmpExpr;
168                     ArrayList JavaDoc steps = tmpLoc.getSteps();
169                     Step lastStep = tmpLoc.getStepNum(steps.size() - 1);
170                     if (lastStep.getExpression() instanceof NodeTest) {
171                         tmpLoc.setNodeAccessor(lastStep.getStepKind());
172                         steps.remove(steps.size() - 1);
173                     }
174                 }
175                 if (tmpList.isEmpty())
176                     tmpList.add(tmpExpr);
177                 else {
178                     boolean found = false;
179                     for (int j = 0; j < tmpList.size(); j++)
180                         if (tmpExpr.getStringValue().equals(((XQueryExpression) tmpList.get(j)).getStringValue())) {
181                             found = true;
182                             break;
183                         }
184                     if (!found)
185                         tmpList.add(tmpExpr);
186                 }
187             }
188         }
189         return tmpList;
190     }
191
192     /**
193      * Returns the PathNode list of the defining expression
194      *
195      */

196     public ArrayList JavaDoc getXTrees() {
197         if (expression == null)
198             return null;
199         return expression.getXTrees();
200     }
201
202     // W3C WD 16 August 2002
203
public void setTypeDeclaration(SequenceType typeDeclaration) {
204         this.typeDeclaration = typeDeclaration;
205         if (typeDeclaration != null) {
206             this.typeDeclaration.setParentModule(parentModule);
207             this.typeDeclaration.setParentExpression(this);
208         }
209     }
210     public SequenceType getTypeDeclaration() {
211         return typeDeclaration;
212     }
213
214     public void setVarName(QName varName) throws XQueryException {
215         if (varName == null || varName.getName().equals(""))
216             throw new XQueryException("name of Variable cannot be null or empty string");
217         this.varName = varName;
218         this.varName.setParentModule(parentModule);
219         this.varName.setParentExpression(this);
220     }
221
222     public QName getVarName() {
223         return varName;
224     }
225     public String JavaDoc getName() {
226         return varName.getStringValue();
227     }
228     public void setName(String JavaDoc name) {
229         varName.setName(name);
230     }
231
232     public void addSubPath(XQueryExpression expr) {
233         if (xpaths == null)
234             xpaths = new ArrayList JavaDoc();
235         if (expr != null && !xpaths.contains(expr))
236             xpaths.add(expr);
237     }
238
239     public byte getCreator() {
240         return creator;
241     }
242
243     public void setCreator(byte creator) {
244         this.creator = creator;
245     }
246
247     public void addParentExpression(XQueryExpression parentExpression) {
248         addParentExpression(parentExpression);
249         varName.addParentExpression(parentExpression);
250         if (expression != null)
251             expression.addParentExpression(parentExpression);
252     }
253
254     public void isDummy(boolean b) {
255         isDummy = b;
256     }
257     
258     public boolean isDummy() {
259         return isDummy;
260     }
261
262     public boolean startsWithVariable() {
263         return true;
264     }
265
266 }
267
Popular Tags