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