1 23 24 package org.xquark.xquery.normalize; 25 26 import java.util.ArrayList ; 27 import java.util.HashMap ; 28 import java.util.Stack ; 29 30 import org.xquark.xquery.parser.*; 31 import org.xquark.xquery.parser.util.Constants; 32 import org.xquark.xquery.typing.TypeVisitor; 33 34 public class CanonizeVisitor extends DefaultParserVisitor { 35 36 private static final String RCSRevision = "$Revision: 1.4 $"; 37 private static final String RCSName = "$Name: $"; 38 39 private Stack parents = null; 40 private Stack FLWRParents = null; 41 private XQueryExpression resultExpr = null; 42 43 private ArrayList QDB = null; 44 private XQueryExpression QMEM = null; 45 private int inReturn = 0; 46 private int inWhere = 0; 47 private Variable inVarDef = null; 48 49 protected TypeVisitor typeVisitor = null; 50 51 public CanonizeVisitor(TypeVisitor typeVisitor) { 53 parents = new Stack (); 54 FLWRParents = new Stack (); 55 this.typeVisitor = typeVisitor; 56 } 57 58 public XQueryExpression getResult() { 59 return resultExpr; 60 } 61 public ArrayList getQDB() { 62 return QDB; 63 } 64 public XQueryExpression getQMEM() { 65 return QMEM; 66 } 67 68 70 public void visit(Element arg) throws XQueryException { 71 resultExpr = null; 72 parents.push(arg); 73 ArrayList attributes = arg.getAttributes(); 74 if (attributes != null) { 75 for (int i = 0; i < attributes.size(); i++) { 76 XQueryExpression expri = (XQueryExpression) attributes.get(i); 77 expri.accept(this); 78 while (resultExpr != null) { 79 attributes.set(i, resultExpr); 80 expri = (XQueryExpression) attributes.get(i); 81 resultExpr = null; 82 expri.accept(this); 83 } 84 } 85 } 86 ArrayList subExpressions = arg.getSubExpressions(); 87 if (subExpressions != null) { 88 for (int i = 0; i < subExpressions.size(); i++) { 89 XQueryExpression expri = (XQueryExpression) subExpressions.get(i); 90 expri.accept(this); 91 while (resultExpr != null) { 92 subExpressions.set(i, resultExpr); 93 expri = (XQueryExpression) subExpressions.get(i); 94 resultExpr = null; 95 expri.accept(this); 96 } 97 } 98 } 99 parents.pop(); 100 } 101 102 public void visit(FLWRExpression arg) throws XQueryException { 103 resultExpr = null; 104 int i, j, k; 105 if (inReturn > 0 && !FLWRParents.isEmpty()) { 107 FLWRExpression parentFLWRExpr = (FLWRExpression) FLWRParents.peek(); 108 Variable var = typeVisitor.getStaticContext().createVariable(Constants.OUTERLET_BINDINGTYPE, arg, Variable.CAN_CREATOR); 111 parentFLWRExpr.addVariable(var); 112 XQueryExpression retClause = parentFLWRExpr.getReturnClause(); 113 if (retClause == arg) 114 parentFLWRExpr.setReturnClause(var); 115 else { 116 HashMap map = new HashMap (); 118 map.put(arg, var); 119 retClause.substitute(map, typeVisitor); 120 } 121 var.accept(typeVisitor); 122 var.accept(this); 123 return; 124 } 125 126 parents.push(arg); 127 FLWRParents.push(arg); 128 129 XQueryExpression whereExpr = arg.getWhereClause(); 130 if (whereExpr != null) { 131 inWhere++; 132 whereExpr.accept(this); 133 inWhere--; 134 whereExpr = arg.getWhereClause(); 135 } 136 137 XQueryExpression returnExpr = arg.getReturnClause(); 138 inReturn++; 139 returnExpr.accept(this); 140 inReturn--; 141 returnExpr = arg.getReturnClause(); 142 143 153 ArrayList variables = arg.getVariables(); 154 Variable tmpVar = null; 155 int varsize = variables.size(); 156 for (i = 0; i < varsize; i++) { 157 tmpVar = (Variable) variables.get(i); 158 Variable tmpVarDef = inVarDef; 159 inVarDef = tmpVar; 160 tmpVar.accept(this); 161 inVarDef = tmpVarDef; 162 } 163 164 ArrayList qdb = new ArrayList (); 165 ArrayList forvarlist = new ArrayList (); 166 ArrayList letvarlist = new ArrayList (); 167 for (i = 0; i < variables.size(); i++) { 171 tmpVar = (Variable) variables.get(i); 172 if (tmpVar.getBindingType() == Constants.OUTERLET_BINDINGTYPE) 173 letvarlist.add(tmpVar); 174 else 175 forvarlist.add(tmpVar); 176 } 177 178 XQueryExpressionSequence tmpSeq = new XQueryExpressionSequence(forvarlist, arg.getParentModule()); 180 tmpSeq.setParenthesis(true); 181 FLWRExpression tmpFLWR = new FLWRExpression(forvarlist, arg.getHintTree(), arg.getOrderBy(), whereExpr, tmpSeq, arg.getParentModule()); 184 tmpFLWR.setParentModule(arg.getParentModule()); 185 tmpVar = typeVisitor.getStaticContext().createVariable(Constants.LET_BINDINGTYPE, tmpFLWR, Variable.CAN_CREATOR); 186 qdb.add(tmpVar); 187 XQueryExpression qmem = returnExpr; 188 189 ArrayList allinnerqdb = new ArrayList (); 190 for (i = 0; i < letvarlist.size(); i++) { 191 Variable letVarListi = (Variable) letvarlist.get(i); 192 FLWRExpression letVarListiExpr = (FLWRExpression) letVarListi.getExpression(); 193 letVarListiExpr.accept(this); 194 ArrayList innerqdb = QDB; 195 XQueryExpression innerqmem = QMEM; 196 ArrayList vars = letVarListiExpr.getVariables(); 198 if (!vars.isEmpty()) { 199 ArrayList dependids = new ArrayList (); 200 for (j = 0; j < vars.size(); j++) { 201 Variable varj = (Variable) vars.get(j); 202 if (varj.getBindingType() == Constants.FOR_BINDINGTYPE) 203 dependids.add(varj); 204 } 205 if (letVarListiExpr.getReturnClause().getDependIDs() == null && !dependids.isEmpty()) 206 letVarListiExpr.getReturnClause().setDependIDs(dependids); 207 } 208 209 for (j = 0; j < innerqdb.size(); j++) { 211 Variable innerQdbVari = (Variable) innerqdb.get(j); 212 FLWRExpression innerQdbVariExpr = (FLWRExpression) innerQdbVari.getExpression(); 213 for (k = 0; k < qdb.size(); k++) { 214 Variable qdbVari = (Variable) qdb.get(k); 215 XQueryExpression qdbVariExpr = qdbVari.getExpression(); 216 tmpVar = typeVisitor.getStaticContext().createVariable(Constants.FOR_BINDINGTYPE, qdbVari, Variable.CAN_CREATOR); 217 innerQdbVariExpr.insertVariable(tmpVar); 218 } 219 } 220 allinnerqdb.addAll(innerqdb); 222 if (qmem == letVarListi) { 225 arg.setReturnClause(innerqmem); 226 qmem = arg.getReturnClause(); 227 } else { 228 ArrayList parentList = letVarListi.getParentExpression(); 229 HashMap map = new HashMap (); 230 map.put(letVarListi, innerqmem); 231 for (j = 0; j < parentList.size(); j++) { 232 XQueryExpression parenti = (XQueryExpression) parentList.get(j); 233 parenti.substitute(map, typeVisitor); 234 } 235 } 236 } 237 238 qdb.addAll(allinnerqdb); 240 if (letvarlist.size() > 0) { 242 ArrayList tmpList = new ArrayList (forvarlist.size()); 243 for (i = 0; i < forvarlist.size(); i++) { 244 Variable var = (Variable) forvarlist.get(i); 245 if (var.getBindingType() != Constants.LET_BINDINGTYPE) 247 tmpList.add(var); 248 } 249 qmem.putSkolemIDs(tmpList); 250 } 251 252 QDB = qdb; 254 QMEM = qmem; 255 256 FLWRParents.pop(); 257 parents.pop(); 258 } 259 260 public void visit(Variable arg) throws XQueryException { 261 resultExpr = null; 262 parents.push(arg); 263 if (inReturn > 0) { 264 if (inVarDef != null) { 265 XQueryExpression expr = arg.getExpression(); 266 expr.accept(this); 267 } 268 } 269 parents.pop(); 270 } 271 272 public void visit(XQueryBinaryOperatorExpression arg) throws XQueryException { 273 resultExpr = null; 274 parents.push(arg); 275 XQueryExpression tmpExpr = arg.getExpression1(); 276 tmpExpr.accept(this); 277 while (resultExpr != null) { 278 arg.setExpression1(resultExpr); 279 tmpExpr = arg.getExpression1(); 280 resultExpr = null; 281 tmpExpr.accept(this); 282 } 283 tmpExpr = arg.getExpression2(); 284 tmpExpr.accept(this); 285 while (resultExpr != null) { 286 arg.setExpression2(resultExpr); 287 tmpExpr = arg.getExpression2(); 288 resultExpr = null; 289 tmpExpr.accept(this); 290 } 291 parents.pop(); 292 } 293 295 public void visit(XQueryModule arg) throws XQueryException { 296 ArrayList expressions = arg.getExpressions(); 298 if (expressions != null && expressions.size() == 1) 299 ((XQueryExpression)expressions.get(0)).accept(this); 300 else 301 resultExpr = null; 302 } 303 304 public void visit(XQueryExpression arg) throws XQueryException { 305 resultExpr = null; 306 } 307 308 public void visit(XQueryExpressionSequence arg) throws XQueryException { 309 resultExpr = null; 310 parents.push(arg); 311 ArrayList subExpressions = arg.getSubExpressions(); 312 ArrayList newSubExpressions = new ArrayList (); 313 XQueryExpression rexExpr = null; 314 if (subExpressions != null) { 315 for (int i = 0; i < subExpressions.size(); i++) { 316 XQueryExpression expri = (XQueryExpression) subExpressions.get(i); 317 expri.accept(this); 318 while (resultExpr != null) { 319 subExpressions.set(i, resultExpr); 320 expri = (XQueryExpression) subExpressions.get(i); 321 resultExpr = null; 322 expri.accept(this); 323 } 324 } 325 } 326 parents.pop(); 327 } 328 329 public void visit(XQueryUnaryOperatorExpression arg) throws XQueryException { 330 resultExpr = null; 331 parents.push(arg); 332 XQueryExpression expr = arg.getExpression(); 333 expr.accept(this); 334 while (resultExpr != null) { 335 arg.setExpression(resultExpr); 336 expr = arg.getExpression(); 337 resultExpr = null; 338 expr.accept(this); 339 } 340 parents.pop(); 341 } 342 343 } 344 | Popular Tags |