1 22 23 package org.xquark.mediator.decomposer; 24 25 import java.util.ArrayList ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 29 import org.xquark.xquery.parser.*; 30 import org.xquark.xquery.parser.util.Constants; 31 32 public class GetVariablePathsVisitor extends DefaultParserVisitor { 33 private static final String RCSRevision = "$Revision: 1.8 $"; 37 private static final String RCSName = "$Name: $"; 38 private HashMap varpaths = null; 42 private ArrayList pathstrlist = null; 44 private boolean eliminateDuplicates = false; 45 private ArrayList allDefinedVars = null; 46 47 private ArrayList currentlist = null; 48 private boolean isrootqdb = false; 49 private boolean isqdb = false; 50 private boolean ismonosource = false; 51 private boolean inWhere = false; 52 private Variable defvar = null; 53 private ArrayList onlyvariables = null; 56 private GetUsedVariablesVisitor guvv = new GetUsedVariablesVisitor(); 57 58 65 71 72 74 public GetVariablePathsVisitor(boolean eliminateDuplicates) { 75 this.eliminateDuplicates = eliminateDuplicates; 76 varpaths = new HashMap (); 77 pathstrlist = new ArrayList (); 78 } 79 80 public GetVariablePathsVisitor(boolean eliminateDuplicates, HashMap varpaths, ArrayList pathstrlist) { 81 this.eliminateDuplicates = eliminateDuplicates; 82 this.varpaths = varpaths; 83 this.pathstrlist = pathstrlist; 84 } 85 86 public ArrayList getPathStrList() { 87 return pathstrlist; 88 } 89 public HashMap getVarPaths() { 90 return varpaths; 91 } 92 public ArrayList getVarPaths(Variable var) { 93 return (ArrayList ) varpaths.get(var); 94 } 95 public ArrayList getPaths() { 96 ArrayList retlist = new ArrayList (); 97 for (Iterator it = varpaths.keySet().iterator(); it.hasNext();) 98 retlist.addAll((ArrayList ) varpaths.get(it.next())); 99 if (retlist.isEmpty()) 100 return null; 101 return retlist; 102 } 103 104 public ArrayList getPaths(ArrayList variables) { 105 if (variables == null) return null; 106 ArrayList retlist = new ArrayList (); 107 for (Iterator it = varpaths.keySet().iterator(); it.hasNext();) { 108 Object tmpvar = it.next(); 109 if (variables.contains(tmpvar)) 110 retlist.addAll((ArrayList ) varpaths.get(tmpvar)); 111 } 112 if (retlist.isEmpty()) 113 return null; 114 return retlist; 115 } 116 117 121 public void reset() { 122 varpaths.clear(); 123 pathstrlist.clear(); 124 } 127 128 public void resetPaths() { 129 varpaths.clear(); 130 pathstrlist.clear(); 131 onlyvariables = null; 132 } 133 134 public void setRootQDB(boolean isrootqdb) { 135 this.isrootqdb = isrootqdb; 136 } 137 public void setQDB(boolean isqdb) { 138 this.isqdb = isqdb; 139 } 140 public void setMonoSource(boolean ismonosource) { 141 this.ismonosource = ismonosource; 142 } 143 144 public void setVariables(ArrayList variables) { 145 this.onlyvariables = variables; 146 } 147 public void setCurrentList(ArrayList currentlist) { 148 this.currentlist = currentlist; 149 } 150 private void addVarList(ArrayList addlist) { 151 if (this.allDefinedVars == null) 152 this.allDefinedVars = new ArrayList (); 153 this.allDefinedVars.addAll(addlist); 154 } 155 private void addVar(Variable var) { 156 if (this.allDefinedVars == null) 157 this.allDefinedVars = new ArrayList (); 158 this.allDefinedVars.add(var); 159 } 160 private void removeVarList(ArrayList removelist) { 161 if (this.allDefinedVars != null) 162 this.allDefinedVars.removeAll(removelist); 163 } 164 public void resetCurrentVarList() { 165 this.currentlist = null; 166 } 167 172 private void addVarPath(Variable var, XQueryExpression expr) { 173 if (onlyvariables != null && !onlyvariables.contains(var)) 174 return; 175 if (allDefinedVars != null) { 176 if (!allDefinedVars.contains(var)) 177 return; 178 } 179 ArrayList tmplist = (ArrayList ) varpaths.get(var); 180 if (tmplist == null) { 181 tmplist = new ArrayList (); 182 tmplist.add(expr); 183 pathstrlist.add(expr.getStringValue()); 184 varpaths.put(var, tmplist); 185 } else if (!pathstrlist.contains(expr.getStringValue()) || !eliminateDuplicates) { 186 tmplist.add(expr); 187 pathstrlist.add(expr.getStringValue()); 188 } 189 } 190 191 public void visit(Element arg) throws XQueryException { 201 ArrayList al = arg.getAttributes(); 202 if (al != null) 203 for (int i = 0; i < al.size(); i++) 204 ((AttributeValuePair) al.get(i)).accept(this); 205 al = arg.getSubExpressions(); 206 if (al != null) 207 for (int i = 0; i < al.size(); i++) 208 ((XQueryExpression) al.get(i)).accept(this); 209 } 210 public void visit(FLWRExpression arg) throws XQueryException { 211 boolean isqdb = this.isqdb; 212 boolean isrootqdb = this.isrootqdb; 213 this.isqdb = false; 214 this.isrootqdb = false; 215 for (int i = 0; i < arg.getVariables().size(); i++) { 218 Variable vari = (Variable) arg.getVariables().get(i); 219 addVar(vari); 222 XQueryExpression expr = vari.getExpression(); 223 expr.accept(this); 229 } 230 if (!isqdb) { 231 ArrayList prevlist = allDefinedVars; 232 allDefinedVars = null; 233 arg.getReturnClause().accept(this); 234 allDefinedVars = prevlist; 235 } 236 XQueryExpression qi = arg.getWhereClause(); 237 if (qi != null) { 238 boolean prevInWhere = inWhere; 247 inWhere = true; 248 qi.accept(this); 249 inWhere = prevInWhere; 250 } 254 ArrayList al = arg.getOrderBy(); 255 if (al != null) 256 for (int i = 0; i < al.size(); i++) { 257 XQueryExpression expri = (XQueryExpression) al.get(i); 258 if (expri.getOrder(0) != Constants.PLACE_ORDER) 259 expri.accept(this); 260 } 261 this.isrootqdb = isrootqdb; 262 this.isqdb = isqdb; 263 } 264 265 public void visit(FunctionCall arg) throws XQueryException { 266 ArrayList al = arg.getArguments(); 275 if (al != null) 276 for (int i = 0; i < al.size(); i++) { 277 ((XQueryExpression) al.get(i)).accept(this); 278 } 279 } 280 281 290 public void visit(ListOpCompExpression arg) throws XQueryException { 308 arg.getExpression1().accept(this); 317 arg.getExpression2().accept(this); 318 } 319 320 public void visit(LocatedExpression arg) throws XQueryException { 324 if (arg.getExpression() instanceof Variable) 325 addVarPath((Variable) arg.getExpression(), arg); 326 for (int i = 0; i < arg.getSteps().size(); i++) 327 arg.getStepNum(i).accept(this); 328 } 329 330 public void visit(Step arg) throws XQueryException { 339 ArrayList predicates = arg.getPredicates(); 340 if (predicates == null) 341 return; 342 for (int i = 0; i < predicates.size(); i++) 343 ((XQueryExpression) predicates.get(i)).accept(this); 344 } 345 public void visit(Variable arg) throws XQueryException { 354 addVarPath(arg, arg); 355 } 356 357 public void visit(XQueryBinaryOperatorExpression arg) throws XQueryException { 358 arg.getExpression1().accept(this); 359 arg.getExpression2().accept(this); 360 } 361 362 public void visit(XQueryExpressionSequence arg) throws XQueryException { 365 ArrayList al = arg.getSubExpressions(); 366 if (al != null) 367 for (int i = 0; i < al.size(); i++) 368 ((XQueryExpression) al.get(i)).accept(this); 369 } 370 371 374 public void visit(XQueryUnaryOperatorExpression arg) throws XQueryException { 375 arg.getExpression().accept(this); 376 } 377 } 378 | Popular Tags |