1 23 24 package org.apache.slide.structure; 25 26 import java.io.Serializable ; 27 import java.util.Collections ; 28 import java.util.Enumeration ; 29 import java.util.Vector ; 30 import java.util.Set ; 31 import java.util.HashSet ; 32 import org.apache.slide.common.ObjectValidationFailedException; 33 import org.apache.slide.common.UriPath; 34 import org.apache.slide.util.EmptyEnumeration; 35 import org.apache.slide.util.Messages; 36 37 44 public abstract class ObjectNode 45 implements Serializable , Cloneable { 46 47 48 51 private Set updatedBindings = null; 52 53 56 protected String uri; 57 58 65 private String uuri; 66 67 72 private Vector links = null; 73 74 80 82 87 private BindingList bindings; 88 private ParentBindingList parentBindings; 89 90 94 private boolean bindingsShared; 95 96 private Vector childrenCache = null; 97 98 private transient UriPath path = null; 99 100 101 104 public ObjectNode() { 105 this.bindings = new BindingList(); 106 this.parentBindings = new ParentBindingList(); 107 } 108 109 112 public ObjectNode(String uri) { 113 this(); 114 this.uri = uri; 115 } 116 117 122 public ObjectNode(String uri, Vector children, Vector links) { 123 this( uri ); 124 this.links = links; 125 addChildren(children); 126 } 127 128 131 public ObjectNode(String uuri, Vector bindings, Vector parentBindings, Vector links) { 132 this(); 133 this.uuri = uuri; 134 this.bindings = new BindingList(bindings); 135 this.parentBindings = new ParentBindingList(parentBindings); 136 this.links = links; 137 Enumeration e = bindings.elements(); 138 if (e.hasMoreElements()) this.updatedBindings = new HashSet (); 139 while(e.hasMoreElements()) { 140 updatedBindings.add(((ObjectNode.Binding)e.nextElement()).getUuri()); 141 } 142 } 143 144 149 public String getUri() { 150 return this.uri; 151 } 152 153 158 public void setUri(String uri) { 159 this.uri = uri; 160 this.path = null; 161 } 162 163 169 public String getUuri() { 170 if (uuri != null) { 171 return uuri; 172 } else { 173 if (uri == null) { 174 throw new IllegalStateException (toString()); 175 } 176 return uri; 177 } 178 } 179 180 183 public void setUuri(String uuri) { 184 if (uuri == null) { 185 throw new IllegalArgumentException (); 186 } 187 this.uuri = uuri; 188 } 189 190 191 public Set getUpdatedBindings() { 192 if (this.updatedBindings == null) { 193 return Collections.EMPTY_SET; 194 } else { 195 return Collections.unmodifiableSet(updatedBindings); 196 } 197 } 198 199 public void resetUpdatedBindings() { 200 this.updatedBindings = null; 201 } 202 203 208 public Vector getChildren() { 209 if (childrenCache == null) { 210 computeChildren(); 211 } 212 return childrenCache; 213 } 214 215 220 public Enumeration enumerateChildren() { 221 return getChildren().elements(); 222 } 223 224 229 public Enumeration enumerateBindings() { 230 return bindings.elements(); 231 } 232 233 238 public Enumeration enumerateParentBindings() { 239 return parentBindings.elements(); 240 } 241 242 248 public String getBindingUuri( String bindingName ) { 249 String result = null; 250 Binding b = bindings.get(bindingName); 251 if (b != null) { 252 result = b.getUuri(); 253 } 254 return result; 255 } 256 257 264 public boolean hasChild(String uri) { 265 return getChildren().contains(uri); 266 } 267 268 275 public boolean hasChild(ObjectNode child) { 276 boolean result = false; 277 if (child != null) { 278 result = getChildren().contains(child.getUri()); 279 } 280 return result; 281 } 282 283 290 public boolean hasBinding( String bindingName ) { 291 boolean result = false; 292 if (bindingName != null) { 293 result = (bindings.get(bindingName) != null); 294 } 295 return result; 296 } 297 298 305 public boolean hasParentBinding( String bindingName ) { 306 boolean result = false; 307 if (bindingName != null) { 308 result = (parentBindings.get(bindingName) != null); 309 } 310 return result; 311 } 312 313 321 public boolean hasBinding( ObjectNode child ) { 322 if (child != null) { 323 Enumeration benum = enumerateBindings(); 324 while (benum.hasMoreElements()) { 325 Binding b = (Binding)benum.nextElement(); 326 if (b.getUuri().equals(child.getUuri())) { 327 return true; 328 } 329 } 330 } 331 return false; 332 } 333 334 337 public int numberOfParentBindings() { 338 return parentBindings.size(); 339 } 340 341 346 public boolean hasChildren() { 347 return !(getChildren().isEmpty()); 348 } 349 350 351 356 public boolean hasLinks() { 357 if (this.links == null) { 358 return false; 359 } else { 360 return !( links.isEmpty()); 361 } 362 } 363 368 public Enumeration enumerateLinks() { 369 if (this.links == null) { 370 return EmptyEnumeration.INSTANCE; 371 } else { 372 return links.elements(); 373 } 374 } 375 376 377 386 public boolean equals(Object obj) { 387 if (obj == this) { 388 return true; 389 } 390 if (obj instanceof ObjectNode) { 391 return getPath().equals(((ObjectNode)obj).getPath()); 392 } 393 else { 394 return false; 395 } 396 } 397 398 public int hashCode() { 399 return getPath().hashCode(); 400 } 401 402 407 public ObjectNode cloneObject() { 408 ObjectNode result = null; 409 410 try { 411 this.bindingsShared=true; 414 result = (ObjectNode) super.clone(); 415 } catch(CloneNotSupportedException e) { 416 e.printStackTrace(); 417 } 418 419 return result; 420 } 421 422 427 public ObjectNode copyObject() { 428 ObjectNode result = null; 429 childrenCache = null; 430 431 try { 432 result = (ObjectNode) super.clone(); 433 result.bindingsShared=false; 435 result.links = new Vector (); 436 result.bindings = new BindingList(); 437 result.parentBindings = new ParentBindingList(); 438 } catch(CloneNotSupportedException e) { 439 e.printStackTrace(); 440 } 441 442 return result; 443 } 444 445 450 public void validate(String expectedUri) { 451 452 if (uri == null) 453 throw new ObjectValidationFailedException 454 (expectedUri, Messages.message 455 (ObjectNode.class.getName() + ".nullUri")); 456 457 if (!uri.equals(expectedUri) && !uuri.equals(expectedUri)) 458 throw new ObjectValidationFailedException 459 (expectedUri, Messages.message 460 (ObjectNode.class.getName() + ".incorrectUri")); 461 462 if (bindings == null || parentBindings == null) 463 throw new ObjectValidationFailedException 464 (uri, Messages.message 465 (ObjectNode.class.getName() + ".nullBindingsVector")); 466 467 } 468 469 473 public void addChild( ObjectNode child ) { 474 addBinding( child.getPath().lastSegment(), child ); 475 } 476 477 478 482 public void addLink( LinkNode link ) { 483 if (this.links == null) this.links = new Vector (); 484 links.add(link.getUri()); 485 } 486 487 492 public void addBinding( String bindingName, ObjectNode source ) { 493 if (this.updatedBindings == null) this.updatedBindings = new HashSet (); 494 updatedBindings.add(source.getUri()); 495 496 if (!hasBinding(bindingName)) { 497 if(bindingsShared) { 498 bindings=(BindingList)bindings.clone(); 500 parentBindings=(ParentBindingList)parentBindings.clone(); 501 bindingsShared=false; 502 } 503 bindings.put(bindingName, source); 504 childrenCache = null; 505 source.addParentBinding(bindingName, this); 506 } 507 else { 508 throw new IllegalStateException ( 509 "Existing binding "+bindingName+" at "+this.uri+" has to be removed first"); 510 } 511 } 512 513 518 public void removeChild(ObjectNode child) { 519 if (this.updatedBindings == null) this.updatedBindings = new HashSet (); 520 updatedBindings.add(child.getUri()); 521 522 if (child == null) { 523 return; 524 } 525 526 if(bindingsShared) { 527 bindings=(BindingList)bindings.clone(); 529 bindingsShared=false; 530 } 531 String bindingName = lastUriSegment( child.getUri() ); 532 bindings.remove(bindingName); 533 childrenCache = null; 534 child.removeParentBinding(bindingName, this); 535 } 536 537 542 public void removeLink(LinkNode link) { 543 if (this.links != null) { 544 links.remove(link.getUri()); 545 } 546 } 547 548 553 public UriPath getPath() { 554 if (path == null) { 555 path = new UriPath(getUri()); 556 } 557 return path; 558 } 559 560 568 private String lastUriSegment( String uri ) { 569 return new UriPath(uri).lastSegment(); 570 } 571 572 private void computeChildren() { 573 childrenCache = new Vector (); 574 Enumeration e = bindings.elements(); 575 while (e.hasMoreElements()) { 576 Binding b = (Binding)e.nextElement(); 577 StringBuffer buf = new StringBuffer (uri); 578 if (!uri.endsWith("/")) { 579 buf.append("/"); 580 } 581 buf.append(b.getName()); 582 childrenCache.add( buf.toString() ); 583 } 584 } 585 586 591 private void addChildren( Vector children ) { 592 Enumeration ch = children.elements(); 593 while (ch.hasMoreElements()) { 594 String c = (String )ch.nextElement(); 595 ObjectNode s = new SubjectNode(c); 596 s.setUuri( s.getUri() ); 597 addBinding( lastUriSegment(c), s ); 598 } 599 ObjectNode p = null; 600 UriPath up = new UriPath(uri); 601 UriPath pup = up.parent(); 602 if (pup != null) { 603 String pUri = pup.toString(); 604 p = new SubjectNode( pUri ); 605 p.setUuri( p.getUri() ); 606 } 607 addParentBinding( getPath().lastSegment(), p ); 608 } 609 610 public void addParentBinding( String bindingName, ObjectNode parent ) { 611 if(bindingsShared) { 612 bindings=(BindingList)bindings.clone(); 614 parentBindings=(ParentBindingList)parentBindings.clone(); 615 bindingsShared=false; 616 } 617 parentBindings.put(bindingName, parent); 618 } 619 620 621 private void removeParentBinding( String bindingName, ObjectNode parent ) { 622 if(bindingsShared) { 623 bindings=(BindingList)bindings.clone(); 625 parentBindings=(ParentBindingList)parentBindings.clone(); 626 bindingsShared=false; 627 } 628 parentBindings.remove(bindingName, parent.getUuri() ); 629 } 630 631 public String toString() { 632 StringBuffer b = new StringBuffer (getUri()); 633 if (!getUri().equals(getUuri())) { 634 b.append(" [").append(getUuri()).append("]"); 635 } 636 return b.toString(); 637 } 638 639 642 public static class Binding implements Serializable , Cloneable { 643 644 protected final String bName; 645 protected final String bUuri; 646 647 public Binding(String bindingName, String bindingUuri) { 648 this.bName = bindingName; 649 this.bUuri = bindingUuri; 650 } 651 652 public String getName() { 653 return bName; 654 } 655 656 public String getUuri() { 657 return bUuri; 658 } 659 660 public boolean equals( Object o ) { 661 boolean result = false; 662 if (o instanceof Binding) { 663 Binding b = (Binding)o; 664 result = getName().equals( b.getName() ); 665 } 666 return result; 667 } 668 669 public String toString() { 670 return bName+"->"+bUuri; 671 } 672 } 673 674 677 public static class ParentBinding extends Binding { 678 679 public ParentBinding(String bindingName, String bindingUuri) { 680 super(bindingName, bindingUuri); 681 } 682 683 public boolean equals( Object o ) { 684 boolean result = false; 685 if (o instanceof ParentBinding) { 686 ParentBinding b = (ParentBinding)o; 687 result = 688 getName().equals(b.getName()) && 689 this.getUuri().equals(b.getUuri()); 690 } 691 return result; 692 } 693 694 public String toString() { 695 return bName+":"+bUuri; 696 } 697 } 698 699 public static class ParentBindingList extends BindingList { 700 701 public ParentBindingList() { 702 super(); 703 } 704 705 public ParentBindingList(Vector container) { 706 super(container); 707 } 708 709 protected Enumeration elements() { 710 return container.elements(); 711 } 712 713 protected Binding put(String bindingName, ObjectNode source) { 714 String uuri = ""; 715 if (source != null && source.getUuri() != null) { 716 uuri = source.getUuri(); 717 } 718 int i = container.indexOf( new ParentBinding(bindingName, uuri) ); 719 ParentBinding result = null; 720 if (i >= 0) { 721 result = (ParentBinding)container.get(i); 722 container.set( i, new ParentBinding(bindingName, uuri) ); 723 } 724 else { 725 container.add( new ParentBinding(bindingName, uuri) ); 726 } 727 return result; 728 } 729 730 protected void remove(String bindingName, String uuri) { 731 container.removeElement( new ParentBinding(bindingName, uuri) ); 732 } 733 734 public synchronized Object clone() { 735 return new ParentBindingList( (Vector )container.clone() ); 736 } 737 738 public String toString() { 739 return String.valueOf(container); 740 } 741 } 742 743 public static class BindingList implements Serializable , Cloneable { 744 745 protected Vector container; 746 747 public BindingList() { 748 this.container = new Vector (); 749 } 750 751 public BindingList(Vector container) { 752 this.container = container; 753 } 754 755 protected Enumeration elements() { 756 return container.elements(); 757 } 758 759 public Binding get(String bindingName) { 760 Binding b = null; 761 int i = container.indexOf( new Binding(bindingName, null) ); 762 if (i >= 0) { 763 b = (Binding)container.get(i); 764 } 765 return b; 766 } 767 768 protected Binding put(String bindingName, ObjectNode source) { 769 Binding result = null; 770 String uuri = ""; 771 if (source != null && source.getUuri() != null) { 772 uuri = source.getUuri(); 773 } 774 int i = container.indexOf( new Binding(bindingName, null) ); 775 if (i >= 0) { 776 result = (Binding)container.get(i); 777 container.set( i, new Binding(bindingName, uuri) ); 778 } 779 else { 780 container.add( new Binding(bindingName, uuri) ); 781 } 782 return result; 783 } 784 785 protected void remove(String bindingName) { 786 container.removeElement( new Binding(bindingName, null) ); 787 } 788 789 public synchronized Object clone() { 790 return new BindingList( (Vector )container.clone() ); 791 } 792 793 public String toString() { 794 return String.valueOf(container); 795 } 796 797 public int size() { 798 return container.size(); 799 } 800 } 801 } 802 803 | Popular Tags |