Commit 8f0ce8da by Ito Kazumitsu Committed by Michael Koch

2004-07-10 Ito Kazumitsu <kaz@maczuka.gcd.org>

	* java/text/MessageFormat.java
	(formatInternal): Append "{n}" if argument n is unavailable.
	(format(Object, StringBuffer, FieldPosition)): This
	should be equivalent to format(Object[],
	StringBuffer, FieldPosition).

From-SVN: r84448
parent b3520980
2004-07-10 Ito Kazumitsu <kaz@maczuka.gcd.org>
* java/text/MessageFormat.java
(formatInternal): Append "{n}" if argument n is unavailable.
(format(Object, StringBuffer, FieldPosition)): This
should be equivalent to format(Object[],
StringBuffer, FieldPosition).
2004-07-09 Bryce McKinlay <mckinlay@redhat.com> 2004-07-09 Bryce McKinlay <mckinlay@redhat.com>
* java.util.Calendar.java (cache): New private static field. Cached * java.util.Calendar.java (cache): New private static field. Cached
......
/* MessageFormat.java - Localized message formatting. /* MessageFormat.java - Localized message formatting.
Copyright (C) 1999, 2001, 2002 Free Software Foundation, Inc. Copyright (C) 1999, 2001, 2002, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath. This file is part of GNU Classpath.
...@@ -157,7 +157,7 @@ public class MessageFormat extends Format ...@@ -157,7 +157,7 @@ public class MessageFormat extends Format
* This is the attribute set for all characters produced * This is the attribute set for all characters produced
* by MessageFormat during a formatting. * by MessageFormat during a formatting.
*/ */
public static final MessageFormat.Field ARGUMENT = new Field("argument"); public static final MessageFormat.Field ARGUMENT = new MessageFormat.Field("argument");
// For deserialization // For deserialization
private Field() private Field()
...@@ -414,10 +414,13 @@ public class MessageFormat extends Format ...@@ -414,10 +414,13 @@ public class MessageFormat extends Format
for (int i = 0; i < elements.length; ++i) for (int i = 0; i < elements.length; ++i)
{ {
if (elements[i].argNumber >= arguments.length) Object thisArg = null;
throw new IllegalArgumentException("Not enough arguments given"); boolean unavailable = false;
if (arguments == null || elements[i].argNumber >= arguments.length)
unavailable = true;
else
thisArg = arguments[elements[i].argNumber];
Object thisArg = arguments[elements[i].argNumber];
AttributedCharacterIterator iterator = null; AttributedCharacterIterator iterator = null;
Format formatter = null; Format formatter = null;
...@@ -425,22 +428,27 @@ public class MessageFormat extends Format ...@@ -425,22 +428,27 @@ public class MessageFormat extends Format
if (fp != null && i == fp.getField() && fp.getFieldAttribute() == Field.ARGUMENT) if (fp != null && i == fp.getField() && fp.getFieldAttribute() == Field.ARGUMENT)
fp.setBeginIndex(appendBuf.length()); fp.setBeginIndex(appendBuf.length());
if (elements[i].setFormat != null) if (unavailable)
formatter = elements[i].setFormat; appendBuf.append("{" + elements[i].argNumber + "}");
else if (elements[i].format != null) else
{ {
if (elements[i].formatClass != null if (elements[i].setFormat != null)
&& ! elements[i].formatClass.isInstance(thisArg)) formatter = elements[i].setFormat;
throw new IllegalArgumentException("Wrong format class"); else if (elements[i].format != null)
{
if (elements[i].formatClass != null
&& ! elements[i].formatClass.isInstance(thisArg))
throw new IllegalArgumentException("Wrong format class");
formatter = elements[i].format; 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);
} }
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 (fp != null && fp.getField() == i && fp.getFieldAttribute() == Field.ARGUMENT) if (fp != null && fp.getField() == i && fp.getFieldAttribute() == Field.ARGUMENT)
fp.setEndIndex(appendBuf.length()); fp.setEndIndex(appendBuf.length());
...@@ -496,29 +504,18 @@ public class MessageFormat extends Format ...@@ -496,29 +504,18 @@ public class MessageFormat extends Format
} }
/** /**
* Returns the pattern with the formatted objects. * Returns the pattern with the formatted objects. The first argument
* must be a array of Objects.
* This is equivalent to format((Object[]) objectArray, appendBuf, fpos)
* *
* @param source The object to be formatted. * @param objectArray The object array to be formatted.
* @param result The StringBuffer where the text is appened. * @param appendBuf The StringBuffer where the text is appened.
* @param fpos A FieldPosition object (it is ignored). * @param fpos A FieldPosition object (it is ignored).
*/ */
public final StringBuffer format (Object singleArg, StringBuffer appendBuf, public final StringBuffer format (Object objectArray, StringBuffer appendBuf,
FieldPosition fpos) FieldPosition fpos)
{ {
Object[] args; return format ((Object[])objectArray, appendBuf, fpos);
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, fpos);
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment