| 1 11 package com.ibm.icu.text; 12 13 import java.io.IOException ; 14 import java.io.InvalidObjectException ; 15 import java.io.ObjectInputStream ; 16 import java.text.ChoiceFormat ; 17 import java.text.FieldPosition ; 18 import java.text.Format ; 19 import java.text.ParseException ; 20 import java.text.ParsePosition ; 21 import java.util.Date ; 22 import java.util.Locale ; 23 24 import com.ibm.icu.impl.Utility; 25 import com.ibm.icu.text.RuleBasedNumberFormat; 26 import com.ibm.icu.util.ULocale; 27 28 327 public class MessageFormat extends UFormat { 328 329 static final long serialVersionUID = 7136212545847378651L; 331 332 344 public MessageFormat(String pattern) { 345 this.ulocale = ULocale.getDefault(); 346 applyPattern(pattern); 347 } 348 349 362 public MessageFormat(String pattern, Locale locale) { 363 this(pattern, ULocale.forLocale(locale)); 364 } 365 366 379 public MessageFormat(String pattern, ULocale locale) { 380 this.ulocale = locale; 381 applyPattern(pattern); 382 } 383 384 394 public void setLocale(Locale locale) { 395 setLocale(ULocale.forLocale(locale)); 396 } 397 398 408 public void setLocale(ULocale locale) { 409 410 411 412 String existingPattern = toPattern(); 413 this.ulocale = locale; 414 applyPattern(existingPattern); 415 } 416 417 423 public Locale getLocale() { 424 return ulocale.toLocale(); 425 } 426 427 433 public ULocale getULocale() { 434 return ulocale; 435 } 436 437 448 public void applyPattern(String pattern) { 449 StringBuffer [] segments = new StringBuffer [4]; 450 for (int i = 0; i < segments.length; ++i) { 451 segments[i] = new StringBuffer (); 452 } 453 int part = 0; 454 int formatNumber = 0; 455 boolean inQuote = false; 456 int braceStack = 0; 457 maxOffset = -1; 458 for (int i = 0; i < pattern.length(); ++i) { 459 char ch = pattern.charAt(i); 460 if (part == 0) { 461 if (ch == '\'') { 462 if (i + 1 < pattern.length() 463 && pattern.charAt(i+1) == '\'') { 464 segments[part].append(ch); ++i; 466 } else { 467 inQuote = !inQuote; 468 } 469 } else if (ch == '{' && !inQuote) { 470 part = 1; 471 } else { 472 segments[part].append(ch); 473 } 474 } else if (inQuote) { segments[part].append(ch); 476 if (ch == '\'') { 477 inQuote = false; 478 } 479 } else { 480 switch (ch) { 481 case ',': 482 if (part < 3) 483 part += 1; 484 else 485 segments[part].append(ch); 486 break; 487 case '{': 488 ++braceStack; 489 segments[part].append(ch); 490 break; 491 case '}': 492 if (braceStack == 0) { 493 part = 0; 494 makeFormat(i, formatNumber, segments); 495 formatNumber++; 496 } else { 497 --braceStack; 498 segments[part].append(ch); 499 } 500 break; 501 case '\'': 502 inQuote = true; 503 default: 505 segments[part].append(ch); 506 break; 507 } 508 } 509 } 510 if (braceStack == 0 && part != 0) { 511 maxOffset = -1; 512 throw new IllegalArgumentException ("Unmatched braces in the pattern."); 513 } 514 this.pattern = segments[0].toString(); 515 } 516 517 518 526 public String toPattern() { 527 int lastOffset = 0; 529 StringBuffer result = new StringBuffer (); 530 for (int i = 0; i <= maxOffset; ++i) { 531 copyAndFixQuotes(pattern, lastOffset, offsets[i],result); 532 lastOffset = offsets[i]; 533 result.append('{'); 534 result.append(argumentNumbers[i]); 535 if (formats[i] == null) { 536 } else if (formats[i] instanceof DecimalFormat) { 538 if (formats[i].equals(NumberFormat.getInstance(ulocale))) { 539 result.append(",number"); 540 } else if (formats[i].equals(NumberFormat.getCurrencyInstance(ulocale))) { 541 result.append(",number,currency"); 542 } else if (formats[i].equals(NumberFormat.getPercentInstance(ulocale))) { 543 result.append(",number,percent"); 544 } else if (formats[i].equals(NumberFormat.getIntegerInstance(ulocale))) { 545 result.append(",number,integer"); 546 } else { 547 result.append(",number," + 548 ((DecimalFormat)formats[i]).toPattern()); 549 } 550 } else if (formats[i] instanceof SimpleDateFormat) { 551 if (formats[i].equals(DateFormat.getDateInstance(DateFormat.DEFAULT,ulocale))) { 552 result.append(",date"); 553 } else if (formats[i].equals(DateFormat.getDateInstance(DateFormat.SHORT,ulocale))) { 554 result.append(",date,short"); 555 } else if (formats[i].equals(DateFormat.getDateInstance(DateFormat.LONG,ulocale))) { 559 result.append(",date,long"); 560 } else if (formats[i].equals(DateFormat.getDateInstance(DateFormat.FULL,ulocale))) { 561 result.append(",date,full"); 562 } else if (formats[i].equals(DateFormat.getTimeInstance(DateFormat.DEFAULT,ulocale))) { 563 result.append(",time"); 564 } else if (formats[i].equals(DateFormat.getTimeInstance(DateFormat.SHORT,ulocale))) { 565 result.append(",time,short"); 566 } else if (formats[i].equals(DateFormat.getTimeInstance(DateFormat.LONG,ulocale))) { 570 result.append(",time,long"); 571 } else if (formats[i].equals(DateFormat.getTimeInstance(DateFormat.FULL,ulocale))) { 572 result.append(",time,full"); 573 } else { 574 result.append(",date," + ((SimpleDateFormat)formats[i]).toPattern()); 575 } 576 } else if (formats[i] instanceof ChoiceFormat ) { 577 result.append(",choice," + ((ChoiceFormat )formats[i]).toPattern()); 578 } else { 579 } 581 result.append('}'); 582 } 583 copyAndFixQuotes(pattern, lastOffset, pattern.length(), result); 584 return result.toString(); 585 } 586 587 610 public void setFormatsByArgumentIndex(Format [] newFormats) { 611 for (int i = 0; i <= maxOffset; i++) { 612 int j = argumentNumbers[i]; 613 if (j < newFormats.length) { 614 formats[i] = newFormats[j]; 615 } 616 } 617 } 618 619 642 public void setFormats(Format [] newFormats) { 643 int runsToCopy = newFormats.length; 644 if (runsToCopy > maxOffset + 1) { 645 runsToCopy = maxOffset + 1; 646 } 647 for (int i = 0; i < runsToCopy; i++) { 648 formats[i] = newFormats[i]; 649 } 650 } 651 652 670 public void setFormatByArgumentIndex(int argumentIndex, Format newFormat) { 671 for (int j = 0; j <= maxOffset; j++) { 672 if (argumentNumbers[j] == argumentIndex) { 673 formats[j] = newFormat; 674 } 675 } 676 } 677 678 696 public void setFormat(int formatElementIndex, Format newFormat) { 697 formats[formatElementIndex] = newFormat; 698 } 699 700 720 public Format [] getFormatsByArgumentIndex() { 721 int maximumArgumentNumber = -1; 722 for (int i = 0; i <= maxOffset; i++) { 723 if (argumentNumbers[i] > maximumArgumentNumber) { 724 maximumArgumentNumber = argumentNumbers[i]; 725 } 726 } 727 Format [] resultArray = new Format [maximumArgumentNumber + 1]; 728 for (int i = 0; i <= maxOffset; i++) { 729 resultArray[argumentNumbers[i]] = formats[i]; 730 } 731 return resultArray; 732 } 733 734 751 public Format [] getFormats() { 752 Format [] resultArray = new Format [maxOffset + 1]; 753 System.arraycopy(formats, 0, resultArray, 0, maxOffset + 1); 754 return resultArray; 755 } 756 757 823 public final StringBuffer format(Object [] arguments, StringBuffer result, 824 FieldPosition pos) 825 { 826 return subformat(arguments, result, pos); 827 } 828 829 842 public static String format(String pattern, Object [] arguments) { 843 MessageFormat temp = new MessageFormat(pattern); 844 return temp.format(arguments); 845 } 846 847 866 public final StringBuffer format(Object arguments, StringBuffer result, 867 FieldPosition pos) 868 { 869 return subformat((Object []) arguments, result, pos); 870 } 871 872 924 |