1 23 24 package org.xquark.xquery.parser; 25 26 import java.util.ArrayList ; 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 { 35 36 private static final String RCSRevision = "$Revision: 1.18 $"; 37 private static final String RCSName = "$Name: $"; 38 39 protected ArrayList steps = null; 41 42 protected boolean inPredicate = false; 43 protected int nodeAccessor = Step.NONE; 45 46 50 public void accept(ParserVisitor visitor) throws XQueryException { 51 visitor.visit(this); 52 } 53 54 58 public LocatedExpression(ArrayList steps, XQueryModule parentModule) throws TypeException, XQueryException { 59 this(steps, false, parentModule); 60 } 61 62 public LocatedExpression(ArrayList steps, boolean inPredicate, XQueryModule parentModule) throws TypeException, XQueryException { 63 init(steps,inPredicate,parentModule,true); 64 } 65 public LocatedExpression(ArrayList steps, boolean inPredicate, XQueryModule parentModule, boolean simplify) throws TypeException, XQueryException { 66 init(steps,inPredicate,parentModule,simplify); 67 } 68 69 private void init(ArrayList 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 88 public String toString() { 89 return toString(false, false, false); 90 } 91 92 public String 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 getSteps() { 110 return steps; 111 } 112 public void setSteps(ArrayList steps) throws XQueryException { 113 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 (1); 130 steps.add(step); 131 setSteps(steps); 132 } 133 134 public void addSteps(ArrayList 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 (1); 154 steps.add(index,step); 155 setSteps(steps); 156 } 157 158 public void addStepsAt(int index, ArrayList 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 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 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 clone() throws CloneNotSupportedException { 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 ("Could not clone : " + this); 230 } 231 } 232 233 234 238 242 public ArrayList getXTrees() { 243 return ((Step) this.steps.get(this.steps.size() - 1)).getXTrees(); 244 } 245 246 250 public boolean startsWithVariable() { 252 return ((Step)steps.get(0)).getExpression() instanceof Variable; 253 } 254 255 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 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 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 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 |