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