1 package com.ca.commons.naming; 2 3 import java.lang.String ; 4 import java.util.Vector ; 5 import java.util.Enumeration ; 6 7 import javax.naming.*; 8 9 11 31 32 33 39 public class DN implements Name 40 { 41 45 private Vector RDNs; 49 boolean binary = false; 52 String errorString = ""; 57 58 NamingException rootException = null; 60 62 65 66 public static String BLANKBASEDN = "World"; 67 68 public DN() 69 { 70 RDNs = new Vector (); 71 } 72 73 79 80 public DN(DN copyMe) 81 { 82 try 83 { 84 RDNs = new Vector (); 85 86 if (copyMe != null) 87 { 88 for (int i=0; i<copyMe.size(); i++) 89 { 90 add(new String (copyMe.get(i))); 91 } 92 } 93 } 94 catch (InvalidNameException e) { 96 setError("error cloningDN " + copyMe.toString(), e); 97 clear(); 98 } 99 } 100 101 108 109 public DN(String ldapDN) 110 { 111 try 112 { 113 RDNs = new Vector (); 114 115 if ("".equals(ldapDN) || BLANKBASEDN.equals(ldapDN)) 116 { 117 return; 118 } 119 120 int start = 0; 121 int end = NameUtility.next(ldapDN, 0, ','); 122 123 while (end!=-1) 125 { 126 String rdn = ldapDN.substring(start,end); 127 128 add(0,rdn); 129 start = end+1; 130 end = NameUtility.next(ldapDN, start, ','); 131 } 132 133 add(0,ldapDN.substring(start).trim()); 135 136 } 137 catch (InvalidNameException e) 138 { 139 setError("unable to make DN from " + ldapDN ,e); 140 clear(); 141 } 142 143 } 144 145 146 147 148 155 156 157 public DN(Name name) 158 { 159 try 160 { 161 RDNs = new Vector (); 162 163 if (name.isEmpty()) return; 164 165 for (int i=0; i<name.size(); i++) 166 { 167 add(i,name.get(i)); 168 } 169 } 170 catch (InvalidNameException e) 171 { 172 setError("unable to create DN from name: " + name.toString(), e); 173 clear(); 174 } 175 } 176 177 183 184 189 190 public String toString() 191 { 192 String ldapDN = ""; 193 for (int i=0; i<RDNs.size(); i++) 194 ldapDN = get(i) + (i!=0?",":"") + ldapDN; 195 if (ldapDN.endsWith(",")) 196 { 197 if (ldapDN.charAt(ldapDN.length()-2) != '\\') 198 { 199 ldapDN = ldapDN.substring(0,ldapDN.length()-1); 200 } 201 } 202 return ldapDN; 203 } 204 205 206 207 212 213 public String toFormattedString() 214 { 215 String ldapDN = ""; 216 for (int i=0; i<RDNs.size(); i++) 217 ldapDN += get(i) + "\n"; 218 return ldapDN; 219 } 220 221 226 227 public String getDN() { return toString(); } 228 236 237 public String getRDNAttribute(int i) 238 { 239 if (isEmpty()) return ""; 240 if (i >= size()) return ""; 241 if (i < 0) return ""; 242 243 return getRDN(i).getAtt(); 244 } 245 246 254 255 public String getRDNValue(int i) 256 { 257 if (isEmpty()) return ""; 258 if (i >= size()) return ""; 259 if (i < 0) return ""; 260 261 return getRDN(i).getRawVal(); 262 } 263 264 267 268 public void debugPrint() 269 { 270 System.out.print("\n"); 271 for (int i=0; i<size(); i++) 272 { 273 System.out.print("element [" + i + "] = " + get(i).toString() + "\n"); 274 getRDN(i).dump(); 275 276 } 277 } 278 279 286 287 public void setRDN(RDN rdn, int i) 288 { 289 if (i<size() && i>= 0) 290 RDNs.setElementAt(rdn, i); 291 } 292 293 294 301 302 public RDN getRDN(int i) 303 { 304 if (i==0 && isEmpty()) return new RDN(); 306 if (i<0) return new RDN(); 307 if (i >= size()) new RDN(); 308 309 return (RDN) RDNs.elementAt(i); 310 } 311 312 317 318 public RDN getRootRDN() 319 { 320 if (isEmpty()) 321 return new RDN(""); 322 else 323 return getRDN(0); 324 } 325 326 333 334 public RDN getLowestRDN() 335 { 336 return getRDN(size()-1); 337 } 338 339 340 347 348 public void addParentRDN(String rdn) 349 { 350 try 351 { 352 add(0,rdn); 353 } 354 catch (InvalidNameException e) 355 { 356 setError("Error adding RDN in DN.addParentRDN()", e); 357 } 358 359 } 360 361 366 367 public void addChildRDN(String rdn) 368 throws InvalidNameException 369 { 370 add(rdn); 371 } 372 373 378 public void addChildRDN(RDN rdn) 379 throws InvalidNameException 380 { 381 add(rdn); 382 } 383 384 385 391 392 public void setLowestRDNRawValue(String value) 394 { 395 try 396 { 397 RDN rdn = getRDN(size()-1); 398 rdn.setRawVal(value); 399 } 400 catch (InvalidNameException e) 401 { 402 setError("Error setting DN.setLowestRDNRawValue: to " + value, e); 403 } 404 405 } 406 407 410 protected String exchangeRDNelementValue(String rdn, String value) 411 { 412 return rdn.substring(0,NameUtility.next(rdn,0,'=')) + "=" + value; 413 414 } 415 416 417 422 423 public boolean equals(DN testDN) 424 { 425 if (testDN == null) return false; 427 428 if (testDN.size()!= size()) return false; 429 430 for (int i=0; i<size(); i++) 431 { 432 if (getRDN(i).equals(testDN.getRDN(i)) == false) 433 return false; 434 } 435 return true; 436 } 437 438 443 public boolean equals(Object o) 444 { 445 if (o == null) 446 return false; 447 if (o instanceof DN) 448 return equals((DN)o); 449 else if (o instanceof Name) 450 return (compareTo((Name)o) == 0); 451 else 452 return false; } 454 460 461 public boolean startsWith(DN testDN) 462 { 463 return startsWith((Name)testDN); 464 } 465 466 473 474 public boolean sharesParent(DN testDN) 475 { 476 if (testDN.size()!= size()) return false; 477 478 for (int i=0; i<size()-1; i++) 479 { 480 if ((testDN.getRDN(i).equals(getRDN(i)))==false) 481 return false; 482 } 483 return true; 484 } 485 486 492 493 public DN parentDN() 494 { 495 497 if (size()<=1) return new DN(); 499 DN newDN = new DN(this); 500 newDN.RDNs.removeElementAt(size()-1); 501 return newDN; 502 } 503 504 507 508 public void reverse() 509 { 510 Vector rev = new Vector (); 511 for (int i=RDNs.size()-1; i>=0; i--) 512 rev.add(RDNs.elementAt(i)); 513 RDNs = rev; 514 } 515 516 519 public void clear() 520 { 521 RDNs.clear(); 522 errorString = null; 523 } 524 525 528 public void setError(String msg, NamingException e) 529 { 530 errorString = msg; 531 rootException = e; 532 System.out.println(e); 533 } 534 535 538 public boolean error() 539 { 540 return (errorString == null); 541 } 542 543 546 public String getError() 547 { 548 return errorString; 549 } 550 551 554 555 public NamingException getNamingException() 556 { 557 return rootException; 558 } 559 560 563 572 576 586 590 600 601 604 public Name add(RDN rdn) 605 { 606 add(size(), rdn); 608 return this; 609 } 610 611 617 618 public Name add(int posn, RDN rdn) 619 { 620 RDNs.insertElementAt(rdn,posn); 621 return this; 622 } 623 624 625 626 632 635 636 639 public Name add(int posn, String rdn) 640 throws InvalidNameException 641 { 642 RDN r = new RDN(rdn); add(posn, r); 644 return this; 645 } 646 647 650 651 public Name add(String rdn) 652 throws InvalidNameException 653 { 654 RDN r = new RDN(rdn); add(size(), r); 656 return this; 657 } 658 659 660 663 664 public Name addAll(int posn, Name n) 665 throws InvalidNameException 666 { 667 Enumeration e = n.getAll(); 668 while (e.hasMoreElements()) 669 add(posn++, e.nextElement().toString()); 670 return this; 671 } 672 673 674 677 678 public Name addAll(Name suffix) 679 throws InvalidNameException 680 { 681 Enumeration e = suffix.getAll(); 682 while (e.hasMoreElements()) 683 add(e.nextElement().toString()); 684 return this; 685 } 686 687 688 691 public Object clone() 692 { 693 return new DN(this); 694 } 695 696 697 702 703 public int compareTo(Object obj) 704 { 705 int val = 0; 706 int pos = 1; 707 if (obj instanceof Name) 708 { 709 Name compareMe = (Name)obj; 710 int size = size(); 711 int compSize = compareMe.size(); 712 while (val == 0) 713 { 714 String RDN = get(size-pos); 715 String compRDN = compareMe.get(compSize-pos); 716 int rdnOrder = RDN.compareTo(compRDN); 717 718 if (rdnOrder != 0) 719 return rdnOrder; 721 pos++; 722 if (pos>size || pos>compSize) 723 { 724 if (size==compSize) 725 return 0; if (pos>size) 727 return -1; else 729 return 1; 730 } 731 } 732 } 733 else 734 throw new ClassCastException ("non Name object in DN.compareTo - object was " + obj.getClass()); 735 736 return 0; } 738 739 740 743 744 public boolean endsWith(Name n) 745 { 746 return false; 747 } 748 749 750 755 756 public String get(int posn) 757 { 758 if (posn==0 && isEmpty()) return ""; 760 return RDNs.elementAt(posn).toString(); 761 } 762 763 766 767 public java.util.Enumeration getAll() 768 { 769 DXNamingEnumeration ret = new DXNamingEnumeration(); 770 for (int i=0; i<size(); i++) 771 ret.add(get(i)); 772 return ret; 773 } 774 775 778 779 public Name getPrefix(int posn) 780 { 781 DN returnMe = new DN(); 782 try 783 { 784 for (int i=0; i<posn; i++) 785 returnMe.add(get(i)); 786 return returnMe; 787 } 788 catch (InvalidNameException e) 789 { 790 System.err.println("unexpected error in DN:\n " + e); 791 return new DN(); 792 } 793 } 794 795 798 799 public Name getSuffix(int posn) 800 { 801 DN returnMe = new DN(); 802 for (int i=posn; i<size(); i++) 803 { 804 returnMe.add(new RDN(getRDN(i))); 805 } 806 return returnMe; 807 } 808 809 813 814 public boolean isEmpty() 815 { 816 return (size()==0); 817 } 818 819 820 823 824 public Object remove(int posn) 825 { 826 return RDNs.remove(posn); 827 } 828 829 834 835 public int size() 836 { 837 return RDNs.size(); 838 } 839 840 846 852 855 856 public boolean startsWith(Name n) 857 { 858 int pos = 0; 859 Enumeration e = n.getAll(); 860 while (e.hasMoreElements()) 861 if (e.nextElement().toString().equalsIgnoreCase(get(pos++).toString())==false) 862 return false; 863 864 return true; } 866 867 868 869 } 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 | Popular Tags |