1 27 28 package org.objectweb.speedo.query.parser; 29 30 import org.objectweb.medor.api.Field; 31 import org.objectweb.medor.api.MedorException; 32 import org.objectweb.medor.query.api.QueryTree; 33 import org.objectweb.util.monolog.api.Logger; 34 import org.objectweb.util.monolog.api.LoggerFactory; 35 import org.objectweb.jorm.util.api.Loggable; 36 import org.objectweb.speedo.query.parser.ASTPrimary; 37 import org.objectweb.speedo.query.parser.ASTType; 38 import org.objectweb.speedo.query.parser.ASTAdditiveExpression; 39 import org.objectweb.speedo.query.parser.ASTLiteral; 40 import org.objectweb.speedo.query.parser.ASTQualifiedName; 41 import org.objectweb.speedo.query.parser.ASTCastExpression; 42 import org.objectweb.speedo.query.parser.ASTRelationalExpression; 43 import org.objectweb.speedo.query.parser.SpeedoQLVisitor; 44 import org.objectweb.speedo.query.parser.ParseException; 45 import org.objectweb.speedo.query.parser.SimpleNode; 46 47 import java.util.ArrayList ; 48 import java.util.Map ; 49 import java.util.Stack ; 50 import java.util.StringTokenizer ; 51 52 56 public class SpeedoQLAbstractVisitor implements SpeedoQLVisitor, Loggable { 57 58 59 public final static String [] METHODS_OPERATOR = { 60 "matches", "startsWith", "endsWith", "equals", "equalsIgnoreCase", "toUpperCase", "toLowerCase", "length", "substring", "contains", "isEmpty" }; 72 public final static int MATCHES_OPERATOR = 0; 73 public final static int STARTS_WITH_OPERATOR = 1; 74 public final static int ENDS_WITH_OPERATOR = 2; 75 public final static int EQUALS_OPERATOR = 3; 76 public final static int EQUALS_IGNORE_CASE_OPERATOR = 4; 77 public final static int TO_UPPER_OPERATOR = 5; 78 public final static int TO_LOWER_OPERATOR = 6; 79 public final static int LENGTH_OPERATOR = 7; 80 public final static int SUBSTRING_OPERATOR = 8; 81 public final static int CONTAINS_OPERATOR = 9; 82 public final static int IS_EMPTY_OPERATOR = 10; 83 84 public final static Byte STR_OPERAND_SUBSTRING = new Byte ((byte) 0); 85 public final static Byte BEGIN_OPERAND_SUBSTRING = new Byte ((byte) 1); 86 public final static Byte LENGTH_OPERAND_SUBSTRING = new Byte ((byte) 2); 87 88 93 protected Map params = null; 94 95 100 protected Map vars = null; 101 102 105 protected String curClass = null; 106 107 115 public final static int UNDEFINED = 0; 116 public final static int EXTENT = 1; 117 public final static int NAVIGATION = 2; 118 public final static int IN_COLLECTION = 3; 119 public final static int MEMBEROF = 4; 120 public final static int IS_EMPTY = 5; 121 public final static int IS_NOT_EMPTY = 6; 122 123 public final static String operationToString(int oc) { 124 switch(oc) { 125 case EXTENT: return "EXTENT"; 126 case NAVIGATION: return "NAVIGATION"; 127 case IN_COLLECTION: return "IN_COLLECTION"; 128 case MEMBEROF: return "MEMBEROF"; 129 case IS_EMPTY: return "IS_EMPTY"; 130 case IS_NOT_EMPTY: return "IS_NOT_EMPTY"; 131 case UNDEFINED: 132 default: return "UNDEFINED"; 133 } 134 } 135 136 138 public Logger logger = null; 139 public boolean debug = false; 140 141 public String lastName; 142 143 146 protected class IdValue { 147 private ArrayList paths = null; 150 151 public String [] name = null; 153 154 public QueryTree qt = null; 156 157 public int nameType = SpeedoQLAbstractVisitor.UNDEFINED; 159 160 public String alias; 161 162 public IdValue(String [] name, int nameType) { 163 this(); 164 this.name = name; 165 this.nameType = nameType; 166 } 167 public IdValue(String pathName, int nameType) { 168 this(pathName); 169 this.nameType = nameType; 170 } 171 172 public IdValue(String pathName) { 173 this(); 174 name = splitPath(pathName); 175 } 176 177 public IdValue() { 178 paths = new ArrayList (); 179 } 180 181 public void addPath(String path) { 182 if (!paths.contains(path)) { 183 paths.add(path); 184 } 185 } 186 187 public String [] getSplitedPath(int idx) { 188 return splitPath((String ) paths.get(idx)); 189 } 190 191 public String getMergedPath(int idx) { 192 return (String ) paths.get(idx); 193 } 194 195 public int getDeclaredPathLength() { 196 return paths.size(); 197 } 198 199 204 public String toString() { 205 String resName = new String (); 206 207 if (name != null) { 208 for (int i = 0; i < name.length; i++) 209 resName += name[i] + "."; 210 211 resName = "[name=" + resName.substring(0, resName.length() - 1) + "]"; 212 } else 213 resName = "[name=null] "; 214 215 String resPath = new String (); 216 for (int i = 0; i < paths.size(); i++) 217 resPath += (String ) paths.get(i) + " & "; 218 219 return resName + " - [path=" + resPath + "] - [QT=" + ((qt == null) ? "null" : "something :-)") + "]"; 220 } 221 } 222 223 226 protected class VisitorException extends RuntimeException { 227 Exception nestedException; 228 229 VisitorException(Exception nestedException) { 230 this.nestedException = nestedException; 231 } 232 233 Exception getNestedException() { 234 return nestedException; 235 } 236 } 237 238 241 protected String [] splitPath(String path) { 242 StringTokenizer st = new StringTokenizer (path, "."); 243 String [] ret = new String [st.countTokens()]; 244 for (int i = 0; i < ret.length; i++) { 245 ret[i] = st.nextToken(); 246 } 247 return ret; 248 } 249 250 253 protected String [] splitEndPath(String path, String prefix) { 254 String end = path.substring(prefix.length()); 255 256 StringTokenizer st = new StringTokenizer (end, "."); 257 String [] ret = new String [st.countTokens() + 1]; 258 ret[0] = prefix; 259 for (int i = 1; i < ret.length; i++) { 260 ret[i] = st.nextToken(); 261 } 262 return ret; 263 } 264 265 protected String mergePath(String [] path, int begin, int length) { 266 if (length == 0) 267 return ""; 268 StringBuffer ret = new StringBuffer (); 269 for (int i = begin; i < (begin + length); i++) { 270 ret.append(path[i]); 271 ret.append('.'); 272 } 273 ret.deleteCharAt(ret.length() - 1); 274 return ret.toString(); 275 } 276 277 281 protected String mergePath(String [] path) { 282 return (mergePath(path, 0, path.length)); 283 } 284 285 protected String getEndString(String total) { 286 return total.substring(total.lastIndexOf('.') + 1, total.length()); 287 } 288 289 296 protected Field getMedorFieldFromPath(String path, Map ids) 297 throws MedorException, ParseException { 298 String [] paths = splitPath(path); 299 QueryTree qt = (QueryTree) ids.get(paths[0]); 300 301 if (paths.length > 2) 302 throw new Error ("recursive path not implemented: " + path); 303 if (qt == null) { 304 throw new ParseException("Undeclared identification variable " + paths[0] + " in path " + path); 305 } 306 return qt.getTupleStructure().getField(path); 307 } 308 309 public String alias2fullname(String alias) { 310 int it = alias.indexOf("this."); 311 if (it == -1) 312 return alias; 313 else { 314 return curClass + "." + alias.substring(5, alias.length()); 315 } 316 } 317 318 public String buildStringwithout(String [] ss, int toforget, String sep) { 319 StringBuffer sb = new StringBuffer (); 320 String s = ""; 321 for(int i=0; i<ss.length; i++) { 322 if (i != toforget) { 323 sb.append(s); 324 s = sep; 325 sb.append(ss[i]); 326 } 327 } 328 return sb.toString(); 329 } 330 331 public String withoutThis(String aname) { 332 int it = aname.indexOf("this."); 333 if (it == -1) 334 return aname; 335 else { 336 return aname.substring(5, aname.length()); 337 } 338 } 339 340 public int isMethodOperator(String str) { 341 for(int i=METHODS_OPERATOR.length-1; i>=0; i--) { 342 if (METHODS_OPERATOR[i].equals(str)) { 343 return i; 344 } 345 } 346 return -1; 347 } 348 349 350 353 354 359 public Object visit(SimpleNode node) throws Exception { 360 return visit(node, new Stack ()); 361 } 362 363 366 public Object visit(SimpleNode node, Object data) { 367 return node.childrenAccept(this, data); 368 } 369 370 public Object visit(ASTSpeedoPrimary node, Object data) { 371 return null; 372 } 373 374 public Object visit(ASTSpeedoQL node, Object data) { 375 return null; 376 } 377 378 public Object visit(ASTPrimary node, Object data) { 379 return null; 380 } 381 382 public Object visit(ASTRelationalExpression node, Object data) { 383 return null; 384 } 385 386 public Object visit(ASTAdditiveExpression node, Object data) { 387 return null; 388 } 389 390 public Object visit(ASTUnaryExpression node, Object data) { 391 return null; 392 } 393 394 public Object visit(ASTCastExpression node, Object data) { 395 return null; 396 } 397 398 public Object visit(ASTArgumentList node, Object data) { 399 return null; 400 } 401 402 public Object visit(ASTLiteral node, Object data) { 403 return null; 404 } 405 406 public Object visit(ASTType node, Object data) { 407 return null; 408 } 409 410 public Object visit(ASTQualifiedName node, Object data) { 411 return null; 412 } 413 414 415 418 public void setParams(Map params) { 419 this.params = params; 420 } 421 422 public void setVars(Map vars) { 423 this.vars = vars; 424 } 425 426 public void setCurrentClass(String curClass) { 427 this.curClass = curClass; 428 } 429 430 431 434 public void setLogger(Logger logger) { 435 this.logger = logger; 436 } 437 438 public Logger getLogger() { 439 return logger; 440 } 441 442 public void setLoggerFactory(LoggerFactory loggerFactory) { 443 } 444 445 public LoggerFactory getLoggerFactory() { 446 return null; 447 } 448 } 449 | Popular Tags |