1 7 8 package javax.naming; 9 10 import java.util.Vector ; 11 import java.util.Enumeration ; 12 import java.util.Properties ; 13 import java.util.NoSuchElementException ; 14 15 25 26 class NameImpl { 27 private static final byte LEFT_TO_RIGHT = 1; 28 private static final byte RIGHT_TO_LEFT = 2; 29 private static final byte FLAT = 0; 30 31 private Vector components; 32 33 private byte syntaxDirection = LEFT_TO_RIGHT; 34 private String syntaxSeparator = "/"; 35 private String syntaxSeparator2 = null; 36 private boolean syntaxCaseInsensitive = false; 37 private boolean syntaxTrimBlanks = false; 38 private String syntaxEscape = "\\"; 39 private String syntaxBeginQuote1 = "\""; 40 private String syntaxEndQuote1 = "\""; 41 private String syntaxBeginQuote2 = "'"; 42 private String syntaxEndQuote2 = "'"; 43 private String syntaxAvaSeparator = null; 44 private String syntaxTypevalSeparator = null; 45 46 private static final int STYLE_NONE = 0; 51 private static final int STYLE_QUOTE1 = 1; 52 private static final int STYLE_QUOTE2 = 2; 53 private static final int STYLE_ESCAPE = 3; 54 private int escapingStyle = STYLE_NONE; 55 56 private final boolean isA(String n, int i, String match) { 59 return (match != null && n.startsWith(match, i)); 60 } 61 62 private final boolean isMeta(String n, int i) { 63 return (isA(n, i, syntaxEscape) || 64 isA(n, i, syntaxBeginQuote1) || 65 isA(n, i, syntaxBeginQuote2) || 66 isSeparator(n, i)); 67 } 68 69 private final boolean isSeparator(String n, int i) { 70 return (isA(n, i, syntaxSeparator) || 71 isA(n, i, syntaxSeparator2)); 72 } 73 74 private final int skipSeparator(String name, int i) { 75 if (isA(name, i, syntaxSeparator)) { 76 i += syntaxSeparator.length(); 77 } else if (isA(name, i, syntaxSeparator2)) { 78 i += syntaxSeparator2.length(); 79 } 80 return (i); 81 } 82 83 private final int extractComp(String name, int i, int len, Vector comps) 84 throws InvalidNameException { 85 String beginQuote; 86 String endQuote; 87 boolean start = true; 88 boolean one = false; 89 StringBuffer answer = new StringBuffer (len); 90 91 while (i < len) { 92 if (start && ((one = isA(name, i, syntaxBeginQuote1)) || 94 isA(name, i, syntaxBeginQuote2))) { 95 96 beginQuote = one ? syntaxBeginQuote1 : syntaxBeginQuote2; 98 endQuote = one ? syntaxEndQuote1 : syntaxEndQuote2; 99 if (escapingStyle == STYLE_NONE) { 100 escapingStyle = one ? STYLE_QUOTE1 : STYLE_QUOTE2; 101 } 102 103 for (i += beginQuote.length(); 105 ((i < len) && !name.startsWith(endQuote, i)); 106 i++) { 107 if (isA(name, i, syntaxEscape) && 110 isA(name, i + syntaxEscape.length(), endQuote)) { 111 i += syntaxEscape.length(); 112 } 113 answer.append(name.charAt(i)); } 115 116 if (i >= len) 118 throw 119 new InvalidNameException (name + ": no close quote"); 120 122 i += endQuote.length(); 123 124 if (i == len || isSeparator(name, i)) { 126 break; 127 } 128 throw (new InvalidNameException (name + 130 ": close quote appears before end of component")); 131 132 } else if (isSeparator(name, i)) { 133 break; 134 135 } else if (isA(name, i, syntaxEscape)) { 136 if (isMeta(name, i + syntaxEscape.length())) { 137 i += syntaxEscape.length(); 140 if (escapingStyle == STYLE_NONE) { 141 escapingStyle = STYLE_ESCAPE; 142 } 143 } else if (i + syntaxEscape.length() >= len) { 144 throw (new InvalidNameException (name + 145 ": unescaped " + syntaxEscape + " at end of component")); 146 } 147 } else if (isA(name, i, syntaxTypevalSeparator) && 148 ((one = isA(name, i+syntaxTypevalSeparator.length(), syntaxBeginQuote1)) || 149 isA(name, i+syntaxTypevalSeparator.length(), syntaxBeginQuote2))) { 150 beginQuote = one ? syntaxBeginQuote1 : syntaxBeginQuote2; 152 endQuote = one ? syntaxEndQuote1 : syntaxEndQuote2; 153 154 i += syntaxTypevalSeparator.length(); 155 answer.append(syntaxTypevalSeparator+beginQuote); 157 for (i += beginQuote.length(); 159 ((i < len) && !name.startsWith(endQuote, i)); 160 i++) { 161 if (isA(name, i, syntaxEscape) && 164 isA(name, i + syntaxEscape.length(), endQuote)) { 165 i += syntaxEscape.length(); 166 } 167 answer.append(name.charAt(i)); } 169 170 if (i >= len) 172 throw 173 new InvalidNameException (name + ": typeval no close quote"); 174 175 i += endQuote.length(); 176 answer.append(endQuote); 178 if (i == len || isSeparator(name, i)) { 180 break; 181 } 182 throw (new InvalidNameException (name.substring(i) + 183 ": typeval close quote appears before end of component")); 184 } 185 186 answer.append(name.charAt(i++)); 187 start = false; 188 } 189 190 if (syntaxDirection == RIGHT_TO_LEFT) 191 comps.insertElementAt(answer.toString(), 0); 192 else 193 comps.addElement(answer.toString()); 194 return i; 195 } 196 197 private static boolean getBoolean(Properties p, String name) { 198 return toBoolean(p.getProperty(name)); 199 } 200 201 private static boolean toBoolean(String name) { 202 return ((name != null) && name.toLowerCase().equals("true")); 203 } 204 205 private final void recordNamingConvention(Properties p) { 206 String syntaxDirectionStr = 207 p.getProperty("jndi.syntax.direction", "flat"); 208 if (syntaxDirectionStr.equals("left_to_right")) { 209 syntaxDirection = LEFT_TO_RIGHT; 210 } else if (syntaxDirectionStr.equals("right_to_left")) { 211 syntaxDirection = RIGHT_TO_LEFT; 212 } else if (syntaxDirectionStr.equals("flat")) { 213 syntaxDirection = FLAT; 214 } else { 215 throw new IllegalArgumentException (syntaxDirectionStr + 216 "is not a valid value for the jndi.syntax.direction property"); 217 } 218 219 if (syntaxDirection != FLAT) { 220 syntaxSeparator = p.getProperty("jndi.syntax.separator"); 221 syntaxSeparator2 = p.getProperty("jndi.syntax.separator2"); 222 if (syntaxSeparator == null) { 223 throw new IllegalArgumentException ( 224 "jndi.syntax.separator property required for non-flat syntax"); 225 } 226 } else { 227 syntaxSeparator = null; 228 } 229 syntaxEscape = p.getProperty("jndi.syntax.escape"); 230 231 syntaxCaseInsensitive = getBoolean(p, "jndi.syntax.ignorecase"); 232 syntaxTrimBlanks = getBoolean(p, "jndi.syntax.trimblanks"); 233 234 syntaxBeginQuote1 = p.getProperty("jndi.syntax.beginquote"); 235 syntaxEndQuote1 = p.getProperty("jndi.syntax.endquote"); 236 if (syntaxEndQuote1 == null && syntaxBeginQuote1 != null) 237 syntaxEndQuote1 = syntaxBeginQuote1; 238 else if (syntaxBeginQuote1 == null && syntaxEndQuote1 != null) 239 syntaxBeginQuote1 = syntaxEndQuote1; 240 syntaxBeginQuote2 = p.getProperty("jndi.syntax.beginquote2"); 241 syntaxEndQuote2 = p.getProperty("jndi.syntax.endquote2"); 242 if (syntaxEndQuote2 == null && syntaxBeginQuote2 != null) 243 syntaxEndQuote2 = syntaxBeginQuote2; 244 else if (syntaxBeginQuote2 == null && syntaxEndQuote2 != null) 245 syntaxBeginQuote2 = syntaxEndQuote2; 246 247 syntaxAvaSeparator = p.getProperty("jndi.syntax.separator.ava"); 248 syntaxTypevalSeparator = 249 p.getProperty("jndi.syntax.separator.typeval"); 250 } 251 252 NameImpl(Properties syntax) { 253 if (syntax != null) { 254 recordNamingConvention(syntax); 255 } 256 components = new Vector (); 257 } 258 259 NameImpl(Properties syntax, String n) throws InvalidNameException { 260 this(syntax); 261 262 boolean rToL = (syntaxDirection == RIGHT_TO_LEFT); 263 boolean compsAllEmpty = true; 264 int len = n.length(); 265 266 for (int i = 0; i < len; ) { 267 i = extractComp(n, i, len, components); 268 269 String comp = rToL 270 ? (String )components.firstElement() 271 : (String )components.lastElement(); 272 if (comp.length() >= 1) { 273 compsAllEmpty = false; 274 } 275 276 if (i < len) { 277 i = skipSeparator(n, i); 278 if ((i == len) && !compsAllEmpty) { 279 if (rToL) { 281 components.insertElementAt("", 0); 282 } else { 283 components.addElement(""); 284 } 285 } 286 } 287 } 288 } 289 290 NameImpl(Properties syntax, Enumeration comps) { 291 this(syntax); 292 293 while (comps.hasMoreElements()) 295 components.addElement(comps.nextElement()); 296 } 297 321 private final String stringifyComp(String comp) { 322 int len = comp.length(); 323 boolean escapeSeparator = false, escapeSeparator2 = false; 324 String beginQuote = null, endQuote = null; 325 StringBuffer strbuf = new StringBuffer (len); 326 327 if (syntaxSeparator != null && 330 comp.indexOf(syntaxSeparator) >= 0) { 331 if (syntaxBeginQuote1 != null) { 332 beginQuote = syntaxBeginQuote1; 333 endQuote = syntaxEndQuote1; 334 } else if (syntaxBeginQuote2 != null) { 335 beginQuote = syntaxBeginQuote2; 336 endQuote = syntaxEndQuote2; 337 } else if (syntaxEscape != null) 338 escapeSeparator = true; 339 } 340 if (syntaxSeparator2 != null && 341 comp.indexOf(syntaxSeparator2) >= 0) { 342 if (syntaxBeginQuote1 != null) { 343 if (beginQuote == null) { 344 beginQuote = syntaxBeginQuote1; 345 endQuote = syntaxEndQuote1; 346 } 347 } else if (syntaxBeginQuote2 != null) { 348 if (beginQuote == null) { 349 beginQuote = syntaxBeginQuote2; 350 endQuote = syntaxEndQuote2; 351 } 352 } else if (syntaxEscape != null) 353 escapeSeparator2 = true; 354 } 355 356 if (beginQuote != null) { 358 359 strbuf = strbuf.append(beginQuote); 361 362 for (int i = 0; i < len; ) { 365 if (comp.startsWith(endQuote, i)) { 366 strbuf.append(syntaxEscape).append(endQuote); 368 i += endQuote.length(); 369 } else { 370 strbuf.append(comp.charAt(i++)); 372 } 373 } 374 375 strbuf.append(endQuote); 377 378 } else { 379 380 386 boolean start = true; 388 for (int i = 0; i < len; ) { 389 if (start && isA(comp, i, syntaxBeginQuote1)) { 391 strbuf.append(syntaxEscape).append(syntaxBeginQuote1); 392 i += syntaxBeginQuote1.length(); 393 } else if (start && isA(comp, i, syntaxBeginQuote2)) { 394 strbuf.append(syntaxEscape).append(syntaxBeginQuote2); 395 i += syntaxBeginQuote2.length(); 396 } else 397 398 if (isA(comp, i, syntaxEscape)) { 401 if (i + syntaxEscape.length() >= len) { 402 strbuf.append(syntaxEscape); 404 } else if (isMeta(comp, i + syntaxEscape.length())) { 405 strbuf.append(syntaxEscape); 407 } 408 strbuf.append(syntaxEscape); 409 i += syntaxEscape.length(); 410 } else 411 412 if (escapeSeparator && comp.startsWith(syntaxSeparator, i)) { 414 strbuf.append(syntaxEscape).append(syntaxSeparator); 416 i += syntaxSeparator.length(); 417 } else if (escapeSeparator2 && 418 comp.startsWith(syntaxSeparator2, i)) { 419 strbuf.append(syntaxEscape).append(syntaxSeparator2); 421 i += syntaxSeparator2.length(); 422 } else { 423 strbuf.append(comp.charAt(i++)); 425 } 426 start = false; 427 } 428 } 429 return (strbuf.toString()); 430 } 431 432 public String toString() { 433 StringBuffer answer = new StringBuffer (); 434 String comp; 435 boolean compsAllEmpty = true; 436 int size = components.size(); 437 438 for (int i = 0; i < size; i++) { 439 if (syntaxDirection == RIGHT_TO_LEFT) { 440 comp = 441 stringifyComp((String ) components.elementAt(size - 1 - i)); 442 } else { 443 comp = stringifyComp((String ) components.elementAt(i)); 444 } 445 if ((i != 0) && (syntaxSeparator != null)) 446 answer.append(syntaxSeparator); 447 if (comp.length() >= 1) 448 compsAllEmpty = false; 449 answer = answer.append(comp); 450 } 451 if (compsAllEmpty && (size >= 1) && (syntaxSeparator != null)) 452 answer = answer.append(syntaxSeparator); 453 return (answer.toString()); 454 } 455 456 public boolean equals(Object obj) { 457 if ((obj != null) && (obj instanceof NameImpl )) { 458 NameImpl target = (NameImpl )obj; 459 if (target.size() == this.size()) { 460 Enumeration mycomps = getAll(); 461 Enumeration comps = target.getAll(); 462 while (mycomps.hasMoreElements()) { 463 String my = (String )mycomps.nextElement(); 465 String his = (String )comps.nextElement(); 466 if (syntaxTrimBlanks) { 467 my = my.trim(); 468 his = his.trim(); 469 } 470 if (syntaxCaseInsensitive) { 471 if (!(my.equalsIgnoreCase(his))) 472 return false; 473 } else { 474 if (!(my.equals(his))) 475 return false; 476 } 477 } 478 return true; 479 } 480 } 481 return false; 482 } 483 484 492 public int compareTo(NameImpl obj) { 493 if (this == obj) { 494 return 0; 495 } 496 497 int len1 = size(); 498 int len2 = obj.size(); 499 int n = Math.min(len1, len2); 500 501 int index1 = 0, index2 = 0; 502 503 while (n-- != 0) { 504 String comp1 = get(index1++); 505 String comp2 = obj.get(index2++); 506 507 if (syntaxTrimBlanks) { 509 comp1 = comp1.trim(); 510 comp2 = comp2.trim(); 511 } 512 if (syntaxCaseInsensitive) { 513 comp1 = comp1.toLowerCase(); 514 comp2 = comp2.toLowerCase(); 515 } 516 int local = comp1.compareTo(comp2); 517 if (local != 0) { 518 return local; 519 } 520 } 521 522 return len1 - len2; 523 } 524 525 public int size() { 526 return (components.size()); 527 } 528 529 public Enumeration getAll() { 530 return components.elements(); 531 } 532 533 public String get(int posn) { 534 return ((String ) components.elementAt(posn)); 535 } 536 537 public Enumeration getPrefix(int posn) { 538 if (posn < 0 || posn > size()) { 539 throw new ArrayIndexOutOfBoundsException (posn); 540 } 541 return new NameImplEnumerator(components, 0, posn); 542 } 543 544 public Enumeration getSuffix(int posn) { 545 int cnt = size(); 546 if (posn < 0 || posn > cnt) { 547 throw new ArrayIndexOutOfBoundsException (posn); 548 } 549 return new NameImplEnumerator(components, posn, cnt); 550 } 551 552 public boolean isEmpty() { 553 return (components.isEmpty()); 554 } 555 556 public boolean startsWith(int posn, Enumeration prefix) { 557 if (posn < 0 || posn > size()) { 558 return false; 559 } 560 try { 561 Enumeration mycomps = getPrefix(posn); 562 while (mycomps.hasMoreElements()) { 563 String my = (String )mycomps.nextElement(); 564 String his = (String )prefix.nextElement(); 565 if (syntaxTrimBlanks) { 566 my = my.trim(); 567 his = his.trim(); 568 } 569 if (syntaxCaseInsensitive) { 570 if (!(my.equalsIgnoreCase(his))) 571 return false; 572 } else { 573 if (!(my.equals(his))) 574 return false; 575 } 576 } 577 } catch (NoSuchElementException e) { 578 return false; 579 } 580 return true; 581 } 582 583 public boolean endsWith(int posn, Enumeration suffix) { 584 int startIndex = size() - posn; 589 if (startIndex < 0 || startIndex > size()) { 590 return false; 591 } 592 try { 593 Enumeration mycomps = getSuffix(startIndex); 594 while (mycomps.hasMoreElements()) { 595 String my = (String )mycomps.nextElement(); 596 String his = (String )suffix.nextElement(); 597 if (syntaxTrimBlanks) { 598 my = my.trim(); 599 his = his.trim(); 600 } 601 if (syntaxCaseInsensitive) { 602 if (!(my.equalsIgnoreCase(his))) 603 return false; 604 } else { 605 if (!(my.equals(his))) 606 return false; 607 } 608 } 609 } catch (NoSuchElementException e) { 610 return false; 611 } 612 return true; 613 } 614 615 public boolean addAll(Enumeration comps) throws InvalidNameException { 616 boolean added = false; 617 while (comps.hasMoreElements()) { 618 try { 619 Object comp = comps.nextElement(); 620 if (size() > 0 && syntaxDirection == FLAT) { 621 throw new InvalidNameException ( 622 "A flat name can only have a single component"); 623 } 624 components.addElement(comp); 625 added = true; 626 } catch (NoSuchElementException e) { 627 break; } 629 } 630 return added; 631 } 632 633 public boolean addAll(int posn, Enumeration comps) 634 throws InvalidNameException { 635 boolean added = false; 636 for (int i = posn; comps.hasMoreElements(); i++) { 637 try { 638 Object comp = comps.nextElement(); 639 if (size() > 0 && syntaxDirection == FLAT) { 640 throw new InvalidNameException ( 641 "A flat name can only have a single component"); 642 } 643 components.insertElementAt(comp, i); 644 added = true; 645 } catch (NoSuchElementException e) { 646 break; } 648 } 649 return added; 650 } 651 652 public void add(String comp) throws InvalidNameException { 653 if (size() > 0 && syntaxDirection == FLAT) { 654 throw new InvalidNameException ( 655 "A flat name can only have a single component"); 656 } 657 components.addElement(comp); 658 } 659 660 public void add(int posn, String comp) throws InvalidNameException { 661 if (size() > 0 && syntaxDirection == FLAT) { 662 throw new InvalidNameException ( 663 "A flat name can only zero or one component"); 664 } 665 components.insertElementAt(comp, posn); 666 } 667 668 public Object remove(int posn) { 669 Object r = components.elementAt(posn); 670 components.removeElementAt(posn); 671 return r; 672 } 673 674 public int hashCode() { 675 int hash = 0; 676 for (Enumeration e = getAll(); e.hasMoreElements();) { 677 String comp = (String )e.nextElement(); 678 if (syntaxTrimBlanks) { 679 comp = comp.trim(); 680 } 681 if (syntaxCaseInsensitive) { 682 comp = comp.toLowerCase(); 683 } 684 685 hash += comp.hashCode(); 686 } 687 return hash; 688 } 689 } 690 691 final 692 class NameImplEnumerator implements Enumeration { 693 Vector vector; 694 int count; 695 int limit; 696 697 NameImplEnumerator(Vector v, int start, int lim) { 698 vector = v; 699 count = start; 700 limit = lim; 701 } 702 703 public boolean hasMoreElements() { 704 return count < limit; 705 } 706 707 public Object nextElement() { 708 if (count < limit) { 709 return vector.elementAt(count++); 710 } 711 throw new NoSuchElementException ("NameImplEnumerator"); 712 } 713 } 714 715 | Popular Tags |