KickJava   Java API By Example, From Geeks To Geeks.

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


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
28 import org.xquark.xpath.Axis;
29 import org.xquark.xpath.PathExpr;
30 import org.xquark.xpath.StepExpr;
31 import org.xquark.xquery.normalize.LocatedExpressionReducer;
32 import org.xquark.xquery.typing.TypeException;
33
34 public class LocatedExpression extends XQueryExpression implements PathExpr, Cloneable JavaDoc {
35
36     private static final String JavaDoc RCSRevision = "$Revision: 1.18 $";
37     private static final String JavaDoc RCSName = "$Name: $";
38
39     // cannot be null or empty
40
protected ArrayList JavaDoc steps = null;
41
42     protected boolean inPredicate = false;
43     // for SQL wrapper specific purposes when last step is NodeTest then set nodeAccessor
44
protected int nodeAccessor = Step.NONE;
45     
46     // #############################################################################
47
// VISITOR STUFF
48
// #############################################################################
49

50     public void accept(ParserVisitor visitor) throws XQueryException {
51         visitor.visit(this);
52     }
53
54     // #############################################################################
55
// CONSTRUCTOR STUFF
56
// #############################################################################
57

58     public LocatedExpression(ArrayList JavaDoc steps, XQueryModule parentModule) throws TypeException, XQueryException {
59         this(steps, false, parentModule);
60     }
61
62     public LocatedExpression(ArrayList JavaDoc steps, boolean inPredicate, XQueryModule parentModule) throws TypeException, XQueryException {
63         init(steps,inPredicate,parentModule,true);
64     }
65     public LocatedExpression(ArrayList JavaDoc steps, boolean inPredicate, XQueryModule parentModule, boolean simplify) throws TypeException, XQueryException {
66         init(steps,inPredicate,parentModule,simplify);
67     }
68     
69     private void init(ArrayList JavaDoc steps, boolean inPredicate, XQueryModule parentModule, boolean simplify) throws TypeException, XQueryException {
70         if (steps == null || steps.isEmpty())
71             throw new XQueryException("steps of LocatedExpression cannot be null or empty");
72         if (simplify) {
73             int retValue = LocatedExpressionReducer.simplifySteps(steps,inPredicate, parentModule) ;
74             if (retValue == 0)
75                 throw new XQueryException("steps of LocatedExpression are not valid");
76         }
77         setSteps(steps);
78         setInPredicate(inPredicate);
79         setParentModule(parentModule);
80         if (parentModule != null && parentModule.getStaticContext().getTypeVisitor() != null)
81             accept(parentModule.getStaticContext().getTypeVisitor());
82     }
83
84     // #############################################################################
85
// ATTRIBUTE ACCESS STUFF
86
// #############################################################################
87

88     public String JavaDoc toString() {
89         return toString(false, false, false);
90     }
91
92     public String JavaDoc getStringValue() {
93         return getStringValue(false, false);
94     }
95
96     public StepExpr getStep(int num) {
97         if (num >= steps.size())
98             return null;
99         return (Step) steps.get(num);
100     }
101     public Step getStepNum(int num) {
102         if (num >= steps.size())
103             return null;
104         return (Step) steps.get(num);
105     }
106     public XQueryExpression getExpression() {
107         return ((Step)steps.get(0)).getExpression();
108     }
109     public ArrayList JavaDoc getSteps() {
110         return steps;
111     }
112     public void setSteps(ArrayList JavaDoc steps) throws XQueryException {
113         // cannot be null or empty
114
if (steps == null || steps.isEmpty())
115             throw new XQueryException("steps of LocatedExpression cannot be null or empty");
116         this.steps = steps;
117         for (int i = 0; i < this.steps.size(); i++) {
118             Step step = (Step) this.steps.get(i);
119             step.setParentExpression(this);
120             step.setParentModule(parentModule);
121         }
122     }
123
124     public void addStep(Step step) throws XQueryException {
125         if (step == null)
126             return;
127         step.setParentModule(parentModule);
128         if (steps == null)
129             steps = new ArrayList JavaDoc(1);
130         steps.add(step);
131         setSteps(steps);
132     }
133
134     public void addSteps(ArrayList JavaDoc steps) throws XQueryException {
135         if (steps == null || steps.isEmpty())
136             return;
137         for (int i=0;i<steps.size();i++) {
138             ((Step)steps.get(i)).setParentModule(parentModule);
139         }
140         if (this.steps == null || this.steps.isEmpty()) {
141             setSteps(steps);
142             return;
143         }
144         this.steps.addAll(steps);
145         setSteps(this.steps);
146     }
147
148     public void addStepAt(int index, Step step) throws XQueryException {
149         if (step == null)
150             return;
151         step.setParentModule(parentModule);
152         if (steps == null)
153             steps = new ArrayList JavaDoc(1);
154         steps.add(index,step);
155         setSteps(steps);
156     }
157
158     public void addStepsAt(int index, ArrayList JavaDoc steps) throws XQueryException {
159         if (steps == null || steps.isEmpty())
160             return;
161         for (int i=0;i<steps.size();i++) {
162             ((Step)steps.get(i)).setParentModule(parentModule);
163         }
164         if (this.steps == null || this.steps.isEmpty()) {
165             setSteps(steps);
166             return;
167         }
168         this.steps.addAll(index, steps);
169         setSteps(this.steps);
170     }
171
172     public void setParentModule(XQueryModule parentUnit) {
173         if (parentUnit == null)
174             return;
175         this.parentModule = parentUnit;
176         for (int i = 0; i < steps.size(); i++)
177              ((Step) steps.get(i)).setParentModule(parentUnit);
178     }
179
180 // public boolean isRelativeExpression() {
181
// Step step0 = (Step)steps.get(0);
182
// if (step0.getAxis() == Axis.NONE && !step0.hasSeparator())
183
// return false;
184
// return true;
185
// }
186

187     public boolean isRelative() {
188         if (((Step) steps.get(0)).getExpression() instanceof QName || ((Step) steps.get(0)).getExpression() instanceof NodeTest)
189             return true;
190         return false;
191     }
192
193     public void setInPredicate(boolean inPredicate) {
194         this.inPredicate = inPredicate;
195     }
196
197     public boolean getInPredicate() {
198         return inPredicate;
199     }
200     
201     public void setNodeAccessor(int nodeAccessor) {
202         this.nodeAccessor = nodeAccessor;
203     }
204
205     public int getNodeAccessor() {
206         return nodeAccessor;
207     }
208
209     public boolean startsWith(Variable var) {
210         return ((Step)steps.get(0)).getExpression() == var;
211     }
212
213
214     // #############################################################################
215
// CLONE STUFF
216
// #############################################################################
217
public void addParentExpression(XQueryExpression parentExpression) {
218         addParentExpression(parentExpression);
219         for (int i = 0; i < steps.size(); i++)
220              ((Step) steps.get(i)).addParentExpression(parentExpression);
221     }
222     
223     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
224         try {
225             CloneVisitor cloneVisitor = new CloneVisitor(parentModule.getStaticContext(),true);
226             this.accept(cloneVisitor);
227             return cloneVisitor.getClone();
228         } catch (XQueryException xqe) {
229             throw new CloneNotSupportedException JavaDoc("Could not clone : " + this);
230         }
231     }
232
233
234     // #############################################################################
235
// XPATH STUFF
236
// #############################################################################
237

238     /**
239      * Returns the PathNode list of the last step.
240      *
241      */

242     public ArrayList JavaDoc getXTrees() {
243         return ((Step) this.steps.get(this.steps.size() - 1)).getXTrees();
244     }
245
246     // #############################################################################
247
// RESTRUCTURATION STUFF
248
// #############################################################################
249

250     // tells whether the located expression starts with a variable
251
public boolean startsWithVariable() {
252         return ((Step)steps.get(0)).getExpression() instanceof Variable;
253     }
254
255     // tells whether the LocatedExpression has a parent step
256
public int hasParentStep() {
257         if (steps != null)
258             for (int i = 0; i < steps.size(); i++)
259                 if (((Step) steps.get(i)).getAxis() == Axis.PARENT)
260                     return i;
261         return -1;
262     }
263
264     // tells whether the LocatedExpression has a parent step
265
public int hasUnionStep() {
266         if (steps != null)
267             for (int i = 0; i < steps.size(); i++)
268                 if (((Step) steps.get(i)).getExpression() instanceof ListOpUNIONExpression)
269                     return i;
270         return -1;
271     }
272
273     // tells whether the LocatedExpression has predicates
274
public boolean hasPredicates() {
275         if (steps != null)
276             for (int i = 0; i < steps.size(); i++)
277                 if (((Step) steps.get(i)).hasPredicates())
278                     return true;
279         return false;
280     }
281
282     // tells whether the LocatedExpression has position predicates
283
public boolean hasPositionPredicates() {
284         if (steps != null)
285             for (int i = 0; i < steps.size(); i++)
286                 if (((Step) steps.get(i)).hasPositionPredicates())
287                     return true;
288         return false;
289     }
290
291 }
292
Popular Tags