1 19 20 package org.netbeans.modules.schema2beans; 21 22 import java.util.*; 23 24 25 42 public class DDRegistryParser implements Iterator { 43 44 static final String CURRENT_CURSOR = "."; 46 49 static public class PathResolver { 50 51 static final char VARBEGIN = '{'; 52 static final char VAREND = '}'; 53 static final char VALUE = '#'; 54 55 static final String VAR_MODNAME = "mname"; 58 static final String VAR_UNAME = "uname"; 61 static final String VAR_PTYPE = "ptype"; 65 static final String VAR_TYPE = "type"; 67 68 String result = null; 69 70 public PathResolver() { 72 } 73 74 public PathResolver(DDCursor cursor, String path) { 76 this.result = this.resolvePath(cursor, path); 77 } 78 79 80 static boolean needResolving(String path) { 81 if (path != null) 82 return (path.indexOf(VARBEGIN) != -1); 83 else 84 return false; 85 } 86 87 99 String resolvePath(DDCursor cur, String path) { 100 if(path.indexOf(VARBEGIN) != -1) { 101 int i1 = path.indexOf(VARBEGIN); 102 int i2 = path.indexOf(VAREND); 103 String v = 104 (String )this.resolvePathVar(cur, path.substring(i1+1, i2)); 105 return path.substring(0, i1).trim() + v + 106 this.resolvePath(cur, path.substring(i2+1)); 107 } 108 return path.trim(); 109 } 110 111 Object resolvePathVar(DDCursor cur, String path) { 112 114 if (path.indexOf(VARBEGIN) != -1 || path.indexOf(VAREND) != -1) { 115 throw new IllegalArgumentException (Common.getMessage( 116 "CannotNestDeclaration_msg")); 117 } 118 119 if (path.indexOf('#') == 0 ) { 120 path = path.substring(1, path.length()); 121 122 String remSuffix = null; 123 int idx = path.indexOf('-'); 124 if (idx != -1) { 125 remSuffix = path.substring(idx+1); 127 path = path.substring(0, idx); 128 } 129 130 if (path.startsWith(VAR_MODNAME)) { 131 int in = path.indexOf(':'); 132 if (in != -1) { 133 String name = path.substring(in+1); 134 path = this.getDDNameValue(cur, name).toString();; 135 path = cur.getRegistry().getName(path); 136 } else { 137 path = cur.getRegistry().getName(cur); 138 } 139 } else 140 if (path.startsWith(VAR_UNAME)) { 141 path = cur.getRegistry().getID(cur); 142 } else 143 if (path.startsWith(VAR_PTYPE)) { 144 int i = path.indexOf('.'); 145 if (i != -1) { 146 String t = path.substring(i+1); 147 DDCursor pc = cur; 148 BaseBean bean; 149 do { 150 bean = this.getBean(pc.getRoot(), t); 151 if (bean == null) { 152 pc = pc.getParent(); 153 } 154 } while(bean == null && pc != null); 155 156 if (bean != null) { 157 path = bean.parent().name(); 158 } 159 } 160 } else 161 if (path.startsWith(VAR_TYPE)) { 162 path = cur.getRoot().name(); 163 } 164 165 if (remSuffix != null) { 166 if (path.endsWith(remSuffix)) { 167 path = path.substring(0, path.length() - 168 remSuffix.length()); 169 } 170 } 171 172 return path; 173 } else { 174 return this.getDDNameValue(cur, path); 175 } 176 } 177 178 private Object getDDNameValue(DDCursor pc, String path) { 179 Object val = null; 180 181 do { 184 val = this.getValue(pc.getRoot(), path); 185 if (val == null) { 186 pc = pc.getParent(); 187 } 188 } while(val == null && pc != null); 189 return val; 190 } 191 192 BaseBean getBean(BaseBean root, String name) { 193 while (root != null && !root.isRoot()) { 194 if (root.hasName(name)) 195 return root; 196 root = root.parent(); 197 } 198 return null; 199 } 200 201 String getValue(BaseBean root, String name) { 202 String val = null; 203 if (root != null) { 204 do { 205 try { 206 val = (String )root.getValue(name); 207 break; 208 } catch(Exception e) { 209 } 211 root = root.parent(); 212 } while (root != null && !root.isRoot()); 213 } 214 return val; 215 } 216 217 public String toString() { 218 return this.result; 219 } 220 } 221 222 227 static public class DDCursor { 228 DDCursor parent; 229 BaseBean root; 230 DDRegistry registry; 231 232 public DDCursor(DDCursor parent, String path) { 233 this(parent, (BaseBean)null); 234 this.resolve(path); 235 } 236 237 public DDCursor(DDCursor parent, BaseBean root) { 238 this.parent = parent; 239 this.root = root; 240 if (this.registry == null && parent != null) 241 this.registry = parent.registry; 242 } 243 244 public DDCursor(DDRegistry reg, String path) { 245 this.parent = null; 246 this.root = null; 247 this.registry = reg; 248 this.resolve(path); 249 } 250 251 public DDCursor(DDRegistry reg, BaseBean root) { 252 this.parent = null; 253 this.root = root; 254 this.registry = reg; 255 } 256 257 public DDRegistry getRegistry() { 258 return this.registry; 259 } 260 261 public BaseBean getRoot() { 262 return this.root; 263 } 264 265 public DDCursor getParent() { 266 return this.parent; 267 } 268 269 public Object getValue(String name) { 270 if (root != null) 271 return this.root.getValue(name); 272 else 273 return null; 274 } 275 276 void resolve(String path) { 277 278 if (path == null) return; 279 path = path.trim(); 280 if (path.equals("")) return; 282 if (path.startsWith("[") && path.endsWith("]")) { this.resolveGraph(path.substring(1,path.length()-1)); 284 return; 285 } 286 287 if (this.parent == null) { 289 throw new IllegalStateException (Common.getMessage( 290 "CantResolveBecauseMissingParent_msg", path)); 291 } 292 293 if (PathResolver.needResolving(path)) 295 path = (new PathResolver(this.parent, path)).toString(); 296 297 BaseBean root = this.parent.getRoot(); 298 299 if (root != null) { 300 DDParser p = new DDParser(root, path); 301 if (p.hasNext()) { 302 Object o = p.next(); 303 if (o instanceof BaseBean) { 304 this.root = (BaseBean)o; 305 } else { 306 throw new IllegalStateException ( 307 Common.getMessage( 308 "ParsingPathDoesntResolveToGraphNodeElement_msg", 309 path, o.getClass().getName(), o.toString())); 310 } 311 } else { 312 throw new IllegalStateException (Common.getMessage( 313 "NoElementFoundPath_msg", path)); 314 } 315 } else { 316 throw new IllegalStateException (Common.getMessage( 317 "NoRootFoundForPath_msg", path)); 318 } 319 } 320 321 void resolveGraph(String path) { 322 String pathRoot = null; 323 324 if (PathResolver.needResolving(path)) 325 path = (new PathResolver(this.parent, path)).toString(); 326 327 int idx = path.indexOf(':'); 328 if (idx != -1) { 329 pathRoot = path.substring(idx+1); 330 path = path.substring(0, idx); 331 } 332 333 BaseBean[] beans = this.registry.getRoots(path); 334 335 if (beans.length > 0) { 336 this.root = beans[0]; 337 if (pathRoot != null) { 338 DDCursor cur = new DDRegistryParser.DDCursor(this, 339 pathRoot); 340 this.root = cur.getRoot(); 341 } 342 } 343 } 344 345 public String toString() { 346 String p, r; 347 348 if (this.parent != null) 349 p = this.parent.toString(); 350 else 351 p = "-"; 353 if (this.root != null) 354 r = root.name(); 355 else 356 r = "-"; return "Parent:"+p+" Root:"+r; } 359 360 public String dump() { 361 if (this.root != null) 362 return this.root.dumpBeanNode(); 363 else 364 return "<null graph>"; } 366 } 367 368 369 DDRegistry registry; 370 371 DDRegistryParser parentParser = null; 378 DDCursor parentCursor = null; 379 DDRegistryParser parserRoot = null; 380 381 ParserSet parser = null; 382 383 public DDRegistryParser(DDRegistry reg, DDRegistryParser rp, 384 String path) { 385 this.registry = reg; 386 this.initialize(path, rp, null); 387 } 388 389 public DDRegistryParser(DDRegistry reg, DDCursor cursor, 390 String path) { 391 this.registry = reg; 392 this.initialize(path, null, cursor); 393 } 394 395 public DDRegistryParser(DDRegistry reg, String path) { 396 this.registry = reg; 397 this.initialize(path, null, null); 398 } 399 400 public DDRegistry getRegistry() { 401 return this.registry; 402 } 403 404 407 void initialize(String path, DDRegistryParser regParser, DDCursor cursor) { 408 String graphName = null; 409 String subpath = null; 410 String parsingPath = null; 411 412 421 DDCursor cur = cursor; 422 if (cur == null && regParser != null) 423 cur = regParser.getCursor(); 424 425 if (path.startsWith("[")) { int idx = path.indexOf(']'); 427 graphName = path.substring(1, idx); 428 429 if (path.length() > idx+1) 430 path = path.substring(idx+1, path.length()); 431 else 432 path = "."; 434 idx = graphName.indexOf(':'); 435 if (idx != -1) { 436 subpath = graphName.substring(idx+1, graphName.length()-1); 437 graphName = graphName.substring(0, idx); 438 439 if (PathResolver.needResolving(subpath)) 440 subpath = (new PathResolver(cur, subpath)).toString(); 441 } 442 443 if (PathResolver.needResolving(graphName)) 444 graphName = (new PathResolver(cur, graphName)).toString(); 445 446 if (graphName.equals(CURRENT_CURSOR) && cursor != null) 447 graphName = null; 448 } 449 450 if (PathResolver.needResolving(path)) 451 parsingPath = (new PathResolver(cur, path)).toString(); 452 else 453 parsingPath = path; 454 455 456 if (graphName == null && regParser == null && cursor == null) { 457 throw new IllegalStateException (Common.getMessage( 458 "CantFindRootForParser_msg")); 459 } 460 461 if (graphName != null) { 466 467 BaseBean[] beans = this.registry.getRoots(graphName); 474 this.parser = new ParserSet(beans, null, parsingPath); 475 this.parser.setRoot(); 476 } else if (regParser != null) { 477 478 if (regParser.isRoot() && regParser.getRoots().length > 1) { 490 BaseBean[] beans = regParser.getRoots(); 491 492 if (regParser.hasParsingPath()) { 495 String pp = regParser.getParsingPath(); 496 ArrayList tmpArr = new ArrayList(); 497 498 for (int i=0; i<beans.length; i++) { 499 DDParser tmp = new DDParser(beans[i], pp); 500 if (tmp.hasNext()) 501 tmpArr.add(tmp.next()); 502 } 503 BaseBean[] newBeans = new BaseBean[tmpArr.size()]; 504 beans = (BaseBean[])tmpArr.toArray(newBeans); 505 } 506 507 this.parser = new ParserSet(beans, null, parsingPath); 508 } else { 509 while (regParser.current() == null && regParser.hasNext()) 510 regParser.next(); 511 512 this.parser = new ParserSet((BaseBean)regParser.current(), cur, 513 parsingPath); 514 515 } 516 517 } else if (cursor != null) { 518 this.parser = new ParserSet(cursor.getRoot(), cur, parsingPath); 522 } else { 523 throw new IllegalStateException ( Common.getMessage( 524 "NoParentSpecified_msg")); 525 } 526 } 527 528 529 boolean isRoot() { 530 return this.parser.isRoot(); 531 } 532 533 boolean hasParsingPath() { 534 return this.parser.hasParsingPath(); 535 } 536 537 String getParsingPath() { 538 return this.parser.getParsingPath(); 539 } 540 541 BaseBean[] getRoots() { 542 return this.parser.getRoots(); 543 } 544 545 public Object next() { 548 return this.parser.next(); 549 } 550 551 public boolean hasNext() { 552 return this.parser.hasNext(); 553 } 554 555 public DDCursor getCursor() { 556 Object o = this.current(); 557 558 if (o instanceof BaseBean) { 559 BaseBean b = (BaseBean)o; 560 if (b == null && this.hasNext()) 561 b = (BaseBean)this.next(); 562 if (b != null) 563 return new DDCursor(this.registry, b); 564 } else { 565 DDCursor cur = this.parser.getParentCursor(); 567 if (cur == null) { 568 BaseBean[] beans = this.parser.getRoots(); 569 cur = new DDCursor(this.registry, beans[0]); 570 } 571 return cur; 572 } 573 return null; 574 } 575 576 public DDParser.DDLocation getLocation() { 577 return this.parser.getLocation(); 578 } 579 580 public Object current() { 581 return this.parser.current(); 582 } 583 584 public void remove() { 585 throw new UnsupportedOperationException (); 586 } 587 588 public Object getValue(String ddName) { 589 BaseBean b = (BaseBean)this.current(); 590 if (b == null && this.hasNext()) 591 b = (BaseBean)this.next(); 592 593 if (b != null) { 594 DDCursor cur = new DDCursor(this.parser.getParentCursor(), b); 598 PathResolver p = new PathResolver(); 599 Object obj = p.resolvePathVar(cur, ddName); 600 return obj; 601 } 602 603 return null; 604 } 605 606 607 612 class ParserSet { 613 private BaseBean[] roots; 614 private int cur; 615 private String parsingPath; 616 private DDParser curParser; 617 private boolean isRoot; 618 private DDCursor parentCursor; 619 620 ParserSet(BaseBean[] roots, DDCursor cur, String path) { 621 if (roots != null && roots.length > 0 && roots[0] != null) { 622 this.cur = 0; 623 this.isRoot = false; 624 this.roots = roots; 625 this.parsingPath = path; 626 this.parentCursor = cur; 627 this.adjustPathRoot(); 628 this.newParser(); 629 } else { 630 throw new IllegalArgumentException (Common.getMessage( 631 "NoRootSpecified_msg", path)); 632 } 633 } 634 635 ParserSet(BaseBean root, DDCursor cur, String path) { 636 this(new BaseBean[] {root}, cur, path); 637 } 638 639 void adjustPathRoot() { 640 if (this.parsingPath.startsWith("../")) { int i = this.parsingPath.lastIndexOf("../"); int n = i/3; 643 do { 644 for (int j=0; j<this.roots.length; j++) { 645 if (this.roots[j].isRoot()) 646 throw new Schema2BeansRuntimeException(Common.getMessage( 647 "CantAccessBaseBeanNode_msg", this.parsingPath)); 648 this.roots[j] = this.roots[j].parent(); 649 } 650 } while(n-- > 0); 651 this.parsingPath = this.parsingPath.substring(i+3); 652 } 653 } 654 655 BaseBean[] getRoots() { 656 return this.roots; 657 } 658 659 DDCursor getParentCursor() { 660 return this.parentCursor; 661 } 662 663 void setRoot() { 664 this.isRoot = true; 665 } 666 667 boolean isRoot() { 668 return this.isRoot; 669 } 670 671 boolean hasParsingPath() { 672 if (this.parsingPath != null) 673 return !this.parsingPath.equals("."); else 675 return false; 676 } 677 678 String getParsingPath() { 679 return this.parsingPath; 680 } 681 682 private boolean newParser() { 683 if (this.cur < this.roots.length) { 684 try { 685 this.curParser = 686 new DDParser(this.roots[this.cur++], this.parsingPath); 687 return true; 688 } catch(NoSuchElementException e) { 689 if(this.parentCursor != null) { 692 this.cur = 0; 693 this.roots = 694 new BaseBean[] {this.parentCursor.getRoot()}; 695 this.parentCursor = this.parentCursor.getParent(); 696 return this.newParser(); 697 } 698 else 699 throw e; 700 } 701 } 702 return false; 703 } 704 705 boolean hasNext() { 707 boolean more = this.curParser.hasNext(); 708 709 while (!more && this.newParser()) 710 more = this.curParser.hasNext(); 711 712 return more; 713 } 714 715 DDParser.DDLocation getLocation() { 716 return this.curParser.getLocation(); 717 } 718 719 Object current() { 721 return this.curParser.current(); 722 } 723 724 Object next() { 726 if (this.hasNext()) { 727 return this.curParser.next(); 728 } else { 729 throw new NoSuchElementException(); 730 } 731 } 732 } 733 } 734 | Popular Tags |