1 22 23 package org.xquark.xquery.normalize; 24 25 import java.util.ArrayList ; 26 import java.util.HashMap ; 27 28 import org.xquark.xpath.Axis; 29 import org.xquark.xquery.parser.*; 30 import org.xquark.xquery.typing.TypeVisitor; 31 32 public class SubstituteVisitor extends DefaultParserVisitor { 33 private static final String RCSRevision = "$Revision: 1.4 $"; 34 private static final String RCSName = "$Name: $"; 35 36 private HashMap map = null; 37 private boolean inReturn = false; 38 private TypeVisitor typeVisitor = null; 39 40 public SubstituteVisitor(HashMap map,TypeVisitor typeVisitor) { 41 setMap(map); 42 this.typeVisitor = typeVisitor; 43 } 44 45 public void setMap(HashMap map) { this.map = map; } 46 47 49 public void visit(AttributeValuePair arg) throws XQueryException { 50 XQueryExpression expr = null; 51 XQueryExpression valueExpr = arg.getAttributeValue(); 52 if ((expr = (XQueryExpression)map.get(valueExpr)) != null) { 53 ArrayList parenti = valueExpr.getParentExpression(); 54 int indexi = parenti.indexOf(arg); 55 if (indexi != -1) parenti.remove(indexi); 56 try { 57 XQueryExpression exprClone = (XQueryExpression)expr.clone(); 58 arg.setAttributeValue(exprClone); 59 } 60 catch (CloneNotSupportedException e) { throw new XQueryException("Could not clone expression : " + expr); } 61 } 62 else valueExpr.accept(this); 63 } 64 65 71 public void visit(Element arg) throws XQueryException { 72 XQueryExpression expr = null; 73 expr = arg.getStartTag(); 74 if ((expr = (XQueryExpression)map.get(expr)) != null) 75 arg.setStartTag(expr); 76 else 77 arg.getStartTag().accept(this); 78 ArrayList attributes = arg.getAttributes(); 79 if (attributes != null) { 80 for (int i=0;i<attributes.size();i++) { 81 AttributeValuePair subi = (AttributeValuePair)attributes.get(i); 82 subi.accept(this); 83 } 84 } 85 ArrayList subExpressions = arg.getSubExpressions(); 86 if (subExpressions != null) { 87 for (int i=0;i<subExpressions.size();i++) { 88 XQueryExpression subi = (XQueryExpression)subExpressions.get(i); 89 if ((expr = (XQueryExpression)map.get(subi)) != null) { 90 subExpressions.remove(i); 91 ArrayList parenti = subi.getParentExpression(); 92 int indexi = parenti.indexOf(arg); 93 if (indexi != -1) parenti.remove(indexi); 94 try { 95 XQueryExpression exprClone = (XQueryExpression)expr.clone(); 96 arg.addSubExpressions(i,exprClone); 97 } 98 catch (CloneNotSupportedException e) { throw new XQueryException("Could not clone expression : " + expr); } 99 } 100 else subi.accept(this); 101 } 102 } 103 } 104 105 public void visit(FLWRExpression arg) throws XQueryException { 106 ArrayList variables = arg.getVariables(); 107 XQueryExpression expr = null; 108 for (int i=0;i<variables.size();i++) { 109 Variable vari = (Variable)variables.get(i); 110 XQueryExpression variExpr = vari.getExpression(); 111 if ((expr = (XQueryExpression)map.get(variExpr)) != null) { 112 ArrayList parenti = variExpr.getParentExpression(); 113 int indexi = parenti.indexOf(arg); 114 if (indexi != -1) parenti.remove(indexi); 115 try { 116 XQueryExpression exprClone = (XQueryExpression)expr.clone(); 117 vari.setExpression(exprClone, false); 118 } 119 catch (CloneNotSupportedException e) { throw new XQueryException("Could not clone expression : " + expr); } 120 } 121 else variExpr.accept(this); 122 } 123 ArrayList orderbys = arg.getOrderBy(); 124 if (orderbys != null) { 125 for (int i=0;i<orderbys.size();i++) { 126 XQueryExpression expri = (XQueryExpression)orderbys.get(i); 127 if ((expr = (XQueryExpression)map.get(expri)) != null) { 128 ArrayList parenti = expri.getParentExpression(); 129 int indexi = parenti.indexOf(arg); 130 if (indexi != -1) parenti.remove(indexi); 131 try { 132 XQueryExpression exprClone = (XQueryExpression)expr.clone(); 133 orderbys.set(i,exprClone); 134 } 135 catch (CloneNotSupportedException e) { throw new XQueryException("Could not clone expression : " + expr); } 136 } 137 else expri.accept(this); 138 } 139 } 140 XQueryExpression whereClause = arg.getWhereClause(); 141 if (whereClause != null){ 142 if ((expr = (XQueryExpression)map.get(whereClause)) != null) { 143 try { 144 XQueryExpression exprClone = (XQueryExpression)expr.clone(); 145 arg.setWhereClause(exprClone); 146 } 147 catch (CloneNotSupportedException e) { throw new XQueryException("Could not clone expression : " + expr); } 148 } 149 else whereClause.accept(this); 150 } 151 XQueryExpression returnClause = arg.getReturnClause(); 152 if ((expr = (XQueryExpression)map.get(returnClause)) != null) { 153 try { 154 XQueryExpression exprClone = (XQueryExpression)expr.clone(); 155 arg.setReturnClause(exprClone); 156 } 157 catch (CloneNotSupportedException e) { throw new XQueryException("Could not clone expression : " + expr); } 158 } 159 else returnClause.accept(this); 160 } 161 162 public void visit(FunctionCall arg) throws XQueryException { 163 ArrayList arguments = arg.getArguments(); 164 if (arguments == null) return; 165 XQueryExpression expr = null; 166 for (int i=0;i<arguments.size();i++) { 167 XQueryExpression argi = (XQueryExpression)arguments.get(i); 168 if ((expr = (XQueryExpression)map.get(argi)) != null) { 169 arguments.remove(i); 170 ArrayList parenti = argi.getParentExpression(); 171 int indexi = parenti.indexOf(arg); 172 if (indexi != -1) parenti.remove(indexi); 173 try { 174 XQueryExpression exprClone = (XQueryExpression)expr.clone(); 175 arg.addArguments(i,exprClone); 176 } 177 catch (CloneNotSupportedException e) { throw new XQueryException("Could not clone expression : " + expr); } 178 } 179 else argi.accept(this); 180 } 181 } 182 public void visit(LocatedExpression arg) throws XQueryException { 195 ArrayList steps = arg.getSteps(); 196 XQueryExpression expr = null; 197 boolean change = false; 198 for (int i=0;i<steps.size();i++) { 199 Step stepi = (Step)steps.get(i); 200 XQueryExpression stepExpr = stepi.getExpression(); 201 if ((expr = (XQueryExpression)map.get(stepExpr)) != null) { 202 steps.remove(i); 204 if (stepi.getPredicates() != null) { 205 ArrayList addSteps = expr.getSteps(); 206 Step lastStep = (Step)addSteps.get(addSteps.size()-1); 207 lastStep.addPredicates(stepi.getPredicates()); 208 } 209 if (expr instanceof LocatedExpression) { 210 try { 211 LocatedExpression locClone = (LocatedExpression)expr.clone(); 212 arg.addStepsAt(i,locClone.getSteps()); 213 } 214 catch (CloneNotSupportedException e) { throw new XQueryException("Could not clone expression : " + expr); } 215 } else { 216 try { 217 ArrayList list = new ArrayList (1); 218 list.add(new Step(false,Axis.NONE,(XQueryExpression)expr.clone(),null,arg.getParentModule())); 219 arg.addStepsAt(i,list); 220 } 221 catch (CloneNotSupportedException e) { throw new XQueryException("Could not clone expression : " + expr); } 222 } 223 change = true; 224 225 277 } 278 stepi.accept(this); 279 } 280 if (change) arg.accept(this.typeVisitor); 281 } 282 public void visit(QuantifiedExpression arg) throws XQueryException { 287 ArrayList variables = arg.getVariables(); 288 XQueryExpression expr = null; 289 for (int i=0;i<variables.size();i++) { 290 Variable vari = (Variable)variables.get(i); 291 XQueryExpression variExpr = vari.getExpression(); 292 if ((expr = (XQueryExpression)map.get(variExpr)) != null) { 293 ArrayList parenti = variExpr.getParentExpression(); 294 int indexi = parenti.indexOf(arg); 295 if (indexi != -1) parenti.remove(indexi); 296 try { 297 XQueryExpression exprClone = (XQueryExpression)expr.clone(); 298 vari.setExpression(exprClone, false); 299 } 300 catch (CloneNotSupportedException e) { throw new XQueryException("Could not clone expression : " + expr); } 301 } 302 else variExpr.accept(this); 303 } 304 XQueryExpression tmpExpr = arg.getExpression(); 305 if ((expr = (XQueryExpression)map.get(tmpExpr)) != null) { 306 try { 307 XQueryExpression exprClone = (XQueryExpression)expr.clone(); 308 arg.setExpression(exprClone); 309 } 310 catch (CloneNotSupportedException e) { throw new XQueryException("Could not clone expression : " + expr); } 311 } 312 else tmpExpr.accept(this); 313 } 314 318 public void visit(Step arg) throws XQueryException { 319 XQueryExpression stepExpr = arg.getExpression(); 320 XQueryExpression expr = null; 321 if ((expr = (XQueryExpression)map.get(stepExpr)) != null && !(expr instanceof LocatedExpression)) { 322 ArrayList parenti = stepExpr.getParentExpression(); 323 int indexi = parenti.indexOf(arg); 324 if (indexi != -1) parenti.remove(indexi); 325 try { 326 XQueryExpression exprClone = (XQueryExpression)expr.clone(); 327 arg.setExpression(exprClone); 328 } 329 catch (CloneNotSupportedException e) { throw new XQueryException("Could not clone expression : " + expr); } 330 } 331 else { 332 stepExpr.accept(this); 333 ArrayList predicates = arg.getPredicates(); 334 expr = null; 335 if (predicates != null) 336 for (int i=0;i<predicates.size();i++) { 337 XQueryExpression subi = (XQueryExpression)predicates.get(i); 338 if ((expr = (XQueryExpression)map.get(subi)) != null) { 339 predicates.remove(i); 340 ArrayList parenti = subi.getParentExpression(); 341 int indexi = parenti.indexOf(arg); 342 if (indexi != -1) parenti.remove(indexi); 343 try { 344 XQueryExpression exprClone = (XQueryExpression)expr.clone(); 345 arg.addPredicate(i,exprClone); 346 } 347 catch (CloneNotSupportedException e) { throw new XQueryException("Could not clone expression : " + expr); } 348 } 349 else subi.accept(this); 350 } 351 } 352 } 353 354 363 public void visit(Variable arg) throws XQueryException { 364 XQueryExpression expr = null; 365 XQueryExpression varExpr = arg.getExpression(); 366 if (varExpr == null) return; 367 if ((expr = (XQueryExpression)map.get(varExpr)) != null) { 368 try { 369 XQueryExpression exprClone = (XQueryExpression)expr.clone(); 370 arg.setExpression(exprClone, false); 371 } 372 catch (CloneNotSupportedException e) { throw new XQueryException("Could not clone expression : " + expr); } 373 } 374 } 376 377 public void visit(XQueryBinaryOperatorExpression arg) throws XQueryException { 378 XQueryExpression expr = null; 379 XQueryExpression tmpExpr = arg.getExpression1(); 380 if ((expr = (XQueryExpression)map.get(tmpExpr)) != null) { 381 try { 382 XQueryExpression exprClone = (XQueryExpression)expr.clone(); 383 arg.setExpression1(exprClone); 384 } 385 catch (CloneNotSupportedException e) { throw new XQueryException("Could not clone expression : " + expr); } 386 } 387 else tmpExpr.accept(this); 388 tmpExpr = arg.getExpression2(); 389 if ((expr = (XQueryExpression)map.get(tmpExpr)) != null) { 390 try { 391 XQueryExpression exprClone = (XQueryExpression)expr.clone(); 392 arg.setExpression2(exprClone); 393 } 394 catch (CloneNotSupportedException e) { throw new XQueryException("Could not clone expression : " + expr); } 395 } 396 else tmpExpr.accept(this); 397 } 398 399 public void visit(XQueryExpression arg) throws XQueryException { 401 } 402 403 public void visit(XQueryExpressionSequence arg) throws XQueryException { 404 ArrayList subExpressions = arg.getSubExpressions(); 405 XQueryExpression expr = null; 406 for (int i=0;i<subExpressions.size();i++) { 407 XQueryExpression subi = (XQueryExpression)subExpressions.get(i); 408 if ((expr = (XQueryExpression)map.get(subi)) != null) { 409 subExpressions.remove(i); 410 ArrayList parenti = subi.getParentExpression(); 411 int indexi = parenti.indexOf(arg); 412 if (indexi != -1) parenti.remove(indexi); 413 try { 414 XQueryExpression exprClone = (XQueryExpression)expr.clone(); 415 arg.addSubExpressions(i,exprClone); 416 } 417 catch (CloneNotSupportedException e) { throw new XQueryException("Could not clone expression : " + expr); } 418 } 419 else subi.accept(this); 420 } 421 } 422 423 427 public void visit(XQueryUnaryOperatorExpression arg) throws XQueryException { 428 XQueryExpression expr = null; 429 XQueryExpression tmpExpr = arg.getExpression(); 430 if ((expr = (XQueryExpression)map.get(tmpExpr)) != null) { 431 try { 432 XQueryExpression exprClone = (XQueryExpression)expr.clone(); 433 arg.setExpression(exprClone); 434 } 435 catch (CloneNotSupportedException e) { throw new XQueryException("Could not clone expression : " + expr); } 436 } 437 else tmpExpr.accept(this); 438 } 439 440 443 445 } 469 | Popular Tags |