From b94d71173297c314ed5cffb0333b24d7a3fcbd18 Mon Sep 17 00:00:00 2001 From: konsoletyper Date: Thu, 26 Jun 2014 18:06:31 +0400 Subject: [PATCH] Completes DateTimeFormat support --- .../impl/unicode/CLDRDateFormats.java | 50 ++++++++++ .../classlib/impl/unicode/CLDRHelper.java | 31 +++--- .../classlib/impl/unicode/CLDRLocale.java | 23 ++--- .../classlib/impl/unicode/CLDRReader.java | 27 ++++- .../impl/unicode/DateFormatCollection.java | 40 ++++++++ .../unicode/DateFormatMetadataGenerator.java | 34 +++---- .../org/teavm/classlib/java/lang/TString.java | 2 +- .../teavm/classlib/java/text/TDateFormat.java | 98 ++++++++++--------- .../classlib/java/text/TSimpleDateFormat.java | 2 +- .../java/text/TSimpleDatePatternParser.java | 2 +- .../resources/org/teavm/javascript/runtime.js | 22 ++++- 11 files changed, 230 insertions(+), 101 deletions(-) create mode 100644 teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/CLDRDateFormats.java create mode 100644 teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/DateFormatCollection.java diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/CLDRDateFormats.java b/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/CLDRDateFormats.java new file mode 100644 index 000000000..ac4bf8d57 --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/CLDRDateFormats.java @@ -0,0 +1,50 @@ +/* + * 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; + +/** + * + * @author Alexey Andreev + */ +public class CLDRDateFormats { + private String shortFormat; + private String mediumFormat; + private String longFormat; + private String fullFormat; + + CLDRDateFormats(String shortFormat, String mediumFormat, String longFormat, String fullFormat) { + this.shortFormat = shortFormat; + this.mediumFormat = mediumFormat; + this.longFormat = longFormat; + this.fullFormat = fullFormat; + } + + public String getShortFormat() { + return shortFormat; + } + + public String getMediumFormat() { + return mediumFormat; + } + + public String getLongFormat() { + return longFormat; + } + + public String getFullFormat() { + return fullFormat; + } +} diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/CLDRHelper.java b/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/CLDRHelper.java index 91058d6fe..9e50d7a61 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/CLDRHelper.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/CLDRHelper.java @@ -108,33 +108,26 @@ public class CLDRHelper { @MetadataProvider(FirstDayOfWeekMetadataGenerator.class) public static native ResourceMap getFirstDayOfWeek(); - public static String resolveDateFormat(String language, String country) { - return resolveFormatSymbols(getDateFormatMap(), language, country); + public static DateFormatCollection resolveDateFormats(String language, String country) { + return resolveDateFormats(getDateFormatMap(), language, country); } @MetadataProvider(DateFormatMetadataGenerator.class) - private static native ResourceMap getDateFormatMap(); + private static native ResourceMap getDateFormatMap(); - public static String resolveFullDateFormat(String language, String country) { - return resolveFormatSymbols(getFullDateFormatMap(), language, country); + public static DateFormatCollection resolveTimeFormats(String language, String country) { + return resolveDateFormats(getTimeFormatMap(), language, country); } @MetadataProvider(DateFormatMetadataGenerator.class) - private static native ResourceMap getFullDateFormatMap(); + private static native ResourceMap getTimeFormatMap(); - public static String resolveLongDateFormat(String language, String country) { - return resolveFormatSymbols(getLongDateFormatMap(), language, country); + public static DateFormatCollection resolveDateTimeFormats(String language, String country) { + return resolveDateFormats(getDateTimeFormatMap(), language, country); } @MetadataProvider(DateFormatMetadataGenerator.class) - private static native ResourceMap getLongDateFormatMap(); - - public static String resolveShortDateFormat(String language, String country) { - return resolveFormatSymbols(getShortDateFormatMap(), language, country); - } - - @MetadataProvider(DateFormatMetadataGenerator.class) - private static native ResourceMap getShortDateFormatMap(); + private static native ResourceMap getDateTimeFormatMap(); public static String resolveNumberFormat(String language, String country) { return resolveFormatSymbols(getNumberFormatMap(), language, country); @@ -154,6 +147,12 @@ public class CLDRHelper { private static native ResourceMap getPercentFormatMap(); + private static DateFormatCollection resolveDateFormats(ResourceMap map, + String language, String country) { + String localeCode = getCode(language, country); + return map.has(localeCode) ? map.get(localeCode) : map.has(language) ? map.get(language) : map.get("root"); + } + private static String resolveFormatSymbols(ResourceMap map, String language, String country) { String localeCode = getCode(language, country); StringResource res = map.has(localeCode) ? map.get(localeCode) : map.has(language) ? map.get(language) : diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/CLDRLocale.java b/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/CLDRLocale.java index ebb359f60..f769e6fd4 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/CLDRLocale.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/CLDRLocale.java @@ -33,10 +33,9 @@ public class CLDRLocale { String[] shortMonths; String[] weekdays; String[] shortWeekdays; - String shortDateFormat; - String mediumDateFormat; - String longDateFormat; - String fullDateFormat; + CLDRDateFormats dateFormats; + CLDRDateFormats timeFormats; + CLDRDateFormats dateTimeFormats; public Map getLanguages() { return Collections.unmodifiableMap(languages); @@ -70,19 +69,15 @@ public class CLDRLocale { return Arrays.copyOf(shortWeekdays, shortWeekdays.length); } - public String getShortDateFormat() { - return shortDateFormat; + public CLDRDateFormats getDateFormats() { + return dateFormats; } - public String getMediumDateFormat() { - return mediumDateFormat; + public CLDRDateFormats getTimeFormats() { + return timeFormats; } - public String getLongDateFormat() { - return longDateFormat; - } - - public String getFullDateFormat() { - return fullDateFormat; + public CLDRDateFormats getDateTimeFormats() { + return dateTimeFormats; } } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/CLDRReader.java b/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/CLDRReader.java index 73d2317a5..38646c7e7 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/CLDRReader.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/CLDRReader.java @@ -109,6 +109,8 @@ public class CLDRReader { readWeekdays(localeName, localeInfo, root); readShortWeekdays(localeName, localeInfo, root); readDateFormats(localeName, localeInfo, root); + readTimeFormats(localeName, localeInfo, root); + readDateTimeFormats(localeName, localeInfo, root); break; } } @@ -209,10 +211,27 @@ public class CLDRReader { JsonObject formatsJson = root.get("main").getAsJsonObject().get(localeCode).getAsJsonObject() .get("dates").getAsJsonObject().get("calendars").getAsJsonObject() .get("gregorian").getAsJsonObject().get("dateFormats").getAsJsonObject(); - locale.shortDateFormat = formatsJson.get("short").getAsString(); - locale.mediumDateFormat = formatsJson.get("medium").getAsString(); - locale.longDateFormat = formatsJson.get("long").getAsString(); - locale.fullDateFormat = formatsJson.get("full").getAsString(); + locale.dateFormats = new CLDRDateFormats(formatsJson.get("short").getAsString(), + formatsJson.get("medium").getAsString(), formatsJson.get("long").getAsString(), + formatsJson.get("full").getAsString()); + } + + private void readTimeFormats(String localeCode, CLDRLocale locale, JsonObject root) { + JsonObject formatsJson = root.get("main").getAsJsonObject().get(localeCode).getAsJsonObject() + .get("dates").getAsJsonObject().get("calendars").getAsJsonObject() + .get("gregorian").getAsJsonObject().get("timeFormats").getAsJsonObject(); + locale.timeFormats = new CLDRDateFormats(formatsJson.get("short").getAsString(), + formatsJson.get("medium").getAsString(), formatsJson.get("long").getAsString(), + formatsJson.get("full").getAsString()); + } + + private void readDateTimeFormats(String localeCode, CLDRLocale locale, JsonObject root) { + JsonObject formatsJson = root.get("main").getAsJsonObject().get(localeCode).getAsJsonObject() + .get("dates").getAsJsonObject().get("calendars").getAsJsonObject() + .get("gregorian").getAsJsonObject().get("dateTimeFormats").getAsJsonObject(); + locale.dateTimeFormats = new CLDRDateFormats(formatsJson.get("short").getAsString(), + formatsJson.get("medium").getAsString(), formatsJson.get("long").getAsString(), + formatsJson.get("full").getAsString()); } private void readWeekData(InputStream input) { diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/DateFormatCollection.java b/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/DateFormatCollection.java new file mode 100644 index 000000000..e3838c879 --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/DateFormatCollection.java @@ -0,0 +1,40 @@ +/* + * 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 org.teavm.platform.metadata.Resource; + +/** + * + * @author Alexey Andreev + */ +public interface DateFormatCollection extends Resource { + String getShortFormat(); + + void setShortFormat(String format); + + String getMediumFormat(); + + void setMediumFormat(String format); + + String getLongFormat(); + + void setLongFormat(String format); + + String getFullFormat(); + + void setFullFormat(String format); +} diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/DateFormatMetadataGenerator.java b/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/DateFormatMetadataGenerator.java index 78eb6d3e4..a73c6a90d 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/DateFormatMetadataGenerator.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/DateFormatMetadataGenerator.java @@ -29,26 +29,20 @@ public class DateFormatMetadataGenerator implements MetadataGenerator { switch (method.getName()) { case "getDateFormatMap": return getDateFormatMap(context, new FormatExtractor() { - @Override public String extract(CLDRLocale locale) { - return locale.getMediumDateFormat(); + @Override public CLDRDateFormats extract(CLDRLocale locale) { + return locale.getDateFormats(); } }); - case "getShortDateFormatMap": + case "getTimeFormatMap": return getDateFormatMap(context, new FormatExtractor() { - @Override public String extract(CLDRLocale locale) { - return locale.getShortDateFormat(); + @Override public CLDRDateFormats extract(CLDRLocale locale) { + return locale.getTimeFormats(); } }); - case "getLongDateFormatMap": + case "getDateTimeFormatMap": return getDateFormatMap(context, new FormatExtractor() { - @Override public String extract(CLDRLocale locale) { - return locale.getLongDateFormat(); - } - }); - case "getFullDateFormatMap": - return getDateFormatMap(context, new FormatExtractor() { - @Override public String extract(CLDRLocale locale) { - return locale.getFullDateFormat(); + @Override public CLDRDateFormats extract(CLDRLocale locale) { + return locale.getDateTimeFormats(); } }); default: @@ -58,16 +52,20 @@ public class DateFormatMetadataGenerator implements MetadataGenerator { private Resource getDateFormatMap(MetadataGeneratorContext context, FormatExtractor extractor) { CLDRReader reader = context.getService(CLDRReader.class); - ResourceMap result = context.createResourceMap(); + ResourceMap result = context.createResourceMap(); for (Map.Entry entry : reader.getKnownLocales().entrySet()) { - StringResource formatRes = context.createResource(StringResource.class); - formatRes.setValue(extractor.extract(entry.getValue())); + DateFormatCollection formatRes = context.createResource(DateFormatCollection.class); + CLDRDateFormats formats = extractor.extract(entry.getValue()); + formatRes.setShortFormat(formats.getShortFormat()); + formatRes.setMediumFormat(formats.getMediumFormat()); + formatRes.setLongFormat(formats.getLongFormat()); + formatRes.setFullFormat(formats.getFullFormat()); result.put(entry.getKey(), formatRes); } return result; } interface FormatExtractor { - String extract(CLDRLocale locale); + CLDRDateFormats extract(CLDRLocale locale); } } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TString.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TString.java index cb04ca2c8..e8dcf653f 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TString.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TString.java @@ -400,7 +400,7 @@ public class TString extends TObject implements TSerializable, TComparable= 0) { while (LongInt_ucompare(t, a) > 0) { LongInt_sub(t, b); - q = (q - 1) | 0; + --digit; } } else { while (true) { @@ -706,7 +724,7 @@ LongInt_div = function(a, b) { break; } t = nextT; - q = (q + 1) | 0; + ++digit; } } LongInt_sub(a, t);