⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 messageformat.java

📁 java源代码 请看看啊 提点宝贵的意见
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/* * @(#)MessageFormat.java	1.51 03/01/23 * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. *//* * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved * *   The original version of this source code and documentation is copyrighted * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These * materials are provided under terms of a License Agreement between Taligent * and Sun. This technology is protected by multiple US and International * patents. This notice and attribution to Taligent may not be removed. *   Taligent is a registered trademark of Taligent, Inc. * */package java.text;import java.io.InvalidObjectException;import java.io.IOException;import java.io.ObjectInputStream;import java.text.DecimalFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;import java.util.Locale;import sun.text.Utility;/** * <code>MessageFormat</code> provides a means to produce concatenated * messages in language-neutral way. Use this to construct messages * displayed for end users. * * <p> * <code>MessageFormat</code> takes a set of objects, formats them, then * inserts the formatted strings into the pattern at the appropriate places. * * <p> * <strong>Note:</strong> * <code>MessageFormat</code> differs from the other <code>Format</code> * classes in that you create a <code>MessageFormat</code> object with one * of its constructors (not with a <code>getInstance</code> style factory * method). The factory methods aren't necessary because <code>MessageFormat</code> * itself doesn't implement locale specific behavior. Any locale specific * behavior is defined by the pattern that you provide as well as the * subformats used for inserted arguments. * * <h4><a name="patterns">Patterns and Their Interpretation</a></h4> * * <code>MessageFormat</code> uses patterns of the following form: * <blockquote><pre> * <i>MessageFormatPattern:</i> *         <i>String</i> *         <i>MessageFormatPattern</i> <i>FormatElement</i> <i>String</i> * * <i>FormatElement:</i> *         { <i>ArgumentIndex</i> } *         { <i>ArgumentIndex</i> , <i>FormatType</i> } *         { <i>ArgumentIndex</i> , <i>FormatType</i> , <i>FormatStyle</i> } * * <i>FormatType: one of </i> *         number date time choice * * <i>FormatStyle:</i> *         short *         medium *         long *         full *         integer *         currency *         percent *         <i>SubformatPattern</i> * * <i>String:</i> *         <i>StringPart<sub>opt</sub></i> *         <i>String</i> <i>StringPart</i> * * <i>StringPart:</i> *         '' *         ' <i>QuotedString</i> ' *         <i>UnquotedString</i> * * <i>SubformatPattern:</i> *         <i>SubformatPatternPart<sub>opt</sub></i> *         <i>SubformatPattern</i> <i>SubformatPatternPart</i> * * <i>SubFormatPatternPart:</i> *         ' <i>QuotedPattern</i> ' *         <i>UnquotedPattern</i> * </pre></blockquote> * * <p> * Within a <i>String</i>, <code>"''"</code> represents a single * quote. A <i>QuotedString</i> can contain arbitrary characters * except single quotes; the surrounding single quotes are removed. * An <i>UnquotedString</i> can contain arbitrary characters * except single quotes and left curly brackets. Thus, a string that * should result in the formatted message "'{0}'" can be written as * <code>"'''{'0}''"</code> or <code>"'''{0}'''"</code>. * <p> * Within a <i>SubformatPattern</i>, different rules apply. * A <i>QuotedPattern</i> can contain arbitrary characters * except single quotes; but the surrounding single quotes are * <strong>not</strong> removed, so they may be interpreted by the * subformat. For example, <code>"{1,number,$'#',##}"</code> will * produce a number format with the pound-sign quoted, with a result * such as: "$#31,45". * An <i>UnquotedPattern</i> can contain arbitrary characters * except single quotes, but curly braces within it must be balanced. * For example, <code>"ab {0} de"</code> and <code>"ab '}' de"</code> * are valid subformat patterns, but <code>"ab {0'}' de"</code> and * <code>"ab } de"</code> are not. * <p> * <dl><dt><b>Warning:</b><dd>The rules for using quotes within message * format patterns unfortunately have shown to be somewhat confusing. * In particular, it isn't always obvious to localizers whether single * quotes need to be doubled or not. Make sure to inform localizers about * the rules, and tell them (for example, by using comments in resource * bundle source files) which strings will be processed by MessageFormat. * Note that localizers may need to use single quotes in translated * strings where the original version doesn't have them. * </dl> * <p> * The <i>ArgumentIndex</i> value is a non-negative integer written * using the digits '0' through '9', and represents an index into the * <code>arguments</code> array passed to the <code>format</code> methods * or the result array returned by the <code>parse</code> methods. * <p> * The <i>FormatType</i> and <i>FormatStyle</i> values are used to create * a <code>Format</code> instance for the format element. The following * table shows how the values map to Format instances. Combinations not * shown in the table are illegal. A <i>SubformatPattern</i> must * be a valid pattern string for the Format subclass used. * <p> * <table border=1 summary="Shows how FormatType and FormatStyle values map to Format instances"> *    <tr> *       <th id="ft">Format Type *       <th id="fs">Format Style *       <th id="sc">Subformat Created *    <tr> *       <td headers="ft"><i>(none)</i> *       <td headers="fs"><i>(none)</i> *       <td headers="sc"><code>null</code> *    <tr> *       <td headers="ft" rowspan=5><code>number</code> *       <td headers="fs"><i>(none)</i> *       <td headers="sc"><code>NumberFormat.getInstance(getLocale())</code> *    <tr> *       <td headers="fs"><code>integer</code> *       <td headers="sc"><code>NumberFormat.getIntegerInstance(getLocale())</code> *    <tr> *       <td headers="fs"><code>currency</code> *       <td headers="sc"><code>NumberFormat.getCurrencyInstance(getLocale())</code> *    <tr> *       <td headers="fs"><code>percent</code> *       <td headers="sc"><code>NumberFormat.getPercentInstance(getLocale())</code> *    <tr> *       <td headers="fs"><i>SubformatPattern</i> *       <td headers="sc"><code>new DecimalFormat(subformatPattern, new DecimalFormatSymbols(getLocale()))</code> *    <tr> *       <td headers="ft" rowspan=6><code>date</code> *       <td headers="fs"><i>(none)</i> *       <td headers="sc"><code>DateFormat.getDateInstance(DateFormat.DEFAULT, getLocale())</code> *    <tr> *       <td headers="fs"><code>short</code> *       <td headers="sc"><code>DateFormat.getDateInstance(DateFormat.SHORT, getLocale())</code> *    <tr> *       <td headers="fs"><code>medium</code> *       <td headers="sc"><code>DateFormat.getDateInstance(DateFormat.DEFAULT, getLocale())</code> *    <tr> *       <td headers="fs"><code>long</code> *       <td headers="sc"><code>DateFormat.getDateInstance(DateFormat.LONG, getLocale())</code> *    <tr> *       <td headers="fs"><code>full</code> *       <td headers="sc"><code>DateFormat.getDateInstance(DateFormat.FULL, getLocale())</code> *    <tr> *       <td headers="fs"><i>SubformatPattern</i> *       <td headers="sc"><code>new SimpleDateFormat(subformatPattern, getLocale()) *    <tr> *       <td headers="ft" rowspan=6><code>time</code> *       <td headers="fs"><i>(none)</i> *       <td headers="sc"><code>DateFormat.getTimeInstance(DateFormat.DEFAULT, getLocale())</code> *    <tr> *       <td headers="fs"><code>short</code> *       <td headers="sc"><code>DateFormat.getTimeInstance(DateFormat.SHORT, getLocale())</code> *    <tr> *       <td headers="fs"><code>medium</code> *       <td headers="sc"><code>DateFormat.getTimeInstance(DateFormat.DEFAULT, getLocale())</code> *    <tr> *       <td headers="fs"><code>long</code> *       <td headers="sc"><code>DateFormat.getTimeInstance(DateFormat.LONG, getLocale())</code> *    <tr> *       <td headers="fs"><code>full</code> *       <td headers="sc"><code>DateFormat.getTimeInstance(DateFormat.FULL, getLocale())</code> *    <tr> *       <td headers="fs"><i>SubformatPattern</i> *       <td headers="sc"><code>new SimpleDateFormat(subformatPattern, getLocale()) *    <tr> *       <td headers="ft"><code>choice</code> *       <td headers="fs"><i>SubformatPattern</i> *       <td headers="sc"><code>new ChoiceFormat(subformatPattern)</code> * </table> * <p> * * <h4>Usage Information</h4> * * <p> * Here are some examples of usage: * <blockquote> * <pre> * Object[] arguments = { *     new Integer(7), *     new Date(System.currentTimeMillis()), *     "a disturbance in the Force" * }; * * String result = MessageFormat.format( *     "At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.", *     arguments); * * <em>output</em>: At 12:30 PM on Jul 3, 2053, there was a disturbance *           in the Force on planet 7. * * </pre> * </blockquote> * Typically, the message format will come from resources, and the * arguments will be dynamically set at runtime. * * <p> * Example 2: * <blockquote> * <pre> * Object[] testArgs = {new Long(3), "MyDisk"}; * * MessageFormat form = new MessageFormat( *     "The disk \"{1}\" contains {0} file(s)."); * * System.out.println(form.format(testArgs)); * * // output, with different testArgs * <em>output</em>: The disk "MyDisk" contains 0 file(s). * <em>output</em>: The disk "MyDisk" contains 1 file(s). * <em>output</em>: The disk "MyDisk" contains 1,273 file(s). * </pre> * </blockquote> * * <p> * For more sophisticated patterns, you can use a <code>ChoiceFormat</code> to get * output such as: * <blockquote> * <pre> * MessageFormat form = new MessageFormat("The disk \"{1}\" contains {0}."); * double[] filelimits = {0,1,2}; * String[] filepart = {"no files","one file","{0,number} files"}; * ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart); * form.setFormatByArgumentIndex(0, fileform); * * Object[] testArgs = {new Long(12373), "MyDisk"}; * * System.out.println(form.format(testArgs)); * * // output, with different testArgs * output: The disk "MyDisk" contains no files. * output: The disk "MyDisk" contains one file. * output: The disk "MyDisk" contains 1,273 files. * </pre> * </blockquote> * You can either do this programmatically, as in the above example, * or by using a pattern (see * {@link ChoiceFormat} * for more information) as in: * <blockquote> * <pre> * form.applyPattern( *    "There {0,choice,0#are no files|1#is one file|1&lt;are {0,number,integer} files}."); * </pre> * </blockquote> * <p> * <strong>Note:</strong> As we see above, the string produced * by a <code>ChoiceFormat</code> in <code>MessageFormat</code> is treated specially; * occurances of '{' are used to indicated subformats, and cause recursion. * If you create both a <code>MessageFormat</code> and <code>ChoiceFormat</code> * programmatically (instead of using the string patterns), then be careful not to * produce a format that recurses on itself, which will cause an infinite loop. * <p> * When a single argument is parsed more than once in the string, the last match * will be the final result of the parsing.  For example, * <pre> * MessageFormat mf = new MessageFormat("{0,number,#.##}, {0,number,#.#}"); * Object[] objs = {new Double(3.1415)}; * String result = mf.format( objs ); * // result now equals "3.14, 3.1" * objs = null;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -