mirror of git://gcc.gnu.org/git/gcc.git
2004-05-05 Guilhem Lavaux <guilhem@kaffe.org>
* java/text/SimpleDateFormat.java: (formatWithAttribute): New method. It implements the formatting process with attributes. (format): Use formatWithAttribute. (formatToCharacterIterator): New method. Use formatWithAttribute. From-SVN: r81493
This commit is contained in:
parent
1f64614267
commit
2225a42a9e
|
|
@ -1,3 +1,12 @@
|
||||||
|
2004-05-05 Guilhem Lavaux <guilhem@kaffe.org>
|
||||||
|
|
||||||
|
* java/text/SimpleDateFormat.java:
|
||||||
|
(formatWithAttribute): New method. It implements
|
||||||
|
the formatting process with attributes.
|
||||||
|
(format): Use formatWithAttribute.
|
||||||
|
(formatToCharacterIterator): New method. Use
|
||||||
|
formatWithAttribute.
|
||||||
|
|
||||||
2004-05-05 Guilhem Lavaux <guilhem@kaffe.org>
|
2004-05-05 Guilhem Lavaux <guilhem@kaffe.org>
|
||||||
|
|
||||||
* java/text/MessageFormat.java:
|
* java/text/MessageFormat.java:
|
||||||
|
|
|
||||||
|
|
@ -39,16 +39,21 @@ exception statement from your version. */
|
||||||
|
|
||||||
package java.text;
|
package java.text;
|
||||||
|
|
||||||
|
import gnu.java.text.AttributedFormatBuffer;
|
||||||
|
import gnu.java.text.FormatBuffer;
|
||||||
|
import gnu.java.text.FormatCharacterIterator;
|
||||||
|
import gnu.java.text.StringFormatBuffer;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.GregorianCalendar;
|
import java.util.GregorianCalendar;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.TimeZone;
|
|
||||||
import java.util.SimpleTimeZone;
|
import java.util.SimpleTimeZone;
|
||||||
import java.io.ObjectInputStream;
|
import java.util.TimeZone;
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SimpleDateFormat provides convenient methods for parsing and formatting
|
* SimpleDateFormat provides convenient methods for parsing and formatting
|
||||||
|
|
@ -82,7 +87,7 @@ public class SimpleDateFormat extends DateFormat
|
||||||
// This string is specified in the JCL. We set it here rather than
|
// This string is specified in the JCL. We set it here rather than
|
||||||
// do a DateFormatSymbols(Locale.US).getLocalPatternChars() since
|
// do a DateFormatSymbols(Locale.US).getLocalPatternChars() since
|
||||||
// someone could theoretically change those values (though unlikely).
|
// someone could theoretically change those values (though unlikely).
|
||||||
private static final String standardChars = "GyMdkHmsSEDFwWahKz";
|
private static final String standardChars = "GyMdkHmsSEDFwWahKzZ";
|
||||||
|
|
||||||
private void readObject(ObjectInputStream stream)
|
private void readObject(ObjectInputStream stream)
|
||||||
throws IOException, ClassNotFoundException
|
throws IOException, ClassNotFoundException
|
||||||
|
|
@ -411,109 +416,164 @@ public class SimpleDateFormat extends DateFormat
|
||||||
* appending to the specified StringBuffer. The input StringBuffer
|
* appending to the specified StringBuffer. The input StringBuffer
|
||||||
* is returned as output for convenience.
|
* is returned as output for convenience.
|
||||||
*/
|
*/
|
||||||
public StringBuffer format(Date date, StringBuffer buffer, FieldPosition pos)
|
final private void formatWithAttribute(Date date, FormatBuffer buffer, FieldPosition pos)
|
||||||
{
|
{
|
||||||
String temp;
|
String temp;
|
||||||
|
AttributedCharacterIterator.Attribute attribute;
|
||||||
calendar.setTime(date);
|
calendar.setTime(date);
|
||||||
|
|
||||||
// go through ArrayList, filling in fields where applicable, else toString
|
// go through vector, filling in fields where applicable, else toString
|
||||||
Iterator i = tokens.iterator();
|
Iterator iter = tokens.iterator();
|
||||||
while (i.hasNext()) {
|
while (iter.hasNext())
|
||||||
Object o = i.next();
|
{
|
||||||
if (o instanceof FieldSizePair) {
|
Object o = iter.next();
|
||||||
FieldSizePair p = (FieldSizePair) o;
|
if (o instanceof FieldSizePair)
|
||||||
int beginIndex = buffer.length();
|
|
||||||
switch (p.field) {
|
|
||||||
case ERA_FIELD:
|
|
||||||
buffer.append(formatData.eras[calendar.get(Calendar.ERA)]);
|
|
||||||
break;
|
|
||||||
case YEAR_FIELD:
|
|
||||||
// If we have two digits, then we truncate. Otherwise, we
|
|
||||||
// use the size of the pattern, and zero pad.
|
|
||||||
if (p.size == 2)
|
|
||||||
{
|
|
||||||
temp = String.valueOf(calendar.get(Calendar.YEAR));
|
|
||||||
buffer.append(temp.substring(temp.length() - 2));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
withLeadingZeros(calendar.get(Calendar.YEAR), p.size, buffer);
|
|
||||||
break;
|
|
||||||
case MONTH_FIELD:
|
|
||||||
if (p.size < 3)
|
|
||||||
withLeadingZeros(calendar.get(Calendar.MONTH)+1,p.size,buffer);
|
|
||||||
else if (p.size < 4)
|
|
||||||
buffer.append(formatData.shortMonths[calendar.get(Calendar.MONTH)]);
|
|
||||||
else
|
|
||||||
buffer.append(formatData.months[calendar.get(Calendar.MONTH)]);
|
|
||||||
break;
|
|
||||||
case DATE_FIELD:
|
|
||||||
withLeadingZeros(calendar.get(Calendar.DATE),p.size,buffer);
|
|
||||||
break;
|
|
||||||
case HOUR_OF_DAY1_FIELD: // 1-24
|
|
||||||
withLeadingZeros(((calendar.get(Calendar.HOUR_OF_DAY)+23)%24)+1,p.size,buffer);
|
|
||||||
break;
|
|
||||||
case HOUR_OF_DAY0_FIELD: // 0-23
|
|
||||||
withLeadingZeros(calendar.get(Calendar.HOUR_OF_DAY),p.size,buffer);
|
|
||||||
break;
|
|
||||||
case MINUTE_FIELD:
|
|
||||||
withLeadingZeros(calendar.get(Calendar.MINUTE),p.size,buffer);
|
|
||||||
break;
|
|
||||||
case SECOND_FIELD:
|
|
||||||
withLeadingZeros(calendar.get(Calendar.SECOND),p.size,buffer);
|
|
||||||
break;
|
|
||||||
case MILLISECOND_FIELD:
|
|
||||||
withLeadingZeros(calendar.get(Calendar.MILLISECOND),p.size,buffer);
|
|
||||||
break;
|
|
||||||
case DAY_OF_WEEK_FIELD:
|
|
||||||
if (p.size < 4)
|
|
||||||
buffer.append(formatData.shortWeekdays[calendar.get(Calendar.DAY_OF_WEEK)]);
|
|
||||||
else
|
|
||||||
buffer.append(formatData.weekdays[calendar.get(Calendar.DAY_OF_WEEK)]);
|
|
||||||
break;
|
|
||||||
case DAY_OF_YEAR_FIELD:
|
|
||||||
withLeadingZeros(calendar.get(Calendar.DAY_OF_YEAR),p.size,buffer);
|
|
||||||
break;
|
|
||||||
case DAY_OF_WEEK_IN_MONTH_FIELD:
|
|
||||||
withLeadingZeros(calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH),p.size,buffer);
|
|
||||||
break;
|
|
||||||
case WEEK_OF_YEAR_FIELD:
|
|
||||||
withLeadingZeros(calendar.get(Calendar.WEEK_OF_YEAR),p.size,buffer);
|
|
||||||
break;
|
|
||||||
case WEEK_OF_MONTH_FIELD:
|
|
||||||
withLeadingZeros(calendar.get(Calendar.WEEK_OF_MONTH),p.size,buffer);
|
|
||||||
break;
|
|
||||||
case AM_PM_FIELD:
|
|
||||||
buffer.append(formatData.ampms[calendar.get(Calendar.AM_PM)]);
|
|
||||||
break;
|
|
||||||
case HOUR1_FIELD: // 1-12
|
|
||||||
withLeadingZeros(((calendar.get(Calendar.HOUR)+11)%12)+1,p.size,buffer);
|
|
||||||
break;
|
|
||||||
case HOUR0_FIELD: // 0-11
|
|
||||||
withLeadingZeros(calendar.get(Calendar.HOUR),p.size,buffer);
|
|
||||||
break;
|
|
||||||
case TIMEZONE_FIELD:
|
|
||||||
TimeZone zone = calendar.getTimeZone();
|
|
||||||
boolean isDST = calendar.get(Calendar.DST_OFFSET) != 0;
|
|
||||||
// FIXME: XXX: This should be a localized time zone.
|
|
||||||
String zoneID = zone.getDisplayName(isDST, p.size > 3 ? TimeZone.LONG : TimeZone.SHORT);
|
|
||||||
buffer.append(zoneID);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new IllegalArgumentException("Illegal pattern character");
|
|
||||||
}
|
|
||||||
if (pos != null && p.field == pos.getField())
|
|
||||||
{
|
{
|
||||||
pos.setBeginIndex(beginIndex);
|
FieldSizePair p = (FieldSizePair) o;
|
||||||
pos.setEndIndex(buffer.length());
|
int beginIndex = buffer.length();
|
||||||
}
|
|
||||||
} else {
|
switch (p.field)
|
||||||
buffer.append(o.toString());
|
{
|
||||||
|
case ERA_FIELD:
|
||||||
|
buffer.append (formatData.eras[calendar.get (Calendar.ERA)], DateFormat.Field.ERA);
|
||||||
|
break;
|
||||||
|
case YEAR_FIELD:
|
||||||
|
// If we have two digits, then we truncate. Otherwise, we
|
||||||
|
// use the size of the pattern, and zero pad.
|
||||||
|
buffer.setDefaultAttribute (DateFormat.Field.YEAR);
|
||||||
|
if (p.size == 2)
|
||||||
|
{
|
||||||
|
temp = String.valueOf (calendar.get (Calendar.YEAR));
|
||||||
|
buffer.append (temp.substring (temp.length() - 2));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
withLeadingZeros (calendar.get (Calendar.YEAR), p.size, buffer);
|
||||||
|
break;
|
||||||
|
case MONTH_FIELD:
|
||||||
|
buffer.setDefaultAttribute (DateFormat.Field.MONTH);
|
||||||
|
if (p.size < 3)
|
||||||
|
withLeadingZeros (calendar.get (Calendar.MONTH) + 1, p.size, buffer);
|
||||||
|
else if (p.size < 4)
|
||||||
|
buffer.append (formatData.shortMonths[calendar.get (Calendar.MONTH)]);
|
||||||
|
else
|
||||||
|
buffer.append (formatData.months[calendar.get (Calendar.MONTH)]);
|
||||||
|
break;
|
||||||
|
case DATE_FIELD:
|
||||||
|
buffer.setDefaultAttribute (DateFormat.Field.DAY_OF_MONTH);
|
||||||
|
withLeadingZeros (calendar.get (Calendar.DATE), p.size, buffer);
|
||||||
|
break;
|
||||||
|
case HOUR_OF_DAY1_FIELD: // 1-24
|
||||||
|
buffer.setDefaultAttribute(DateFormat.Field.HOUR_OF_DAY1);
|
||||||
|
withLeadingZeros ( ((calendar.get (Calendar.HOUR_OF_DAY) + 23) % 24) + 1,
|
||||||
|
p.size, buffer);
|
||||||
|
break;
|
||||||
|
case HOUR_OF_DAY0_FIELD: // 0-23
|
||||||
|
buffer.setDefaultAttribute (DateFormat.Field.HOUR_OF_DAY0);
|
||||||
|
withLeadingZeros (calendar.get (Calendar.HOUR_OF_DAY), p.size, buffer);
|
||||||
|
break;
|
||||||
|
case MINUTE_FIELD:
|
||||||
|
buffer.setDefaultAttribute (DateFormat.Field.MINUTE);
|
||||||
|
withLeadingZeros (calendar.get (Calendar.MINUTE),
|
||||||
|
p.size, buffer);
|
||||||
|
break;
|
||||||
|
case SECOND_FIELD:
|
||||||
|
buffer.setDefaultAttribute (DateFormat.Field.SECOND);
|
||||||
|
withLeadingZeros(calendar.get (Calendar.SECOND),
|
||||||
|
p.size, buffer);
|
||||||
|
break;
|
||||||
|
case MILLISECOND_FIELD:
|
||||||
|
buffer.setDefaultAttribute (DateFormat.Field.MILLISECOND);
|
||||||
|
withLeadingZeros (calendar.get (Calendar.MILLISECOND), p.size, buffer);
|
||||||
|
break;
|
||||||
|
case DAY_OF_WEEK_FIELD:
|
||||||
|
buffer.setDefaultAttribute (DateFormat.Field.DAY_OF_WEEK);
|
||||||
|
if (p.size < 4)
|
||||||
|
buffer.append (formatData.shortWeekdays[calendar.get (Calendar.DAY_OF_WEEK)]);
|
||||||
|
else
|
||||||
|
buffer.append (formatData.weekdays[calendar.get (Calendar.DAY_OF_WEEK)]);
|
||||||
|
break;
|
||||||
|
case DAY_OF_YEAR_FIELD:
|
||||||
|
buffer.setDefaultAttribute (DateFormat.Field.DAY_OF_YEAR);
|
||||||
|
withLeadingZeros (calendar.get (Calendar.DAY_OF_YEAR), p.size, buffer);
|
||||||
|
break;
|
||||||
|
case DAY_OF_WEEK_IN_MONTH_FIELD:
|
||||||
|
buffer.setDefaultAttribute (DateFormat.Field.DAY_OF_WEEK_IN_MONTH);
|
||||||
|
withLeadingZeros (calendar.get (Calendar.DAY_OF_WEEK_IN_MONTH),
|
||||||
|
p.size, buffer);
|
||||||
|
break;
|
||||||
|
case WEEK_OF_YEAR_FIELD:
|
||||||
|
buffer.setDefaultAttribute (DateFormat.Field.WEEK_OF_YEAR);
|
||||||
|
withLeadingZeros (calendar.get (Calendar.WEEK_OF_YEAR),
|
||||||
|
p.size, buffer);
|
||||||
|
break;
|
||||||
|
case WEEK_OF_MONTH_FIELD:
|
||||||
|
buffer.setDefaultAttribute (DateFormat.Field.WEEK_OF_MONTH);
|
||||||
|
withLeadingZeros (calendar.get (Calendar.WEEK_OF_MONTH),
|
||||||
|
p.size, buffer);
|
||||||
|
break;
|
||||||
|
case AM_PM_FIELD:
|
||||||
|
buffer.setDefaultAttribute (DateFormat.Field.AM_PM);
|
||||||
|
buffer.append (formatData.ampms[calendar.get (Calendar.AM_PM)]);
|
||||||
|
break;
|
||||||
|
case HOUR1_FIELD: // 1-12
|
||||||
|
buffer.setDefaultAttribute (DateFormat.Field.HOUR1);
|
||||||
|
withLeadingZeros (((calendar.get (Calendar.HOUR) + 11) % 12) + 1, p.size, buffer);
|
||||||
|
break;
|
||||||
|
case HOUR0_FIELD: // 0-11
|
||||||
|
buffer.setDefaultAttribute (DateFormat.Field.HOUR0);
|
||||||
|
withLeadingZeros (calendar.get (Calendar.HOUR), p.size, buffer);
|
||||||
|
break;
|
||||||
|
case TIMEZONE_FIELD:
|
||||||
|
buffer.setDefaultAttribute (DateFormat.Field.TIME_ZONE);
|
||||||
|
TimeZone zone = calendar.getTimeZone();
|
||||||
|
boolean isDST = calendar.get (Calendar.DST_OFFSET) != 0;
|
||||||
|
// FIXME: XXX: This should be a localized time zone.
|
||||||
|
String zoneID = zone.getDisplayName (isDST, p.size > 3 ? TimeZone.LONG : TimeZone.SHORT);
|
||||||
|
buffer.append (zoneID);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new IllegalArgumentException ("Illegal pattern character " + p.field);
|
||||||
|
}
|
||||||
|
if (pos != null && (buffer.getDefaultAttribute() == pos.getFieldAttribute()
|
||||||
|
|| p.field == pos.getField()))
|
||||||
|
{
|
||||||
|
pos.setBeginIndex(beginIndex);
|
||||||
|
pos.setEndIndex(buffer.length());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
buffer.append(o.toString(), null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public StringBuffer format(Date date, StringBuffer buffer, FieldPosition pos)
|
||||||
|
{
|
||||||
|
formatWithAttribute(date, new StringFormatBuffer (buffer), pos);
|
||||||
|
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void withLeadingZeros(int value, int length, StringBuffer buffer)
|
public AttributedCharacterIterator formatToCharacterIterator(Object date)
|
||||||
|
throws IllegalArgumentException
|
||||||
|
{
|
||||||
|
if (date == null)
|
||||||
|
throw new NullPointerException("null argument");
|
||||||
|
if (!(date instanceof Date))
|
||||||
|
throw new IllegalArgumentException("argument should be an instance of java.util.Date");
|
||||||
|
|
||||||
|
AttributedFormatBuffer buf = new AttributedFormatBuffer();
|
||||||
|
formatWithAttribute((Date)date, buf,
|
||||||
|
null);
|
||||||
|
buf.sync();
|
||||||
|
|
||||||
|
return new FormatCharacterIterator(buf.getBuffer().toString(),
|
||||||
|
buf.getRanges(),
|
||||||
|
buf.getAttributes());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void withLeadingZeros(int value, int length, FormatBuffer buffer)
|
||||||
{
|
{
|
||||||
String valStr = String.valueOf(value);
|
String valStr = String.valueOf(value);
|
||||||
for (length -= valStr.length(); length > 0; length--)
|
for (length -= valStr.length(); length > 0; length--)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue