2 * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
27 * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
28 * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
30 * The original version of this source code and documentation is copyrighted
31 * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
32 * materials are provided under terms of a License Agreement between Taligent
33 * and Sun. This technology is protected by multiple US and International
34 * patents. This notice and attribution to Taligent may not be removed.
35 * Taligent is a registered trademark of Taligent, Inc.
41 import java.io.InvalidObjectException;
42 import java.io.IOException;
43 import java.io.ObjectInputStream;
44 import java.text.DecimalFormat;
45 import java.util.ArrayList;
46 import java.util.Arrays;
47 import java.util.Date;
48 import java.util.List;
49 import java.util.Locale;
53 * <code>MessageFormat</code> provides a means to produce concatenated
54 * messages in a language-neutral way. Use this to construct messages
55 * displayed for end users.
58 * <code>MessageFormat</code> takes a set of objects, formats them, then
59 * inserts the formatted strings into the pattern at the appropriate places.
62 * <strong>Note:</strong>
63 * <code>MessageFormat</code> differs from the other <code>Format</code>
64 * classes in that you create a <code>MessageFormat</code> object with one
65 * of its constructors (not with a <code>getInstance</code> style factory
66 * method). The factory methods aren't necessary because <code>MessageFormat</code>
67 * itself doesn't implement locale specific behavior. Any locale specific
68 * behavior is defined by the pattern that you provide as well as the
69 * subformats used for inserted arguments.
71 * <h4><a name="patterns">Patterns and Their Interpretation</a></h4>
73 * <code>MessageFormat</code> uses patterns of the following form:
75 * <i>MessageFormatPattern:</i>
77 * <i>MessageFormatPattern</i> <i>FormatElement</i> <i>String</i>
79 * <i>FormatElement:</i>
80 * { <i>ArgumentIndex</i> }
81 * { <i>ArgumentIndex</i> , <i>FormatType</i> }
82 * { <i>ArgumentIndex</i> , <i>FormatType</i> , <i>FormatStyle</i> }
84 * <i>FormatType: one of </i>
85 * number date time choice
95 * <i>SubformatPattern</i>
98 * <p>Within a <i>String</i>, a pair of single quotes can be used to
99 * quote any arbitrary characters except single quotes. For example,
100 * pattern string <code>"'{0}'"</code> represents string
101 * <code>"{0}"</code>, not a <i>FormatElement</i>. A single quote itself
102 * must be represented by doubled single quotes {@code ''} throughout a
103 * <i>String</i>. For example, pattern string <code>"'{''}'"</code> is
104 * interpreted as a sequence of <code>'{</code> (start of quoting and a
105 * left curly brace), <code>''</code> (a single quote), and
106 * <code>}'</code> (a right curly brace and end of quoting),
107 * <em>not</em> <code>'{'</code> and <code>'}'</code> (quoted left and
108 * right curly braces): representing string <code>"{'}"</code>,
109 * <em>not</em> <code>"{}"</code>.
111 * <p>A <i>SubformatPattern</i> is interpreted by its corresponding
112 * subformat, and subformat-dependent pattern rules apply. For example,
113 * pattern string <code>"{1,number,<u>$'#',##</u>}"</code>
114 * (<i>SubformatPattern</i> with underline) will produce a number format
115 * with the pound-sign quoted, with a result such as: {@code
116 * "$#31,45"}. Refer to each {@code Format} subclass documentation for
119 * <p>Any unmatched quote is treated as closed at the end of the given
120 * pattern. For example, pattern string {@code "'{0}"} is treated as
121 * pattern {@code "'{0}'"}.
123 * <p>Any curly braces within an unquoted pattern must be balanced. For
124 * example, <code>"ab {0} de"</code> and <code>"ab '}' de"</code> are
125 * valid patterns, but <code>"ab {0'}' de"</code>, <code>"ab } de"</code>
126 * and <code>"''{''"</code> are not.
129 * <dl><dt><b>Warning:</b><dd>The rules for using quotes within message
130 * format patterns unfortunately have shown to be somewhat confusing.
131 * In particular, it isn't always obvious to localizers whether single
132 * quotes need to be doubled or not. Make sure to inform localizers about
133 * the rules, and tell them (for example, by using comments in resource
134 * bundle source files) which strings will be processed by {@code MessageFormat}.
135 * Note that localizers may need to use single quotes in translated
136 * strings where the original version doesn't have them.
139 * The <i>ArgumentIndex</i> value is a non-negative integer written
140 * using the digits {@code '0'} through {@code '9'}, and represents an index into the
141 * {@code arguments} array passed to the {@code format} methods
142 * or the result array returned by the {@code parse} methods.
144 * The <i>FormatType</i> and <i>FormatStyle</i> values are used to create
145 * a {@code Format} instance for the format element. The following
146 * table shows how the values map to {@code Format} instances. Combinations not
147 * shown in the table are illegal. A <i>SubformatPattern</i> must
148 * be a valid pattern string for the {@code Format} subclass used.
150 * <table border=1 summary="Shows how FormatType and FormatStyle values map to Format instances">
152 * <th id="ft" class="TableHeadingColor">FormatType
153 * <th id="fs" class="TableHeadingColor">FormatStyle
154 * <th id="sc" class="TableHeadingColor">Subformat Created
156 * <td headers="ft"><i>(none)</i>
157 * <td headers="fs"><i>(none)</i>
158 * <td headers="sc"><code>null</code>
160 * <td headers="ft" rowspan=5><code>number</code>
161 * <td headers="fs"><i>(none)</i>
162 * <td headers="sc">{@link NumberFormat#getInstance(Locale) NumberFormat.getInstance}{@code (getLocale())}
164 * <td headers="fs"><code>integer</code>
165 * <td headers="sc">{@link NumberFormat#getIntegerInstance(Locale) NumberFormat.getIntegerInstance}{@code (getLocale())}
167 * <td headers="fs"><code>currency</code>
168 * <td headers="sc">{@link NumberFormat#getCurrencyInstance(Locale) NumberFormat.getCurrencyInstance}{@code (getLocale())}
170 * <td headers="fs"><code>percent</code>
171 * <td headers="sc">{@link NumberFormat#getPercentInstance(Locale) NumberFormat.getPercentInstance}{@code (getLocale())}
173 * <td headers="fs"><i>SubformatPattern</i>
174 * <td headers="sc">{@code new} {@link DecimalFormat#DecimalFormat(String,DecimalFormatSymbols) DecimalFormat}{@code (subformatPattern,} {@link DecimalFormatSymbols#getInstance(Locale) DecimalFormatSymbols.getInstance}{@code (getLocale()))}
176 * <td headers="ft" rowspan=6><code>date</code>
177 * <td headers="fs"><i>(none)</i>
178 * <td headers="sc">{@link DateFormat#getDateInstance(int,Locale) DateFormat.getDateInstance}{@code (}{@link DateFormat#DEFAULT}{@code , getLocale())}
180 * <td headers="fs"><code>short</code>
181 * <td headers="sc">{@link DateFormat#getDateInstance(int,Locale) DateFormat.getDateInstance}{@code (}{@link DateFormat#SHORT}{@code , getLocale())}
183 * <td headers="fs"><code>medium</code>
184 * <td headers="sc">{@link DateFormat#getDateInstance(int,Locale) DateFormat.getDateInstance}{@code (}{@link DateFormat#DEFAULT}{@code , getLocale())}
186 * <td headers="fs"><code>long</code>
187 * <td headers="sc">{@link DateFormat#getDateInstance(int,Locale) DateFormat.getDateInstance}{@code (}{@link DateFormat#LONG}{@code , getLocale())}
189 * <td headers="fs"><code>full</code>
190 * <td headers="sc">{@link DateFormat#getDateInstance(int,Locale) DateFormat.getDateInstance}{@code (}{@link DateFormat#FULL}{@code , getLocale())}
192 * <td headers="fs"><i>SubformatPattern</i>
193 * <td headers="sc">{@code new} {@link SimpleDateFormat#SimpleDateFormat(String,Locale) SimpleDateFormat}{@code (subformatPattern, getLocale())}
195 * <td headers="ft" rowspan=6><code>time</code>
196 * <td headers="fs"><i>(none)</i>
197 * <td headers="sc">{@link DateFormat#getTimeInstance(int,Locale) DateFormat.getTimeInstance}{@code (}{@link DateFormat#DEFAULT}{@code , getLocale())}
199 * <td headers="fs"><code>short</code>
200 * <td headers="sc">{@link DateFormat#getTimeInstance(int,Locale) DateFormat.getTimeInstance}{@code (}{@link DateFormat#SHORT}{@code , getLocale())}
202 * <td headers="fs"><code>medium</code>
203 * <td headers="sc">{@link DateFormat#getTimeInstance(int,Locale) DateFormat.getTimeInstance}{@code (}{@link DateFormat#DEFAULT}{@code , getLocale())}
205 * <td headers="fs"><code>long</code>
206 * <td headers="sc">{@link DateFormat#getTimeInstance(int,Locale) DateFormat.getTimeInstance}{@code (}{@link DateFormat#LONG}{@code , getLocale())}
208 * <td headers="fs"><code>full</code>
209 * <td headers="sc">{@link DateFormat#getTimeInstance(int,Locale) DateFormat.getTimeInstance}{@code (}{@link DateFormat#FULL}{@code , getLocale())}
211 * <td headers="fs"><i>SubformatPattern</i>
212 * <td headers="sc">{@code new} {@link SimpleDateFormat#SimpleDateFormat(String,Locale) SimpleDateFormat}{@code (subformatPattern, getLocale())}
214 * <td headers="ft"><code>choice</code>
215 * <td headers="fs"><i>SubformatPattern</i>
216 * <td headers="sc">{@code new} {@link ChoiceFormat#ChoiceFormat(String) ChoiceFormat}{@code (subformatPattern)}
220 * <h4>Usage Information</h4>
223 * Here are some examples of usage.
224 * In real internationalized programs, the message format pattern and other
225 * static strings will, of course, be obtained from resource bundles.
226 * Other parameters will be dynamically determined at runtime.
228 * The first example uses the static method <code>MessageFormat.format</code>,
229 * which internally creates a <code>MessageFormat</code> for one-time use:
232 * String event = "a disturbance in the Force";
234 * String result = MessageFormat.format(
235 * "At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.",
236 * planet, new Date(), event);
237 * </pre></blockquote>
240 * At 12:30 PM on Jul 3, 2053, there was a disturbance in the Force on planet 7.
241 * </pre></blockquote>
244 * The following example creates a <code>MessageFormat</code> instance that
245 * can be used repeatedly:
247 * int fileCount = 1273;
248 * String diskName = "MyDisk";
249 * Object[] testArgs = {new Long(fileCount), diskName};
251 * MessageFormat form = new MessageFormat(
252 * "The disk \"{1}\" contains {0} file(s).");
254 * System.out.println(form.format(testArgs));
255 * </pre></blockquote>
256 * The output with different values for <code>fileCount</code>:
258 * The disk "MyDisk" contains 0 file(s).
259 * The disk "MyDisk" contains 1 file(s).
260 * The disk "MyDisk" contains 1,273 file(s).
261 * </pre></blockquote>
264 * For more sophisticated patterns, you can use a <code>ChoiceFormat</code>
265 * to produce correct forms for singular and plural:
267 * MessageFormat form = new MessageFormat("The disk \"{1}\" contains {0}.");
268 * double[] filelimits = {0,1,2};
269 * String[] filepart = {"no files","one file","{0,number} files"};
270 * ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
271 * form.setFormatByArgumentIndex(0, fileform);
273 * int fileCount = 1273;
274 * String diskName = "MyDisk";
275 * Object[] testArgs = {new Long(fileCount), diskName};
277 * System.out.println(form.format(testArgs));
278 * </pre></blockquote>
279 * The output with different values for <code>fileCount</code>:
281 * The disk "MyDisk" contains no files.
282 * The disk "MyDisk" contains one file.
283 * The disk "MyDisk" contains 1,273 files.
284 * </pre></blockquote>
287 * You can create the <code>ChoiceFormat</code> programmatically, as in the
288 * above example, or by using a pattern. See {@link ChoiceFormat}
289 * for more information.
292 * "There {0,choice,0#are no files|1#is one file|1<are {0,number,integer} files}.");
293 * </pre></blockquote>
296 * <strong>Note:</strong> As we see above, the string produced
297 * by a <code>ChoiceFormat</code> in <code>MessageFormat</code> is treated as special;
298 * occurrences of '{' are used to indicate subformats, and cause recursion.
299 * If you create both a <code>MessageFormat</code> and <code>ChoiceFormat</code>
300 * programmatically (instead of using the string patterns), then be careful not to
301 * produce a format that recurses on itself, which will cause an infinite loop.
303 * When a single argument is parsed more than once in the string, the last match
304 * will be the final result of the parsing. For example,
306 * MessageFormat mf = new MessageFormat("{0,number,#.##}, {0,number,#.#}");
307 * Object[] objs = {new Double(3.1415)};
308 * String result = mf.format( objs );
309 * // result now equals "3.14, 3.1"
311 * objs = mf.parse(result, new ParsePosition(0));
312 * // objs now equals {new Double(3.1)}
313 * </pre></blockquote>
316 * Likewise, parsing with a {@code MessageFormat} object using patterns containing
317 * multiple occurrences of the same argument would return the last match. For
320 * MessageFormat mf = new MessageFormat("{0}, {0}, {0}");
321 * String forParsing = "x, y, z";
322 * Object[] objs = mf.parse(forParsing, new ParsePosition(0));
323 * // result now equals {new String("z")}
324 * </pre></blockquote>
326 * <h4><a name="synchronization">Synchronization</a></h4>
329 * Message formats are not synchronized.
330 * It is recommended to create separate format instances for each thread.
331 * If multiple threads access a format concurrently, it must be synchronized
334 * @see java.util.Locale
338 * @see DecimalFormatSymbols
341 * @see SimpleDateFormat
346 public class MessageFormat extends Format {
348 private static final long serialVersionUID = 6479157306784022952L;
351 * Constructs a MessageFormat for the default locale and the
353 * The constructor first sets the locale, then parses the pattern and
354 * creates a list of subformats for the format elements contained in it.
355 * Patterns and their interpretation are specified in the
356 * <a href="#patterns">class description</a>.
358 * @param pattern the pattern for this message format
359 * @exception IllegalArgumentException if the pattern is invalid
361 public MessageFormat(String pattern) {
362 this.locale = Locale.getDefault(Locale.Category.FORMAT);
363 applyPattern(pattern);
367 * Constructs a MessageFormat for the specified locale and
369 * The constructor first sets the locale, then parses the pattern and
370 * creates a list of subformats for the format elements contained in it.
371 * Patterns and their interpretation are specified in the
372 * <a href="#patterns">class description</a>.
374 * @param pattern the pattern for this message format
375 * @param locale the locale for this message format
376 * @exception IllegalArgumentException if the pattern is invalid
379 public MessageFormat(String pattern, Locale locale) {
380 this.locale = locale;
381 applyPattern(pattern);
385 * Sets the locale to be used when creating or comparing subformats.
386 * This affects subsequent calls
388 * <li>to the {@link #applyPattern applyPattern}
389 * and {@link #toPattern toPattern} methods if format elements specify
390 * a format type and therefore have the subformats created in the
391 * <code>applyPattern</code> method, as well as
392 * <li>to the <code>format</code> and
393 * {@link #formatToCharacterIterator formatToCharacterIterator} methods
394 * if format elements do not specify a format type and therefore have
395 * the subformats created in the formatting methods.
397 * Subformats that have already been created are not affected.
399 * @param locale the locale to be used when creating or comparing subformats
401 public void setLocale(Locale locale) {
402 this.locale = locale;
406 * Gets the locale that's used when creating or comparing subformats.
408 * @return the locale used when creating or comparing subformats
410 public Locale getLocale() {
416 * Sets the pattern used by this message format.
417 * The method parses the pattern and creates a list of subformats
418 * for the format elements contained in it.
419 * Patterns and their interpretation are specified in the
420 * <a href="#patterns">class description</a>.
422 * @param pattern the pattern for this message format
423 * @exception IllegalArgumentException if the pattern is invalid
425 public void applyPattern(String pattern) {
426 StringBuilder[] segments = new StringBuilder[4];
427 // Allocate only segments[SEG_RAW] here. The rest are
428 // allocated on demand.
429 segments[SEG_RAW] = new StringBuilder();
432 int formatNumber = 0;
433 boolean inQuote = false;
436 for (int i = 0; i < pattern.length(); ++i) {
437 char ch = pattern.charAt(i);
438 if (part == SEG_RAW) {
440 if (i + 1 < pattern.length()
441 && pattern.charAt(i+1) == '\'') {
442 segments[part].append(ch); // handle doubles
447 } else if (ch == '{' && !inQuote) {
449 if (segments[SEG_INDEX] == null) {
450 segments[SEG_INDEX] = new StringBuilder();
453 segments[part].append(ch);
456 if (inQuote) { // just copy quotes in parts
457 segments[part].append(ch);
464 if (part < SEG_MODIFIER) {
465 if (segments[++part] == null) {
466 segments[part] = new StringBuilder();
469 segments[part].append(ch);
474 segments[part].append(ch);
477 if (braceStack == 0) {
479 makeFormat(i, formatNumber, segments);
481 // throw away other segments
482 segments[SEG_INDEX] = null;
483 segments[SEG_TYPE] = null;
484 segments[SEG_MODIFIER] = null;
487 segments[part].append(ch);
491 // Skip any leading space chars for SEG_TYPE.
492 if (part != SEG_TYPE || segments[SEG_TYPE].length() > 0) {
493 segments[part].append(ch);
498 // fall through, so we keep quotes in other parts
500 segments[part].append(ch);
506 if (braceStack == 0 && part != 0) {
508 throw new IllegalArgumentException("Unmatched braces in the pattern.");
510 this.pattern = segments[0].toString();
515 * Returns a pattern representing the current state of the message format.
516 * The string is constructed from internal information and therefore
517 * does not necessarily equal the previously applied pattern.
519 * @return a pattern representing the current state of the message format
521 public String toPattern() {
522 // later, make this more extensible
524 StringBuilder result = new StringBuilder();
525 for (int i = 0; i <= maxOffset; ++i) {
526 copyAndFixQuotes(pattern, lastOffset, offsets[i], result);
527 lastOffset = offsets[i];
528 result.append('{').append(argumentNumbers[i]);
529 Format fmt = formats[i];
531 // do nothing, string format
532 } else if (fmt instanceof NumberFormat) {
533 if (fmt.equals(NumberFormat.getInstance(locale))) {
534 result.append(",number");
535 } else if (fmt.equals(NumberFormat.getCurrencyInstance(locale))) {
536 result.append(",number,currency");
537 } else if (fmt.equals(NumberFormat.getPercentInstance(locale))) {
538 result.append(",number,percent");
539 } else if (fmt.equals(NumberFormat.getIntegerInstance(locale))) {
540 result.append(",number,integer");
542 if (fmt instanceof DecimalFormat) {
543 result.append(",number,").append(((DecimalFormat)fmt).toPattern());
544 } else if (fmt instanceof ChoiceFormat) {
545 result.append(",choice,").append(((ChoiceFormat)fmt).toPattern());
550 } else if (fmt instanceof DateFormat) {
552 for (index = MODIFIER_DEFAULT; index < DATE_TIME_MODIFIERS.length; index++) {
553 DateFormat df = DateFormat.getDateInstance(DATE_TIME_MODIFIERS[index],
555 if (fmt.equals(df)) {
556 result.append(",date");
559 df = DateFormat.getTimeInstance(DATE_TIME_MODIFIERS[index],
561 if (fmt.equals(df)) {
562 result.append(",time");
566 if (index >= DATE_TIME_MODIFIERS.length) {
567 if (fmt instanceof SimpleDateFormat) {
568 result.append(",date,").append(((SimpleDateFormat)fmt).toPattern());
572 } else if (index != MODIFIER_DEFAULT) {
573 result.append(',').append(DATE_TIME_MODIFIER_KEYWORDS[index]);
576 //result.append(", unknown");
580 copyAndFixQuotes(pattern, lastOffset, pattern.length(), result);
581 return result.toString();
585 * Sets the formats to use for the values passed into
586 * <code>format</code> methods or returned from <code>parse</code>
587 * methods. The indices of elements in <code>newFormats</code>
588 * correspond to the argument indices used in the previously set
590 * The order of formats in <code>newFormats</code> thus corresponds to
591 * the order of elements in the <code>arguments</code> array passed
592 * to the <code>format</code> methods or the result array returned
593 * by the <code>parse</code> methods.
595 * If an argument index is used for more than one format element
596 * in the pattern string, then the corresponding new format is used
597 * for all such format elements. If an argument index is not used
598 * for any format element in the pattern string, then the
599 * corresponding new format is ignored. If fewer formats are provided
600 * than needed, then only the formats for argument indices less
601 * than <code>newFormats.length</code> are replaced.
603 * @param newFormats the new formats to use
604 * @exception NullPointerException if <code>newFormats</code> is null
607 public void setFormatsByArgumentIndex(Format[] newFormats) {
608 for (int i = 0; i <= maxOffset; i++) {
609 int j = argumentNumbers[i];
610 if (j < newFormats.length) {
611 formats[i] = newFormats[j];
617 * Sets the formats to use for the format elements in the
618 * previously set pattern string.
619 * The order of formats in <code>newFormats</code> corresponds to
620 * the order of format elements in the pattern string.
622 * If more formats are provided than needed by the pattern string,
623 * the remaining ones are ignored. If fewer formats are provided
624 * than needed, then only the first <code>newFormats.length</code>
625 * formats are replaced.
627 * Since the order of format elements in a pattern string often
628 * changes during localization, it is generally better to use the
629 * {@link #setFormatsByArgumentIndex setFormatsByArgumentIndex}
630 * method, which assumes an order of formats corresponding to the
631 * order of elements in the <code>arguments</code> array passed to
632 * the <code>format</code> methods or the result array returned by
633 * the <code>parse</code> methods.
635 * @param newFormats the new formats to use
636 * @exception NullPointerException if <code>newFormats</code> is null
638 public void setFormats(Format[] newFormats) {
639 int runsToCopy = newFormats.length;
640 if (runsToCopy > maxOffset + 1) {
641 runsToCopy = maxOffset + 1;
643 for (int i = 0; i < runsToCopy; i++) {
644 formats[i] = newFormats[i];
649 * Sets the format to use for the format elements within the
650 * previously set pattern string that use the given argument
652 * The argument index is part of the format element definition and
653 * represents an index into the <code>arguments</code> array passed
654 * to the <code>format</code> methods or the result array returned
655 * by the <code>parse</code> methods.
657 * If the argument index is used for more than one format element
658 * in the pattern string, then the new format is used for all such
659 * format elements. If the argument index is not used for any format
660 * element in the pattern string, then the new format is ignored.
662 * @param argumentIndex the argument index for which to use the new format
663 * @param newFormat the new format to use
666 public void setFormatByArgumentIndex(int argumentIndex, Format newFormat) {
667 for (int j = 0; j <= maxOffset; j++) {
668 if (argumentNumbers[j] == argumentIndex) {
669 formats[j] = newFormat;
675 * Sets the format to use for the format element with the given
676 * format element index within the previously set pattern string.
677 * The format element index is the zero-based number of the format
678 * element counting from the start of the pattern string.
680 * Since the order of format elements in a pattern string often
681 * changes during localization, it is generally better to use the
682 * {@link #setFormatByArgumentIndex setFormatByArgumentIndex}
683 * method, which accesses format elements based on the argument
684 * index they specify.
686 * @param formatElementIndex the index of a format element within the pattern
687 * @param newFormat the format to use for the specified format element
688 * @exception ArrayIndexOutOfBoundsException if {@code formatElementIndex} is equal to or
689 * larger than the number of format elements in the pattern string
691 public void setFormat(int formatElementIndex, Format newFormat) {
692 formats[formatElementIndex] = newFormat;
696 * Gets the formats used for the values passed into
697 * <code>format</code> methods or returned from <code>parse</code>
698 * methods. The indices of elements in the returned array
699 * correspond to the argument indices used in the previously set
701 * The order of formats in the returned array thus corresponds to
702 * the order of elements in the <code>arguments</code> array passed
703 * to the <code>format</code> methods or the result array returned
704 * by the <code>parse</code> methods.
706 * If an argument index is used for more than one format element
707 * in the pattern string, then the format used for the last such
708 * format element is returned in the array. If an argument index
709 * is not used for any format element in the pattern string, then
710 * null is returned in the array.
712 * @return the formats used for the arguments within the pattern
715 public Format[] getFormatsByArgumentIndex() {
716 int maximumArgumentNumber = -1;
717 for (int i = 0; i <= maxOffset; i++) {
718 if (argumentNumbers[i] > maximumArgumentNumber) {
719 maximumArgumentNumber = argumentNumbers[i];
722 Format[] resultArray = new Format[maximumArgumentNumber + 1];
723 for (int i = 0; i <= maxOffset; i++) {
724 resultArray[argumentNumbers[i]] = formats[i];
730 * Gets the formats used for the format elements in the
731 * previously set pattern string.
732 * The order of formats in the returned array corresponds to
733 * the order of format elements in the pattern string.
735 * Since the order of format elements in a pattern string often
736 * changes during localization, it's generally better to use the
737 * {@link #getFormatsByArgumentIndex getFormatsByArgumentIndex}
738 * method, which assumes an order of formats corresponding to the
739 * order of elements in the <code>arguments</code> array passed to
740 * the <code>format</code> methods or the result array returned by
741 * the <code>parse</code> methods.
743 * @return the formats used for the format elements in the pattern
745 public Format[] getFormats() {
746 Format[] resultArray = new Format[maxOffset + 1];
747 System.arraycopy(formats, 0, resultArray, 0, maxOffset + 1);
752 * Formats an array of objects and appends the <code>MessageFormat</code>'s
753 * pattern, with format elements replaced by the formatted objects, to the
754 * provided <code>StringBuffer</code>.
756 * The text substituted for the individual format elements is derived from
757 * the current subformat of the format element and the
758 * <code>arguments</code> element at the format element's argument index
759 * as indicated by the first matching line of the following table. An
760 * argument is <i>unavailable</i> if <code>arguments</code> is
761 * <code>null</code> or has fewer than argumentIndex+1 elements.
763 * <table border=1 summary="Examples of subformat,argument,and formatted text">
770 * <td><i>unavailable</i>
771 * <td><code>"{" + argumentIndex + "}"</code>
774 * <td><code>null</code>
775 * <td><code>"null"</code>
777 * <td><code>instanceof ChoiceFormat</code>
779 * <td><code>subformat.format(argument).indexOf('{') >= 0 ?<br>
780 * (new MessageFormat(subformat.format(argument), getLocale())).format(argument) :
781 * subformat.format(argument)</code>
783 * <td><code>!= null</code>
785 * <td><code>subformat.format(argument)</code>
787 * <td><code>null</code>
788 * <td><code>instanceof Number</code>
789 * <td><code>NumberFormat.getInstance(getLocale()).format(argument)</code>
791 * <td><code>null</code>
792 * <td><code>instanceof Date</code>
793 * <td><code>DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, getLocale()).format(argument)</code>
795 * <td><code>null</code>
796 * <td><code>instanceof String</code>
797 * <td><code>argument</code>
799 * <td><code>null</code>
801 * <td><code>argument.toString()</code>
804 * If <code>pos</code> is non-null, and refers to
805 * <code>Field.ARGUMENT</code>, the location of the first formatted
806 * string will be returned.
808 * @param arguments an array of objects to be formatted and substituted.
809 * @param result where text is appended.
810 * @param pos On input: an alignment field, if desired.
811 * On output: the offsets of the alignment field.
812 * @exception IllegalArgumentException if an argument in the
813 * <code>arguments</code> array is not of the type
814 * expected by the format element(s) that use it.
816 public final StringBuffer format(Object[] arguments, StringBuffer result,
819 return subformat(arguments, result, pos, null);
823 * Creates a MessageFormat with the given pattern and uses it
824 * to format the given arguments. This is equivalent to
826 * <code>(new {@link #MessageFormat(String) MessageFormat}(pattern)).{@link #format(java.lang.Object[], java.lang.StringBuffer, java.text.FieldPosition) format}(arguments, new StringBuffer(), null).toString()</code>
829 * @exception IllegalArgumentException if the pattern is invalid,
830 * or if an argument in the <code>arguments</code> array
831 * is not of the type expected by the format element(s)
834 public static String format(String pattern, Object ... arguments) {
835 MessageFormat temp = new MessageFormat(pattern);
836 return temp.format(arguments);
841 * Formats an array of objects and appends the <code>MessageFormat</code>'s
842 * pattern, with format elements replaced by the formatted objects, to the
843 * provided <code>StringBuffer</code>.
844 * This is equivalent to
846 * <code>{@link #format(java.lang.Object[], java.lang.StringBuffer, java.text.FieldPosition) format}((Object[]) arguments, result, pos)</code>
849 * @param arguments an array of objects to be formatted and substituted.
850 * @param result where text is appended.
851 * @param pos On input: an alignment field, if desired.
852 * On output: the offsets of the alignment field.
853 * @exception IllegalArgumentException if an argument in the
854 * <code>arguments</code> array is not of the type
855 * expected by the format element(s) that use it.
857 public final StringBuffer format(Object arguments, StringBuffer result,
860 return subformat((Object[]) arguments, result, pos, null);
864 * Formats an array of objects and inserts them into the
865 * <code>MessageFormat</code>'s pattern, producing an
866 * <code>AttributedCharacterIterator</code>.
867 * You can use the returned <code>AttributedCharacterIterator</code>
868 * to build the resulting String, as well as to determine information
869 * about the resulting String.
871 * The text of the returned <code>AttributedCharacterIterator</code> is
872 * the same that would be returned by
874 * <code>{@link #format(java.lang.Object[], java.lang.StringBuffer, java.text.FieldPosition) format}(arguments, new StringBuffer(), null).toString()</code>
877 * In addition, the <code>AttributedCharacterIterator</code> contains at
878 * least attributes indicating where text was generated from an
879 * argument in the <code>arguments</code> array. The keys of these attributes are of
880 * type <code>MessageFormat.Field</code>, their values are
881 * <code>Integer</code> objects indicating the index in the <code>arguments</code>
882 * array of the argument from which the text was generated.
884 * The attributes/value from the underlying <code>Format</code>
885 * instances that <code>MessageFormat</code> uses will also be
886 * placed in the resulting <code>AttributedCharacterIterator</code>.
887 * This allows you to not only find where an argument is placed in the
888 * resulting String, but also which fields it contains in turn.
890 * @param arguments an array of objects to be formatted and substituted.
891 * @return AttributedCharacterIterator describing the formatted value.
892 * @exception NullPointerException if <code>arguments</code> is null.
893 * @exception IllegalArgumentException if an argument in the
894 * <code>arguments</code> array is not of the type
895 * expected by the format element(s) that use it.
898 public AttributedCharacterIterator formatToCharacterIterator(Object arguments) {
899 StringBuffer result = new StringBuffer();
900 ArrayList iterators = new ArrayList();
902 if (arguments == null) {
903 throw new NullPointerException(
904 "formatToCharacterIterator must be passed non-null object");
906 subformat((Object[]) arguments, result, null, iterators);
907 if (iterators.size() == 0) {
908 return createAttributedCharacterIterator("");
910 return createAttributedCharacterIterator(
911 (AttributedCharacterIterator[])iterators.toArray(
912 new AttributedCharacterIterator[iterators.size()]));
918 * <p>Caveats: The parse may fail in a number of circumstances.
921 * <li>If one of the arguments does not occur in the pattern.
922 * <li>If the format of an argument loses information, such as
923 * with a choice format where a large number formats to "many".
924 * <li>Does not yet handle recursion (where
925 * the substituted strings contain {n} references.)
926 * <li>Will not always find a match (or the correct match)
927 * if some part of the parse is ambiguous.
928 * For example, if the pattern "{1},{2}" is used with the
929 * string arguments {"a,b", "c"}, it will format as "a,b,c".
930 * When the result is parsed, it will return {"a", "b,c"}.
931 * <li>If a single argument is parsed more than once in the string,
932 * then the later parse wins.
934 * When the parse fails, use ParsePosition.getErrorIndex() to find out
935 * where in the string the parsing failed. The returned error
936 * index is the starting offset of the sub-patterns that the string
937 * is comparing with. For example, if the parsing string "AAA {0} BBB"
938 * is comparing against the pattern "AAD {0} BBB", the error index is
939 * 0. When an error occurs, the call to this method will return null.
940 * If the source is null, return an empty array.
942 public Object[] parse(String source, ParsePosition pos) {
943 if (source == null) {
948 int maximumArgumentNumber = -1;
949 for (int i = 0; i <= maxOffset; i++) {
950 if (argumentNumbers[i] > maximumArgumentNumber) {
951 maximumArgumentNumber = argumentNumbers[i];
954 Object[] resultArray = new Object[maximumArgumentNumber + 1];
956 int patternOffset = 0;
957 int sourceOffset = pos.index;
958 ParsePosition tempStatus = new ParsePosition(0);
959 for (int i = 0; i <= maxOffset; ++i) {
960 // match up to format
961 int len = offsets[i] - patternOffset;
962 if (len == 0 || pattern.regionMatches(patternOffset,
963 source, sourceOffset, len)) {
965 patternOffset += len;
967 pos.errorIndex = sourceOffset;
968 return null; // leave index as is to signal error
972 if (formats[i] == null) { // string format
973 // if at end, use longest possible match
974 // otherwise uses first match to intervening string
975 // does NOT recursively try all possibilities
976 int tempLength = (i != maxOffset) ? offsets[i+1] : pattern.length();
979 if (patternOffset >= tempLength) {
980 next = source.length();
982 next = source.indexOf(pattern.substring(patternOffset, tempLength),
987 pos.errorIndex = sourceOffset;
988 return null; // leave index as is to signal error
990 String strValue= source.substring(sourceOffset,next);
991 if (!strValue.equals("{"+argumentNumbers[i]+"}"))
992 resultArray[argumentNumbers[i]]
993 = source.substring(sourceOffset,next);
997 tempStatus.index = sourceOffset;
998 resultArray[argumentNumbers[i]]
999 = formats[i].parseObject(source,tempStatus);
1000 if (tempStatus.index == sourceOffset) {
1001 pos.errorIndex = sourceOffset;
1002 return null; // leave index as is to signal error
1004 sourceOffset = tempStatus.index; // update
1007 int len = pattern.length() - patternOffset;
1008 if (len == 0 || pattern.regionMatches(patternOffset,
1009 source, sourceOffset, len)) {
1010 pos.index = sourceOffset + len;
1012 pos.errorIndex = sourceOffset;
1013 return null; // leave index as is to signal error
1019 * Parses text from the beginning of the given string to produce an object
1021 * The method may not use the entire text of the given string.
1023 * See the {@link #parse(String, ParsePosition)} method for more information
1024 * on message parsing.
1026 * @param source A <code>String</code> whose beginning should be parsed.
1027 * @return An <code>Object</code> array parsed from the string.
1028 * @exception ParseException if the beginning of the specified string
1031 public Object[] parse(String source) throws ParseException {
1032 ParsePosition pos = new ParsePosition(0);
1033 Object[] result = parse(source, pos);
1034 if (pos.index == 0) // unchanged, returned object is null
1035 throw new ParseException("MessageFormat parse error!", pos.errorIndex);
1041 * Parses text from a string to produce an object array.
1043 * The method attempts to parse text starting at the index given by
1045 * If parsing succeeds, then the index of <code>pos</code> is updated
1046 * to the index after the last character used (parsing does not necessarily
1047 * use all characters up to the end of the string), and the parsed
1048 * object array is returned. The updated <code>pos</code> can be used to
1049 * indicate the starting point for the next call to this method.
1050 * If an error occurs, then the index of <code>pos</code> is not
1051 * changed, the error index of <code>pos</code> is set to the index of
1052 * the character where the error occurred, and null is returned.
1054 * See the {@link #parse(String, ParsePosition)} method for more information
1055 * on message parsing.
1057 * @param source A <code>String</code>, part of which should be parsed.
1058 * @param pos A <code>ParsePosition</code> object with index and error
1059 * index information as described above.
1060 * @return An <code>Object</code> array parsed from the string. In case of
1061 * error, returns null.
1062 * @exception NullPointerException if <code>pos</code> is null.
1064 public Object parseObject(String source, ParsePosition pos) {
1065 return parse(source, pos);
1069 * Creates and returns a copy of this object.
1071 * @return a clone of this instance.
1073 public Object clone() {
1074 MessageFormat other = (MessageFormat) super.clone();
1076 // clone arrays. Can't do with utility because of bug in Cloneable
1077 other.formats = (Format[]) formats.clone(); // shallow clone
1078 for (int i = 0; i < formats.length; ++i) {
1079 if (formats[i] != null)
1080 other.formats[i] = (Format)formats[i].clone();
1082 // for primitives or immutables, shallow clone is enough
1083 other.offsets = (int[]) offsets.clone();
1084 other.argumentNumbers = (int[]) argumentNumbers.clone();
1090 * Equality comparison between two message format objects
1092 public boolean equals(Object obj) {
1093 if (this == obj) // quick check
1095 if (obj == null || getClass() != obj.getClass())
1097 MessageFormat other = (MessageFormat) obj;
1098 return (maxOffset == other.maxOffset
1099 && pattern.equals(other.pattern)
1100 && ((locale != null && locale.equals(other.locale))
1101 || (locale == null && other.locale == null))
1102 && Arrays.equals(offsets,other.offsets)
1103 && Arrays.equals(argumentNumbers,other.argumentNumbers)
1104 && Arrays.equals(formats,other.formats));
1108 * Generates a hash code for the message format object.
1110 public int hashCode() {
1111 return pattern.hashCode(); // enough for reasonable distribution
1116 * Defines constants that are used as attribute keys in the
1117 * <code>AttributedCharacterIterator</code> returned
1118 * from <code>MessageFormat.formatToCharacterIterator</code>.
1122 public static class Field extends Format.Field {
1124 // Proclaim serial compatibility with 1.4 FCS
1125 private static final long serialVersionUID = 7899943957617360810L;
1128 * Creates a Field with the specified name.
1130 * @param name Name of the attribute
1132 protected Field(String name) {
1137 * Resolves instances being deserialized to the predefined constants.
1139 * @throws InvalidObjectException if the constant could not be
1141 * @return resolved MessageFormat.Field constant
1143 protected Object readResolve() throws InvalidObjectException {
1144 if (this.getClass() != MessageFormat.Field.class) {
1145 throw new InvalidObjectException("subclass didn't correctly implement readResolve");
1156 * Constant identifying a portion of a message that was generated
1157 * from an argument passed into <code>formatToCharacterIterator</code>.
1158 * The value associated with the key will be an <code>Integer</code>
1159 * indicating the index in the <code>arguments</code> array of the
1160 * argument from which the text was generated.
1162 public final static Field ARGUMENT =
1163 new Field("message argument field");
1166 // ===========================privates============================
1169 * The locale to use for formatting numbers and dates.
1172 private Locale locale;
1175 * The string that the formatted values are to be plugged into. In other words, this
1176 * is the pattern supplied on construction with all of the {} expressions taken out.
1179 private String pattern = "";
1181 /** The initially expected number of subformats in the format */
1182 private static final int INITIAL_FORMATS = 10;
1185 * An array of formatters, which are used to format the arguments.
1188 private Format[] formats = new Format[INITIAL_FORMATS];
1191 * The positions where the results of formatting each argument are to be inserted
1195 private int[] offsets = new int[INITIAL_FORMATS];
1198 * The argument numbers corresponding to each formatter. (The formatters are stored
1199 * in the order they occur in the pattern, not in the order in which the arguments
1203 private int[] argumentNumbers = new int[INITIAL_FORMATS];
1206 * One less than the number of entries in <code>offsets</code>. Can also be thought of
1207 * as the index of the highest-numbered element in <code>offsets</code> that is being used.
1208 * All of these arrays should have the same number of elements being used as <code>offsets</code>
1209 * does, and so this variable suffices to tell us how many entries are in all of them.
1212 private int maxOffset = -1;
1215 * Internal routine used by format. If <code>characterIterators</code> is
1216 * non-null, AttributedCharacterIterator will be created from the
1217 * subformats as necessary. If <code>characterIterators</code> is null
1218 * and <code>fp</code> is non-null and identifies
1219 * <code>Field.MESSAGE_ARGUMENT</code>, the location of
1220 * the first replaced argument will be set in it.
1222 * @exception IllegalArgumentException if an argument in the
1223 * <code>arguments</code> array is not of the type
1224 * expected by the format element(s) that use it.
1226 private StringBuffer subformat(Object[] arguments, StringBuffer result,
1227 FieldPosition fp, List characterIterators) {
1228 // note: this implementation assumes a fast substring & index.
1229 // if this is not true, would be better to append chars one by one.
1231 int last = result.length();
1232 for (int i = 0; i <= maxOffset; ++i) {
1233 result.append(pattern.substring(lastOffset, offsets[i]));
1234 lastOffset = offsets[i];
1235 int argumentNumber = argumentNumbers[i];
1236 if (arguments == null || argumentNumber >= arguments.length) {
1237 result.append('{').append(argumentNumber).append('}');
1240 // int argRecursion = ((recursionProtection >> (argumentNumber*2)) & 0x3);
1241 if (false) { // if (argRecursion == 3){
1243 result.append('\uFFFD');
1245 Object obj = arguments[argumentNumber];
1247 Format subFormatter = null;
1250 } else if (formats[i] != null) {
1251 subFormatter = formats[i];
1252 if (subFormatter instanceof ChoiceFormat) {
1253 arg = formats[i].format(obj);
1254 if (arg.indexOf('{') >= 0) {
1255 subFormatter = new MessageFormat(arg, locale);
1260 } else if (obj instanceof Number) {
1261 // format number if can
1262 subFormatter = NumberFormat.getInstance(locale);
1263 } else if (obj instanceof Date) {
1264 // format a Date if can
1265 subFormatter = DateFormat.getDateTimeInstance(
1266 DateFormat.SHORT, DateFormat.SHORT, locale);//fix
1267 } else if (obj instanceof String) {
1271 arg = obj.toString();
1272 if (arg == null) arg = "null";
1275 // At this point we are in two states, either subFormatter
1276 // is non-null indicating we should format obj using it,
1277 // or arg is non-null and we should use it as the value.
1279 if (characterIterators != null) {
1280 // If characterIterators is non-null, it indicates we need
1281 // to get the CharacterIterator from the child formatter.
1282 if (last != result.length()) {
1283 characterIterators.add(
1284 createAttributedCharacterIterator(result.substring
1286 last = result.length();
1288 if (subFormatter != null) {
1289 AttributedCharacterIterator subIterator =
1290 subFormatter.formatToCharacterIterator(obj);
1292 append(result, subIterator);
1293 if (last != result.length()) {
1294 characterIterators.add(
1295 createAttributedCharacterIterator(
1296 subIterator, Field.ARGUMENT,
1297 Integer.valueOf(argumentNumber)));
1298 last = result.length();
1302 if (arg != null && arg.length() > 0) {
1304 characterIterators.add(
1305 createAttributedCharacterIterator(
1306 arg, Field.ARGUMENT,
1307 Integer.valueOf(argumentNumber)));
1308 last = result.length();
1312 if (subFormatter != null) {
1313 arg = subFormatter.format(obj);
1315 last = result.length();
1317 if (i == 0 && fp != null && Field.ARGUMENT.equals(
1318 fp.getFieldAttribute())) {
1319 fp.setBeginIndex(last);
1320 fp.setEndIndex(result.length());
1322 last = result.length();
1326 result.append(pattern.substring(lastOffset, pattern.length()));
1327 if (characterIterators != null && last != result.length()) {
1328 characterIterators.add(createAttributedCharacterIterator(
1329 result.substring(last)));
1335 * Convenience method to append all the characters in
1336 * <code>iterator</code> to the StringBuffer <code>result</code>.
1338 private void append(StringBuffer result, CharacterIterator iterator) {
1339 if (iterator.first() != CharacterIterator.DONE) {
1342 result.append(iterator.first());
1343 while ((aChar = iterator.next()) != CharacterIterator.DONE) {
1344 result.append(aChar);
1349 // Indices for segments
1350 private static final int SEG_RAW = 0;
1351 private static final int SEG_INDEX = 1;
1352 private static final int SEG_TYPE = 2;
1353 private static final int SEG_MODIFIER = 3; // modifier or subformat
1355 // Indices for type keywords
1356 private static final int TYPE_NULL = 0;
1357 private static final int TYPE_NUMBER = 1;
1358 private static final int TYPE_DATE = 2;
1359 private static final int TYPE_TIME = 3;
1360 private static final int TYPE_CHOICE = 4;
1362 private static final String[] TYPE_KEYWORDS = {
1370 // Indices for number modifiers
1371 private static final int MODIFIER_DEFAULT = 0; // common in number and date-time
1372 private static final int MODIFIER_CURRENCY = 1;
1373 private static final int MODIFIER_PERCENT = 2;
1374 private static final int MODIFIER_INTEGER = 3;
1376 private static final String[] NUMBER_MODIFIER_KEYWORDS = {
1383 // Indices for date-time modifiers
1384 private static final int MODIFIER_SHORT = 1;
1385 private static final int MODIFIER_MEDIUM = 2;
1386 private static final int MODIFIER_LONG = 3;
1387 private static final int MODIFIER_FULL = 4;
1389 private static final String[] DATE_TIME_MODIFIER_KEYWORDS = {
1397 // Date-time style values corresponding to the date-time modifiers.
1398 private static final int[] DATE_TIME_MODIFIERS = {
1406 private void makeFormat(int position, int offsetNumber,
1407 StringBuilder[] textSegments)
1409 String[] segments = new String[textSegments.length];
1410 for (int i = 0; i < textSegments.length; i++) {
1411 StringBuilder oneseg = textSegments[i];
1412 segments[i] = (oneseg != null) ? oneseg.toString() : "";
1415 // get the argument number
1418 argumentNumber = Integer.parseInt(segments[SEG_INDEX]); // always unlocalized!
1419 } catch (NumberFormatException e) {
1420 throw new IllegalArgumentException("can't parse argument number: "
1421 + segments[SEG_INDEX], e);
1423 if (argumentNumber < 0) {
1424 throw new IllegalArgumentException("negative argument number: "
1428 // resize format information arrays if necessary
1429 if (offsetNumber >= formats.length) {
1430 int newLength = formats.length * 2;
1431 Format[] newFormats = new Format[newLength];
1432 int[] newOffsets = new int[newLength];
1433 int[] newArgumentNumbers = new int[newLength];
1434 System.arraycopy(formats, 0, newFormats, 0, maxOffset + 1);
1435 System.arraycopy(offsets, 0, newOffsets, 0, maxOffset + 1);
1436 System.arraycopy(argumentNumbers, 0, newArgumentNumbers, 0, maxOffset + 1);
1437 formats = newFormats;
1438 offsets = newOffsets;
1439 argumentNumbers = newArgumentNumbers;
1441 int oldMaxOffset = maxOffset;
1442 maxOffset = offsetNumber;
1443 offsets[offsetNumber] = segments[SEG_RAW].length();
1444 argumentNumbers[offsetNumber] = argumentNumber;
1446 // now get the format
1447 Format newFormat = null;
1448 if (segments[SEG_TYPE].length() != 0) {
1449 int type = findKeyword(segments[SEG_TYPE], TYPE_KEYWORDS);
1452 // Type "" is allowed. e.g., "{0,}", "{0,,}", and "{0,,#}"
1453 // are treated as "{0}".
1457 switch (findKeyword(segments[SEG_MODIFIER], NUMBER_MODIFIER_KEYWORDS)) {
1458 case MODIFIER_DEFAULT:
1459 newFormat = NumberFormat.getInstance(locale);
1461 case MODIFIER_CURRENCY:
1462 newFormat = NumberFormat.getCurrencyInstance(locale);
1464 case MODIFIER_PERCENT:
1465 newFormat = NumberFormat.getPercentInstance(locale);
1467 case MODIFIER_INTEGER:
1468 newFormat = NumberFormat.getIntegerInstance(locale);
1470 default: // DecimalFormat pattern
1472 newFormat = new DecimalFormat(segments[SEG_MODIFIER],
1473 DecimalFormatSymbols.getInstance(locale));
1474 } catch (IllegalArgumentException e) {
1475 maxOffset = oldMaxOffset;
1484 int mod = findKeyword(segments[SEG_MODIFIER], DATE_TIME_MODIFIER_KEYWORDS);
1485 if (mod >= 0 && mod < DATE_TIME_MODIFIER_KEYWORDS.length) {
1486 if (type == TYPE_DATE) {
1487 newFormat = DateFormat.getDateInstance(DATE_TIME_MODIFIERS[mod],
1490 newFormat = DateFormat.getTimeInstance(DATE_TIME_MODIFIERS[mod],
1494 // SimpleDateFormat pattern
1496 newFormat = new SimpleDateFormat(segments[SEG_MODIFIER], locale);
1497 } catch (IllegalArgumentException e) {
1498 maxOffset = oldMaxOffset;
1506 // ChoiceFormat pattern
1507 newFormat = new ChoiceFormat(segments[SEG_MODIFIER]);
1508 } catch (Exception e) {
1509 maxOffset = oldMaxOffset;
1510 throw new IllegalArgumentException("Choice Pattern incorrect: "
1511 + segments[SEG_MODIFIER], e);
1516 maxOffset = oldMaxOffset;
1517 throw new IllegalArgumentException("unknown format type: " +
1518 segments[SEG_TYPE]);
1521 formats[offsetNumber] = newFormat;
1524 private static final int findKeyword(String s, String[] list) {
1525 for (int i = 0; i < list.length; ++i) {
1526 if (s.equals(list[i]))
1530 // Try trimmed lowercase.
1531 String ls = s.trim().toLowerCase(Locale.ROOT);
1533 for (int i = 0; i < list.length; ++i) {
1534 if (ls.equals(list[i]))
1541 private static final void copyAndFixQuotes(String source, int start, int end,
1542 StringBuilder target) {
1543 boolean quoted = false;
1545 for (int i = start; i < end; ++i) {
1546 char ch = source.charAt(i);
1549 target.append('\'');
1553 } else if (ch == '\'') {
1554 target.append("''");
1557 target.append('\'');
1564 target.append('\'');
1569 * After reading an object from the input stream, do a simple verification
1570 * to maintain class invariants.
1571 * @throws InvalidObjectException if the objects read from the stream is invalid.
1573 private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
1574 in.defaultReadObject();
1575 boolean isValid = maxOffset >= -1
1576 && formats.length > maxOffset
1577 && offsets.length > maxOffset
1578 && argumentNumbers.length > maxOffset;
1580 int lastOffset = pattern.length() + 1;
1581 for (int i = maxOffset; i >= 0; --i) {
1582 if ((offsets[i] < 0) || (offsets[i] > lastOffset)) {
1586 lastOffset = offsets[i];
1591 throw new InvalidObjectException("Could not reconstruct MessageFormat from corrupt stream.");