1 23 24 package org.xquark.mediator.decomposer; 25 26 import java.util.ArrayList ; 27 28 import org.xquark.xquery.normalize.*; 29 import org.xquark.xquery.parser.*; 30 31 public class IsolateWhereVisitor extends DefaultParserVisitor { 32 private static final String RCSRevision = "$Revision: 1.9 $"; 36 private static final String RCSName = "$Name: $"; 37 private ArrayList vars = null; 41 private ArrayList varsstr = null; 42 private boolean putvar = false; 43 boolean includenone = true; 44 private GetBoundVariablesVisitor gbv_visitor = null; 45 46 private XQueryExpression isolatedExpression = null; 48 private XQueryExpression remainingExpression = null; 49 53 59 public IsolateWhereVisitor() { 60 init(null, new GetBoundVariablesVisitor(true, false)); 61 } 62 public IsolateWhereVisitor(ArrayList vars) { 63 init(vars, new GetBoundVariablesVisitor(true, false)); 64 } 65 public IsolateWhereVisitor(ArrayList vars, GetBoundVariablesVisitor gbv_visitor) { 66 init(vars, gbv_visitor); 67 } 68 69 private void init(ArrayList vars, GetBoundVariablesVisitor gbv_visitor) { 70 this.isolatedExpression = null; 71 this.remainingExpression = null; 72 this.vars = vars; 73 if (this.vars == null) 74 this.vars = new ArrayList (); 75 varsstr = new ArrayList (this.vars.size()); 76 for (int i = 0; i < this.vars.size(); i++) 77 varsstr.add(((Variable) this.vars.get(i)).getStringValue()); 78 this.gbv_visitor = gbv_visitor; 79 gbv_visitor.reset(); 80 } 81 82 public void includeNone(boolean includenone) { 83 this.includenone = includenone; 84 } 85 86 public void putVar(boolean putvar) { 87 this.putvar = putvar; 88 } 89 public XQueryExpression getIsolatedExpression() { 90 return isolatedExpression; 91 } 92 public XQueryExpression getRemainingExpression() { 93 return remainingExpression; 94 } 95 public void reset(ArrayList vars) { 96 init(vars, this.gbv_visitor); 97 } 98 99 104 public void visit(BinOpANDExpression arg) throws XQueryException { 105 arg.getExpression1().accept(this); 106 XQueryExpression isolated1 = isolatedExpression; 108 XQueryExpression remaining1 = remainingExpression; 109 arg.getExpression2().accept(this); 110 XQueryExpression isolated2 = isolatedExpression; 112 XQueryExpression remaining2 = remainingExpression; 113 114 if (isolated1 == arg.getExpression1() && isolated2 == arg.getExpression2()) { 115 isolatedExpression = arg; 116 remainingExpression = null; 117 } else { 118 if (isolated1 != null) 119 if (isolated2 != null) 120 isolatedExpression = new BinOpANDExpression(isolated1, isolated2, null); 121 else 122 isolatedExpression = isolated1; 123 else 124 isolatedExpression = isolated2; 125 if (remaining1 != null) 126 if (remaining2 != null) 127 remainingExpression = new BinOpANDExpression(remaining1, remaining2, null); 128 else 129 remainingExpression = remaining1; 130 else 131 remainingExpression = remaining2; 132 } 133 } 134 135 public void visit(BinOpORExpression arg) throws XQueryException { 136 137 arg.getExpression1().accept(this); 138 XQueryExpression remaining1 = remainingExpression; 141 if (remaining1 != null) { 142 isolatedExpression = null; 143 remainingExpression = arg; 144 return; 145 } 146 147 arg.getExpression2().accept(this); 148 XQueryExpression remaining2 = remainingExpression; 151 152 if (remaining2 == null) { 153 isolatedExpression = arg; 154 remainingExpression = null; 155 } else { 156 isolatedExpression = null; 157 remainingExpression = arg; 158 } 159 160 } 180 181 183 public void visit(FLWRExpression arg) throws XQueryException { 189 ArrayList variables = arg.getVariables(); 190 if (putvar) 192 for (int i = 0; i < variables.size(); i++) { 193 Variable tmpvar = (Variable) variables.get(i); 194 if (!varsstr.contains(tmpvar.getStringValue())) { 195 vars.add(tmpvar); 196 varsstr.add(tmpvar.getStringValue()); 197 } 198 } 199 isolatedExpression = null; 200 XQueryExpression wexp = arg.getWhereClause(); 201 if (wexp != null) 202 wexp.accept(this); 203 XQueryExpression newwexp = isolatedExpression; 205 206 XQueryExpression rexp = arg.getReturnClause(); 208 rexp.accept(gbv_visitor); 209 XQueryExpression newrexp = new XQueryExpressionSequence(gbv_visitor.getVariables(), null); 210 211 ArrayList flwrvars = arg.getVariables(); 212 213 if (putvar) 214 isolatedExpression = new FLWRExpression(variables, newwexp, newrexp, null); 215 else 216 isolatedExpression = new FLWRExpression(vars, newwexp, newrexp, null); 217 isolatedExpression.setParentModule(arg.getParentModule()); 218 remainingExpression = null; 219 } 220 221 public void visit(FunctionCall arg) throws XQueryException { 222 if (arg.getArguments() != null) { 223 for (int i = 0; i < arg.getArguments().size(); i++) { 224 XQueryExpression expi = (XQueryExpression) arg.getArgument(i); 225 expi.accept(this); 226 if (isolatedExpression == null && remainingExpression == null) 227 return; 228 if (remainingExpression != null) { 229 isolatedExpression = null; 230 remainingExpression = arg; 231 return; 232 } 233 } 234 } 235 isolatedExpression = arg; 236 remainingExpression = null; 237 } 238 239 public void visit(QuantifiedExpression arg) throws XQueryException { 257 isolatedExpression = null; 259 remainingExpression = null; 260 265 } 266 public void visit(Variable arg) throws XQueryException { 278 if (varsstr.contains(arg.getStringValue())) { 279 isolatedExpression = arg; 280 remainingExpression = null; 281 } else { 282 isolatedExpression = null; 283 remainingExpression = arg; 284 } 285 } 286 287 public void visit(XQueryExpression arg) throws XQueryException { 290 gbv_visitor.reset(); 292 arg.accept(gbv_visitor); 293 ArrayList boundvars = gbv_visitor.getVariables(); 294 295 if (boundvars != null && !boundvars.isEmpty()) 296 for (int i = 0; i < boundvars.size(); i++) 297 if (!varsstr.contains(((Variable) boundvars.get(i)).getStringValue())) { 298 isolatedExpression = null; 299 remainingExpression = arg; 300 return; 301 } 302 if ((boundvars == null || boundvars.isEmpty()) && !includenone) { 303 isolatedExpression = null; 304 remainingExpression = arg; 305 } else { 306 isolatedExpression = arg; 307 remainingExpression = null; 308 } 309 } 310 311 public void visit(XQueryExpressionSequence arg) throws XQueryException { 312 ArrayList sequence = arg.getSubExpressions(); 313 ArrayList alnew = new ArrayList (); 314 ArrayList alold = new ArrayList (); 315 for (int i = 0; i < sequence.size(); i++) { 316 XQueryExpression expi = (XQueryExpression) sequence.get(i); 317 expi.accept(this); 318 XQueryExpression isolated = isolatedExpression; 319 XQueryExpression remaining = remainingExpression; 320 if (isolated != null) 321 alnew.add(isolated); 322 if (remaining != null) 323 alold.add(remaining); 324 } 325 326 if (alnew.isEmpty()) 327 isolatedExpression = null; 328 else { 329 isolatedExpression = new XQueryExpressionSequence(alnew, null); 330 isolatedExpression.setParenthesis(true); 331 } 332 if (alold.isEmpty()) 333 remainingExpression = null; 334 else { 335 remainingExpression = new XQueryExpressionSequence(alold, null); 336 remainingExpression.setParenthesis(true); 337 } 338 } 339 340 } 344 | Popular Tags |