1 7 8 package javax.naming.ldap; 9 10 import javax.naming.Name ; 11 import javax.naming.InvalidNameException ; 12 13 import java.util.Enumeration ; 14 import java.util.Collection ; 15 import java.util.ArrayList ; 16 import java.util.List ; 17 import java.util.Iterator ; 18 import java.util.ListIterator ; 19 import java.util.Collections ; 20 21 import java.io.ObjectOutputStream ; 22 import java.io.ObjectInputStream ; 23 import java.io.IOException ; 24 25 87 88 public class LdapName implements Name { 89 90 92 private transient ArrayList rdns; private transient String unparsed; private static final long serialVersionUID = -1595520034788997356L; 95 96 106 public LdapName(String name) throws InvalidNameException { 107 unparsed = name; 108 parse(); 109 } 110 111 119 public LdapName(List <Rdn > rdns) { 120 121 130 this.rdns = new ArrayList (rdns.size()); 131 for (int i = 0; i < rdns.size(); i++) { 132 Object obj = rdns.get(i); 133 if (!(obj instanceof Rdn )) { 134 throw new IllegalArgumentException ("Entry:" + obj + 135 " not a valid type;list entries must be of type Rdn"); 136 } 137 this.rdns.add(obj); 138 } 139 } 140 141 147 149 private LdapName(String name, ArrayList rdns, int beg, int end) { 150 unparsed = name; 151 153 List sList = rdns.subList(beg, end); 154 this.rdns = new ArrayList (sList); 155 } 156 157 161 public int size() { 162 return rdns.size(); 163 } 164 165 170 public boolean isEmpty() { 171 return rdns.isEmpty(); 172 } 173 174 186 public Enumeration <String > getAll() { 187 final Iterator iter = rdns.iterator(); 188 189 return new Enumeration <String >() { 190 public boolean hasMoreElements() { 191 return iter.hasNext(); 192 } 193 public String nextElement() { 194 return iter.next().toString(); 195 } 196 }; 197 } 198 199 207 public String get(int posn) { 208 return rdns.get(posn).toString(); 209 } 210 211 219 public Rdn getRdn(int posn) { 220 return (Rdn ) rdns.get(posn); 221 } 222 223 236 public Name getPrefix(int posn) { 237 try { 238 return new LdapName (null, rdns, 0, posn); 239 } catch (IllegalArgumentException e) { 240 throw new IndexOutOfBoundsException ( 241 "Posn: " + posn + ", Size: "+ rdns.size()); 242 } 243 } 244 245 260 public Name getSuffix(int posn) { 261 try { 262 return new LdapName (null, rdns, posn, rdns.size()); 263 } catch (IllegalArgumentException e) { 264 throw new IndexOutOfBoundsException ( 265 "Posn: " + posn + ", Size: "+ rdns.size()); 266 } 267 } 268 269 282 public boolean startsWith(Name n) { 283 if (n == null) { 284 return false; 285 } 286 int len1 = rdns.size(); 287 int len2 = n.size(); 288 return (len1 >= len2 && 289 matches(0, len2, n)); 290 } 291 292 303 public boolean startsWith(List <Rdn > rdns) { 304 if (rdns == null) { 305 return false; 306 } 307 int len1 = this.rdns.size(); 308 int len2 = rdns.size(); 309 return (len1 >= len2 && 310 doesListMatch(0, len2, rdns)); 311 } 312 313 325 public boolean endsWith(Name n) { 326 if (n == null) { 327 return false; 328 } 329 int len1 = rdns.size(); 330 int len2 = n.size(); 331 return (len1 >= len2 && 332 matches(len1 - len2, len1, n)); 333 } 334 335 346 public boolean endsWith(List <Rdn > rdns) { 347 if (rdns == null) { 348 return false; 349 } 350 int len1 = this.rdns.size(); 351 int len2 = rdns.size(); 352 return (len1 >= len2 && 353 doesListMatch(len1 - len2, len1, rdns)); 354 } 355 356 private boolean doesListMatch(int beg, int end, List rdns) { 357 for (int i = beg; i < end; i++) { 358 if (!this.rdns.get(i).equals(rdns.get(i - beg))) { 359 return false; 360 } 361 } 362 return true; 363 } 364 365 372 private boolean matches(int beg, int end, Name n) { 373 if (n instanceof LdapName ) { 374 LdapName ln = (LdapName ) n; 375 return doesListMatch(beg, end, ln.rdns); 376 } else { 377 for (int i = beg; i < end; i++) { 378 Rdn rdn; 379 String rdnString = n.get(i - beg); 380 try { 381 rdn = (new Rfc2253Parser (rdnString)).parseRdn(); 382 } catch (InvalidNameException e) { 383 return false; 384 } 385 if (!rdn.equals(rdns.get(i))) { 386 return false; 387 } 388 } 389 } 390 return true; 391 } 392 393 403 public Name addAll(Name suffix) throws InvalidNameException { 404 return addAll(size(), suffix); 405 } 406 407 408 414 public Name addAll(List <Rdn > suffixRdns) { 415 return addAll(size(), suffixRdns); 416 } 417 418 436 public Name addAll(int posn, Name suffix) 437 throws InvalidNameException { 438 unparsed = null; if (suffix instanceof LdapName ) { 440 LdapName s = (LdapName ) suffix; 441 rdns.addAll(posn, s.rdns); 442 } else { 443 Enumeration comps = suffix.getAll(); 444 while (comps.hasMoreElements()) { 445 rdns.add(posn++, 446 (new Rfc2253Parser ((String ) comps.nextElement()). 447 parseRdn())); 448 } 449 } 450 return this; 451 } 452 453 467 public Name addAll(int posn, List <Rdn > suffixRdns) { 468 unparsed = null; 469 for (int i = 0; i < suffixRdns.size(); i++) { 470 Object obj = suffixRdns.get(i); 471 if (!(obj instanceof Rdn )) { 472 throw new IllegalArgumentException ("Entry:" + obj + 473 " not a valid type;suffix list entries must be of type Rdn"); 474 } 475 rdns.add(i + posn, obj); 476 } 477 return this; 478 } 479 480 489 public Name add(String comp) throws InvalidNameException { 490 return add(size(), comp); 491 } 492 493 501 public Name add(Rdn comp) { 502 return add(size(), comp); 503 } 504 505 522 public Name add(int posn, String comp) throws InvalidNameException { 523 Rdn rdn = (new Rfc2253Parser (comp)).parseRdn(); 524 rdns.add(posn, rdn); 525 unparsed = null; return this; 527 } 528 529 544 public Name add(int posn, Rdn comp) { 545 if (comp == null) { 546 throw new NullPointerException ("Cannot set comp to null"); 547 } 548 rdns.add(posn, comp); 549 unparsed = null; return this; 551 } 552 553 568 public Object remove(int posn) throws InvalidNameException { 569 unparsed = null; return rdns.remove(posn).toString(); 571 } 572 573 583 public List <Rdn > getRdns() { 584 return Collections.unmodifiableList(rdns); 585 } 586 587 594 public Object clone() { 595 return new LdapName (unparsed, rdns, 0, rdns.size()); 596 } 597 598 606 public String toString() { 607 if (unparsed != null) { 608 return unparsed; 609 } 610 StringBuilder builder = new StringBuilder (); 611 int size = rdns.size(); 612 if ((size - 1) >= 0) { 613 builder.append((Rdn ) rdns.get(size - 1)); 614 } 615 for (int next = size - 2; next >= 0; next--) { 616 builder.append(','); 617 builder.append((Rdn ) rdns.get(next)); 618 } 619 unparsed = builder.toString(); 620 return unparsed; 621 } 622 623 639 public boolean equals(Object obj) { 640 if (obj == this) { 642 return true; 643 } 644 if (!(obj instanceof LdapName )) { 645 return false; 646 } 647 LdapName that = (LdapName ) obj; 648 if (rdns.size() != that.rdns.size()) { 649 return false; 650 } 651 if (unparsed != null && unparsed.equalsIgnoreCase( 652 that.unparsed)) { 653 return true; 654 } 655 for (int i = 0; i < rdns.size(); i++) { 657 Rdn rdn1 = (Rdn ) rdns.get(i); 659 Rdn rdn2 = (Rdn ) that.rdns.get(i); 660 if (!rdn1.equals(rdn2)) { 661 return false; 662 } 663 } 664 return true; 665 } 666 667 692 public int compareTo(Object obj) { 693 694 if (!(obj instanceof LdapName )) { 695 throw new ClassCastException ("The obj is not a LdapName"); 696 } 697 698 if (obj == this) { 700 return 0; 701 } 702 LdapName that = (LdapName ) obj; 703 704 if (unparsed != null && unparsed.equalsIgnoreCase( 705 that.unparsed)) { 706 return 0; 707 } 708 709 int minSize = Math.min(rdns.size(), that.rdns.size()); 711 for (int i = 0; i < minSize; i++) { 712 Rdn rdn1 = (Rdn )rdns.get(i); 714 Rdn rdn2 = (Rdn )that.rdns.get(i); 715 716 int diff = rdn1.compareTo(rdn2); 717 if (diff != 0) { 718 return diff; 719 } 720 } 721 return (rdns.size() - that.rdns.size()); } 723 724 732 public int hashCode() { 733 int hash = 0; 735 736 for (int i = 0; i < rdns.size(); i++) { 738 Rdn rdn = (Rdn ) rdns.get(i); 739 hash += rdn.hashCode(); 740 } 741 return hash; 742 } 743 744 750 private void writeObject(ObjectOutputStream s) 751 throws java.io.IOException { 752 s.defaultWriteObject(); 753 s.writeObject(toString()); 754 } 755 756 private void readObject(ObjectInputStream s) 757 throws java.io.IOException , ClassNotFoundException { 758 s.defaultReadObject(); 759 unparsed = (String )s.readObject(); 760 try { 761 parse(); 762 } catch (InvalidNameException e) { 763 throw new java.io.StreamCorruptedException ( 765 "Invalid name: " + unparsed); 766 } 767 } 768 769 private void parse() throws InvalidNameException { 770 772 rdns = (ArrayList ) (new Rfc2253Parser (unparsed)).parseDn(); 773 } 774 } 775 | Popular Tags |