Adds era translation support. Adds T prefix to java.text classes

This commit is contained in:
konsoletyper 2014-06-16 16:38:43 +04:00
parent 23726b8ea0
commit ee5bcf2c32
14 changed files with 169 additions and 99 deletions

View File

@ -36,12 +36,15 @@ public class CLDRHelper {
@MetadataProvider(LikelySubtagsMetadataGenerator.class) @MetadataProvider(LikelySubtagsMetadataGenerator.class)
private static native ResourceMap<StringResource> getLikelySubtagsMap(); private static native ResourceMap<StringResource> getLikelySubtagsMap();
public static String[] resolveEras(String localeCode) { public static String[] resolveEras(String language, String country) {
ResourceMap<ResourceArray<StringResource>> map = getErasMap(); ResourceMap<ResourceArray<StringResource>> map = getErasMap();
ResourceArray<StringResource> arrayRes = map.has(localeCode) ? map.get(localeCode) : map.get("root"); String localeCode = getCode(language, country);
ResourceArray<StringResource> arrayRes = map.has(localeCode) ? map.get(localeCode) :
map.has(language) ? map.get(language) : map.get("root");
return new String[] { arrayRes.get(0).getValue(), arrayRes.get(1).getValue() }; return new String[] { arrayRes.get(0).getValue(), arrayRes.get(1).getValue() };
} }
@MetadataProvider(DateSymbolsMetadataGenerator.class)
private static native ResourceMap<ResourceArray<StringResource>> getErasMap(); private static native ResourceMap<ResourceArray<StringResource>> getErasMap();
@MetadataProvider(LanguageMetadataGenerator.class) @MetadataProvider(LanguageMetadataGenerator.class)

View File

@ -0,0 +1,51 @@
/*
* Copyright 2014 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.impl.unicode;
import java.util.Map;
import org.teavm.model.MethodReference;
import org.teavm.platform.metadata.*;
/**
*
* @author Alexey Andreev
*/
public class DateSymbolsMetadataGenerator implements MetadataGenerator {
@Override
public Resource generateMetadata(MetadataGeneratorContext context, MethodReference method) {
switch (method.getName()) {
case "getErasMap":
return generateEras(context);
default:
throw new AssertionError("Unsupported method: " + method);
}
}
private Resource generateEras(MetadataGeneratorContext context) {
CLDRReader reader = context.getService(CLDRReader.class);
ResourceMap<ResourceArray<StringResource>> result = context.createResourceMap();
for (Map.Entry<String, CLDRLocale> localeEntry : reader.getKnownLocales().entrySet()) {
ResourceArray<StringResource> erasRes = context.createResourceArray();
result.put(localeEntry.getKey(), erasRes);
for (String era : localeEntry.getValue().getEras()) {
StringResource eraRes = context.createResource(StringResource.class);
eraRes.setValue(era);
erasRes.add(eraRes);
}
}
return result;
}
}

View File

@ -17,10 +17,10 @@
package org.teavm.classlib.java.text; package org.teavm.classlib.java.text;
public class Annotation { public class TAnnotation {
private Object value; private Object value;
public Annotation(Object attribute) { public TAnnotation(Object attribute) {
value = attribute; value = attribute;
} }

View File

@ -21,7 +21,7 @@ import org.teavm.classlib.java.io.TSerializable;
import org.teavm.classlib.java.util.TMap; import org.teavm.classlib.java.util.TMap;
import org.teavm.classlib.java.util.TSet; import org.teavm.classlib.java.util.TSet;
public interface AttributedCharacterIterator extends CharacterIterator { public interface TAttributedCharacterIterator extends TCharacterIterator {
public static class Attribute implements TSerializable { public static class Attribute implements TSerializable {
public static final Attribute INPUT_METHOD_SEGMENT = new Attribute( public static final Attribute INPUT_METHOD_SEGMENT = new Attribute(

View File

@ -17,14 +17,14 @@
package org.teavm.classlib.java.text; package org.teavm.classlib.java.text;
import org.teavm.classlib.java.text.AttributedCharacterIterator.Attribute; import org.teavm.classlib.java.text.TAttributedCharacterIterator.Attribute;
import org.teavm.classlib.java.util.*; import org.teavm.classlib.java.util.*;
public class AttributedString { public class TAttributedString {
String text; String text;
TMap<AttributedCharacterIterator.Attribute, TList<Range>> attributeMap; TMap<TAttributedCharacterIterator.Attribute, TList<Range>> attributeMap;
static class Range { static class Range {
int start; int start;
@ -40,22 +40,22 @@ public class AttributedString {
} }
} }
static class AttributedIterator implements AttributedCharacterIterator { static class AttributedIterator implements TAttributedCharacterIterator {
private int begin, end, offset; private int begin, end, offset;
private AttributedString attrString; private TAttributedString attrString;
private THashSet<Attribute> attributesAllowed; private THashSet<Attribute> attributesAllowed;
AttributedIterator(AttributedString attrString) { AttributedIterator(TAttributedString attrString) {
this.attrString = attrString; this.attrString = attrString;
begin = 0; begin = 0;
end = attrString.text.length(); end = attrString.text.length();
offset = 0; offset = 0;
} }
AttributedIterator(AttributedString attrString, AttributedCharacterIterator.Attribute[] attributes, int begin, AttributedIterator(TAttributedString attrString, TAttributedCharacterIterator.Attribute[] attributes, int begin,
int end) { int end) {
if (begin < 0 || end > attrString.text.length() || begin > end) { if (begin < 0 || end > attrString.text.length() || begin > end) {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
@ -120,7 +120,7 @@ public class AttributedString {
} }
private boolean inRange(Range range) { private boolean inRange(Range range) {
if (!(range.value instanceof Annotation)) { if (!(range.value instanceof TAnnotation)) {
return true; return true;
} }
return range.start >= begin && range.start < end && range.end > begin && range.end <= end; return range.start >= begin && range.start < end && range.end > begin && range.end <= end;
@ -131,9 +131,9 @@ public class AttributedString {
while (it.hasNext()) { while (it.hasNext()) {
Range range = it.next(); Range range = it.next();
if (range.start >= begin && range.start < end) { if (range.start >= begin && range.start < end) {
return !(range.value instanceof Annotation) || (range.end > begin && range.end <= end); return !(range.value instanceof TAnnotation) || (range.end > begin && range.end <= end);
} else if (range.end > begin && range.end <= end) { } else if (range.end > begin && range.end <= end) {
return !(range.value instanceof Annotation) || (range.start >= begin && range.start < end); return !(range.value instanceof TAnnotation) || (range.start >= begin && range.start < end);
} }
} }
return false; return false;
@ -171,7 +171,7 @@ public class AttributedString {
} }
@Override @Override
public Object getAttribute(AttributedCharacterIterator.Attribute attribute) { public Object getAttribute(TAttributedCharacterIterator.Attribute attribute) {
if (attributesAllowed != null && !attributesAllowed.contains(attribute)) { if (attributesAllowed != null && !attributesAllowed.contains(attribute)) {
return null; return null;
} }
@ -222,7 +222,7 @@ public class AttributedString {
} }
@Override @Override
public int getRunLimit(AttributedCharacterIterator.Attribute attribute) { public int getRunLimit(TAttributedCharacterIterator.Attribute attribute) {
if (attributesAllowed != null && !attributesAllowed.contains(attribute)) { if (attributesAllowed != null && !attributesAllowed.contains(attribute)) {
return end; return end;
} }
@ -238,7 +238,7 @@ public class AttributedString {
int limit = end; int limit = end;
TIterator<? extends Attribute> it = attributes.iterator(); TIterator<? extends Attribute> it = attributes.iterator();
while (it.hasNext()) { while (it.hasNext()) {
AttributedCharacterIterator.Attribute attribute = it.next(); TAttributedCharacterIterator.Attribute attribute = it.next();
int newLimit = getRunLimit(attribute); int newLimit = getRunLimit(attribute);
if (newLimit < limit) { if (newLimit < limit) {
limit = newLimit; limit = newLimit;
@ -271,7 +271,7 @@ public class AttributedString {
} }
@Override @Override
public int getRunStart(AttributedCharacterIterator.Attribute attribute) { public int getRunStart(TAttributedCharacterIterator.Attribute attribute) {
if (attributesAllowed != null && !attributesAllowed.contains(attribute)) { if (attributesAllowed != null && !attributesAllowed.contains(attribute)) {
return begin; return begin;
} }
@ -287,7 +287,7 @@ public class AttributedString {
int start = begin; int start = begin;
TIterator<? extends Attribute> it = attributes.iterator(); TIterator<? extends Attribute> it = attributes.iterator();
while (it.hasNext()) { while (it.hasNext()) {
AttributedCharacterIterator.Attribute attribute = it.next(); TAttributedCharacterIterator.Attribute attribute = it.next();
int newStart = getRunStart(attribute); int newStart = getRunStart(attribute);
if (newStart > start) { if (newStart > start) {
start = newStart; start = newStart;
@ -335,7 +335,7 @@ public class AttributedString {
} }
} }
public AttributedString(AttributedCharacterIterator iterator) { public TAttributedString(TAttributedCharacterIterator iterator) {
if (iterator.getBeginIndex() > iterator.getEndIndex()) { if (iterator.getBeginIndex() > iterator.getEndIndex()) {
throw new IllegalArgumentException("Invalid substring range"); throw new IllegalArgumentException("Invalid substring range");
} }
@ -345,7 +345,7 @@ public class AttributedString {
iterator.next(); iterator.next();
} }
text = buffer.toString(); text = buffer.toString();
TSet<AttributedCharacterIterator.Attribute> attributes = iterator.getAllAttributeKeys(); TSet<TAttributedCharacterIterator.Attribute> attributes = iterator.getAllAttributeKeys();
if (attributes == null) { if (attributes == null) {
return; return;
} }
@ -353,9 +353,9 @@ public class AttributedString {
TIterator<Attribute> it = attributes.iterator(); TIterator<Attribute> it = attributes.iterator();
while (it.hasNext()) { while (it.hasNext()) {
AttributedCharacterIterator.Attribute attribute = it.next(); TAttributedCharacterIterator.Attribute attribute = it.next();
iterator.setIndex(0); iterator.setIndex(0);
while (iterator.current() != CharacterIterator.DONE) { while (iterator.current() != TCharacterIterator.DONE) {
int start = iterator.getRunStart(attribute); int start = iterator.getRunStart(attribute);
int limit = iterator.getRunLimit(attribute); int limit = iterator.getRunLimit(attribute);
Object value = iterator.getAttribute(attribute); Object value = iterator.getAttribute(attribute);
@ -367,7 +367,7 @@ public class AttributedString {
} }
} }
private AttributedString(AttributedCharacterIterator iterator, int start, int end, TSet<Attribute> attributes) { private TAttributedString(TAttributedCharacterIterator iterator, int start, int end, TSet<Attribute> attributes) {
if (start < iterator.getBeginIndex() || end > iterator.getEndIndex() || start > end) { if (start < iterator.getBeginIndex() || end > iterator.getEndIndex() || start > end) {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
@ -387,14 +387,14 @@ public class AttributedString {
TIterator<Attribute> it = attributes.iterator(); TIterator<Attribute> it = attributes.iterator();
while (it.hasNext()) { while (it.hasNext()) {
AttributedCharacterIterator.Attribute attribute = it.next(); TAttributedCharacterIterator.Attribute attribute = it.next();
iterator.setIndex(start); iterator.setIndex(start);
while (iterator.getIndex() < end) { while (iterator.getIndex() < end) {
Object value = iterator.getAttribute(attribute); Object value = iterator.getAttribute(attribute);
int runStart = iterator.getRunStart(attribute); int runStart = iterator.getRunStart(attribute);
int limit = iterator.getRunLimit(attribute); int limit = iterator.getRunLimit(attribute);
if ((value instanceof Annotation && runStart >= start && limit <= end) || if ((value instanceof TAnnotation && runStart >= start && limit <= end) ||
(value != null && !(value instanceof Annotation))) { (value != null && !(value instanceof TAnnotation))) {
addAttribute(attribute, value, (runStart < start ? start : runStart) - start, (limit > end ? end addAttribute(attribute, value, (runStart < start ? start : runStart) - start, (limit > end ? end
: limit) - start); : limit) - start);
} }
@ -403,16 +403,16 @@ public class AttributedString {
} }
} }
public AttributedString(AttributedCharacterIterator iterator, int start, int end) { public TAttributedString(TAttributedCharacterIterator iterator, int start, int end) {
this(iterator, start, end, iterator.getAllAttributeKeys()); this(iterator, start, end, iterator.getAllAttributeKeys());
} }
public AttributedString(AttributedCharacterIterator iterator, int start, int end, public TAttributedString(TAttributedCharacterIterator iterator, int start, int end,
AttributedCharacterIterator.Attribute[] attributes) { TAttributedCharacterIterator.Attribute[] attributes) {
this(iterator, start, end, new THashSet<>(TArrays.asList(attributes))); this(iterator, start, end, new THashSet<>(TArrays.asList(attributes)));
} }
public AttributedString(String value) { public TAttributedString(String value) {
if (value == null) { if (value == null) {
throw new NullPointerException(); throw new NullPointerException();
} }
@ -420,7 +420,7 @@ public class AttributedString {
attributeMap = new THashMap<>(11); attributeMap = new THashMap<>(11);
} }
public AttributedString(String value, TMap<? extends AttributedCharacterIterator.Attribute, ?> attributes) { public TAttributedString(String value, TMap<? extends TAttributedCharacterIterator.Attribute, ?> attributes) {
if (value == null) { if (value == null) {
throw new NullPointerException(); throw new NullPointerException();
} }
@ -434,7 +434,7 @@ public class AttributedString {
TMap.Entry<?, ?> entry = (TMap.Entry<?, ?>) it.next(); TMap.Entry<?, ?> entry = (TMap.Entry<?, ?>) it.next();
TArrayList<Range> ranges = new TArrayList<>(1); TArrayList<Range> ranges = new TArrayList<>(1);
ranges.add(new Range(0, text.length(), entry.getValue())); ranges.add(new Range(0, text.length(), entry.getValue()));
attributeMap.put((AttributedCharacterIterator.Attribute) entry.getKey(), ranges); attributeMap.put((TAttributedCharacterIterator.Attribute) entry.getKey(), ranges);
} }
} }
@ -451,7 +451,7 @@ public class AttributedString {
* @throws NullPointerException * @throws NullPointerException
* if {@code attribute} is {@code null}. * if {@code attribute} is {@code null}.
*/ */
public void addAttribute(AttributedCharacterIterator.Attribute attribute, Object value) { public void addAttribute(TAttributedCharacterIterator.Attribute attribute, Object value) {
if (null == attribute) { if (null == attribute) {
throw new NullPointerException(); throw new NullPointerException();
} }
@ -487,7 +487,7 @@ public class AttributedString {
* @throws NullPointerException * @throws NullPointerException
* if {@code attribute} is {@code null}. * if {@code attribute} is {@code null}.
*/ */
public void addAttribute(AttributedCharacterIterator.Attribute attribute, Object value, int start, int end) { public void addAttribute(TAttributedCharacterIterator.Attribute attribute, Object value, int start, int end) {
if (null == attribute) { if (null == attribute) {
throw new NullPointerException(); throw new NullPointerException();
} }
@ -575,11 +575,11 @@ public class AttributedString {
* if {@code start < 0}, {@code end} is greater than the length * if {@code start < 0}, {@code end} is greater than the length
* of this string, or if {@code start >= end}. * of this string, or if {@code start >= end}.
*/ */
public void addAttributes(TMap<? extends AttributedCharacterIterator.Attribute, ?> attributes, int start, int end) { public void addAttributes(TMap<? extends TAttributedCharacterIterator.Attribute, ?> attributes, int start, int end) {
TIterator<?> it = attributes.entrySet().iterator(); TIterator<?> it = attributes.entrySet().iterator();
while (it.hasNext()) { while (it.hasNext()) {
TMap.Entry<?, ?> entry = (TMap.Entry<?, ?>) it.next(); TMap.Entry<?, ?> entry = (TMap.Entry<?, ?>) it.next();
addAttribute((AttributedCharacterIterator.Attribute) entry.getKey(), entry.getValue(), start, end); addAttribute((TAttributedCharacterIterator.Attribute) entry.getKey(), entry.getValue(), start, end);
} }
} }
@ -589,7 +589,7 @@ public class AttributedString {
* *
* @return the newly created {@code AttributedCharacterIterator}. * @return the newly created {@code AttributedCharacterIterator}.
*/ */
public AttributedCharacterIterator getIterator() { public TAttributedCharacterIterator getIterator() {
return new AttributedIterator(this); return new AttributedIterator(this);
} }
@ -604,7 +604,7 @@ public class AttributedString {
* iterator if they are defined for this text. * iterator if they are defined for this text.
* @return the newly created {@code AttributedCharacterIterator}. * @return the newly created {@code AttributedCharacterIterator}.
*/ */
public AttributedCharacterIterator getIterator(AttributedCharacterIterator.Attribute[] attributes) { public TAttributedCharacterIterator getIterator(TAttributedCharacterIterator.Attribute[] attributes) {
return new AttributedIterator(this, attributes, 0, text.length()); return new AttributedIterator(this, attributes, 0, text.length());
} }
@ -623,7 +623,7 @@ public class AttributedString {
* the end index of the iterator on the underlying text. * the end index of the iterator on the underlying text.
* @return the newly created {@code AttributedCharacterIterator}. * @return the newly created {@code AttributedCharacterIterator}.
*/ */
public AttributedCharacterIterator getIterator(AttributedCharacterIterator.Attribute[] attributes, int start, public TAttributedCharacterIterator getIterator(TAttributedCharacterIterator.Attribute[] attributes, int start,
int end) { int end) {
return new AttributedIterator(this, attributes, start, end); return new AttributedIterator(this, attributes, start, end);
} }

View File

@ -17,7 +17,7 @@
package org.teavm.classlib.java.text; package org.teavm.classlib.java.text;
public interface CharacterIterator extends Cloneable { public interface TCharacterIterator extends Cloneable {
public static final char DONE = '\uffff'; public static final char DONE = '\uffff';
public Object clone(); public Object clone();

View File

@ -19,7 +19,7 @@ package org.teavm.classlib.java.text;
import org.teavm.classlib.java.util.*; import org.teavm.classlib.java.util.*;
public abstract class DateFormat extends Format { public abstract class TDateFormat extends TFormat {
protected TCalendar calendar; protected TCalendar calendar;
//protected NumberFormat numberFormat; //protected NumberFormat numberFormat;
public final static int DEFAULT = 2; public final static int DEFAULT = 2;
@ -46,12 +46,12 @@ public abstract class DateFormat extends Format {
public final static int HOUR0_FIELD = 16; public final static int HOUR0_FIELD = 16;
public final static int TIMEZONE_FIELD = 17; public final static int TIMEZONE_FIELD = 17;
protected DateFormat() { protected TDateFormat() {
} }
@Override @Override
public Object clone() { public Object clone() {
DateFormat clone = (DateFormat) super.clone(); TDateFormat clone = (TDateFormat) super.clone();
clone.calendar = (TCalendar) calendar.clone(); clone.calendar = (TCalendar) calendar.clone();
// TODO: implement // TODO: implement
//clone.numberFormat = (NumberFormat) numberFormat.clone(); //clone.numberFormat = (NumberFormat) numberFormat.clone();
@ -76,7 +76,7 @@ public abstract class DateFormat extends Format {
} }
@Override @Override
public final StringBuffer format(Object object, StringBuffer buffer, FieldPosition field) { public final StringBuffer format(Object object, StringBuffer buffer, TFieldPosition field) {
if (object instanceof TDate) { if (object instanceof TDate) {
return format((TDate) object, buffer, field); return format((TDate) object, buffer, field);
} }
@ -87,10 +87,10 @@ public abstract class DateFormat extends Format {
} }
public final String format(TDate date) { public final String format(TDate date) {
return format(date, new StringBuffer(), new FieldPosition(0)).toString(); return format(date, new StringBuffer(), new TFieldPosition(0)).toString();
} }
public abstract StringBuffer format(TDate date, StringBuffer buffer, FieldPosition field); public abstract StringBuffer format(TDate date, StringBuffer buffer, TFieldPosition field);
public static TLocale[] getAvailableLocales() { public static TLocale[] getAvailableLocales() {
return TLocale.getAvailableLocales(); return TLocale.getAvailableLocales();
@ -100,16 +100,16 @@ public abstract class DateFormat extends Format {
return calendar; return calendar;
} }
public final static DateFormat getDateInstance() { public final static TDateFormat getDateInstance() {
return getDateInstance(DEFAULT); return getDateInstance(DEFAULT);
} }
public final static DateFormat getDateInstance(int style) { public final static TDateFormat getDateInstance(int style) {
checkDateStyle(style); checkDateStyle(style);
return getDateInstance(style, TLocale.getDefault()); return getDateInstance(style, TLocale.getDefault());
} }
public final static DateFormat getDateInstance(int style, TLocale locale) { public final static TDateFormat getDateInstance(int style, TLocale locale) {
/*checkDateStyle(style); /*checkDateStyle(style);
com.ibm.icu.text.DateFormat icuFormat = com.ibm.icu.text.DateFormat.getDateInstance(style, locale); com.ibm.icu.text.DateFormat icuFormat = com.ibm.icu.text.DateFormat.getDateInstance(style, locale);
return new SimpleDateFormat(locale, (com.ibm.icu.text.SimpleDateFormat) icuFormat);*/ return new SimpleDateFormat(locale, (com.ibm.icu.text.SimpleDateFormat) icuFormat);*/
@ -117,17 +117,17 @@ public abstract class DateFormat extends Format {
return null; return null;
} }
public final static DateFormat getDateTimeInstance() { public final static TDateFormat getDateTimeInstance() {
return getDateTimeInstance(DEFAULT, DEFAULT); return getDateTimeInstance(DEFAULT, DEFAULT);
} }
public final static DateFormat getDateTimeInstance(int dateStyle, int timeStyle) { public final static TDateFormat getDateTimeInstance(int dateStyle, int timeStyle) {
checkTimeStyle(timeStyle); checkTimeStyle(timeStyle);
checkDateStyle(dateStyle); checkDateStyle(dateStyle);
return getDateTimeInstance(dateStyle, timeStyle, TLocale.getDefault()); return getDateTimeInstance(dateStyle, timeStyle, TLocale.getDefault());
} }
public final static DateFormat getDateTimeInstance(int dateStyle, int timeStyle, TLocale locale) { public final static TDateFormat getDateTimeInstance(int dateStyle, int timeStyle, TLocale locale) {
/*checkTimeStyle(timeStyle); /*checkTimeStyle(timeStyle);
checkDateStyle(dateStyle); checkDateStyle(dateStyle);
com.ibm.icu.text.DateFormat icuFormat = com.ibm.icu.text.DateFormat.getDateTimeInstance(dateStyle, timeStyle, com.ibm.icu.text.DateFormat icuFormat = com.ibm.icu.text.DateFormat.getDateTimeInstance(dateStyle, timeStyle,
@ -137,7 +137,7 @@ public abstract class DateFormat extends Format {
return null; return null;
} }
public final static DateFormat getInstance() { public final static TDateFormat getInstance() {
return getDateTimeInstance(SHORT, SHORT); return getDateTimeInstance(SHORT, SHORT);
} }
@ -166,16 +166,16 @@ public abstract class DateFormat extends Format {
return styleName; return styleName;
} }
public final static DateFormat getTimeInstance() { public final static TDateFormat getTimeInstance() {
return getTimeInstance(DEFAULT); return getTimeInstance(DEFAULT);
} }
public final static DateFormat getTimeInstance(int style) { public final static TDateFormat getTimeInstance(int style) {
checkTimeStyle(style); checkTimeStyle(style);
return getTimeInstance(style, TLocale.getDefault()); return getTimeInstance(style, TLocale.getDefault());
} }
public final static DateFormat getTimeInstance(int style, TLocale locale) { public final static TDateFormat getTimeInstance(int style, TLocale locale) {
/*checkTimeStyle(style); /*checkTimeStyle(style);
com.ibm.icu.text.DateFormat icuFormat = com.ibm.icu.text.DateFormat.getTimeInstance(style, locale); com.ibm.icu.text.DateFormat icuFormat = com.ibm.icu.text.DateFormat.getTimeInstance(style, locale);
return new SimpleDateFormat(locale, (com.ibm.icu.text.SimpleDateFormat) icuFormat);*/ return new SimpleDateFormat(locale, (com.ibm.icu.text.SimpleDateFormat) icuFormat);*/
@ -196,19 +196,19 @@ public abstract class DateFormat extends Format {
return calendar.isLenient(); return calendar.isLenient();
} }
public TDate parse(String string) throws ParseException { public TDate parse(String string) throws TParseException {
ParsePosition position = new ParsePosition(0); TParsePosition position = new TParsePosition(0);
TDate date = parse(string, position); TDate date = parse(string, position);
if (position.getIndex() == 0) { if (position.getIndex() == 0) {
throw new ParseException("Unparseable date" + string, position.getErrorIndex()); throw new TParseException("Unparseable date" + string, position.getErrorIndex());
} }
return date; return date;
} }
public abstract TDate parse(String string, ParsePosition position); public abstract TDate parse(String string, TParsePosition position);
@Override @Override
public Object parseObject(String string, ParsePosition position) { public Object parseObject(String string, TParsePosition position) {
return parse(string, position); return parse(string, position);
} }
@ -224,7 +224,7 @@ public abstract class DateFormat extends Format {
numberFormat = format; numberFormat = format;
}*/ }*/
public static class Field extends Format.Field { public static class Field extends TFormat.Field {
private static THashMap<Integer, Field> table = new THashMap<>(); private static THashMap<Integer, Field> table = new THashMap<>();
public final static Field ERA = new Field("era", TCalendar.ERA); public final static Field ERA = new Field("era", TCalendar.ERA);
public final static Field YEAR = new Field("year", TCalendar.YEAR); public final static Field YEAR = new Field("year", TCalendar.YEAR);

View File

@ -23,24 +23,24 @@ import org.teavm.classlib.java.io.TSerializable;
import org.teavm.classlib.java.lang.TCloneable; import org.teavm.classlib.java.lang.TCloneable;
import org.teavm.classlib.java.util.TLocale; import org.teavm.classlib.java.util.TLocale;
public class DateFormatSymbols implements TSerializable, TCloneable { public class TDateFormatSymbols implements TSerializable, TCloneable {
private TLocale locale; private TLocale locale;
private String localPatternChars; private String localPatternChars;
String[] ampms, eras, months, shortMonths, shortWeekdays, weekdays; String[] ampms, eras, months, shortMonths, shortWeekdays, weekdays;
String[][] zoneStrings; String[][] zoneStrings;
public DateFormatSymbols() { public TDateFormatSymbols() {
this(TLocale.getDefault()); this(TLocale.getDefault());
} }
public DateFormatSymbols(TLocale locale) { public TDateFormatSymbols(TLocale locale) {
this.locale = locale; this.locale = locale;
} }
@Override @Override
public Object clone() { public Object clone() {
DateFormatSymbols symbols = new DateFormatSymbols(locale); TDateFormatSymbols symbols = new TDateFormatSymbols(locale);
if (ampms != null) { if (ampms != null) {
symbols.ampms = Arrays.copyOf(ampms, ampms.length); symbols.ampms = Arrays.copyOf(ampms, ampms.length);
} }
@ -73,11 +73,11 @@ public class DateFormatSymbols implements TSerializable, TCloneable {
if (this == object) { if (this == object) {
return true; return true;
} }
if (!(object instanceof DateFormatSymbols)) { if (!(object instanceof TDateFormatSymbols)) {
return false; return false;
} }
DateFormatSymbols obj = (DateFormatSymbols) object; TDateFormatSymbols obj = (TDateFormatSymbols) object;
if (!locale.equals(obj.locale)) { if (!locale.equals(obj.locale)) {
return false; return false;
} }
@ -124,7 +124,7 @@ public class DateFormatSymbols implements TSerializable, TCloneable {
public String[] getEras() { public String[] getEras() {
if (eras == null) { if (eras == null) {
eras = CLDRHelper.resolveEras(CLDRHelper.getCode(locale.getLanguage(), locale.getCountry())); eras = CLDRHelper.resolveEras(locale.getLanguage(), locale.getCountry());
} }
return eras.clone(); return eras.clone();
} }

View File

@ -17,20 +17,20 @@
package org.teavm.classlib.java.text; package org.teavm.classlib.java.text;
public class FieldPosition { public class TFieldPosition {
private int myField, beginIndex, endIndex; private int myField, beginIndex, endIndex;
private Format.Field myAttribute; private TFormat.Field myAttribute;
public FieldPosition(int field) { public TFieldPosition(int field) {
myField = field; myField = field;
} }
public FieldPosition(Format.Field attribute) { public TFieldPosition(TFormat.Field attribute) {
myAttribute = attribute; myAttribute = attribute;
myField = -1; myField = -1;
} }
public FieldPosition(Format.Field attribute, int field) { public TFieldPosition(TFormat.Field attribute, int field) {
myAttribute = attribute; myAttribute = attribute;
myField = field; myField = field;
} }
@ -41,10 +41,10 @@ public class FieldPosition {
@Override @Override
public boolean equals(Object object) { public boolean equals(Object object) {
if (!(object instanceof FieldPosition)) { if (!(object instanceof TFieldPosition)) {
return false; return false;
} }
FieldPosition pos = (FieldPosition) object; TFieldPosition pos = (TFieldPosition) object;
return myField == pos.myField && myAttribute == pos.myAttribute && beginIndex == pos.beginIndex && return myField == pos.myField && myAttribute == pos.myAttribute && beginIndex == pos.beginIndex &&
endIndex == pos.endIndex; endIndex == pos.endIndex;
} }
@ -61,7 +61,7 @@ public class FieldPosition {
return myField; return myField;
} }
public Format.Field getFieldAttribute() { public TFormat.Field getFieldAttribute() {
return myAttribute; return myAttribute;
} }

View File

@ -20,8 +20,8 @@ package org.teavm.classlib.java.text;
import org.teavm.classlib.java.io.TSerializable; import org.teavm.classlib.java.io.TSerializable;
import org.teavm.classlib.java.lang.TCloneable; import org.teavm.classlib.java.lang.TCloneable;
public abstract class Format implements TSerializable, TCloneable { public abstract class TFormat implements TSerializable, TCloneable {
public Format() { public TFormat() {
} }
@Override @Override
@ -61,27 +61,27 @@ public abstract class Format implements TSerializable, TCloneable {
} }
public final String format(Object object) { public final String format(Object object) {
return format(object, new StringBuffer(), new FieldPosition(0)).toString(); return format(object, new StringBuffer(), new TFieldPosition(0)).toString();
} }
public abstract StringBuffer format(Object object, StringBuffer buffer, FieldPosition field); public abstract StringBuffer format(Object object, StringBuffer buffer, TFieldPosition field);
public AttributedCharacterIterator formatToCharacterIterator(Object object) { public TAttributedCharacterIterator formatToCharacterIterator(Object object) {
return new AttributedString(format(object)).getIterator(); return new TAttributedString(format(object)).getIterator();
} }
public Object parseObject(String string) throws ParseException { public Object parseObject(String string) throws TParseException {
ParsePosition position = new ParsePosition(0); TParsePosition position = new TParsePosition(0);
Object result = parseObject(string, position); Object result = parseObject(string, position);
if (position.getIndex() == 0) { if (position.getIndex() == 0) {
throw new ParseException("Format.parseObject(String) parse failure", position.getErrorIndex()); throw new TParseException("Format.parseObject(String) parse failure", position.getErrorIndex());
} }
return result; return result;
} }
public abstract Object parseObject(String string, ParsePosition position); public abstract Object parseObject(String string, TParsePosition position);
static boolean upTo(String string, ParsePosition position, StringBuffer buffer, char stop) { static boolean upTo(String string, TParsePosition position, StringBuffer buffer, char stop) {
int index = position.getIndex(), length = string.length(); int index = position.getIndex(), length = string.length();
boolean lastQuote = false, quote = false; boolean lastQuote = false, quote = false;
while (index < length) { while (index < length) {
@ -104,7 +104,7 @@ public abstract class Format implements TSerializable, TCloneable {
return false; return false;
} }
static boolean upToWithQuotes(String string, ParsePosition position, StringBuffer buffer, char stop, char start) { static boolean upToWithQuotes(String string, TParsePosition position, StringBuffer buffer, char stop, char start) {
int index = position.getIndex(), length = string.length(), count = 1; int index = position.getIndex(), length = string.length(), count = 1;
boolean quote = false; boolean quote = false;
while (index < length) { while (index < length) {
@ -129,7 +129,7 @@ public abstract class Format implements TSerializable, TCloneable {
throw new IllegalArgumentException("Unmatched braces in the pattern"); throw new IllegalArgumentException("Unmatched braces in the pattern");
} }
public static class Field extends AttributedCharacterIterator.Attribute { public static class Field extends TAttributedCharacterIterator.Attribute {
protected Field(String fieldName) { protected Field(String fieldName) {
super(fieldName); super(fieldName);
} }

View File

@ -20,7 +20,7 @@ package org.teavm.classlib.java.text;
/** /**
* Thrown when the string being parsed is not in the correct form. * Thrown when the string being parsed is not in the correct form.
*/ */
public class ParseException extends Exception { public class TParseException extends Exception {
private static final long serialVersionUID = 2703218443322787634L; private static final long serialVersionUID = 2703218443322787634L;
@ -35,7 +35,7 @@ public class ParseException extends Exception {
* @param location * @param location
* the index at which the parse exception occurred. * the index at which the parse exception occurred.
*/ */
public ParseException(String detailMessage, int location) { public TParseException(String detailMessage, int location) {
super(detailMessage); super(detailMessage);
errorOffset = location; errorOffset = location;
} }

View File

@ -17,20 +17,20 @@
package org.teavm.classlib.java.text; package org.teavm.classlib.java.text;
public class ParsePosition { public class TParsePosition {
private int currentPosition, errorIndex = -1; private int currentPosition, errorIndex = -1;
public ParsePosition(int index) { public TParsePosition(int index) {
currentPosition = index; currentPosition = index;
} }
@Override @Override
public boolean equals(Object object) { public boolean equals(Object object) {
if (!(object instanceof ParsePosition)) { if (!(object instanceof TParsePosition)) {
return false; return false;
} }
ParsePosition pos = (ParsePosition) object; TParsePosition pos = (TParsePosition) object;
return currentPosition == pos.currentPosition && errorIndex == pos.errorIndex; return currentPosition == pos.currentPosition && errorIndex == pos.errorIndex;
} }

View File

@ -15,6 +15,7 @@
*/ */
package org.teavm.samples; package org.teavm.samples;
import java.text.DateFormatSymbols;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
@ -156,6 +157,20 @@ public class DateTime {
HTMLInputElement fieldValueElem = (HTMLInputElement)document.getElementById("field-value"); HTMLInputElement fieldValueElem = (HTMLInputElement)document.getElementById("field-value");
Calendar calendar = new GregorianCalendar(currentLocale); Calendar calendar = new GregorianCalendar(currentLocale);
calendar.setTime(currentDate); calendar.setTime(currentDate);
fieldValueElem.setValue(String.valueOf(calendar.get(currentField))); int value = calendar.get(currentField);
fieldValueElem.setValue(String.valueOf(value));
DateFormatSymbols symbols = new DateFormatSymbols(currentLocale);
String text;
switch (currentField) {
case Calendar.ERA:
text = symbols.getEras()[value];
break;
default:
text = "";
break;
}
HTMLInputElement fieldTextElem = (HTMLInputElement)document.getElementById("field-value-text");
fieldTextElem.setValue(text);
} }
} }

View File

@ -43,6 +43,7 @@
<div> <div>
<label for="field-value">Field value is:</label> <label for="field-value">Field value is:</label>
<input type="text" id="field-value" readonly> <input type="text" id="field-value" readonly>
<input type="text" id="field-value-text" readonly>
</div> </div>
</body> </body>
</html> </html>