1 package persistence.antlr; 2 3 8 9 import persistence.antlr.collections.AST; 10 import persistence.antlr.collections.ASTEnumeration; 11 import persistence.antlr.collections.impl.ASTEnumerator; 12 import persistence.antlr.collections.impl.Vector; 13 14 import java.io.Serializable ; 15 import java.io.IOException ; 16 import java.io.Writer ; 17 18 43 public abstract class BaseAST implements AST, Serializable { 44 protected BaseAST down; 45 protected BaseAST right; 46 47 private static boolean verboseStringConversion = false; 48 private static String [] tokenNames = null; 49 50 51 public void addChild(AST node) { 52 if (node == null) return; 53 BaseAST t = this.down; 54 if (t != null) { 55 while (t.right != null) { 56 t = t.right; 57 } 58 t.right = (BaseAST)node; 59 } 60 else { 61 this.down = (BaseAST)node; 62 } 63 } 64 65 66 public int getNumberOfChildren() { 67 BaseAST t = this.down; 68 int n = 0; 69 if (t != null) { 70 n = 1; 71 while (t.right != null) { 72 t = t.right; 73 n++; 74 } 75 return n; 76 } 77 return n; 78 } 79 80 private void doWorkForFindAll(Vector v, AST target, boolean partialMatch) { 81 AST sibling; 82 83 siblingWalk: 85 for (sibling = this; 86 sibling != null; 87 sibling = sibling.getNextSibling()) { 88 if ((partialMatch && sibling.equalsTreePartial(target)) || 89 (!partialMatch && sibling.equalsTree(target))) { 90 v.appendElement(sibling); 91 } 92 if (sibling.getFirstChild() != null) { 94 ((BaseAST)sibling.getFirstChild()).doWorkForFindAll(v, target, partialMatch); 95 } 96 } 97 } 98 99 100 public boolean equals(AST t) { 101 if (t == null) return false; 102 return this.getText().equals(t.getText()) && 103 this.getType() == t.getType(); 104 } 105 106 109 public boolean equalsList(AST t) { 110 AST sibling; 111 112 if (t == null) { 114 return false; 115 } 116 117 for (sibling = this; 119 sibling != null && t != null; 120 sibling = sibling.getNextSibling(), t = t.getNextSibling()) 121 { 122 if (!sibling.equals(t)) { 124 return false; 125 } 126 if (sibling.getFirstChild() != null) { 128 if (!sibling.getFirstChild().equalsList(t.getFirstChild())) { 129 return false; 130 } 131 } 132 else if (t.getFirstChild() != null) { 134 return false; 135 } 136 } 137 if (sibling == null && t == null) { 138 return true; 139 } 140 return false; 142 } 143 144 147 public boolean equalsListPartial(AST sub) { 148 AST sibling; 149 150 if (sub == null) { 152 return true; 153 } 154 155 for (sibling = this; 157 sibling != null && sub != null; 158 sibling = sibling.getNextSibling(), sub = sub.getNextSibling()) { 159 if (!sibling.equals(sub)) return false; 161 if (sibling.getFirstChild() != null) { 163 if (!sibling.getFirstChild().equalsListPartial(sub.getFirstChild())) return false; 164 } 165 } 166 if (sibling == null && sub != null) { 167 return false; 169 } 170 return true; 172 } 173 174 177 public boolean equalsTree(AST t) { 178 if (!this.equals(t)) return false; 180 if (this.getFirstChild() != null) { 182 if (!this.getFirstChild().equalsList(t.getFirstChild())) return false; 183 } 184 else if (t.getFirstChild() != null) { 186 return false; 187 } 188 return true; 189 } 190 191 194 public boolean equalsTreePartial(AST sub) { 195 if (sub == null) { 197 return true; 198 } 199 200 if (!this.equals(sub)) return false; 202 if (this.getFirstChild() != null) { 204 if (!this.getFirstChild().equalsListPartial(sub.getFirstChild())) return false; 205 } 206 return true; 207 } 208 209 213 public ASTEnumeration findAll(AST target) { 214 Vector roots = new Vector(10); 215 AST sibling; 216 217 if (target == null) { 219 return null; 220 } 221 222 doWorkForFindAll(roots, target, false); 224 return new ASTEnumerator(roots); 225 } 226 227 231 public ASTEnumeration findAllPartial(AST sub) { 232 Vector roots = new Vector(10); 233 AST sibling; 234 235 if (sub == null) { 237 return null; 238 } 239 240 doWorkForFindAll(roots, sub, true); 242 return new ASTEnumerator(roots); 243 } 244 245 246 public AST getFirstChild() { 247 return down; 248 } 249 250 251 public AST getNextSibling() { 252 return right; 253 } 254 255 256 public String getText() { 257 return ""; 258 } 259 260 261 public int getType() { 262 return 0; 263 } 264 265 public int getLine() { 266 return 0; 267 } 268 269 public int getColumn() { 270 return 0; 271 } 272 273 public abstract void initialize(int t, String txt); 274 275 public abstract void initialize(AST t); 276 277 public abstract void initialize(Token t); 278 279 280 public void removeChildren() { 281 down = null; 282 } 283 284 public void setFirstChild(AST c) { 285 down = (BaseAST)c; 286 } 287 288 public void setNextSibling(AST n) { 289 right = (BaseAST)n; 290 } 291 292 293 public void setText(String text) { 294 } 295 296 297 public void setType(int ttype) { 298 } 299 300 public static void setVerboseStringConversion(boolean verbose, String [] names) { 301 verboseStringConversion = verbose; 302 tokenNames = names; 303 } 304 305 306 public static String [] getTokenNames() { 307 return tokenNames; 308 } 309 310 public String toString() { 311 StringBuffer b = new StringBuffer (); 312 if (verboseStringConversion && 314 !getText().equalsIgnoreCase(tokenNames[getType()]) && 315 !getText().equalsIgnoreCase(StringUtils.stripFrontBack(tokenNames[getType()], "\"", "\""))) { 316 b.append('['); 317 b.append(getText()); 318 b.append(",<"); 319 b.append(tokenNames[getType()]); 320 b.append(">]"); 321 return b.toString(); 322 } 323 return getText(); 324 } 325 326 327 public String toStringList() { 328 AST t = this; 329 String ts = ""; 330 if (t.getFirstChild() != null) ts += " ("; 331 ts += " " + this.toString(); 332 if (t.getFirstChild() != null) { 333 ts += ((BaseAST)t.getFirstChild()).toStringList(); 334 } 335 if (t.getFirstChild() != null) ts += " )"; 336 if (t.getNextSibling() != null) { 337 ts += ((BaseAST)t.getNextSibling()).toStringList(); 338 } 339 return ts; 340 } 341 342 public String toStringTree() { 343 AST t = this; 344 String ts = ""; 345 if (t.getFirstChild() != null) ts += " ("; 346 ts += " " + this.toString(); 347 if (t.getFirstChild() != null) { 348 ts += ((BaseAST)t.getFirstChild()).toStringList(); 349 } 350 if (t.getFirstChild() != null) ts += " )"; 351 return ts; 352 } 353 354 public static String decode(String text) { 355 char c, c1, c2, c3, c4, c5; 356 StringBuffer n = new StringBuffer (); 357 for (int i = 0; i < text.length(); i++) { 358 c = text.charAt(i); 359 if (c == '&') { 360 c1 = text.charAt(i + 1); 361 c2 = text.charAt(i + 2); 362 c3 = text.charAt(i + 3); 363 c4 = text.charAt(i + 4); 364 c5 = text.charAt(i + 5); 365 366 if (c1 == 'a' && c2 == 'm' && c3 == 'p' && c4 == ';') { 367 n.append("&"); 368 i += 5; 369 } 370 else if (c1 == 'l' && c2 == 't' && c3 == ';') { 371 n.append("<"); 372 i += 4; 373 } 374 else if (c1 == 'g' && c2 == 't' && c3 == ';') { 375 n.append(">"); 376 i += 4; 377 } 378 else if (c1 == 'q' && c2 == 'u' && c3 == 'o' && 379 c4 == 't' && c5 == ';') { 380 n.append("\""); 381 i += 6; 382 } 383 else if (c1 == 'a' && c2 == 'p' && c3 == 'o' && 384 c4 == 's' && c5 == ';') { 385 n.append("'"); 386 i += 6; 387 } 388 else 389 n.append("&"); 390 } 391 else 392 n.append(c); 393 } 394 return new String (n); 395 } 396 397 public static String encode(String text) { 398 char c; 399 StringBuffer n = new StringBuffer (); 400 for (int i = 0; i < text.length(); i++) { 401 c = text.charAt(i); 402 switch (c) { 403 case '&': 404 { 405 n.append("&"); 406 break; 407 } 408 case '<': 409 { 410 n.append("<"); 411 break; 412 } 413 case '>': 414 { 415 n.append(">"); 416 break; 417 } 418 case '"': 419 { 420 n.append("""); 421 break; 422 } 423 case '\'': 424 { 425 n.append("'"); 426 break; 427 } 428 default : 429 { 430 n.append(c); 431 break; 432 } 433 } 434 } 435 return new String (n); 436 } 437 438 public void xmlSerializeNode(Writer out) 439 throws IOException { 440 StringBuffer buf = new StringBuffer (100); 441 buf.append("<"); 442 buf.append(getClass().getName() + " "); 443 buf.append("text=\"" + encode(getText()) + "\" type=\"" + 444 getType() + "\"/>"); 445 out.write(buf.toString()); 446 } 447 448 public void xmlSerializeRootOpen(Writer out) 449 throws IOException { 450 StringBuffer buf = new StringBuffer (100); 451 buf.append("<"); 452 buf.append(getClass().getName() + " "); 453 buf.append("text=\"" + encode(getText()) + "\" type=\"" + 454 getType() + "\">\n"); 455 out.write(buf.toString()); 456 } 457 458 public void xmlSerializeRootClose(Writer out) 459 throws IOException { 460 out.write("</" + getClass().getName() + ">\n"); 461 } 462 463 public void xmlSerialize(Writer out) throws IOException { 464 for (AST node = this; 466 node != null; 467 node = node.getNextSibling()) { 468 if (node.getFirstChild() == null) { 469 ((BaseAST)node).xmlSerializeNode(out); 471 } 472 else { 473 ((BaseAST)node).xmlSerializeRootOpen(out); 474 475 ((BaseAST)node.getFirstChild()).xmlSerialize(out); 477 478 ((BaseAST)node).xmlSerializeRootClose(out); 480 } 481 } 482 } 483 484 } 485 | Popular Tags |