From 7512817b3ab561ad5adf55fa941dbec4250155c1 Mon Sep 17 00:00:00 2001 From: konsoletyper Date: Wed, 11 Jun 2014 17:10:58 +0400 Subject: [PATCH] Goes on further with rewriting locale code with metadata providers --- .../org/teavm/classlib/impl/JCLPlugin.java | 1 - .../classlib/impl/unicode/CLDRHelper.java | 6 +++ .../unicode/CountryMetadataGenerator.java | 29 ++++++++++++ .../DefaultLocaleMetadataGenerator.java | 35 +++++++++++++++ .../unicode/LanguageMetadataGenerator.java | 20 ++------- .../impl/unicode/LocaleMetadataGenerator.java | 45 +++++++++++++++++++ .../java/util/LocaleNativeGenerator.java | 18 -------- .../util/LocaleSettingsNativeGenerator.java | 38 ---------------- .../org/teavm/classlib/java/util/TLocale.java | 27 +++++------ 9 files changed, 129 insertions(+), 90 deletions(-) create mode 100644 teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/CountryMetadataGenerator.java create mode 100644 teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/DefaultLocaleMetadataGenerator.java create mode 100644 teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/LocaleMetadataGenerator.java diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/impl/JCLPlugin.java b/teavm-classlib/src/main/java/org/teavm/classlib/impl/JCLPlugin.java index cb3702e93..0fcb68aed 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/impl/JCLPlugin.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/impl/JCLPlugin.java @@ -47,7 +47,6 @@ public class JCLPlugin implements TeaVMPlugin { host.registerService(CLDRReader.class, new CLDRReader(host.getProperties(), host.getClassLoader())); Generator localeGen = new LocaleSettingsNativeGenerator(host.getClassLoader(), host.getProperties()); - host.add(new MethodReference("java.util.Locale", "readCountriesFromCLDR", ValueType.VOID), localeGen); host.add(new MethodReference("java.util.Calendar", "readWeeksFromCDLR", ValueType.VOID), localeGen); host.add(new MethodReference(CLDRHelper.class.getName(), "readLikelySubtagsFromCLDR", ValueType.VOID), localeGen); 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 259a76b40..5f579335c 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 @@ -61,4 +61,10 @@ public class CLDRHelper { @MetadataProvider(LanguageMetadataGenerator.class) public static native ResourceMap> getLanguagesMap(); + + @MetadataProvider(CountryMetadataGenerator.class) + public static native ResourceMap> getCountriesMap(); + + @MetadataProvider(DefaultLocaleMetadataGenerator.class) + public static native StringResource getDefaultLocale(); } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/CountryMetadataGenerator.java b/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/CountryMetadataGenerator.java new file mode 100644 index 000000000..ac422e66b --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/CountryMetadataGenerator.java @@ -0,0 +1,29 @@ +/* + * 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; + +/** + * + * @author Alexey Andreev + */ +public class CountryMetadataGenerator extends LocaleMetadataGenerator { + @Override + protected Map getNameMap(CLDRLocale locale) { + return locale.getTerritories(); + } +} diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/DefaultLocaleMetadataGenerator.java b/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/DefaultLocaleMetadataGenerator.java new file mode 100644 index 000000000..4bfb05ecd --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/DefaultLocaleMetadataGenerator.java @@ -0,0 +1,35 @@ +/* + * 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.model.MethodReference; +import org.teavm.platform.metadata.MetadataGenerator; +import org.teavm.platform.metadata.MetadataGeneratorContext; +import org.teavm.platform.metadata.Resource; +import org.teavm.platform.metadata.StringResource; + +/** + * + * @author Alexey Andreev + */ +public class DefaultLocaleMetadataGenerator implements MetadataGenerator { + @Override + public Resource generateMetadata(MetadataGeneratorContext context, MethodReference method) { + StringResource result = context.createResource(StringResource.class); + result.setValue(context.getProperties().getProperty("java.util.Locale.default", "en_EN")); + return result; + } +} diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/LanguageMetadataGenerator.java b/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/LanguageMetadataGenerator.java index fd5f20c96..b3123b644 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/LanguageMetadataGenerator.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/LanguageMetadataGenerator.java @@ -16,28 +16,14 @@ 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 LanguageMetadataGenerator implements MetadataGenerator { +public class LanguageMetadataGenerator extends LocaleMetadataGenerator { @Override - public Resource generateMetadata(MetadataGeneratorContext context, MethodReference method) { - ResourceMap> languages = context.createResourceMap(); - CLDRReader reader = context.getService(CLDRReader.class); - for (Map.Entry entry : reader.getKnownLocales().entrySet()) { - CLDRLocale locale = entry.getValue(); - ResourceMap languageNames = context.createResourceMap(); - languages.put(entry.getKey(), languageNames); - for (Map.Entry language : locale.getLanguages().entrySet()) { - StringResource languageName = context.createResource(StringResource.class); - languageName.setValue(language.getValue()); - languageNames.put(language.getKey(), languageName); - } - } - return languages; + protected Map getNameMap(CLDRLocale locale) { + return locale.getLanguages(); } } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/LocaleMetadataGenerator.java b/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/LocaleMetadataGenerator.java new file mode 100644 index 000000000..c872e6d8d --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/LocaleMetadataGenerator.java @@ -0,0 +1,45 @@ +/* + * 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 abstract class LocaleMetadataGenerator implements MetadataGenerator { + @Override + public Resource generateMetadata(MetadataGeneratorContext context, MethodReference method) { + ResourceMap> result = context.createResourceMap(); + CLDRReader reader = context.getService(CLDRReader.class); + for (Map.Entry entry : reader.getKnownLocales().entrySet()) { + CLDRLocale locale = entry.getValue(); + ResourceMap names = context.createResourceMap(); + result.put(entry.getKey(), names); + for (Map.Entry nameEntry : getNameMap(locale).entrySet()) { + StringResource name = context.createResource(StringResource.class); + name.setValue(nameEntry.getValue()); + names.put(nameEntry.getKey(), name); + } + } + return result; + } + + protected abstract Map getNameMap(CLDRLocale locale); +} diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/LocaleNativeGenerator.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/LocaleNativeGenerator.java index ff0d18604..8ad145288 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/LocaleNativeGenerator.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/LocaleNativeGenerator.java @@ -32,23 +32,6 @@ public class LocaleNativeGenerator implements Generator, DependencyPlugin { @Override public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) throws IOException { switch (methodRef.getName()) { - case "prepareCLDR": - writer.appendClass("java.util.Locale").append(".$CLDR = {};").softNewLine(); - break; - case "getDisplayCountry": - writer.append("var result = ").appendClass("java.util.Locale").append(".$CLDR.territories[$rt_ustr(") - .append(context.getParameterName(1)).append(")];").softNewLine(); - writer.append("result = result ? result[$rt_ustr(") - .append(context.getParameterName(2)).append(")] : undefined;").softNewLine(); - writer.append("return result ? $rt_str(result) : null").softNewLine(); - break; - case "getDisplayLanguage": - writer.append("var result = ").appendClass("java.util.Locale").append(".$CLDR.languages[$rt_ustr(") - .append(context.getParameterName(1)).append(")];").softNewLine(); - writer.append("result = result ? result[$rt_ustr(") - .append(context.getParameterName(2)).append(")] : undefined;").softNewLine(); - writer.append("return result ? $rt_str(result) : null;").softNewLine(); - break; case "getAvailableLocaleStrings": generateAvailableLocales(writer); break; @@ -69,7 +52,6 @@ public class LocaleNativeGenerator implements Generator, DependencyPlugin { @Override public void methodAchieved(DependencyAgent agent, MethodDependency method) { switch (method.getMethod().getName()) { - case "getDefaultLocale": case "getDisplayCountry": case "getDisplayLanguage": method.getResult().propagate("java.lang.String"); diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/LocaleSettingsNativeGenerator.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/LocaleSettingsNativeGenerator.java index 14f756e49..a161ffc94 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/LocaleSettingsNativeGenerator.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/LocaleSettingsNativeGenerator.java @@ -193,9 +193,6 @@ public class LocaleSettingsNativeGenerator implements Generator { public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) throws IOException { init(); switch (methodRef.getName()) { - case "readCountriesFromCLDR": - generateReadCountriesFromCLDR(writer); - break; case "readWeeksFromCDLR": generateReadWeeksFromCDLR(writer); break; @@ -205,9 +202,6 @@ public class LocaleSettingsNativeGenerator implements Generator { case "readAvailableLocales": generateReadAvailableLocales(writer); break; - case "getDefaultLocale": - generateGetDefaultLocale(writer); - break; } } @@ -232,33 +226,6 @@ public class LocaleSettingsNativeGenerator implements Generator { writer.append("];").softNewLine(); } - private void generateReadCountriesFromCLDR(SourceWriter writer) throws IOException { - generateDefender(writer, "territories"); - writer.appendClass("java.util.Locale").append(".$CLDR.territories = {").indent().softNewLine(); - boolean firstLocale = true; - for (Map.Entry entry : knownLocales.entrySet()) { - if (!firstLocale) { - writer.append(",").softNewLine(); - } - firstLocale = false; - writer.append('"').append(Renderer.escapeString(entry.getKey())).append('"').ws().append(":").ws() - .append('{').indent().softNewLine(); - - boolean first = true; - for (Map.Entry langEntry : entry.getValue().territories.entrySet()) { - if (!first) { - writer.append(',').softNewLine(); - } - first = false; - writer.append('"').append(Renderer.escapeString(langEntry.getKey())).append('"').ws().append(':') - .ws().append('"').append(Renderer.escapeString(langEntry.getValue())).append('"'); - } - - writer.outdent().append('}'); - } - writer.outdent().append("};").softNewLine(); - } - private void generateReadWeeksFromCDLR(SourceWriter writer) throws IOException { generateDefender(writer, "minDays"); writer.appendClass("java.util.Locale").append(".$CLDR.minDays = {").indent().softNewLine(); @@ -301,11 +268,6 @@ public class LocaleSettingsNativeGenerator implements Generator { writer.outdent().append("};").softNewLine(); } - private void generateGetDefaultLocale(SourceWriter writer) throws IOException { - String locale = properties.getProperty("java.util.Locale.default", "en_EN"); - writer.append("return $rt_str(\"").append(Renderer.escapeString(locale)).append("\");").softNewLine(); - } - static class LocaleInfo { Map languages = new LinkedHashMap<>(); Map territories = new LinkedHashMap<>(); diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TLocale.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TLocale.java index 5fc084da3..587cc60b2 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TLocale.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TLocale.java @@ -68,26 +68,15 @@ public final class TLocale implements TCloneable, TSerializable { private static TLocale[] availableLocales; static { - String localeName = getDefaultLocale(); + String localeName = CLDRHelper.getDefaultLocale().getValue(); int countryIndex = localeName.indexOf('_'); defaultLocale = new TLocale(localeName.substring(0, countryIndex), localeName.substring(countryIndex) + 1, ""); - prepareCLDR(); } private transient String countryCode; private transient String languageCode; private transient String variantCode; - // Redefined by JCLPlugin - @PluggableDependency(LocaleNativeGenerator.class) - private static native String getDefaultLocale(); - - @GeneratedBy(LocaleNativeGenerator.class) - private static native void prepareCLDR(); - - // Redefined by JCLPlugin - private static native void readCountriesFromCLDR(); - // Redefined by JCLPlugin private static native void readAvailableLocales(); @@ -172,7 +161,6 @@ public final class TLocale implements TCloneable, TSerializable { } public String getDisplayCountry(TLocale locale) { - readCountriesFromCLDR(); String result = getDisplayCountry(locale.getLanguage() + "-" + locale.getCountry(), countryCode); if (result == null) { result = getDisplayCountry(locale.getLanguage(), countryCode); @@ -180,9 +168,16 @@ public final class TLocale implements TCloneable, TSerializable { return result != null ? result : countryCode; } - @GeneratedBy(LocaleNativeGenerator.class) - @PluggableDependency(LocaleNativeGenerator.class) - private static native String getDisplayCountry(String localeName, String country); + private static String getDisplayCountry(String localeName, String country) { + if (!CLDRHelper.getCountriesMap().has(localeName)) { + return null; + } + ResourceMap countries = CLDRHelper.getCountriesMap().get(localeName); + if (!countries.has(country)) { + return null; + } + return countries.get(country).getValue(); + } public final String getDisplayLanguage() { return getDisplayLanguage(getDefault());