1 7 8 20 21 package java.text; 22 23 import java.lang.Character ; 24 import java.util.Vector ; 25 import sun.text.Normalizer; 26 import sun.text.NormalizerUtilities; 27 28 83 public final class CollationElementIterator 84 { 85 89 public final static int NULLORDER = 0xffffffff; 90 91 99 CollationElementIterator(String sourceText, RuleBasedCollator owner) { 100 this.owner = owner; 101 ordering = owner.getTables(); 102 if ( sourceText.length() != 0 ) { 103 Normalizer.Mode mode = 104 NormalizerUtilities.toNormalizerMode(owner.getDecomposition()); 105 text = new Normalizer (sourceText, mode); 106 } 107 } 108 109 117 CollationElementIterator(CharacterIterator sourceText, RuleBasedCollator owner) { 118 this.owner = owner; 119 ordering = owner.getTables(); 120 Normalizer.Mode mode = 121 NormalizerUtilities.toNormalizerMode(owner.getDecomposition()); 122 text = new Normalizer (sourceText, mode); 123 } 124 128 public void reset() 129 { 130 if (text != null) { 131 text.reset(); 132 Normalizer.Mode mode = 133 NormalizerUtilities.toNormalizerMode(owner.getDecomposition()); 134 text.setMode(mode); 135 } 136 buffer = null; 137 expIndex = 0; 138 swapOrder = 0; 139 } 140 141 155 public int next() 156 { 157 if (text == null) { 158 return NULLORDER; 159 } 160 Normalizer.Mode textMode = text.getMode(); 161 Normalizer.Mode ownerMode = 163 NormalizerUtilities.toNormalizerMode(owner.getDecomposition()); 164 if (textMode != ownerMode) { 165 text.setMode(ownerMode); 166 } 167 168 if (buffer != null) { 172 if (expIndex < buffer.length) { 173 return strengthOrder(buffer[expIndex++]); 174 } else { 175 buffer = null; 176 expIndex = 0; 177 } 178 } else if (swapOrder != 0) { 179 if (Character.isSupplementaryCodePoint(swapOrder)) { 180 char[] chars = Character.toChars(swapOrder); 181 swapOrder = chars[1]; 182 return chars[0] << 16; 183 } 184 int order = swapOrder << 16; 185 swapOrder = 0; 186 return order; 187 } 188 int ch = text.next(); 189 190 if (ch == Normalizer.DONE) { 192 return NULLORDER; 193 } 194 195 int value = ordering.getUnicodeOrder(ch); 196 if (value == RuleBasedCollator.UNMAPPED) { 197 swapOrder = ch; 198 return UNMAPPEDCHARVALUE; 199 } 200 else if (value >= RuleBasedCollator.CONTRACTCHARINDEX) { 201 value = nextContractChar(ch); 202 } 203 if (value >= RuleBasedCollator.EXPANDCHARINDEX) { 204 buffer = ordering.getExpandValueList(value); 205 expIndex = 0; 206 value = buffer[expIndex++]; 207 } 208 209 if (ordering.isSEAsianSwapping()) { 210 int consonant; 211 if (isThaiPreVowel(ch)) { 212 consonant = text.next(); 213 if (isThaiBaseConsonant(consonant)) { 214 buffer = makeReorderedBuffer(consonant, value, buffer, true); 215 value = buffer[0]; 216 expIndex = 1; 217 } else { 218 text.previous(); 219 } 220 } 221 if (isLaoPreVowel(ch)) { 222 consonant = text.next(); 223 if (isLaoBaseConsonant(consonant)) { 224 buffer = makeReorderedBuffer(consonant, value, buffer, true); 225 value = buffer[0]; 226 expIndex = 1; 227 } else { 228 text.previous(); 229 } 230 } 231 } 232 233 return strengthOrder(value); 234 } 235 236 251 public int previous() 252 { 253 if (text == null) { 254 return NULLORDER; 255 } 256 Normalizer.Mode textMode = text.getMode(); 257 Normalizer.Mode ownerMode = 259 NormalizerUtilities.toNormalizerMode(owner.getDecomposition()); 260 if (textMode != ownerMode) { 261 text.setMode(ownerMode); 262 } 263 if (buffer != null) { 264 if (expIndex > 0) { 265 return strengthOrder(buffer[--expIndex]); 266 } else { 267 buffer = null; 268 expIndex = 0; 269 } 270 } else if (swapOrder != 0) { 271 if (Character.isSupplementaryCodePoint(swapOrder)) { 272 char[] chars = Character.toChars(swapOrder); 273 swapOrder = chars[1]; 274 return chars[0] << 16; 275 } 276 int order = swapOrder << 16; 277 swapOrder = 0; 278 return order; 279 } 280 int ch = text.previous(); 281 if (ch == Normalizer.DONE) { 282 return NULLORDER; 283 } 284 285 int value = ordering.getUnicodeOrder(ch); 286 287 if (value == RuleBasedCollator.UNMAPPED) { 288 swapOrder = UNMAPPEDCHARVALUE; 289 return ch; 290 } else if (value >= RuleBasedCollator.CONTRACTCHARINDEX) { 291 value = prevContractChar(ch); 292 } 293 if (value >= RuleBasedCollator.EXPANDCHARINDEX) { 294 buffer = ordering.getExpandValueList(value); 295 expIndex = buffer.length; 296 value = buffer[--expIndex]; 297 } 298 299 if (ordering.isSEAsianSwapping()) { 300 int vowel; 301 if (isThaiBaseConsonant(ch)) { 302 vowel = text.previous(); 303 if (isThaiPreVowel(vowel)) { 304 buffer = makeReorderedBuffer(vowel, value, buffer, false); 305 expIndex = buffer.length - 1; 306 value = buffer[expIndex]; 307 } else { 308 text.next(); 309 } 310 } 311 if (isLaoBaseConsonant(ch)) { 312 vowel = text.previous(); 313 if (isLaoPreVowel(vowel)) { 314 buffer = makeReorderedBuffer(vowel, value, buffer, false); 315 expIndex = buffer.length - 1; 316 value = buffer[expIndex]; 317 } else { 318 text.next(); 319 } 320 } 321 } 322 323 return strengthOrder(value); 324 } 325 326 331 public final static int primaryOrder(int order) 332 { 333 order &= RBCollationTables.PRIMARYORDERMASK; 334 return (order >>> RBCollationTables.PRIMARYORDERSHIFT); 335 } 336 341 public final static short secondaryOrder(int order) 342 { 343 order = order & RBCollationTables.SECONDARYORDERMASK; 344 return ((short)(order >> RBCollationTables.SECONDARYORDERSHIFT)); 345 } 346 351 public final static short tertiaryOrder(int order) 352 { 353 return ((short)(order &= RBCollationTables.TERTIARYORDERMASK)); 354 } 355 356 361 final int strengthOrder(int order) 362 { 363 int s = owner.getStrength(); 364 if (s == Collator.PRIMARY) 365 { 366 order &= RBCollationTables.PRIMARYDIFFERENCEONLY; 367 } else if (s == Collator.SECONDARY) 368 { 369 order &= RBCollationTables.SECONDARYDIFFERENCEONLY; 370 } 371 return order; 372 } 373 374 389 public void setOffset(int newOffset) 390 { 391 if (text != null) { 392 if (newOffset < text.getBeginIndex() 393 || newOffset >= text.getEndIndex()) { 394 text.setIndexOnly(newOffset); 395 } else { 396 int c = text.setIndex(newOffset); 397 398 if (ordering.usedInContractSeq(c)) { 402 while (ordering.usedInContractSeq(c)) { 405 c = text.previous(); 406 } 407 int last = text.getIndex(); 412 while (text.getIndex() <= newOffset) { 413 last = text.getIndex(); 414 next(); 415 } 416 text.setIndexOnly(last); 417 } 422 } 423 } 424 buffer = null; 425 expIndex = 0; 426 swapOrder = 0; 427 } 428 429 443 public int getOffset() 444 { 445 return (text != null) ? text.getIndex() : 0; 446 } 447 448 449 457 public int getMaxExpansion(int order) 458 { 459 return ordering.getMaxExpansion(order); 460 } 461 462 468 public void setText(String source) 469 { 470 buffer = null; 471 swapOrder = 0; 472 expIndex = 0; 473 Normalizer.Mode mode = 474 NormalizerUtilities.toNormalizerMode(owner.getDecomposition()); 475 if (text == null) { 476 text = new Normalizer (source, mode); 477 } else { 478 text.setMode(mode); 479 text.setText(source); 480 } 481 } 482 483 489 public void setText(CharacterIterator source) 490 { 491 buffer = null; 492 swapOrder = 0; 493 expIndex = 0; 494 Normalizer.Mode mode = 495 NormalizerUtilities.toNormalizerMode(owner.getDecomposition()); 496 if (text == null) { 497 text = new Normalizer (source, mode); 498 } else { 499 text.setMode(mode); 500 text.setText(source); 501 } 502 } 503 504 508 512 private final static boolean isThaiPreVowel(int ch) { 513 return (ch >= 0x0e40) && (ch <= 0x0e44); 514 } 515 516 519 private final static boolean isThaiBaseConsonant(int ch) { 520 return (ch >= 0x0e01) && (ch <= 0x0e2e); 521 } 522 523 527 private final static boolean isLaoPreVowel(int ch) { 528 return (ch >= 0x0ec0) && (ch <= 0x0ec4); 529 } 530 531 534 private final static boolean isLaoBaseConsonant(int ch) { 535 return (ch >= 0x0e81) && (ch <= 0x0eae); 536 } 537 538 548 private int[] makeReorderedBuffer(int colFirst, 549 int lastValue, 550 int[] lastExpansion, 551 boolean forward) { 552 553 int[] result; 554 555 int firstValue = ordering.getUnicodeOrder(colFirst); 556 if (firstValue >= RuleBasedCollator.CONTRACTCHARINDEX) { 557 firstValue = forward? nextContractChar(colFirst) : prevContractChar(colFirst); 558 } 559 560 int[] firstExpansion = null; 561 if (firstValue >= RuleBasedCollator.EXPANDCHARINDEX) { 562 firstExpansion = ordering.getExpandValueList(firstValue); 563 } 564 565 if (!forward) { 566 int temp1 = firstValue; 567 firstValue = lastValue; 568 lastValue = temp1; 569 int[] temp2 = firstExpansion; 570 firstExpansion = lastExpansion; 571 lastExpansion = temp2; 572 } 573 574 if (firstExpansion == null && lastExpansion == null) { 575 result = new int [2]; 576 result[0] = firstValue; 577 result[1] = lastValue; 578 } 579 else { 580 int firstLength = firstExpansion==null? 1 : firstExpansion.length; 581 int lastLength = lastExpansion==null? 1 : lastExpansion.length; 582 result = new int[firstLength + lastLength]; 583 584 if (firstExpansion == null) { 585 result[0] = firstValue; 586 } 587 else { 588 System.arraycopy(firstExpansion, 0, result, 0, firstLength); 589 } 590 591 if (lastExpansion == null) { 592 result[firstLength] = lastValue; 593 } 594 else { 595 System.arraycopy(lastExpansion, 0, result, firstLength, lastLength); 596 } 597 } 598 599 return result; 600 } 601 602 606 final static boolean isIgnorable(int order) 607 { 608 return ((primaryOrder(order) == 0) ? true : false); 609 } 610 611 618 private int nextContractChar(int ch) 619 { 620 Vector list = ordering.getContractValues(ch); 623 EntryPair pair = (EntryPair )list.firstElement(); 624 int order = pair.value; 625 626 pair = (EntryPair )list.lastElement(); 630 int maxLength = pair.entryName.length(); 631 632 Normalizer tempText = (Normalizer )text.clone(); 635 636 tempText.previous(); 640 key.setLength(0); 641 int c = tempText.next(); 642 while (maxLength > 0 && c != Normalizer.DONE) { 643 if (Character.isSupplementaryCodePoint(c)) { 644 key.append(Character.toChars(c)); 645 maxLength -= 2; 646 } else { 647 key.append((char)c); 648 --maxLength; 649 } 650 c = tempText.next(); 651 } 652 String fragment = key.toString(); 653 maxLength = 1; 660 for (int i = list.size() - 1; i > 0; i--) { 661 pair = (EntryPair )list.elementAt(i); 662 if (!pair.fwd) 663 continue; 664 665 if (fragment.startsWith(pair.entryName) && pair.entryName.length() 666 > maxLength) { 667 maxLength = pair.entryName.length(); 668 order = pair.value; 669 } 670 } 671 672 while (maxLength > 1) { 677 c = text.next(); 678 maxLength -= Character.charCount(c); 679 } 680 return order; 681 } 682 683 690 private int prevContractChar(int ch) 691 { 692 Vector list = ordering.getContractValues(ch); 699 EntryPair pair = (EntryPair )list.firstElement(); 700 int order = pair.value; 701 702 pair = (EntryPair )list.lastElement(); 703 int maxLength = pair.entryName.length(); 704 705 Normalizer tempText = (Normalizer )text.clone(); 706 707 tempText.next(); 708 key.setLength(0); 709 int c = tempText.previous(); 710 while (maxLength > 0 && c != Normalizer.DONE) { 711 if (Character.isSupplementaryCodePoint(c)) { 712 key.append(Character.toChars(c)); 713 maxLength -= 2; 714 } else { 715 key.append((char)c); 716 --maxLength; 717 } 718 c = tempText.previous(); 719 } 720 String fragment = key.toString(); 721 722 maxLength = 1; 723 for (int i = list.size() - 1; i > 0; i--) { 724 pair = (EntryPair )list.elementAt(i); 725 if (pair.fwd) 726 continue; 727 728 if (fragment.startsWith(pair.entryName) && pair.entryName.length() 729 > maxLength) { 730 maxLength = pair.entryName.length(); 731 order = pair.value; 732 } 733 } 734 735 while (maxLength > 1) { 736 c = text.previous(); 737 maxLength -= Character.charCount(c); 738 } 739 return order; 740 } 741 742 final static int UNMAPPEDCHARVALUE = 0x7FFF0000; 743 744 private Normalizer text = null; 745 private int[] buffer = null; 746 private int expIndex = 0; 747 private StringBuffer key = new StringBuffer (5); 748 private int swapOrder = 0; 749 private RBCollationTables ordering; 750 private RuleBasedCollator owner; 751 } 752 | Popular Tags |