messageformat.java
来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 621 行 · 第 1/2 页
JAVA
621 行
public boolean equals (Object obj)
{
if (! (obj instanceof MessageFormat))
return false;
MessageFormat mf = (MessageFormat) obj;
return (pattern.equals(mf.pattern)
&& locale.equals(mf.locale));
}
/**
* A convinience method to format patterns.
*
* @param aPattern The pattern used when formatting.
* @param arguments The array containing the objects to be formatted.
*/
public static String format (String pattern, Object arguments[])
{
MessageFormat mf = new MessageFormat (pattern);
StringBuffer sb = new StringBuffer ();
FieldPosition fp = new FieldPosition (NumberFormat.INTEGER_FIELD);
return mf.format(arguments, sb, fp).toString();
}
/**
* Returns the pattern with the formatted objects.
*
* @param source The array containing the objects to be formatted.
* @param result The StringBuffer where the text is appened.
* @param fp A FieldPosition object (it is ignored).
*/
public final StringBuffer format (Object arguments[], StringBuffer appendBuf,
FieldPosition ignore)
{
appendBuf.append(leader);
for (int i = 0; i < elements.length; ++i)
{
if (elements[i].argNumber >= arguments.length)
throw new IllegalArgumentException ();
Object thisArg = arguments[elements[i].argNumber];
Format formatter = null;
if (elements[i].setFormat != null)
formatter = elements[i].setFormat;
else if (elements[i].format != null)
{
if (elements[i].formatClass != null
&& ! elements[i].formatClass.isInstance(thisArg))
throw new IllegalArgumentException ();
formatter = elements[i].format;
}
else if (thisArg instanceof Number)
formatter = NumberFormat.getInstance(locale);
else if (thisArg instanceof Date)
formatter = DateFormat.getTimeInstance(DateFormat.DEFAULT, locale);
else
appendBuf.append(thisArg);
if (formatter != null)
{
// Special-case ChoiceFormat.
if (formatter instanceof ChoiceFormat)
{
StringBuffer buf = new StringBuffer ();
formatter.format(thisArg, buf, ignore);
MessageFormat mf = new MessageFormat ();
mf.setLocale(locale);
mf.applyPattern(buf.toString());
mf.format(arguments, appendBuf, ignore);
}
else
formatter.format(thisArg, appendBuf, ignore);
}
appendBuf.append(elements[i].trailer);
}
return appendBuf;
}
/**
* Returns the pattern with the formatted objects.
*
* @param source The object to be formatted.
* @param result The StringBuffer where the text is appened.
* @param fp A FieldPosition object (it is ignored).
*/
public final StringBuffer format (Object singleArg, StringBuffer appendBuf,
FieldPosition ignore)
{
Object[] args;
if (singleArg instanceof Object[])
{
// This isn't specified in any manual, but it follows the
// JDK implementation.
args = (Object[]) singleArg;
}
else
{
args = new Object[1];
args[0] = singleArg;
}
return format (args, appendBuf, ignore);
}
/**
* Returns an array with the Formats for
* the arguments.
*/
public Format[] getFormats ()
{
Format[] f = new Format[elements.length];
for (int i = elements.length - 1; i >= 0; --i)
f[i] = elements[i].setFormat;
return f;
}
/**
* Returns the locale.
*/
public Locale getLocale ()
{
return locale;
}
/**
* Overrides Format.hashCode()
*/
public int hashCode ()
{
// FIXME: not a very good hash.
return pattern.hashCode() + locale.hashCode();
}
private MessageFormat ()
{
}
/**
* Creates a new MessageFormat object with
* the specified pattern
*
* @param aPattern The Pattern
*/
public MessageFormat (String pattern)
{
locale = Locale.getDefault();
applyPattern (pattern);
}
public Object[] parse (String sourceStr, ParsePosition pos)
{
// Check initial text.
int index = pos.getIndex();
if (! sourceStr.startsWith(leader, index))
{
pos.setErrorIndex(index);
return null;
}
index += leader.length();
Vector results = new Vector (elements.length, 1);
// Now check each format.
for (int i = 0; i < elements.length; ++i)
{
Format formatter = null;
if (elements[i].setFormat != null)
formatter = elements[i].setFormat;
else if (elements[i].format != null)
formatter = elements[i].format;
Object value = null;
if (formatter instanceof ChoiceFormat)
{
// We must special-case a ChoiceFormat because it might
// have recursive formatting.
ChoiceFormat cf = (ChoiceFormat) formatter;
String[] formats = (String[]) cf.getFormats();
double[] limits = (double[]) cf.getLimits();
MessageFormat subfmt = new MessageFormat ();
subfmt.setLocale(locale);
ParsePosition subpos = new ParsePosition (index);
int j;
for (j = 0; value == null && j < limits.length; ++j)
{
subfmt.applyPattern(formats[j]);
subpos.setIndex(index);
value = subfmt.parse(sourceStr, subpos);
}
if (value != null)
{
index = subpos.getIndex();
value = new Double (limits[j]);
}
}
else if (formatter != null)
{
pos.setIndex(index);
value = formatter.parseObject(sourceStr, pos);
if (value != null)
index = pos.getIndex();
}
else
{
// We have a String format. This can lose in a number
// of ways, but we give it a shot.
int next_index = sourceStr.indexOf(elements[i].trailer, index);
if (next_index == -1)
{
pos.setErrorIndex(index);
return null;
}
value = sourceStr.substring(index, next_index);
index = next_index;
}
if (value == null
|| ! sourceStr.startsWith(elements[i].trailer, index))
{
pos.setErrorIndex(index);
return null;
}
if (elements[i].argNumber >= results.size())
results.setSize(elements[i].argNumber + 1);
results.setElementAt(value, elements[i].argNumber);
index += elements[i].trailer.length();
}
Object[] r = new Object[results.size()];
results.copyInto(r);
return r;
}
public Object[] parse (String sourceStr) throws ParseException
{
ParsePosition pp = new ParsePosition (0);
Object[] r = parse (sourceStr, pp);
if (r == null)
throw new ParseException ("couldn't parse string", pp.getErrorIndex());
return r;
}
public Object parseObject (String sourceStr, ParsePosition pos)
{
return parse (sourceStr, pos);
}
/**
* Sets the format for the argument at an specified
* index.
*
* @param index The index.
* @format The Format object.
*/
public void setFormat (int variableNum, Format newFormat)
{
elements[variableNum].setFormat = newFormat;
}
/**
* Sets the formats for the arguments.
*
* @param formats An array of Format objects.
*/
public void setFormats (Format[] newFormats)
{
if (newFormats.length < elements.length)
throw new IllegalArgumentException ();
int len = Math.min(newFormats.length, elements.length);
for (int i = 0; i < len; ++i)
elements[i].setFormat = newFormats[i];
}
/**
* Sets the locale.
*
* @param locale A Locale
*/
public void setLocale (Locale loc)
{
locale = loc;
if (elements != null)
{
for (int i = 0; i < elements.length; ++i)
elements[i].setLocale(loc);
}
}
/**
* Returns the pattern.
*/
public String toPattern ()
{
return pattern;
}
// The pattern string.
private String pattern;
// The locale.
private Locale locale;
// Variables.
private MessageFormatElement[] elements;
// Leader text.
private String leader;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?