| 1 7 8 20 21 package java.text; 22 23 import java.io.InvalidObjectException ; 24 import java.io.IOException ; 25 import java.io.ObjectInputStream ; 26 import java.text.DecimalFormat ; 27 import java.util.ArrayList ; 28 import java.util.Date ; 29 import java.util.List ; 30 import java.util.Locale ; 31 import sun.text.Utility; 32 33 34 332 333 public class MessageFormat extends Format { 334 335 private static final long serialVersionUID = 6479157306784022952L; 336 337 348 public MessageFormat(String pattern) { 349 this.locale = Locale.getDefault(); 350 applyPattern(pattern); 351 } 352 353 366 public MessageFormat(String pattern, Locale locale) { 367 this.locale = locale; 368 applyPattern(pattern); 369 } 370 371 380 public void setLocale(Locale locale) { 381 this.locale = locale; 382 } 383 384 389 public Locale getLocale() { 390 return locale; 391 } 392 393 394 404 public void applyPattern(String pattern) { 405 StringBuffer [] segments = new StringBuffer [4]; 406 for (int i = 0; i < segments.length; ++i) { 407 segments[i] = new StringBuffer (); 408 } 409 int part = 0; 410 int formatNumber = 0; 411 boolean inQuote = false; 412 int braceStack = 0; 413 maxOffset = -1; 414 for (int i = 0; i < pattern.length(); ++i) { 415 char ch = pattern.charAt(i); 416 if (part == 0) { 417 if (ch == '\'') { 418 if (i + 1 < pattern.length() 419 && pattern.charAt(i+1) == '\'') { 420 segments[part].append(ch); ++i; 422 } else { 423 inQuote = !inQuote; 424 } 425 } else if (ch == '{' && !inQuote) { 426 part = 1; 427 } else { 428 segments[part].append(ch); 429 } 430 } else if (inQuote) { segments[part].append(ch); 432 if (ch == '\'') { 433 inQuote = false; 434 } 435 } else { 436 switch (ch) { 437 case ',': 438 if (part < 3) 439 part += 1; 440 else 441 segments[part].append(ch); 442 break; 443 case '{': 444 ++braceStack; 445 segments[part].append(ch); 446 break; 447 case '}': 448 if (braceStack == 0) { 449 part = 0; 450 makeFormat(i, formatNumber, segments); 451 formatNumber++; 452 } else { 453 --braceStack; 454 segments[part].append(ch); 455 } 456 break; 457 case '\'': 458 inQuote = true; 459 default: 461 segments[part].append(ch); 462 break; 463 } 464 } 465 } 466 if (braceStack == 0 && part != 0) { 467 maxOffset = -1; 468 throw new IllegalArgumentException ("Unmatched braces in the pattern."); 469 } 470 this.pattern = segments[0].toString(); 471 } 472 473 474 481 public String toPattern() { 482 int lastOffset = 0; 484 StringBuffer result = new StringBuffer (); 485 for (int i = 0; i <= maxOffset; ++i) { 486 copyAndFixQuotes(pattern, lastOffset, offsets[i],result); 487 lastOffset = offsets[i]; 488 result.append('{'); 489 result.append(argumentNumbers[i]); 490 if (formats[i] == null) { 491 } else if (formats[i] instanceof DecimalFormat ) { 493 if (formats[i].equals(NumberFormat.getInstance(locale))) { 494 result.append(",number"); 495 } else if (formats[i].equals(NumberFormat.getCurrencyInstance(locale))) { 496 result.append(",number,currency"); 497 } else if (formats[i].equals(NumberFormat.getPercentInstance(locale))) { 498 result.append(",number,percent"); 499 } else if (formats[i].equals(NumberFormat.getIntegerInstance(locale))) { 500 result.append(",number,integer"); 501 } else { 502 result.append(",number," + 503 ((DecimalFormat )formats[i]).toPattern()); 504 } 505 } else if (formats[i] instanceof SimpleDateFormat ) { 506 if (formats[i].equals(DateFormat.getDateInstance( 507 DateFormat.DEFAULT,locale))) { 508 result.append(",date"); 509 } else if (formats[i].equals(DateFormat.getDateInstance( 510 DateFormat.SHORT,locale))) { 511 result.append(",date,short"); 512 } else if (formats[i].equals(DateFormat.getDateInstance( 513 DateFormat.DEFAULT,locale))) { 514 result.append(",date,medium"); 515 } else if (formats[i].equals(DateFormat.getDateInstance( 516 DateFormat.LONG,locale))) { 517 result.append(",date,long"); 518 } else if (formats[i].equals(DateFormat.getDateInstance( 519 DateFormat.FULL,locale))) { 520 result.append(",date,full"); 521 } else if (formats[i].equals(DateFormat.getTimeInstance( 522 DateFormat.DEFAULT,locale))) { 523 result.append(",time"); 524 } else if (formats[i].equals(DateFormat.getTimeInstance( 525 DateFormat.SHORT,locale))) { 526 result.append(",time,short"); 527 } else if (formats[i].equals(DateFormat.getTimeInstance( 528 DateFormat.DEFAULT,locale))) { 529 result.append(",time,medium"); 530 } else if (formats[i].equals(DateFormat.getTimeInstance( 531 DateFormat.LONG,locale))) { 532 result.append(",time,long"); 533 } else if (formats[i].equals(DateFormat.getTimeInstance( 534 DateFormat.FULL,locale))) { 535 result.append(",time,full"); 536 } else { 537 result.append(",date," 538 + ((SimpleDateFormat )formats[i]).toPattern()); 539 } 540 } else if (formats[i] instanceof ChoiceFormat ) { 541 result.append(",choice," 542 + ((ChoiceFormat )formats[i]).toPattern()); 543 } else { 544 } 546 result.append('}'); 547 } 548 copyAndFixQuotes(pattern, lastOffset, pattern.length(), result); 549 return result.toString(); 550 } 551 552 575 public void setFormatsByArgumentIndex(Format [] newFormats) { 576 for (int i = 0; i <= maxOffset; i++) { 577 int j = argumentNumbers[i]; 578 if (j < newFormats.length) { 579 formats[i] = newFormats[j]; 580 } 581 } 582 } 583 584 606 public void setFormats(Format [] newFormats) { 607 int runsToCopy = newFormats.length; 608 if (runsToCopy > maxOffset + 1) { 609 runsToCopy = maxOffset + 1; 610 } 611 for (int i = 0; i < runsToCopy; i++) { 612 formats[i] = newFormats[i]; 613 } 614 } 615 616 634 public void setFormatByArgumentIndex(int argumentIndex, Format newFormat) { 635 for (int j = 0; j <= maxOffset; j++) { 636 if (argumentNumbers[j] == argumentIndex) { 637 formats[j] = newFormat; 638 } 639 } 640 } 641 642 659 public void setFormat(int formatElementIndex, Format newFormat) { 660 formats[formatElementIndex] = newFormat; 661 } 662 663 683 public Format [] getFormatsByArgumentIndex() { 684 int maximumArgumentNumber = -1; 685 for (int i = 0; i <= maxOffset; i++) { 686 if (argumentNumbers[i] > maximumArgumentNumber) { 687 maximumArgumentNumber = argumentNumbers[i]; 688 } 689 } 690 Format [] resultArray = new Format [maximumArgumentNumber + 1]; 691 for (int i = 0; i <= maxOffset; i++) { 692 resultArray[argumentNumbers[i]] = formats[i]; 693 } 694 return resultArray; 695 } 696 697 713 public Format [] getFormats() { 714 Format [] resultArray = new Format [maxOffset + 1]; 715 System.arraycopy(formats, 0, resultArray, 0, maxOffset + 1); 716 return resultArray; 717 } 718 719 784 public final StringBuffer format(Object [] arguments, StringBuffer result, 785 FieldPosition pos) 786 { 787 return subformat(arguments, result, pos, null); 788 } 789 790 802 public static String format(String pattern, Object ... arguments) { 803 MessageFormat temp = new MessageFormat (pattern); 804 return temp.format(arguments); 805 } 806 807 825 public final StringBuffer format(Object arguments, StringBuffer result, 826 FieldPosition pos) 827 { 828 return subformat((Object []) arguments, result, pos, null); 829 } 830 831 866 public AttributedCharacterIterator formatToCharacterIterator(Object arguments) { 867 StringBuffer result = new StringBuffer (); 868 ArrayList iterators = new ArrayList (); 869 870 if (arguments == null) { 871 throw new NullPointerException ( 872 "formatToCharacterIterator must be passed non-null object"); 873 } 874 subformat((Object []) arguments, result, null, iterators); 875 if (iterators.size() == 0) { 876 return createAttributedCharacterIterator(""); 877 } 878 return createAttributedCharacterIterator( 879 (AttributedCharacterIterator [])iterators.toArray( 880 new AttributedCharacterIterator [iterators.size()])); 881 } 882 883 |