jdk/src/share/classes/java/text/MessageFormat.java
author rriggs
Tue, 22 Oct 2013 17:02:08 -0400
changeset 21334 c60dfce46a77
parent 19054 a64012cb49d6
permissions -rw-r--r--
8026982: javadoc errors in core libs Summary: Cleanup of javadoc -Xlint errors Reviewed-by: lancea, mduigou, darcy, mullan, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 16010
diff changeset
     2
 * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *   The original version of this source code and documentation is copyrighted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * materials are provided under terms of a License Agreement between Taligent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * and Sun. This technology is protected by multiple US and International
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * patents. This notice and attribution to Taligent may not be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *   Taligent is a registered trademark of Taligent, Inc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
package java.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.InvalidObjectException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.text.DecimalFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <code>MessageFormat</code> provides a means to produce concatenated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * messages in a language-neutral way. Use this to construct messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * displayed for end users.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <code>MessageFormat</code> takes a set of objects, formats them, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * inserts the formatted strings into the pattern at the appropriate places.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <strong>Note:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <code>MessageFormat</code> differs from the other <code>Format</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * classes in that you create a <code>MessageFormat</code> object with one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * of its constructors (not with a <code>getInstance</code> style factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * method). The factory methods aren't necessary because <code>MessageFormat</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * itself doesn't implement locale specific behavior. Any locale specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * behavior is defined by the pattern that you provide as well as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * subformats used for inserted arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 16010
diff changeset
    71
 * <h3><a name="patterns">Patterns and Their Interpretation</a></h3>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <code>MessageFormat</code> uses patterns of the following form:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <i>MessageFormatPattern:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *         <i>String</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *         <i>MessageFormatPattern</i> <i>FormatElement</i> <i>String</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <i>FormatElement:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *         { <i>ArgumentIndex</i> }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *         { <i>ArgumentIndex</i> , <i>FormatType</i> }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *         { <i>ArgumentIndex</i> , <i>FormatType</i> , <i>FormatStyle</i> }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * <i>FormatType: one of </i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *         number date time choice
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <i>FormatStyle:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *         short
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *         medium
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *         long
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *         full
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *         integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *         currency
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *         percent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *         <i>SubformatPattern</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *
7780
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
    98
 * <p>Within a <i>String</i>, a pair of single quotes can be used to
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
    99
 * quote any arbitrary characters except single quotes. For example,
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   100
 * pattern string <code>"'{0}'"</code> represents string
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   101
 * <code>"{0}"</code>, not a <i>FormatElement</i>. A single quote itself
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   102
 * must be represented by doubled single quotes {@code ''} throughout a
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   103
 * <i>String</i>.  For example, pattern string <code>"'{''}'"</code> is
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   104
 * interpreted as a sequence of <code>'{</code> (start of quoting and a
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   105
 * left curly brace), <code>''</code> (a single quote), and
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   106
 * <code>}'</code> (a right curly brace and end of quoting),
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   107
 * <em>not</em> <code>'{'</code> and <code>'}'</code> (quoted left and
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   108
 * right curly braces): representing string <code>"{'}"</code>,
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   109
 * <em>not</em> <code>"{}"</code>.
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   110
 *
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   111
 * <p>A <i>SubformatPattern</i> is interpreted by its corresponding
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   112
 * subformat, and subformat-dependent pattern rules apply. For example,
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   113
 * pattern string <code>"{1,number,<u>$'#',##</u>}"</code>
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   114
 * (<i>SubformatPattern</i> with underline) will produce a number format
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   115
 * with the pound-sign quoted, with a result such as: {@code
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   116
 * "$#31,45"}. Refer to each {@code Format} subclass documentation for
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   117
 * details.
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   118
 *
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   119
 * <p>Any unmatched quote is treated as closed at the end of the given
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   120
 * pattern. For example, pattern string {@code "'{0}"} is treated as
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   121
 * pattern {@code "'{0}'"}.
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   122
 *
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   123
 * <p>Any curly braces within an unquoted pattern must be balanced. For
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   124
 * example, <code>"ab {0} de"</code> and <code>"ab '}' de"</code> are
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   125
 * valid patterns, but <code>"ab {0'}' de"</code>, <code>"ab } de"</code>
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   126
 * and <code>"''{''"</code> are not.
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   127
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * <dl><dt><b>Warning:</b><dd>The rules for using quotes within message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * format patterns unfortunately have shown to be somewhat confusing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * In particular, it isn't always obvious to localizers whether single
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * quotes need to be doubled or not. Make sure to inform localizers about
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * the rules, and tell them (for example, by using comments in resource
7780
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   133
 * bundle source files) which strings will be processed by {@code MessageFormat}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * Note that localizers may need to use single quotes in translated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * strings where the original version doesn't have them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * The <i>ArgumentIndex</i> value is a non-negative integer written
7780
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   139
 * using the digits {@code '0'} through {@code '9'}, and represents an index into the
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   140
 * {@code arguments} array passed to the {@code format} methods
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   141
 * or the result array returned by the {@code parse} methods.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * The <i>FormatType</i> and <i>FormatStyle</i> values are used to create
7780
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   144
 * a {@code Format} instance for the format element. The following
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   145
 * table shows how the values map to {@code Format} instances. Combinations not
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * shown in the table are illegal. A <i>SubformatPattern</i> must
7780
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   147
 * be a valid pattern string for the {@code Format} subclass used.
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19054
diff changeset
   148
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * <table border=1 summary="Shows how FormatType and FormatStyle values map to Format instances">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 *    <tr>
7780
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   151
 *       <th id="ft" class="TableHeadingColor">FormatType
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   152
 *       <th id="fs" class="TableHeadingColor">FormatStyle
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   153
 *       <th id="sc" class="TableHeadingColor">Subformat Created
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 *    <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 *       <td headers="ft"><i>(none)</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 *       <td headers="fs"><i>(none)</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 *       <td headers="sc"><code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 *    <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 *       <td headers="ft" rowspan=5><code>number</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 *       <td headers="fs"><i>(none)</i>
7780
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   161
 *       <td headers="sc">{@link NumberFormat#getInstance(Locale) NumberFormat.getInstance}{@code (getLocale())}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 *    <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 *       <td headers="fs"><code>integer</code>
7780
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   164
 *       <td headers="sc">{@link NumberFormat#getIntegerInstance(Locale) NumberFormat.getIntegerInstance}{@code (getLocale())}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 *    <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 *       <td headers="fs"><code>currency</code>
7780
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   167
 *       <td headers="sc">{@link NumberFormat#getCurrencyInstance(Locale) NumberFormat.getCurrencyInstance}{@code (getLocale())}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 *    <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 *       <td headers="fs"><code>percent</code>
7780
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   170
 *       <td headers="sc">{@link NumberFormat#getPercentInstance(Locale) NumberFormat.getPercentInstance}{@code (getLocale())}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 *    <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 *       <td headers="fs"><i>SubformatPattern</i>
7780
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   173
 *       <td headers="sc">{@code new} {@link DecimalFormat#DecimalFormat(String,DecimalFormatSymbols) DecimalFormat}{@code (subformatPattern,} {@link DecimalFormatSymbols#getInstance(Locale) DecimalFormatSymbols.getInstance}{@code (getLocale()))}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 *    <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 *       <td headers="ft" rowspan=6><code>date</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 *       <td headers="fs"><i>(none)</i>
7780
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   177
 *       <td headers="sc">{@link DateFormat#getDateInstance(int,Locale) DateFormat.getDateInstance}{@code (}{@link DateFormat#DEFAULT}{@code , getLocale())}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 *    <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 *       <td headers="fs"><code>short</code>
7780
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   180
 *       <td headers="sc">{@link DateFormat#getDateInstance(int,Locale) DateFormat.getDateInstance}{@code (}{@link DateFormat#SHORT}{@code , getLocale())}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 *    <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 *       <td headers="fs"><code>medium</code>
7780
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   183
 *       <td headers="sc">{@link DateFormat#getDateInstance(int,Locale) DateFormat.getDateInstance}{@code (}{@link DateFormat#DEFAULT}{@code , getLocale())}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 *    <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 *       <td headers="fs"><code>long</code>
7780
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   186
 *       <td headers="sc">{@link DateFormat#getDateInstance(int,Locale) DateFormat.getDateInstance}{@code (}{@link DateFormat#LONG}{@code , getLocale())}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 *    <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 *       <td headers="fs"><code>full</code>
7780
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   189
 *       <td headers="sc">{@link DateFormat#getDateInstance(int,Locale) DateFormat.getDateInstance}{@code (}{@link DateFormat#FULL}{@code , getLocale())}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 *    <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 *       <td headers="fs"><i>SubformatPattern</i>
7780
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   192
 *       <td headers="sc">{@code new} {@link SimpleDateFormat#SimpleDateFormat(String,Locale) SimpleDateFormat}{@code (subformatPattern, getLocale())}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 *    <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
 *       <td headers="ft" rowspan=6><code>time</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 *       <td headers="fs"><i>(none)</i>
7780
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   196
 *       <td headers="sc">{@link DateFormat#getTimeInstance(int,Locale) DateFormat.getTimeInstance}{@code (}{@link DateFormat#DEFAULT}{@code , getLocale())}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
 *    <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 *       <td headers="fs"><code>short</code>
7780
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   199
 *       <td headers="sc">{@link DateFormat#getTimeInstance(int,Locale) DateFormat.getTimeInstance}{@code (}{@link DateFormat#SHORT}{@code , getLocale())}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 *    <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 *       <td headers="fs"><code>medium</code>
7780
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   202
 *       <td headers="sc">{@link DateFormat#getTimeInstance(int,Locale) DateFormat.getTimeInstance}{@code (}{@link DateFormat#DEFAULT}{@code , getLocale())}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 *    <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 *       <td headers="fs"><code>long</code>
7780
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   205
 *       <td headers="sc">{@link DateFormat#getTimeInstance(int,Locale) DateFormat.getTimeInstance}{@code (}{@link DateFormat#LONG}{@code , getLocale())}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 *    <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 *       <td headers="fs"><code>full</code>
7780
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   208
 *       <td headers="sc">{@link DateFormat#getTimeInstance(int,Locale) DateFormat.getTimeInstance}{@code (}{@link DateFormat#FULL}{@code , getLocale())}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
 *    <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
 *       <td headers="fs"><i>SubformatPattern</i>
7780
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   211
 *       <td headers="sc">{@code new} {@link SimpleDateFormat#SimpleDateFormat(String,Locale) SimpleDateFormat}{@code (subformatPattern, getLocale())}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
 *    <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
 *       <td headers="ft"><code>choice</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
 *       <td headers="fs"><i>SubformatPattern</i>
7780
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   215
 *       <td headers="sc">{@code new} {@link ChoiceFormat#ChoiceFormat(String) ChoiceFormat}{@code (subformatPattern)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
 * <h4>Usage Information</h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
 * Here are some examples of usage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
 * In real internationalized programs, the message format pattern and other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
 * static strings will, of course, be obtained from resource bundles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
 * Other parameters will be dynamically determined at runtime.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
 * The first example uses the static method <code>MessageFormat.format</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
 * which internally creates a <code>MessageFormat</code> for one-time use:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
 * int planet = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
 * String event = "a disturbance in the Force";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
 * String result = MessageFormat.format(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
 *     "At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
 *     planet, new Date(), event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
 * The output is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
 * At 12:30 PM on Jul 3, 2053, there was a disturbance in the Force on planet 7.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
 * The following example creates a <code>MessageFormat</code> instance that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
 * can be used repeatedly:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
 * int fileCount = 1273;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
 * String diskName = "MyDisk";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
 * Object[] testArgs = {new Long(fileCount), diskName};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
 * MessageFormat form = new MessageFormat(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
 *     "The disk \"{1}\" contains {0} file(s).");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
 * System.out.println(form.format(testArgs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
 * The output with different values for <code>fileCount</code>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
 * The disk "MyDisk" contains 0 file(s).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
 * The disk "MyDisk" contains 1 file(s).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
 * The disk "MyDisk" contains 1,273 file(s).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
 * For more sophisticated patterns, you can use a <code>ChoiceFormat</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
 * to produce correct forms for singular and plural:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
 * MessageFormat form = new MessageFormat("The disk \"{1}\" contains {0}.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
 * double[] filelimits = {0,1,2};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
 * String[] filepart = {"no files","one file","{0,number} files"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
 * ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
 * form.setFormatByArgumentIndex(0, fileform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
 * int fileCount = 1273;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
 * String diskName = "MyDisk";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
 * Object[] testArgs = {new Long(fileCount), diskName};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
 * System.out.println(form.format(testArgs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
 * The output with different values for <code>fileCount</code>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
 * The disk "MyDisk" contains no files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
 * The disk "MyDisk" contains one file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
 * The disk "MyDisk" contains 1,273 files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
 * You can create the <code>ChoiceFormat</code> programmatically, as in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
 * above example, or by using a pattern. See {@link ChoiceFormat}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
 * for more information.
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 16010
diff changeset
   288
 * <blockquote><pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
 * form.applyPattern(
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 16010
diff changeset
   290
 *    "There {0,choice,0#are no files|1#is one file|1<are {0,number,integer} files}.");
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 16010
diff changeset
   291
 * }</pre></blockquote>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
 * <strong>Note:</strong> As we see above, the string produced
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
 * by a <code>ChoiceFormat</code> in <code>MessageFormat</code> is treated as special;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
 * occurrences of '{' are used to indicate subformats, and cause recursion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
 * If you create both a <code>MessageFormat</code> and <code>ChoiceFormat</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
 * programmatically (instead of using the string patterns), then be careful not to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
 * produce a format that recurses on itself, which will cause an infinite loop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
 * When a single argument is parsed more than once in the string, the last match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
 * will be the final result of the parsing.  For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
 * MessageFormat mf = new MessageFormat("{0,number,#.##}, {0,number,#.#}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
 * Object[] objs = {new Double(3.1415)};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
 * String result = mf.format( objs );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
 * // result now equals "3.14, 3.1"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
 * objs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
 * objs = mf.parse(result, new ParsePosition(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
 * // objs now equals {new Double(3.1)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
 * <p>
7780
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   314
 * Likewise, parsing with a {@code MessageFormat} object using patterns containing
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
 * multiple occurrences of the same argument would return the last match.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
 * example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
 * MessageFormat mf = new MessageFormat("{0}, {0}, {0}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
 * String forParsing = "x, y, z";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
 * Object[] objs = mf.parse(forParsing, new ParsePosition(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
 * // result now equals {new String("z")}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
 * <h4><a name="synchronization">Synchronization</a></h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
 * Message formats are not synchronized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
 * It is recommended to create separate format instances for each thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
 * If multiple threads access a format concurrently, it must be synchronized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
 * externally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
 * @see          java.util.Locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
 * @see          Format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
 * @see          NumberFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
 * @see          DecimalFormat
7780
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   336
 * @see          DecimalFormatSymbols
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
 * @see          ChoiceFormat
7780
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   338
 * @see          DateFormat
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   339
 * @see          SimpleDateFormat
fc2905d8201f 6983207: API: MessageFormat quote handling discrepancy
okutsu
parents: 6489
diff changeset
   340
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
 * @author       Mark Davis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
public class MessageFormat extends Format {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    private static final long serialVersionUID = 6479157306784022952L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    /**
16010
2727163b5df5 7092447: Clarify the default locale used in each locale sensitive operation
naoto
parents: 14342
diff changeset
   349
     * Constructs a MessageFormat for the default
2727163b5df5 7092447: Clarify the default locale used in each locale sensitive operation
naoto
parents: 14342
diff changeset
   350
     * {@link java.util.Locale.Category#FORMAT FORMAT} locale and the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * specified pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * The constructor first sets the locale, then parses the pattern and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * creates a list of subformats for the format elements contained in it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * Patterns and their interpretation are specified in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * <a href="#patterns">class description</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @param pattern the pattern for this message format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @exception IllegalArgumentException if the pattern is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    public MessageFormat(String pattern) {
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   361
        this.locale = Locale.getDefault(Locale.Category.FORMAT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        applyPattern(pattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * Constructs a MessageFormat for the specified locale and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * The constructor first sets the locale, then parses the pattern and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * creates a list of subformats for the format elements contained in it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * Patterns and their interpretation are specified in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * <a href="#patterns">class description</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @param pattern the pattern for this message format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @param locale the locale for this message format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * @exception IllegalArgumentException if the pattern is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    public MessageFormat(String pattern, Locale locale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        this.locale = locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        applyPattern(pattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * Sets the locale to be used when creating or comparing subformats.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * This affects subsequent calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * <li>to the {@link #applyPattern applyPattern}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *     and {@link #toPattern toPattern} methods if format elements specify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     *     a format type and therefore have the subformats created in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *     <code>applyPattern</code> method, as well as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * <li>to the <code>format</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *     {@link #formatToCharacterIterator formatToCharacterIterator} methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     *     if format elements do not specify a format type and therefore have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *     the subformats created in the formatting methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * Subformats that have already been created are not affected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @param locale the locale to be used when creating or comparing subformats
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    public void setLocale(Locale locale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        this.locale = locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * Gets the locale that's used when creating or comparing subformats.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @return the locale used when creating or comparing subformats
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    public Locale getLocale() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        return locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * Sets the pattern used by this message format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * The method parses the pattern and creates a list of subformats
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * for the format elements contained in it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * Patterns and their interpretation are specified in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * <a href="#patterns">class description</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * @param pattern the pattern for this message format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @exception IllegalArgumentException if the pattern is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     */
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 7793
diff changeset
   424
    @SuppressWarnings("fallthrough") // fallthrough in switch is expected, suppress it
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    public void applyPattern(String pattern) {
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   426
            StringBuilder[] segments = new StringBuilder[4];
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   427
            // Allocate only segments[SEG_RAW] here. The rest are
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   428
            // allocated on demand.
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   429
            segments[SEG_RAW] = new StringBuilder();
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   430
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   431
            int part = SEG_RAW;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            int formatNumber = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            boolean inQuote = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            int braceStack = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            maxOffset = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            for (int i = 0; i < pattern.length(); ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                char ch = pattern.charAt(i);
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   438
                if (part == SEG_RAW) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                    if (ch == '\'') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                        if (i + 1 < pattern.length()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                            && pattern.charAt(i+1) == '\'') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                            segments[part].append(ch);  // handle doubles
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                            ++i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                            inQuote = !inQuote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    } else if (ch == '{' && !inQuote) {
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   448
                        part = SEG_INDEX;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   449
                        if (segments[SEG_INDEX] == null) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   450
                            segments[SEG_INDEX] = new StringBuilder();
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   451
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                        segments[part].append(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    }
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   455
                } else  {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   456
                    if (inQuote) {              // just copy quotes in parts
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                        segments[part].append(ch);
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   458
                        if (ch == '\'') {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   459
                            inQuote = false;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   460
                        }
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   461
                    } else {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   462
                        switch (ch) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   463
                        case ',':
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   464
                            if (part < SEG_MODIFIER) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   465
                                if (segments[++part] == null) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   466
                                    segments[part] = new StringBuilder();
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   467
                                }
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   468
                            } else {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   469
                                segments[part].append(ch);
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   470
                            }
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   471
                            break;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   472
                        case '{':
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   473
                            ++braceStack;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                            segments[part].append(ch);
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   475
                            break;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   476
                        case '}':
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   477
                            if (braceStack == 0) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   478
                                part = SEG_RAW;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   479
                                makeFormat(i, formatNumber, segments);
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   480
                                formatNumber++;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   481
                                // throw away other segments
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   482
                                segments[SEG_INDEX] = null;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   483
                                segments[SEG_TYPE] = null;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   484
                                segments[SEG_MODIFIER] = null;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   485
                            } else {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   486
                                --braceStack;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   487
                                segments[part].append(ch);
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   488
                            }
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   489
                            break;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   490
                        case ' ':
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   491
                            // Skip any leading space chars for SEG_TYPE.
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   492
                            if (part != SEG_TYPE || segments[SEG_TYPE].length() > 0) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   493
                                segments[part].append(ch);
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   494
                            }
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   495
                            break;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   496
                        case '\'':
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   497
                            inQuote = true;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   498
                            // fall through, so we keep quotes in other parts
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   499
                        default:
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   500
                            segments[part].append(ch);
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   501
                            break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            if (braceStack == 0 && part != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                maxOffset = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                throw new IllegalArgumentException("Unmatched braces in the pattern.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            this.pattern = segments[0].toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * Returns a pattern representing the current state of the message format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * The string is constructed from internal information and therefore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * does not necessarily equal the previously applied pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * @return a pattern representing the current state of the message format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    public String toPattern() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        // later, make this more extensible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        int lastOffset = 0;
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   524
        StringBuilder result = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        for (int i = 0; i <= maxOffset; ++i) {
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   526
            copyAndFixQuotes(pattern, lastOffset, offsets[i], result);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            lastOffset = offsets[i];
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   528
            result.append('{').append(argumentNumbers[i]);
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   529
            Format fmt = formats[i];
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   530
            if (fmt == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                // do nothing, string format
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   532
            } else if (fmt instanceof NumberFormat) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   533
                if (fmt.equals(NumberFormat.getInstance(locale))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                    result.append(",number");
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   535
                } else if (fmt.equals(NumberFormat.getCurrencyInstance(locale))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                    result.append(",number,currency");
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   537
                } else if (fmt.equals(NumberFormat.getPercentInstance(locale))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                    result.append(",number,percent");
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   539
                } else if (fmt.equals(NumberFormat.getIntegerInstance(locale))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                    result.append(",number,integer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                } else {
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   542
                    if (fmt instanceof DecimalFormat) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   543
                        result.append(",number,").append(((DecimalFormat)fmt).toPattern());
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   544
                    } else if (fmt instanceof ChoiceFormat) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   545
                        result.append(",choice,").append(((ChoiceFormat)fmt).toPattern());
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   546
                    } else {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   547
                        // UNKNOWN
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   548
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                }
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   550
            } else if (fmt instanceof DateFormat) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   551
                int index;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   552
                for (index = MODIFIER_DEFAULT; index < DATE_TIME_MODIFIERS.length; index++) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   553
                    DateFormat df = DateFormat.getDateInstance(DATE_TIME_MODIFIERS[index],
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   554
                                                               locale);
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   555
                    if (fmt.equals(df)) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   556
                        result.append(",date");
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   557
                        break;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   558
                    }
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   559
                    df = DateFormat.getTimeInstance(DATE_TIME_MODIFIERS[index],
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   560
                                                    locale);
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   561
                    if (fmt.equals(df)) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   562
                        result.append(",time");
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   563
                        break;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   564
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                }
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   566
                if (index >= DATE_TIME_MODIFIERS.length) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   567
                    if (fmt instanceof SimpleDateFormat) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   568
                        result.append(",date,").append(((SimpleDateFormat)fmt).toPattern());
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   569
                    } else {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   570
                        // UNKNOWN
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   571
                    }
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   572
                } else if (index != MODIFIER_DEFAULT) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   573
                    result.append(',').append(DATE_TIME_MODIFIER_KEYWORDS[index]);
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   574
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                //result.append(", unknown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            result.append('}');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        copyAndFixQuotes(pattern, lastOffset, pattern.length(), result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        return result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * Sets the formats to use for the values passed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * <code>format</code> methods or returned from <code>parse</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * methods. The indices of elements in <code>newFormats</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * correspond to the argument indices used in the previously set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * pattern string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * The order of formats in <code>newFormats</code> thus corresponds to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * the order of elements in the <code>arguments</code> array passed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * to the <code>format</code> methods or the result array returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * by the <code>parse</code> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * If an argument index is used for more than one format element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * in the pattern string, then the corresponding new format is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * for all such format elements. If an argument index is not used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * for any format element in the pattern string, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * corresponding new format is ignored. If fewer formats are provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * than needed, then only the formats for argument indices less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * than <code>newFormats.length</code> are replaced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * @param newFormats the new formats to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * @exception NullPointerException if <code>newFormats</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    public void setFormatsByArgumentIndex(Format[] newFormats) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        for (int i = 0; i <= maxOffset; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            int j = argumentNumbers[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            if (j < newFormats.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                formats[i] = newFormats[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * Sets the formats to use for the format elements in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * previously set pattern string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * The order of formats in <code>newFormats</code> corresponds to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * the order of format elements in the pattern string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * If more formats are provided than needed by the pattern string,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * the remaining ones are ignored. If fewer formats are provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * than needed, then only the first <code>newFormats.length</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * formats are replaced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * Since the order of format elements in a pattern string often
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * changes during localization, it is generally better to use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * {@link #setFormatsByArgumentIndex setFormatsByArgumentIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * method, which assumes an order of formats corresponding to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * order of elements in the <code>arguments</code> array passed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * the <code>format</code> methods or the result array returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * the <code>parse</code> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * @param newFormats the new formats to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * @exception NullPointerException if <code>newFormats</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    public void setFormats(Format[] newFormats) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        int runsToCopy = newFormats.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        if (runsToCopy > maxOffset + 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            runsToCopy = maxOffset + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        for (int i = 0; i < runsToCopy; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            formats[i] = newFormats[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * Sets the format to use for the format elements within the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * previously set pattern string that use the given argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * The argument index is part of the format element definition and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * represents an index into the <code>arguments</code> array passed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * to the <code>format</code> methods or the result array returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * by the <code>parse</code> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * If the argument index is used for more than one format element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * in the pattern string, then the new format is used for all such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * format elements. If the argument index is not used for any format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * element in the pattern string, then the new format is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * @param argumentIndex the argument index for which to use the new format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * @param newFormat the new format to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    public void setFormatByArgumentIndex(int argumentIndex, Format newFormat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        for (int j = 0; j <= maxOffset; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            if (argumentNumbers[j] == argumentIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                formats[j] = newFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * Sets the format to use for the format element with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * format element index within the previously set pattern string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * The format element index is the zero-based number of the format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * element counting from the start of the pattern string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * Since the order of format elements in a pattern string often
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * changes during localization, it is generally better to use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * {@link #setFormatByArgumentIndex setFormatByArgumentIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * method, which accesses format elements based on the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * index they specify.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * @param formatElementIndex the index of a format element within the pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * @param newFormat the format to use for the specified format element
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   688
     * @exception ArrayIndexOutOfBoundsException if {@code formatElementIndex} is equal to or
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     *            larger than the number of format elements in the pattern string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    public void setFormat(int formatElementIndex, Format newFormat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        formats[formatElementIndex] = newFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * Gets the formats used for the values passed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * <code>format</code> methods or returned from <code>parse</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * methods. The indices of elements in the returned array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * correspond to the argument indices used in the previously set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * pattern string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * The order of formats in the returned array thus corresponds to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * the order of elements in the <code>arguments</code> array passed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * to the <code>format</code> methods or the result array returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * by the <code>parse</code> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * If an argument index is used for more than one format element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * in the pattern string, then the format used for the last such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * format element is returned in the array. If an argument index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * is not used for any format element in the pattern string, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * null is returned in the array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * @return the formats used for the arguments within the pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    public Format[] getFormatsByArgumentIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        int maximumArgumentNumber = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        for (int i = 0; i <= maxOffset; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            if (argumentNumbers[i] > maximumArgumentNumber) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                maximumArgumentNumber = argumentNumbers[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        Format[] resultArray = new Format[maximumArgumentNumber + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        for (int i = 0; i <= maxOffset; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            resultArray[argumentNumbers[i]] = formats[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        return resultArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * Gets the formats used for the format elements in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * previously set pattern string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * The order of formats in the returned array corresponds to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * the order of format elements in the pattern string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * Since the order of format elements in a pattern string often
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * changes during localization, it's generally better to use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * {@link #getFormatsByArgumentIndex getFormatsByArgumentIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * method, which assumes an order of formats corresponding to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * order of elements in the <code>arguments</code> array passed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * the <code>format</code> methods or the result array returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * the <code>parse</code> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * @return the formats used for the format elements in the pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    public Format[] getFormats() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        Format[] resultArray = new Format[maxOffset + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        System.arraycopy(formats, 0, resultArray, 0, maxOffset + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        return resultArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * Formats an array of objects and appends the <code>MessageFormat</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * pattern, with format elements replaced by the formatted objects, to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * provided <code>StringBuffer</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * The text substituted for the individual format elements is derived from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * the current subformat of the format element and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * <code>arguments</code> element at the format element's argument index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * as indicated by the first matching line of the following table. An
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * argument is <i>unavailable</i> if <code>arguments</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * <code>null</code> or has fewer than argumentIndex+1 elements.
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19054
diff changeset
   762
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * <table border=1 summary="Examples of subformat,argument,and formatted text">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     *    <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     *       <th>Subformat
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     *       <th>Argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     *       <th>Formatted Text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     *    <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     *       <td><i>any</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     *       <td><i>unavailable</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     *       <td><code>"{" + argumentIndex + "}"</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     *    <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     *       <td><i>any</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     *       <td><code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     *       <td><code>"null"</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     *    <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     *       <td><code>instanceof ChoiceFormat</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     *       <td><i>any</i>
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 16010
diff changeset
   779
     *       <td><code>subformat.format(argument).indexOf('{') &gt;= 0 ?<br>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     *           (new MessageFormat(subformat.format(argument), getLocale())).format(argument) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     *           subformat.format(argument)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     *    <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     *       <td><code>!= null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     *       <td><i>any</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     *       <td><code>subformat.format(argument)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     *    <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     *       <td><code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     *       <td><code>instanceof Number</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     *       <td><code>NumberFormat.getInstance(getLocale()).format(argument)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     *    <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     *       <td><code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     *       <td><code>instanceof Date</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     *       <td><code>DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, getLocale()).format(argument)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     *    <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     *       <td><code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     *       <td><code>instanceof String</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     *       <td><code>argument</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     *    <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     *       <td><code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     *       <td><i>any</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     *       <td><code>argument.toString()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * If <code>pos</code> is non-null, and refers to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * <code>Field.ARGUMENT</code>, the location of the first formatted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * string will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * @param arguments an array of objects to be formatted and substituted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * @param result where text is appended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * @param pos On input: an alignment field, if desired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     *            On output: the offsets of the alignment field.
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 16010
diff changeset
   812
     * @return the string buffer passed in as {@code result}, with formatted
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 16010
diff changeset
   813
     * text appended
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     * @exception IllegalArgumentException if an argument in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     *            <code>arguments</code> array is not of the type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     *            expected by the format element(s) that use it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    public final StringBuffer format(Object[] arguments, StringBuffer result,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                                     FieldPosition pos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        return subformat(arguments, result, pos, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * Creates a MessageFormat with the given pattern and uses it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * to format the given arguments. This is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     *     <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>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     *
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 16010
diff changeset
   831
     * @param pattern   the pattern string
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 16010
diff changeset
   832
     * @param arguments object(s) to format
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 16010
diff changeset
   833
     * @return the formatted string
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * @exception IllegalArgumentException if the pattern is invalid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     *            or if an argument in the <code>arguments</code> array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     *            is not of the type expected by the format element(s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     *            that use it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    public static String format(String pattern, Object ... arguments) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        MessageFormat temp = new MessageFormat(pattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        return temp.format(arguments);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    // Overrides
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * Formats an array of objects and appends the <code>MessageFormat</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * pattern, with format elements replaced by the formatted objects, to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * provided <code>StringBuffer</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * This is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     *     <code>{@link #format(java.lang.Object[], java.lang.StringBuffer, java.text.FieldPosition) format}((Object[]) arguments, result, pos)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * @param arguments an array of objects to be formatted and substituted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * @param result where text is appended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * @param pos On input: an alignment field, if desired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     *            On output: the offsets of the alignment field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * @exception IllegalArgumentException if an argument in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     *            <code>arguments</code> array is not of the type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     *            expected by the format element(s) that use it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    public final StringBuffer format(Object arguments, StringBuffer result,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                                     FieldPosition pos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        return subformat((Object[]) arguments, result, pos, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * Formats an array of objects and inserts them into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * <code>MessageFormat</code>'s pattern, producing an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * <code>AttributedCharacterIterator</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * You can use the returned <code>AttributedCharacterIterator</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * to build the resulting String, as well as to determine information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * about the resulting String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * The text of the returned <code>AttributedCharacterIterator</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * the same that would be returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     *     <code>{@link #format(java.lang.Object[], java.lang.StringBuffer, java.text.FieldPosition) format}(arguments, new StringBuffer(), null).toString()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * In addition, the <code>AttributedCharacterIterator</code> contains at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * least attributes indicating where text was generated from an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * argument in the <code>arguments</code> array. The keys of these attributes are of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * type <code>MessageFormat.Field</code>, their values are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * <code>Integer</code> objects indicating the index in the <code>arguments</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * array of the argument from which the text was generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * The attributes/value from the underlying <code>Format</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * instances that <code>MessageFormat</code> uses will also be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * placed in the resulting <code>AttributedCharacterIterator</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * This allows you to not only find where an argument is placed in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * resulting String, but also which fields it contains in turn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * @param arguments an array of objects to be formatted and substituted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * @return AttributedCharacterIterator describing the formatted value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * @exception NullPointerException if <code>arguments</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * @exception IllegalArgumentException if an argument in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     *            <code>arguments</code> array is not of the type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     *            expected by the format element(s) that use it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    public AttributedCharacterIterator formatToCharacterIterator(Object arguments) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        StringBuffer result = new StringBuffer();
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 7793
diff changeset
   905
        ArrayList<AttributedCharacterIterator> iterators = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        if (arguments == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            throw new NullPointerException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                   "formatToCharacterIterator must be passed non-null object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        subformat((Object[]) arguments, result, null, iterators);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        if (iterators.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            return createAttributedCharacterIterator("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        return createAttributedCharacterIterator(
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 7793
diff changeset
   916
                     iterators.toArray(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                     new AttributedCharacterIterator[iterators.size()]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * Parses the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * <p>Caveats: The parse may fail in a number of circumstances.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * For example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * <li>If one of the arguments does not occur in the pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * <li>If the format of an argument loses information, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     *     with a choice format where a large number formats to "many".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * <li>Does not yet handle recursion (where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     *     the substituted strings contain {n} references.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * <li>Will not always find a match (or the correct match)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     *     if some part of the parse is ambiguous.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     *     For example, if the pattern "{1},{2}" is used with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     *     string arguments {"a,b", "c"}, it will format as "a,b,c".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     *     When the result is parsed, it will return {"a", "b,c"}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * <li>If a single argument is parsed more than once in the string,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     *     then the later parse wins.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * When the parse fails, use ParsePosition.getErrorIndex() to find out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * where in the string the parsing failed.  The returned error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * index is the starting offset of the sub-patterns that the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * is comparing with.  For example, if the parsing string "AAA {0} BBB"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * is comparing against the pattern "AAD {0} BBB", the error index is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * 0. When an error occurs, the call to this method will return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * If the source is null, return an empty array.
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 16010
diff changeset
   946
     *
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 16010
diff changeset
   947
     * @param source the string to parse
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 16010
diff changeset
   948
     * @param pos    the parse position
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 16010
diff changeset
   949
     * @return an array of parsed objects
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    public Object[] parse(String source, ParsePosition pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        if (source == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            Object[] empty = {};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            return empty;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        int maximumArgumentNumber = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        for (int i = 0; i <= maxOffset; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            if (argumentNumbers[i] > maximumArgumentNumber) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                maximumArgumentNumber = argumentNumbers[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        Object[] resultArray = new Object[maximumArgumentNumber + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        int patternOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        int sourceOffset = pos.index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        ParsePosition tempStatus = new ParsePosition(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        for (int i = 0; i <= maxOffset; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            // match up to format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            int len = offsets[i] - patternOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            if (len == 0 || pattern.regionMatches(patternOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                                                  source, sourceOffset, len)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                sourceOffset += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                patternOffset += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                pos.errorIndex = sourceOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                return null; // leave index as is to signal error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            // now use format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            if (formats[i] == null) {   // string format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                // if at end, use longest possible match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                // otherwise uses first match to intervening string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                // does NOT recursively try all possibilities
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                int tempLength = (i != maxOffset) ? offsets[i+1] : pattern.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                int next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                if (patternOffset >= tempLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                    next = source.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                }else{
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   991
                    next = source.indexOf(pattern.substring(patternOffset, tempLength),
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
   992
                                          sourceOffset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                if (next < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                    pos.errorIndex = sourceOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                    return null; // leave index as is to signal error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                    String strValue= source.substring(sourceOffset,next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                    if (!strValue.equals("{"+argumentNumbers[i]+"}"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                        resultArray[argumentNumbers[i]]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                            = source.substring(sourceOffset,next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                    sourceOffset = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                tempStatus.index = sourceOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                resultArray[argumentNumbers[i]]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                    = formats[i].parseObject(source,tempStatus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                if (tempStatus.index == sourceOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                    pos.errorIndex = sourceOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                    return null; // leave index as is to signal error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                sourceOffset = tempStatus.index; // update
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        int len = pattern.length() - patternOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        if (len == 0 || pattern.regionMatches(patternOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                                              source, sourceOffset, len)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            pos.index = sourceOffset + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            pos.errorIndex = sourceOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            return null; // leave index as is to signal error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        return resultArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * Parses text from the beginning of the given string to produce an object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * The method may not use the entire text of the given string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * See the {@link #parse(String, ParsePosition)} method for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     * on message parsing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     * @param source A <code>String</code> whose beginning should be parsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     * @return An <code>Object</code> array parsed from the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     * @exception ParseException if the beginning of the specified string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     *            cannot be parsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
    public Object[] parse(String source) throws ParseException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        ParsePosition pos  = new ParsePosition(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        Object[] result = parse(source, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        if (pos.index == 0)  // unchanged, returned object is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
            throw new ParseException("MessageFormat parse error!", pos.errorIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * Parses text from a string to produce an object array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * The method attempts to parse text starting at the index given by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * <code>pos</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * If parsing succeeds, then the index of <code>pos</code> is updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * to the index after the last character used (parsing does not necessarily
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * use all characters up to the end of the string), and the parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * object array is returned. The updated <code>pos</code> can be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * indicate the starting point for the next call to this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * If an error occurs, then the index of <code>pos</code> is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * changed, the error index of <code>pos</code> is set to the index of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * the character where the error occurred, and null is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * See the {@link #parse(String, ParsePosition)} method for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     * on message parsing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * @param source A <code>String</code>, part of which should be parsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * @param pos A <code>ParsePosition</code> object with index and error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     *            index information as described above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     * @return An <code>Object</code> array parsed from the string. In case of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     *         error, returns null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     * @exception NullPointerException if <code>pos</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
    public Object parseObject(String source, ParsePosition pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        return parse(source, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * Creates and returns a copy of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * @return a clone of this instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        MessageFormat other = (MessageFormat) super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        // clone arrays. Can't do with utility because of bug in Cloneable
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 7793
diff changeset
  1086
        other.formats = formats.clone(); // shallow clone
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        for (int i = 0; i < formats.length; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
            if (formats[i] != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                other.formats[i] = (Format)formats[i].clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        // for primitives or immutables, shallow clone is enough
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 7793
diff changeset
  1092
        other.offsets = offsets.clone();
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 7793
diff changeset
  1093
        other.argumentNumbers = argumentNumbers.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        return other;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * Equality comparison between two message format objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        if (this == obj)                      // quick check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        if (obj == null || getClass() != obj.getClass())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        MessageFormat other = (MessageFormat) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        return (maxOffset == other.maxOffset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                && pattern.equals(other.pattern)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                && ((locale != null && locale.equals(other.locale))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                 || (locale == null && other.locale == null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                && Arrays.equals(offsets,other.offsets)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                && Arrays.equals(argumentNumbers,other.argumentNumbers)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                && Arrays.equals(formats,other.formats));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     * Generates a hash code for the message format object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        return pattern.hashCode(); // enough for reasonable distribution
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * Defines constants that are used as attribute keys in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * <code>AttributedCharacterIterator</code> returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     * from <code>MessageFormat.formatToCharacterIterator</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    public static class Field extends Format.Field {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        // Proclaim serial compatibility with 1.4 FCS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        private static final long serialVersionUID = 7899943957617360810L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
         * Creates a Field with the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
         * @param name Name of the attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        protected Field(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
            super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
         * Resolves instances being deserialized to the predefined constants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
         * @throws InvalidObjectException if the constant could not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
         *         resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
         * @return resolved MessageFormat.Field constant
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        protected Object readResolve() throws InvalidObjectException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
            if (this.getClass() != MessageFormat.Field.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
                throw new InvalidObjectException("subclass didn't correctly implement readResolve");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            return ARGUMENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        // The constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
         * Constant identifying a portion of a message that was generated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
         * from an argument passed into <code>formatToCharacterIterator</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
         * The value associated with the key will be an <code>Integer</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
         * indicating the index in the <code>arguments</code> array of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
         * argument from which the text was generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        public final static Field ARGUMENT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
                           new Field("message argument field");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
    // ===========================privates============================
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     * The locale to use for formatting numbers and dates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
    private Locale locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * The string that the formatted values are to be plugged into.  In other words, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     * is the pattern supplied on construction with all of the {} expressions taken out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
    private String pattern = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
    /** The initially expected number of subformats in the format */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
    private static final int INITIAL_FORMATS = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     * An array of formatters, which are used to format the arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
    private Format[] formats = new Format[INITIAL_FORMATS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * The positions where the results of formatting each argument are to be inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     * into the pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
    private int[] offsets = new int[INITIAL_FORMATS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     * The argument numbers corresponding to each formatter.  (The formatters are stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     * in the order they occur in the pattern, not in the order in which the arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * are specified.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
    private int[] argumentNumbers = new int[INITIAL_FORMATS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     * One less than the number of entries in <code>offsets</code>.  Can also be thought of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * as the index of the highest-numbered element in <code>offsets</code> that is being used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     * All of these arrays should have the same number of elements being used as <code>offsets</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     * does, and so this variable suffices to tell us how many entries are in all of them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
    private int maxOffset = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     * Internal routine used by format. If <code>characterIterators</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
     * non-null, AttributedCharacterIterator will be created from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     * subformats as necessary. If <code>characterIterators</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     * and <code>fp</code> is non-null and identifies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     * <code>Field.MESSAGE_ARGUMENT</code>, the location of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     * the first replaced argument will be set in it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     * @exception IllegalArgumentException if an argument in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     *            <code>arguments</code> array is not of the type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     *            expected by the format element(s) that use it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
    private StringBuffer subformat(Object[] arguments, StringBuffer result,
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 7793
diff changeset
  1236
                                   FieldPosition fp, List<AttributedCharacterIterator> characterIterators) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        // note: this implementation assumes a fast substring & index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        // if this is not true, would be better to append chars one by one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        int lastOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        int last = result.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        for (int i = 0; i <= maxOffset; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
            result.append(pattern.substring(lastOffset, offsets[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
            lastOffset = offsets[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
            int argumentNumber = argumentNumbers[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
            if (arguments == null || argumentNumber >= arguments.length) {
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1246
                result.append('{').append(argumentNumber).append('}');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
            // int argRecursion = ((recursionProtection >> (argumentNumber*2)) & 0x3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
            if (false) { // if (argRecursion == 3){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
                // prevent loop!!!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
                result.append('\uFFFD');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                Object obj = arguments[argumentNumber];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
                String arg = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                Format subFormatter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
                if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
                    arg = "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
                } else if (formats[i] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
                    subFormatter = formats[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
                    if (subFormatter instanceof ChoiceFormat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
                        arg = formats[i].format(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
                        if (arg.indexOf('{') >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
                            subFormatter = new MessageFormat(arg, locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
                            obj = arguments;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
                            arg = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
                } else if (obj instanceof Number) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
                    // format number if can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
                    subFormatter = NumberFormat.getInstance(locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
                } else if (obj instanceof Date) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                    // format a Date if can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
                    subFormatter = DateFormat.getDateTimeInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
                             DateFormat.SHORT, DateFormat.SHORT, locale);//fix
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                } else if (obj instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                    arg = (String) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                    arg = obj.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                    if (arg == null) arg = "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                // At this point we are in two states, either subFormatter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                // is non-null indicating we should format obj using it,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                // or arg is non-null and we should use it as the value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                if (characterIterators != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
                    // If characterIterators is non-null, it indicates we need
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
                    // to get the CharacterIterator from the child formatter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
                    if (last != result.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
                        characterIterators.add(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
                            createAttributedCharacterIterator(result.substring
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
                                                              (last)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                        last = result.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
                    if (subFormatter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
                        AttributedCharacterIterator subIterator =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
                                   subFormatter.formatToCharacterIterator(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
                        append(result, subIterator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                        if (last != result.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
                            characterIterators.add(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
                                         createAttributedCharacterIterator(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
                                         subIterator, Field.ARGUMENT,
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
  1306
                                         Integer.valueOf(argumentNumber)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
                            last = result.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
                        arg = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
                    if (arg != null && arg.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
                        result.append(arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                        characterIterators.add(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                                 createAttributedCharacterIterator(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                                 arg, Field.ARGUMENT,
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
  1316
                                 Integer.valueOf(argumentNumber)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
                        last = result.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                    if (subFormatter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                        arg = subFormatter.format(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                    last = result.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
                    result.append(arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
                    if (i == 0 && fp != null && Field.ARGUMENT.equals(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
                                  fp.getFieldAttribute())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                        fp.setBeginIndex(last);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                        fp.setEndIndex(result.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                    last = result.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        result.append(pattern.substring(lastOffset, pattern.length()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        if (characterIterators != null && last != result.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
            characterIterators.add(createAttributedCharacterIterator(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
                                   result.substring(last)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
     * Convenience method to append all the characters in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     * <code>iterator</code> to the StringBuffer <code>result</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
    private void append(StringBuffer result, CharacterIterator iterator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
        if (iterator.first() != CharacterIterator.DONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
            char aChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
            result.append(iterator.first());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
            while ((aChar = iterator.next()) != CharacterIterator.DONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
                result.append(aChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1358
    // Indices for segments
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1359
    private static final int SEG_RAW      = 0;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1360
    private static final int SEG_INDEX    = 1;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1361
    private static final int SEG_TYPE     = 2;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1362
    private static final int SEG_MODIFIER = 3; // modifier or subformat
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1363
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1364
    // Indices for type keywords
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1365
    private static final int TYPE_NULL    = 0;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1366
    private static final int TYPE_NUMBER  = 1;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1367
    private static final int TYPE_DATE    = 2;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1368
    private static final int TYPE_TIME    = 3;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1369
    private static final int TYPE_CHOICE  = 4;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1370
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1371
    private static final String[] TYPE_KEYWORDS = {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1372
        "",
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1373
        "number",
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1374
        "date",
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1375
        "time",
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1376
        "choice"
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1377
    };
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1378
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1379
    // Indices for number modifiers
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1380
    private static final int MODIFIER_DEFAULT  = 0; // common in number and date-time
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1381
    private static final int MODIFIER_CURRENCY = 1;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1382
    private static final int MODIFIER_PERCENT  = 2;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1383
    private static final int MODIFIER_INTEGER  = 3;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1384
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1385
    private static final String[] NUMBER_MODIFIER_KEYWORDS = {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1386
        "",
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1387
        "currency",
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1388
        "percent",
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1389
        "integer"
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1390
    };
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1391
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1392
    // Indices for date-time modifiers
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1393
    private static final int MODIFIER_SHORT   = 1;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1394
    private static final int MODIFIER_MEDIUM  = 2;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1395
    private static final int MODIFIER_LONG    = 3;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1396
    private static final int MODIFIER_FULL    = 4;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1397
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1398
    private static final String[] DATE_TIME_MODIFIER_KEYWORDS = {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1399
        "",
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1400
        "short",
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1401
        "medium",
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1402
        "long",
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1403
        "full"
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1404
    };
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1405
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1406
    // Date-time style values corresponding to the date-time modifiers.
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1407
    private static final int[] DATE_TIME_MODIFIERS = {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1408
        DateFormat.DEFAULT,
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1409
        DateFormat.SHORT,
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1410
        DateFormat.MEDIUM,
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1411
        DateFormat.LONG,
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1412
        DateFormat.FULL,
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1413
    };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
    private void makeFormat(int position, int offsetNumber,
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1416
                            StringBuilder[] textSegments)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
    {
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1418
        String[] segments = new String[textSegments.length];
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1419
        for (int i = 0; i < textSegments.length; i++) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1420
            StringBuilder oneseg = textSegments[i];
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1421
            segments[i] = (oneseg != null) ? oneseg.toString() : "";
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1422
        }
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1423
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
        // get the argument number
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
        int argumentNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
        try {
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1427
            argumentNumber = Integer.parseInt(segments[SEG_INDEX]); // always unlocalized!
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        } catch (NumberFormatException e) {
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1429
            throw new IllegalArgumentException("can't parse argument number: "
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1430
                                               + segments[SEG_INDEX], e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
        if (argumentNumber < 0) {
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1433
            throw new IllegalArgumentException("negative argument number: "
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1434
                                               + argumentNumber);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
        // resize format information arrays if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
        if (offsetNumber >= formats.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            int newLength = formats.length * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
            Format[] newFormats = new Format[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
            int[] newOffsets = new int[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
            int[] newArgumentNumbers = new int[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
            System.arraycopy(formats, 0, newFormats, 0, maxOffset + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
            System.arraycopy(offsets, 0, newOffsets, 0, maxOffset + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
            System.arraycopy(argumentNumbers, 0, newArgumentNumbers, 0, maxOffset + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
            formats = newFormats;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
            offsets = newOffsets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
            argumentNumbers = newArgumentNumbers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
        int oldMaxOffset = maxOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
        maxOffset = offsetNumber;
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1452
        offsets[offsetNumber] = segments[SEG_RAW].length();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
        argumentNumbers[offsetNumber] = argumentNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
        // now get the format
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
        Format newFormat = null;
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1457
        if (segments[SEG_TYPE].length() != 0) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1458
            int type = findKeyword(segments[SEG_TYPE], TYPE_KEYWORDS);
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1459
            switch (type) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1460
            case TYPE_NULL:
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1461
                // Type "" is allowed. e.g., "{0,}", "{0,,}", and "{0,,#}"
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1462
                // are treated as "{0}".
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
                break;
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1464
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1465
            case TYPE_NUMBER:
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1466
                switch (findKeyword(segments[SEG_MODIFIER], NUMBER_MODIFIER_KEYWORDS)) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1467
                case MODIFIER_DEFAULT:
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1468
                    newFormat = NumberFormat.getInstance(locale);
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1469
                    break;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1470
                case MODIFIER_CURRENCY:
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1471
                    newFormat = NumberFormat.getCurrencyInstance(locale);
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1472
                    break;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1473
                case MODIFIER_PERCENT:
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1474
                    newFormat = NumberFormat.getPercentInstance(locale);
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1475
                    break;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1476
                case MODIFIER_INTEGER:
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1477
                    newFormat = NumberFormat.getIntegerInstance(locale);
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1478
                    break;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1479
                default: // DecimalFormat pattern
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1480
                    try {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1481
                        newFormat = new DecimalFormat(segments[SEG_MODIFIER],
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1482
                                                      DecimalFormatSymbols.getInstance(locale));
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1483
                    } catch (IllegalArgumentException e) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1484
                        maxOffset = oldMaxOffset;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1485
                        throw e;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1486
                    }
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1487
                    break;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1488
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
                break;
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1490
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1491
            case TYPE_DATE:
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1492
            case TYPE_TIME:
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1493
                int mod = findKeyword(segments[SEG_MODIFIER], DATE_TIME_MODIFIER_KEYWORDS);
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1494
                if (mod >= 0 && mod < DATE_TIME_MODIFIER_KEYWORDS.length) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1495
                    if (type == TYPE_DATE) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1496
                        newFormat = DateFormat.getDateInstance(DATE_TIME_MODIFIERS[mod],
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1497
                                                               locale);
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1498
                    } else {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1499
                        newFormat = DateFormat.getTimeInstance(DATE_TIME_MODIFIERS[mod],
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1500
                                                               locale);
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1501
                    }
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1502
                } else {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1503
                    // SimpleDateFormat pattern
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1504
                    try {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1505
                        newFormat = new SimpleDateFormat(segments[SEG_MODIFIER], locale);
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1506
                    } catch (IllegalArgumentException e) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1507
                        maxOffset = oldMaxOffset;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1508
                        throw e;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1509
                    }
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1510
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
                break;
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1512
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1513
            case TYPE_CHOICE:
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1514
                try {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1515
                    // ChoiceFormat pattern
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1516
                    newFormat = new ChoiceFormat(segments[SEG_MODIFIER]);
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1517
                } catch (Exception e) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1518
                    maxOffset = oldMaxOffset;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1519
                    throw new IllegalArgumentException("Choice Pattern incorrect: "
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1520
                                                       + segments[SEG_MODIFIER], e);
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1521
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
                break;
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1523
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
            default:
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1525
                maxOffset = oldMaxOffset;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1526
                throw new IllegalArgumentException("unknown format type: " +
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1527
                                                   segments[SEG_TYPE]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
        formats[offsetNumber] = newFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
    private static final int findKeyword(String s, String[] list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
        for (int i = 0; i < list.length; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
            if (s.equals(list[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
                return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
        }
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1538
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1539
        // Try trimmed lowercase.
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1540
        String ls = s.trim().toLowerCase(Locale.ROOT);
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1541
        if (ls != s) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1542
            for (int i = 0; i < list.length; ++i) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1543
                if (ls.equals(list[i]))
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1544
                    return i;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1545
            }
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1546
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1550
    private static final void copyAndFixQuotes(String source, int start, int end,
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1551
                                               StringBuilder target) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1552
        boolean quoted = false;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1553
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
        for (int i = start; i < end; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
            char ch = source.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
            if (ch == '{') {
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1557
                if (!quoted) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1558
                    target.append('\'');
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1559
                    quoted = true;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1560
                }
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1561
                target.append(ch);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
            } else if (ch == '\'') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
                target.append("''");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
            } else {
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1565
                if (quoted) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1566
                    target.append('\'');
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1567
                    quoted = false;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1568
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
                target.append(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
        }
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1572
        if (quoted) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1573
            target.append('\'');
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 7780
diff changeset
  1574
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
     * After reading an object from the input stream, do a simple verification
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
     * to maintain class invariants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
     * @throws InvalidObjectException if the objects read from the stream is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
        in.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
        boolean isValid = maxOffset >= -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
                && formats.length > maxOffset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
                && offsets.length > maxOffset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
                && argumentNumbers.length > maxOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
        if (isValid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
            int lastOffset = pattern.length() + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
            for (int i = maxOffset; i >= 0; --i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
                if ((offsets[i] < 0) || (offsets[i] > lastOffset)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
                    isValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
                    lastOffset = offsets[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
        if (!isValid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
            throw new InvalidObjectException("Could not reconstruct MessageFormat from corrupt stream.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
}