1 7 8 20 21 package java.text; 22 23 import java.util.Vector ; 24 import java.util.Locale ; 25 import sun.text.Normalizer; 26 import sun.text.NormalizerUtilities; 27 28 275 public class RuleBasedCollator extends Collator { 276 298 309 public RuleBasedCollator(String rules) throws ParseException { 310 this(rules, Collator.CANONICAL_DECOMPOSITION); 311 } 312 313 326 RuleBasedCollator(String rules, int decomp) throws ParseException { 327 setStrength(Collator.TERTIARY); 328 setDecomposition(decomp); 329 tables = new RBCollationTables (rules, decomp); 330 } 331 332 335 private RuleBasedCollator(RuleBasedCollator that) { 336 setStrength(that.getStrength()); 337 setDecomposition(that.getDecomposition()); 338 tables = that.tables; 339 } 340 341 346 public String getRules() 347 { 348 return tables.getRules(); 349 } 350 351 355 public CollationElementIterator getCollationElementIterator(String source) { 356 return new CollationElementIterator ( source, this ); 357 } 358 359 364 public CollationElementIterator getCollationElementIterator( 365 CharacterIterator source) { 366 return new CollationElementIterator ( source, this ); 367 } 368 369 375 public synchronized int compare(String source, String target) 376 { 377 396 int result = Collator.EQUAL; 397 398 if (sourceCursor == null) { 399 sourceCursor = getCollationElementIterator(source); 400 } else { 401 sourceCursor.setText(source); 402 } 403 if (targetCursor == null) { 404 targetCursor = getCollationElementIterator(target); 405 } else { 406 targetCursor.setText(target); 407 } 408 409 int sOrder = 0, tOrder = 0; 410 411 boolean initialCheckSecTer = getStrength() >= Collator.SECONDARY; 412 boolean checkSecTer = initialCheckSecTer; 413 boolean checkTertiary = getStrength() >= Collator.TERTIARY; 414 415 boolean gets = true, gett = true; 416 417 while(true) { 418 if (gets) sOrder = sourceCursor.next(); else gets = true; 421 if (gett) tOrder = targetCursor.next(); else gett = true; 422 423 if ((sOrder == CollationElementIterator.NULLORDER)|| 425 (tOrder == CollationElementIterator.NULLORDER)) 426 break; 427 428 int pSOrder = CollationElementIterator.primaryOrder(sOrder); 429 int pTOrder = CollationElementIterator.primaryOrder(tOrder); 430 431 if (sOrder == tOrder) { 433 if (tables.isFrenchSec() && pSOrder != 0) { 434 if (!checkSecTer) { 435 checkSecTer = initialCheckSecTer; 438 checkTertiary = false; 441 } 442 } 443 continue; 444 } 445 446 if ( pSOrder != pTOrder ) 448 { 449 if (sOrder == 0) { 450 gett = false; 453 continue; 454 } 455 if (tOrder == 0) { 456 gets = false; 457 continue; 458 } 459 460 463 if (pSOrder == 0) { 465 if (checkSecTer) { 468 result = Collator.GREATER; checkSecTer = false; 470 } 471 gett = false; 473 } 474 else if (pTOrder == 0) 475 { 476 if (checkSecTer) { 478 result = Collator.LESS; checkSecTer = false; 480 } 481 gets = false; 483 } else { 484 if (pSOrder < pTOrder) { 488 return Collator.LESS; } else { 490 return Collator.GREATER; } 492 } 493 } else { 498 if (checkSecTer) { 499 short secSOrder = CollationElementIterator.secondaryOrder(sOrder); 501 short secTOrder = CollationElementIterator.secondaryOrder(tOrder); 502 if (secSOrder != secTOrder) { 503 result = (secSOrder < secTOrder) ? Collator.LESS : Collator.GREATER; 505 checkSecTer = false; 507 } else { 510 if (checkTertiary) { 511 short terSOrder = CollationElementIterator.tertiaryOrder(sOrder); 513 short terTOrder = CollationElementIterator.tertiaryOrder(tOrder); 514 if (terSOrder != terTOrder) { 515 result = (terSOrder < terTOrder) ? Collator.LESS : Collator.GREATER; 517 checkTertiary = false; 519 } 520 } 521 } 522 } 524 } } 527 if (sOrder != CollationElementIterator.NULLORDER) { 528 do { 532 if (CollationElementIterator.primaryOrder(sOrder) != 0) { 533 return Collator.GREATER; } 537 else if (CollationElementIterator.secondaryOrder(sOrder) != 0) { 538 if (checkSecTer) { 540 result = Collator.GREATER; checkSecTer = false; 542 } 543 } 544 } while ((sOrder = sourceCursor.next()) != CollationElementIterator.NULLORDER); 545 } 546 else if (tOrder != CollationElementIterator.NULLORDER) { 547 do { 549 if (CollationElementIterator.primaryOrder(tOrder) != 0) 550 return Collator.LESS; else if (CollationElementIterator.secondaryOrder(tOrder) != 0) { 554 if (checkSecTer) { 556 result = Collator.LESS; checkSecTer = false; 558 } 559 } 560 } while ((tOrder = targetCursor.next()) != CollationElementIterator.NULLORDER); 561 } 562 563 if (result == 0 && getStrength() == IDENTICAL) { 566 Normalizer.Mode mode = NormalizerUtilities.toNormalizerMode(getDecomposition()); 567 String sourceDecomposition = Normalizer.normalize(source, mode, 0); 568 String targetDecomposition = Normalizer.normalize(target, mode, 0); 569 result = sourceDecomposition.compareTo(targetDecomposition); 570 } 571 return result; 572 } 573 574 579 public synchronized CollationKey getCollationKey(String source) 580 { 581 if (source == null) 612 return null; 613 614 if (primResult == null) { 615 primResult = new StringBuffer (); 616 secResult = new StringBuffer (); 617 terResult = new StringBuffer (); 618 } else { 619 primResult.setLength(0); 620 secResult.setLength(0); 621 terResult.setLength(0); 622 } 623 int order = 0; 624 boolean compareSec = (getStrength() >= Collator.SECONDARY); 625 boolean compareTer = (getStrength() >= Collator.TERTIARY); 626 int secOrder = CollationElementIterator.NULLORDER; 627 int terOrder = CollationElementIterator.NULLORDER; 628 int preSecIgnore = 0; 629 630 if (sourceCursor == null) { 631 sourceCursor = getCollationElementIterator(source); 632 } else { 633 sourceCursor.setText(source); 634 } 635 636 while ((order = sourceCursor.next()) != 638 CollationElementIterator.NULLORDER) 639 { 640 secOrder = CollationElementIterator.secondaryOrder(order); 641 terOrder = CollationElementIterator.tertiaryOrder(order); 642 if (!CollationElementIterator.isIgnorable(order)) 643 { 644 primResult.append((char) (CollationElementIterator.primaryOrder(order) 645 + COLLATIONKEYOFFSET)); 646 647 if (compareSec) { 648 if (tables.isFrenchSec() && preSecIgnore < secResult.length()) { 653 RBCollationTables.reverse(secResult, preSecIgnore, secResult.length()); 659 } 660 secResult.append((char)(secOrder+ COLLATIONKEYOFFSET)); 663 preSecIgnore = secResult.length(); 664 } 665 if (compareTer) { 666 terResult.append((char)(terOrder+ COLLATIONKEYOFFSET)); 667 } 668 } 669 else 670 { 671 if (compareSec && secOrder != 0) 672 secResult.append((char) 673 (secOrder + tables.getMaxSecOrder() + COLLATIONKEYOFFSET)); 674 if (compareTer && terOrder != 0) 675 terResult.append((char) 676 (terOrder + tables.getMaxTerOrder() + COLLATIONKEYOFFSET)); 677 } 678 } 679 if (tables.isFrenchSec()) 680 { 681 if (preSecIgnore < secResult.length()) { 682 RBCollationTables.reverse(secResult, preSecIgnore, secResult.length()); 685 } 686 RBCollationTables.reverse(secResult, 0, secResult.length()); 688 } 689 primResult.append((char)0); 690 secResult.append((char)0); 691 secResult.append(terResult.toString()); 692 primResult.append(secResult.toString()); 693 694 if (getStrength() == IDENTICAL) { 695 primResult.append((char)0); 696 Normalizer.Mode mode = NormalizerUtilities.toNormalizerMode(getDecomposition()); 697 primResult.append(Normalizer.normalize(source, mode, 0)); 698 } 699 return new CollationKey (source, primResult.toString()); 700 } 701 702 705 public Object clone() { 706 if (getClass() == RuleBasedCollator .class) { 710 return new RuleBasedCollator (this); 711 } 712 else { 713 RuleBasedCollator result = (RuleBasedCollator ) super.clone(); 714 result.primResult = null; 715 result.secResult = null; 716 result.terResult = null; 717 result.sourceCursor = null; 718 result.targetCursor = null; 719 return result; 720 } 721 } 722 723 729 public boolean equals(Object obj) { 730 if (obj == null) return false; 731 if (!super.equals(obj)) return false; RuleBasedCollator other = (RuleBasedCollator ) obj; 733 return (getRules().equals(other.getRules())); 735 } 736 737 740 public int hashCode() { 741 return getRules().hashCode(); 742 } 743 744 747 RBCollationTables getTables() { 748 return tables; 749 } 750 751 755 final static int CHARINDEX = 0x70000000; final static int EXPANDCHARINDEX = 0x7E000000; final static int CONTRACTCHARINDEX = 0x7F000000; final static int UNMAPPED = 0xFFFFFFFF; 759 760 private final static int COLLATIONKEYOFFSET = 1; 761 762 private RBCollationTables tables = null; 763 764 private StringBuffer primResult = null; 767 private StringBuffer secResult = null; 768 private StringBuffer terResult = null; 769 private CollationElementIterator sourceCursor = null; 770 private CollationElementIterator targetCursor = null; 771 } 772 773 | Popular Tags |