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