1 21 package oracle.toplink.essentials.internal.parsing; 23 24 import java.util.*; 25 import oracle.toplink.essentials.internal.localization.*; 26 import oracle.toplink.essentials.descriptors.ClassDescriptor; 27 import oracle.toplink.essentials.exceptions.EJBQLException; 28 29 46 public class ParseTreeContext { 47 private Map variableDecls; 48 private int currentScope; 49 private Set outerScopeVariables; 50 private Map fetchJoins; 51 private TypeHelper typeHelper; 52 private Map parameterTypes; 53 private List parameterNames; 54 55 59 public ParseTreeContext() { 60 super(); 61 variableDecls = new HashMap(); 62 currentScope = 0; 63 fetchJoins = new HashMap(); 64 typeHelper = null; 65 parameterTypes = new HashMap(); 66 parameterNames = new ArrayList(); 67 } 68 69 73 public void registerSchema(String variable, String schema) { 74 VariableDecl decl = (VariableDecl)variableDecls.get(variable); 75 if (decl == null) { 76 decl = new VariableDecl(variable, schema); 77 variableDecls.put(variable, decl); 78 } else { 79 String text = decl.isRangeVariable ? decl.schema : decl.path.getAsString(); 80 throw EJBQLException.multipleVariableDeclaration(variable, text); 81 } 82 } 83 84 88 public void registerJoinVariable(String variable, Node path) { 89 VariableDecl decl = (VariableDecl)variableDecls.get(variable); 90 if (decl == null) { 91 decl = new VariableDecl(variable, path); 92 variableDecls.put(variable, decl); 93 } else { 94 String text = decl.isRangeVariable ? decl.schema : decl.path.getAsString(); 95 throw EJBQLException.multipleVariableDeclaration(variable, text); 96 } 97 } 98 99 103 public boolean isVariable(String variable) { 104 VariableDecl decl = (VariableDecl)variableDecls.get(variable); 105 return decl != null; 106 } 107 108 112 113 public boolean isRangeVariable(String variable) { 114 VariableDecl decl = (VariableDecl)variableDecls.get(variable); 115 return (decl != null) && decl.isRangeVariable; 116 } 117 118 123 public String schemaForVariable(String variable) { 124 VariableDecl decl = (VariableDecl)variableDecls.get(variable); 125 return (decl != null) ? decl.schema : null; 126 } 127 128 132 public Class classForSchemaName(String schemaName, GenerationContext context) { 133 ClassDescriptor descriptor = context.getSession().getDescriptorForAlias(schemaName); 134 if (descriptor == null) { 135 throw EJBQLException.missingDescriptorException(schemaName); 136 } 137 Class theClass = descriptor.getJavaClass(); 138 if (theClass == null) { 139 throw EJBQLException.resolutionClassNotFoundException(schemaName); 140 } 141 return theClass; 142 } 143 144 151 public String getVariableNameForClass(Class theClass, GenerationContext context) { 152 for (Iterator i = variableDecls.entrySet().iterator(); i.hasNext(); ) { 153 Map.Entry entry = (Map.Entry)i.next(); 154 String nextVariable = (String )entry.getKey(); 155 VariableDecl decl = (VariableDecl)entry.getValue(); 156 if ((decl.schema != null) && 157 (theClass == this.classForSchemaName(decl.schema, context))) { 158 return nextVariable; 159 } 160 } 161 return null; 162 } 163 164 169 public Node pathForVariable(String variable) { 170 VariableDecl decl = (VariableDecl)variableDecls.get(variable); 171 return (decl != null) ? decl.path : null; 172 } 173 174 179 public boolean isDeclaredInOuterScope(String variable) { 180 VariableDecl decl = (VariableDecl)variableDecls.get(variable); 181 return (decl != null) ? (decl.scope < currentScope) : false; 182 } 183 184 188 public void setScopeOfVariable(String variable) { 189 VariableDecl decl = (VariableDecl)variableDecls.get(variable); 190 if (decl != null) { 191 decl.scope = currentScope; 192 } 193 } 194 195 199 public void enterScope() { 200 currentScope++; 201 resetOuterScopeVariables(); 202 } 203 204 208 public void leaveScope() { 209 currentScope--; 210 } 211 212 216 public void registerOuterScopeVariable(String variable) { 217 outerScopeVariables.add(variable); 218 } 219 220 224 public Set getOuterScopeVariables() { 225 return outerScopeVariables; 226 } 227 228 232 public void resetOuterScopeVariables() { 233 outerScopeVariables = new HashSet(); 234 } 235 236 240 public void resetOuterScopeVariables(Set variables) { 241 outerScopeVariables = variables; 242 } 243 244 248 public void registerFetchJoin(String variableName, Node node) { 249 List joins = (List)fetchJoins.get(variableName); 250 if (joins == null) { 251 joins = new ArrayList(); 252 fetchJoins.put(variableName, joins); 253 } 254 joins.add(node); 255 } 256 257 259 public List getFetchJoins(String variableName) { 260 return (List)fetchJoins.get(variableName); 261 } 262 263 265 public void usedVariable(String variable) { 266 VariableDecl decl = (VariableDecl)variableDecls.get(variable); 267 if ((decl != null) && (decl.scope == currentScope)) { 268 decl.used = true; 269 } 270 } 271 272 274 public Set getUnusedVariables() { 275 Set unused = new HashSet(); 276 for (Iterator i = variableDecls.entrySet().iterator(); i.hasNext();) { 277 Map.Entry entry = (Map.Entry)i.next(); 278 String variable = (String )entry.getKey(); 279 VariableDecl decl = (VariableDecl)entry.getValue(); 280 if ((decl.scope == currentScope) && !decl.used) { 281 unused.add(variable); 282 } 283 } 284 return unused; 285 } 286 287 public boolean hasMoreThanOneVariablePerType() { 291 Map typeNamesToVariables = new HashMap(); 292 int nrOfRangeVariables = 0; 293 for (Iterator i = variableDecls.entrySet().iterator(); i.hasNext(); ) { 295 Map.Entry entry = (Map.Entry)i.next(); 296 String variable = (String )entry.getKey(); 297 VariableDecl decl = (VariableDecl)entry.getValue(); 298 if (decl.isRangeVariable) { 299 nrOfRangeVariables++; 300 typeNamesToVariables.put(decl.schema, variable); 301 } 302 } 303 return typeNamesToVariables.size() != nrOfRangeVariables; 304 } 305 306 public boolean hasMoreThanOneAliasInFrom() { 311 Map typeNamesToVariables = new HashMap(); 312 for (Iterator i = variableDecls.entrySet().iterator(); i.hasNext(); ) { 313 Map.Entry entry = (Map.Entry)i.next(); 314 String variable = (String )entry.getKey(); 315 VariableDecl decl = (VariableDecl)entry.getValue(); 316 if (decl.isRangeVariable) { 317 typeNamesToVariables.put(decl.schema, variable); 318 } 319 } 320 return typeNamesToVariables.size() > 1; 321 } 322 323 327 public TypeHelper getTypeHelper() { 328 return typeHelper; 329 } 330 331 335 public void setTypeHelper(TypeHelper typeHelper) { 336 this.typeHelper = typeHelper; 337 } 338 339 343 public void addParameter(String parameterName) { 344 if (!parameterNames.contains(parameterName)){ 345 parameterNames.add(parameterName); 346 } 347 } 348 349 353 public void defineParameterType(String parameterName, Object parameterType) { 354 if (parameterTypes.containsKey(parameterName)) { 355 Object oldType = parameterTypes.get(parameterName); 357 if (typeHelper.isAssignableFrom(oldType, parameterType)) { 358 } else if (typeHelper.isAssignableFrom(parameterType, oldType)) { 360 parameterTypes.put(parameterName, parameterType); 362 } else { 363 throw EJBQLException.invalidMultipleUseOfSameParameter( 365 parameterName, typeHelper.getTypeName(oldType), 366 typeHelper.getTypeName(parameterType)); 367 } 368 } else { 369 parameterTypes.put(parameterName, parameterType); 371 } 372 } 373 374 378 public boolean hasParameters() { 379 return !parameterNames.isEmpty(); 380 } 381 382 386 public Object getParameterType(String parameter) { 387 return parameterTypes.get(parameter); 388 } 389 390 394 public List getParameterNames() { 395 return parameterNames; 396 } 397 398 409 static class VariableDecl { 410 public final String variable; 411 public final boolean isRangeVariable; 412 public final String schema; 413 public final Node path; 414 public int scope; 415 public boolean used; 416 public VariableDecl(String variable, String schema) { 417 this.variable = variable; 418 this.isRangeVariable = true; 419 this.schema = schema; 420 this.path = null; 421 this.scope = scope; 422 this.used = false; 423 } 424 public VariableDecl(String variable, Node path) { 425 this.variable = variable; 426 this.isRangeVariable = false; 427 this.schema = null; 428 this.path = path; 429 this.scope = scope; 430 this.used = false; 431 } 432 } 433 } 434 | Popular Tags |