1 22 23 package org.xquark.xquery.normalize; 24 25 import java.util.ArrayList ; 26 import java.util.HashSet ; 27 import java.util.Set ; 28 29 import org.apache.commons.collections.set.ListOrderedSet; 30 import org.xquark.xquery.parser.*; 31 32 public class GetBoundVariablesVisitor extends DefaultParserVisitor { 33 34 private static final String RCSRevision = "$Revision: 1.4 $"; 35 private static final String RCSName = "$Name: $"; 36 37 private Set variables = null; 38 private Set IDs = null; 39 private boolean doVariables = true; 40 private boolean doIDs = true; 41 43 48 public GetBoundVariablesVisitor(boolean doVariables, boolean doIDs) { 49 this.doVariables = doVariables; 50 if (doVariables) 51 variables = ListOrderedSet.decorate(new HashSet ()); 52 this.doIDs = doIDs; 53 if (doIDs) 54 IDs = ListOrderedSet.decorate(new HashSet ()); 55 } 56 57 public ArrayList getVariables() { 58 ArrayList all = new ArrayList (); 59 if (doVariables && variables != null) 60 all.addAll(variables); 61 if (doIDs && IDs != null) 62 all.addAll(IDs); 63 return all; 64 } 65 66 public ArrayList getAllVariables() { 67 ArrayList all = new ArrayList (); 68 if (variables != null) all.addAll(variables); 69 if (IDs != null) all.addAll(IDs); 70 return all; 71 } 72 73 public ArrayList getOnlyVariables() { 74 ArrayList ret = new ArrayList (variables); 75 return ret; 76 } 77 78 public ArrayList getIDsVariables() { 79 ArrayList ret = new ArrayList (IDs); 80 return ret; 81 } 82 83 public void reset(boolean doIDs, boolean doVariables) { 84 this.doIDs = doIDs; 85 this.doVariables = doVariables; 86 } 87 88 public void reset() { 89 if (doVariables) 90 variables.clear(); 91 if (doIDs) 92 IDs.clear(); 93 } 94 95 99 public void visit(Element arg) throws XQueryException { 107 if (doIDs) { 108 if (arg.getSkolemIDs() != null) IDs.addAll(arg.getSkolemIDs()); 109 if (arg.getDependIDs() != null) IDs.addAll(arg.getDependIDs()); 110 } 111 if (arg.getStartTag() != null) { 112 arg.getStartTag().accept(this); 113 } 114 if (arg.getAttributes() != null) { 115 for (int i = 0; i < arg.getAttributes().size(); i++) { 116 ((AttributeValuePair) arg.getAttributes().get(i)).accept(this); 117 } 118 } 119 if (arg.getSubExpressions() != null) { 120 for (int j = 0; j < arg.getSubExpressions().size(); j++) { 121 ((XQueryExpression) arg.getSubExpressions().get(j)).accept(this); 122 } 123 } 124 } 125 126 151 public void visit(FunctionCall arg) throws XQueryException { 152 if (doIDs) { 153 if (arg.getSkolemIDs() != null) IDs.addAll(arg.getSkolemIDs()); 154 if (arg.getDependIDs() != null) IDs.addAll(arg.getDependIDs()); 155 } 156 if (arg.getArguments() != null) { 157 for (int i = 0; i < arg.getArguments().size(); i++) { 158 ((XQueryExpression) arg.getArguments().get(i)).accept(this); 159 } 160 } 161 } 162 163 167 public void visit(ITEExpression arg) throws XQueryException { 168 if (doIDs) { 169 if (arg.getSkolemIDs() != null) IDs.addAll(arg.getSkolemIDs()); 170 if (arg.getDependIDs() != null) IDs.addAll(arg.getDependIDs()); 171 } 172 arg.getIfExpression().accept(this); 173 arg.getElseExpression().accept(this); 174 arg.getThenExpression().accept(this); 175 } 176 177 public void visit(LocatedExpression arg) throws XQueryException { 186 if (doIDs) { 187 if (arg.getSkolemIDs() != null) IDs.addAll(arg.getSkolemIDs()); 188 if (arg.getDependIDs() != null) IDs.addAll(arg.getDependIDs()); 189 } 190 for (int i = 0; i < arg.getSteps().size(); i++) { 191 ((Step) arg.getSteps().get(i)).accept(this); 192 } 193 } 194 195 public void visit(Variable arg) throws XQueryException { 211 if (doIDs) { 212 if (arg.getSkolemIDs() != null) IDs.addAll(arg.getSkolemIDs()); 213 if (arg.getDependIDs() != null) IDs.addAll(arg.getDependIDs()); 214 } 215 if (doVariables) 217 variables.add(arg); 218 } 219 220 public void visit(XQueryBinaryOperatorExpression arg) throws XQueryException { 221 if (doIDs) { 222 if (arg.getSkolemIDs() != null) IDs.addAll(arg.getSkolemIDs()); 223 if (arg.getDependIDs() != null) IDs.addAll(arg.getDependIDs()); 224 } 225 if (arg.getExpression1() != null) { 226 arg.getExpression1().accept(this); 227 } 228 if (arg.getExpression2() != null) { 229 arg.getExpression2().accept(this); 230 } 231 } 232 233 235 public void visit(XQueryExpression arg) throws XQueryException { 236 if (doIDs) { 237 if (arg.getSkolemIDs() != null) IDs.addAll(arg.getSkolemIDs()); 238 if (arg.getDependIDs() != null) IDs.addAll(arg.getDependIDs()); 239 } 240 } 241 242 public void visit(XQueryExpressionSequence arg) throws XQueryException { 243 if (doIDs) { 244 if (arg.getSkolemIDs() != null) IDs.addAll(arg.getSkolemIDs()); 245 if (arg.getDependIDs() != null) IDs.addAll(arg.getDependIDs()); 246 } 247 for (int i = 0; i < arg.getSubExpressions().size(); i++) { 248 ((XQueryExpression) arg.getSubExpressions().get(i)).accept(this); 249 } 250 } 251 252 public void visit(XQueryUnaryOperatorExpression arg) throws XQueryException { 255 if (doIDs) { 256 if (arg.getSkolemIDs() != null) IDs.addAll(arg.getSkolemIDs()); 257 if (arg.getDependIDs() != null) IDs.addAll(arg.getDependIDs()); 258 } 259 if (arg.getExpression() != null) { 260 arg.getExpression().accept(this); 261 } 262 } 263 264 public void visit(XQueryModule arg) throws XQueryException { 265 for (int i = 0; i < arg.getExpressions().size(); i++) { 266 XQueryExpression expressioni = (XQueryExpression) arg.getExpressions().get(i); 267 expressioni.accept(this); 268 } 269 } 270 271 273 } 274 | Popular Tags |