1 22 23 package org.xquark.xquery.parser; 24 25 import java.util.ArrayList ; 26 27 import org.xquark.xquery.parser.hinttree.HintTree; 28 import org.xquark.xquery.parser.util.Constants; 29 import org.xquark.xquery.typing.TypeException; 30 31 public class FLWRExpression extends XQueryExpression implements Cloneable { 32 33 private static final String RCSRevision = "$Revision: 1.10 $"; 34 private static final String RCSName = "$Name: $"; 35 36 protected ArrayList variables = null; 38 protected XQueryExpression whereClause = null; 39 protected XQueryExpression returnClause = null; 41 protected ArrayList orderby = null; 43 protected ArrayList listWhereAdded = new ArrayList (); 44 protected HintTree hinttree = null; 45 49 public void accept(ParserVisitor visitor) throws XQueryException { 50 visitor.visit(this); 51 } 52 53 57 public FLWRExpression(ArrayList variables, XQueryExpression whereClause, XQueryExpression returnClause, XQueryModule parentModule) throws TypeException, XQueryException { 58 setVariables(variables); 59 setWhereClause(whereClause); 60 setReturnClause(returnClause,false); 61 setParentModule(parentModule); 62 if (parentModule != null && parentModule.getStaticContext().getTypeVisitor() != null) 63 accept(parentModule.getStaticContext().getTypeVisitor()); 64 } 65 66 public FLWRExpression(ArrayList variables, ArrayList orderby, XQueryExpression whereClause, XQueryExpression returnClause, XQueryModule parentModule) throws TypeException, XQueryException { 67 setVariables(variables); 68 setOrderBy(orderby); 69 setWhereClause(whereClause); 70 setReturnClause(returnClause,false); 71 setParentModule(parentModule); 72 if (parentModule != null && parentModule.getStaticContext().getTypeVisitor() != null) 73 accept(parentModule.getStaticContext().getTypeVisitor()); 74 } 75 76 public FLWRExpression(ArrayList variables, HintTree hintClause, XQueryExpression whereClause, ArrayList orderby, XQueryExpression returnClause, XQueryModule parentModule) throws TypeException, XQueryException { 77 setVariables(variables); 78 setHintTree(hintClause); 79 setWhereClause(whereClause); 80 setOrderBy(orderby); 81 setReturnClause(returnClause,false); 82 setParentModule(parentModule); 83 if (parentModule != null && parentModule.getStaticContext().getTypeVisitor() != null) 84 accept(parentModule.getStaticContext().getTypeVisitor()); 85 } 86 87 public FLWRExpression(ArrayList variables, HintTree hintClause, ArrayList orderby, XQueryExpression whereClause, XQueryExpression returnClause, XQueryModule parentModule) throws TypeException, XQueryException { 88 setVariables(variables); 89 setHintTree(hintClause); 90 setOrderBy(orderby); 91 setWhereClause(whereClause); 92 setReturnClause(returnClause,false); 93 setParentModule(parentModule); 94 if (parentModule != null && parentModule.getStaticContext().getTypeVisitor() != null) 95 accept(parentModule.getStaticContext().getTypeVisitor()); 96 } 97 98 102 public ArrayList getVariables() { 103 return variables; 104 } 105 public void setVariables(ArrayList variables) throws XQueryException { 106 if (variables == null || variables.isEmpty()) 108 throw new XQueryException("variables of FLWRExpression cannot be null or empty"); 109 this.variables = variables; 110 for (int i = 0; i < this.variables.size(); i++) { 111 Variable var = (Variable) this.variables.get(i); 112 var.setParentFLWRExpression(this); 113 var.setParentModule(parentModule); 114 } 115 } 116 public void insertVariable(Variable variable) { 117 variable.setParentFLWRExpression(this); 119 variable.setParentModule(parentModule); 120 variables.add(0, variable); 121 } 122 public void addVariable(Variable variable) { 123 variable.setParentFLWRExpression(this); 125 variable.setParentModule(parentModule); 126 variables.add(variable); 127 } 128 public void addVariables(ArrayList varlist) { 129 if (varlist == null || varlist.isEmpty()) 130 return; 131 for (int i = 0; i < varlist.size(); i++) 132 addVariable((Variable) varlist.get(i)); 133 } 134 public void addVariable(int index, Variable variable) { 135 variable.setParentFLWRExpression(this); 137 variable.setParentModule(parentModule); 138 variables.add(index, variable); 139 } 140 public void addVariables(int index, ArrayList varlist) { 141 if (varlist == null || varlist.isEmpty()) 142 return; 143 for (int i = 0; i < varlist.size(); i++) 144 addVariable(index+i, (Variable) varlist.get(i)); 145 } 146 147 public ArrayList getOrderBy() { 148 return orderby; 149 } 150 public void setOrderBy(ArrayList orderby) { 151 this.orderby = orderby; 152 } 153 154 public void addOrderBy(XQueryExpression expr) { 155 expr.setParentExpression(this); 157 expr.setParentModule(parentModule); 158 if (orderby == null) 159 orderby = new ArrayList (1); 160 orderby.add(expr); 161 } 162 163 public void addOrderBy(int index, XQueryExpression expr) { 164 expr.setParentExpression(this); 166 expr.setParentModule(parentModule); 167 if (orderby == null) 168 orderby = new ArrayList (1); 169 orderby.add(index, expr); 170 } 171 172 public XQueryExpression getReturnClause() { 173 return returnClause; 174 } 175 public void setReturnClause(XQueryExpression returnClause) throws XQueryException { 176 setReturnClause(returnClause,true); 177 } 178 public void setReturnClause(XQueryExpression returnClause, boolean typing) throws XQueryException { 179 if (returnClause == null) 181 throw new XQueryException("returnClause of FWLRExpression cannot be null"); 182 this.returnClause = returnClause; 183 this.returnClause.setParentExpression(this); 184 if (typing && parentModule != null && parentModule.getStaticContext().getTypeVisitor() != null) { 185 qtype = null; 186 accept(parentModule.getStaticContext().getTypeVisitor()); 187 } 188 } 189 190 public XQueryExpression getWhereClause() { 191 return whereClause; 192 } 193 public void setWhereClause(XQueryExpression whereClause) { 194 this.whereClause = whereClause; 195 if (this.whereClause != null) { 196 this.whereClause.setParentExpression(this); 197 this.whereClause.setParentModule(parentModule); 198 } 199 } 200 201 public int getVarPosition(Variable var) { 202 if (variables == null) 203 return -1; 204 return variables.indexOf(var); 205 } 206 207 public ArrayList getXTrees() { 208 return returnClause.getXTrees(); 209 } 210 211 public HintTree getHintTree() { 212 return hinttree; 213 } 214 215 public void setHintTree(HintTree hinttree) { 216 this.hinttree = hinttree; 217 } 218 219 private int GetFirstForVariable(int index) { 220 for (int i = index; i < variables.size(); i++) { 221 if (((Variable) variables.get(i)).getBindingType() == Constants.FOR_BINDINGTYPE) 222 return i; 223 } 224 return -1; 225 } 226 227 231 public void addParentExpression(XQueryExpression parentExpression) { 232 addParentExpression(parentExpression); 233 234 if (variables != null) 235 for (int i = 0; i < variables.size(); i++) 236 ((Variable) variables.get(i)).addParentExpression(parentExpression); 237 if (whereClause != null) 238 whereClause.addParentExpression(parentExpression); 239 if (returnClause != null) 240 returnClause.addParentExpression(parentExpression); 241 } 242 243 public void insertVariableAtPosition(Variable var, int position) { 247 variables.add(position, var); 248 var.setParentFLWRExpression(this); 249 var.setParentModule(parentModule); 250 } 251 252 public void addWhereClause(XQueryExpression addWhereClause) throws XQueryException { 253 if (addWhereClause == null) 254 return; 255 if (whereClause == null) { 256 listWhereAdded.add(addWhereClause); 257 setWhereClause(addWhereClause); 258 return; 259 } 260 if (!listWhereAdded.contains(addWhereClause)) { 261 listWhereAdded.add(addWhereClause); 262 whereClause.setParenthesis(true); 263 addWhereClause.setParenthesis(true); 264 setWhereClause(new BinOpANDExpression(whereClause, addWhereClause, parentModule)); 265 } 266 } 267 268 } 269 | Popular Tags |