1 7 8 22 23 package java.text; 24 25 import java.util.Vector ; 26 import java.util.Locale ; 27 import java.util.ResourceBundle ; 28 import java.util.MissingResourceException ; 29 import sun.text.resources.LocaleData; 30 import java.text.CharacterIterator ; 31 import java.text.StringCharacterIterator ; 32 33 import java.net.URL ; 34 import java.io.InputStream ; 35 import java.io.IOException ; 36 37 import java.lang.ref.SoftReference ; 38 import java.security.AccessController ; 39 import java.security.PrivilegedAction ; 40 41 212 213 public abstract class BreakIterator implements Cloneable 214 { 215 218 protected BreakIterator() 219 { 220 } 221 222 226 public Object clone() 227 { 228 try { 229 return super.clone(); 230 } 231 catch (CloneNotSupportedException e) { 232 throw new InternalError (); 233 } 234 } 235 236 240 public static final int DONE = -1; 241 242 247 public abstract int first(); 248 249 254 public abstract int last(); 255 256 263 public abstract int next(int n); 264 265 270 public abstract int next(); 271 272 277 public abstract int previous(); 278 279 289 public abstract int following(int offset); 290 291 301 public int preceding(int offset) { 302 int pos = following(offset); 306 while (pos >= offset && pos != DONE) 307 pos = previous(); 308 return pos; 309 } 310 311 317 public boolean isBoundary(int offset) { 318 if (offset == 0) 327 return true; 328 else 329 return following(offset - 1) == offset; 330 } 331 332 337 public abstract int current(); 338 339 343 public abstract CharacterIterator getText(); 344 345 350 public void setText(String newText) 351 { 352 setText(new StringCharacterIterator (newText)); 353 } 354 355 360 public abstract void setText(CharacterIterator newText); 361 362 private static final int CHARACTER_INDEX = 0; 363 private static final int WORD_INDEX = 1; 364 private static final int LINE_INDEX = 2; 365 private static final int SENTENCE_INDEX = 3; 366 private static final SoftReference [] iterCache = new SoftReference [4]; 367 368 375 public static BreakIterator getWordInstance() 376 { 377 return getWordInstance(Locale.getDefault()); 378 } 379 380 388 public static BreakIterator getWordInstance(Locale where) 389 { 390 return getBreakInstance(where, 391 WORD_INDEX, 392 "WordData", 393 "WordDictionary"); 394 } 395 396 405 public static BreakIterator getLineInstance() 406 { 407 return getLineInstance(Locale.getDefault()); 408 } 409 410 420 public static BreakIterator getLineInstance(Locale where) 421 { 422 return getBreakInstance(where, 423 LINE_INDEX, 424 "LineData", 425 "LineDictionary"); 426 } 427 428 435 public static BreakIterator getCharacterInstance() 436 { 437 return getCharacterInstance(Locale.getDefault()); 438 } 439 440 448 public static BreakIterator getCharacterInstance(Locale where) 449 { 450 return getBreakInstance(where, 451 CHARACTER_INDEX, 452 "CharacterData", 453 "CharacterDictionary"); 454 } 455 456 462 public static BreakIterator getSentenceInstance() 463 { 464 return getSentenceInstance(Locale.getDefault()); 465 } 466 467 474 public static BreakIterator getSentenceInstance(Locale where) 475 { 476 return getBreakInstance(where, 477 SENTENCE_INDEX, 478 "SentenceData", 479 "SentenceDictionary"); 480 } 481 482 private static BreakIterator getBreakInstance(Locale where, 483 int type, 484 String dataName, 485 String dictionaryName) { 486 if (iterCache[type] != null) { 487 BreakIteratorCache cache = (BreakIteratorCache) iterCache[type].get(); 488 if (cache != null) { 489 if (cache.getLocale().equals(where)) { 490 return cache.createBreakInstance(); 491 } 492 } 493 } 494 495 BreakIterator result = createBreakInstance(where, 496 type, 497 dataName, 498 dictionaryName); 499 BreakIteratorCache cache = new BreakIteratorCache(where, result); 500 iterCache[type] = new SoftReference (cache); 501 return result; 502 } 503 504 private static ResourceBundle getBundle(final String baseName, final Locale locale) { 505 return (ResourceBundle ) AccessController.doPrivileged(new PrivilegedAction () { 506 public Object run() { 507 return ResourceBundle.getBundle(baseName, locale); 508 } 509 }); 510 } 511 512 private static BreakIterator createBreakInstance(Locale where, 513 int type, 514 String dataName, 515 String dictionaryName) { 516 517 ResourceBundle bundle = getBundle( 518 "sun.text.resources.BreakIteratorInfo", where); 519 String [] classNames = bundle.getStringArray("BreakIteratorClasses"); 520 521 String dataFile = bundle.getString(dataName); 522 523 try { 524 if (classNames[type].equals("RuleBasedBreakIterator")) { 525 return new RuleBasedBreakIterator (dataFile); 526 } 527 else if (classNames[type].equals("DictionaryBasedBreakIterator")) { 528 String dictionaryFile = bundle.getString(dictionaryName); 529 return new DictionaryBasedBreakIterator (dataFile, dictionaryFile); 530 } 531 else { 532 throw new IllegalArgumentException ("Invalid break iterator class \"" + 533 classNames[type] + "\""); 534 } 535 } 536 catch (Exception e) { 537 throw new InternalError (e.toString()); 538 } 539 } 540 541 551 public static synchronized Locale [] getAvailableLocales() 552 { 553 return LocaleData.getAvailableLocales("NumberPatterns"); 556 } 557 558 private static final class BreakIteratorCache { 559 560 private BreakIterator iter; 561 private Locale where; 562 563 BreakIteratorCache(Locale where, BreakIterator iter) { 564 this.where = where; 565 this.iter = (BreakIterator ) iter.clone(); 566 } 567 568 Locale getLocale() { 569 return where; 570 } 571 572 BreakIterator createBreakInstance() { 573 return (BreakIterator ) iter.clone(); 574 } 575 } 576 577 protected static long getLong(byte[] buf, int offset) { 578 long num = buf[offset]&0xFF; 579 for (int i = 1; i < 8; i++) { 580 num = num<<8 | (buf[offset+i]&0xFF); 581 } 582 return num; 583 } 584 585 protected static int getInt(byte[] buf, int offset) { 586 int num = buf[offset]&0xFF; 587 for (int i = 1; i < 4; i++) { 588 num = num<<8 | (buf[offset+i]&0xFF); 589 } 590 return num; 591 } 592 593 protected static short getShort(byte[] buf, int offset) { 594 short num = (short)(buf[offset]&0xFF); 595 num = (short)(num<<8 | (buf[offset+1]&0xFF)); 596 return num; 597 } 598 } 599 | Popular Tags |