diff options
Diffstat (limited to 'tools')
10 files changed, 261 insertions, 141 deletions
diff --git a/tools/dicttool/Android.mk b/tools/dicttool/Android.mk index f49dee70f..e12d7e0b5 100644 --- a/tools/dicttool/Android.mk +++ b/tools/dicttool/Android.mk @@ -85,7 +85,6 @@ LOCAL_JAVA_LIBRARIES := junit LOCAL_ADDITIONAL_DEPENDENCIES := $(LATINIME_HOST_NATIVE_LIBNAME) LOCAL_JAR_MANIFEST := etc/manifest.txt LOCAL_MODULE := dicttool_aosp -LOCAL_IS_HOST_MODULE := true include $(BUILD_HOST_JAVA_LIBRARY) include $(LOCAL_PATH)/etc/Android.mk diff --git a/tools/dicttool/NativeLib.mk b/tools/dicttool/NativeLib.mk index 0d3507be3..74034482b 100644 --- a/tools/dicttool/NativeLib.mk +++ b/tools/dicttool/NativeLib.mk @@ -29,13 +29,7 @@ ifeq ($(FLAG_DBG), true) LOCAL_CFLAGS += -DFLAG_DBG -funwind-tables -fno-inline endif #FLAG_DBG -ifneq ($(strip $(HOST_JDK_IS_64BIT_VERSION)),) -LOCAL_CFLAGS += -m64 -LOCAL_LDFLAGS += -m64 -endif #HOST_JDK_IS_64BIT_VERSION - -LOCAL_CFLAGS += -DHOST_TOOL -fPIC -Wno-deprecated -LOCAL_NO_DEFAULT_COMPILER_FLAGS := true +LOCAL_CFLAGS += -DHOST_TOOL -fPIC -Wno-deprecated -Wno-unused-parameter -Wno-unused-function LOCAL_CLANG := true # For C++11 @@ -44,7 +38,6 @@ LOCAL_CFLAGS += -std=c++11 LATINIME_NATIVE_JNI_DIR := $(LATINIME_DIR_RELATIVE_TO_DICTTOOL)/native/jni LATINIME_NATIVE_SRC_DIR := $(LATINIME_DIR_RELATIVE_TO_DICTTOOL)/native/jni/src LOCAL_C_INCLUDES := $(LOCAL_PATH)/$(LATINIME_NATIVE_SRC_DIR) -# Used in jni_common.cpp to avoid registering useless methods. include $(LOCAL_PATH)/$(LATINIME_NATIVE_JNI_DIR)/NativeFileList.mk @@ -53,7 +46,6 @@ LOCAL_SRC_FILES := \ $(addprefix $(LATINIME_NATIVE_SRC_DIR)/, $(LATIN_IME_CORE_SRC_FILES)) LOCAL_MODULE := $(LATINIME_HOST_NATIVE_LIBNAME) -LOCAL_IS_HOST_MODULE := true include $(BUILD_HOST_SHARED_LIBRARY) diff --git a/tools/dicttool/etc/dicttool_aosp b/tools/dicttool/etc/dicttool_aosp index 65a1c3a1c..09d65c691 100755 --- a/tools/dicttool/etc/dicttool_aosp +++ b/tools/dicttool/etc/dicttool_aosp @@ -68,5 +68,14 @@ else libpath="$frameworkdir/$lib" fi +# Check if the host Java executable supports a 32-bit JVM. It needs to do because the JNI +# library is 32-bit. +${DICTTOOL_JAVA-java} -d32 -version > /dev/null 2>&1 +if [[ $? != 0 ]] ; then + echo Please specify a Java executable that supports a 32-bit JVM as DICTTOOL_JAVA. + exit 1 +fi + # might need more memory, e.g. -Xmx128M -exec java -ea -classpath "$libpath":"$jarpath" -Djava.library.path="$libdir" "$classname" "$@" +exec ${DICTTOOL_JAVA-java} -d32 -ea -classpath "$libpath":"$jarpath" \ + -Djava.library.path="$libdir" "$classname" "$@" diff --git a/tools/make-keyboard-text/res/src/com/android/inputmethod/keyboard/internal/KeyboardTextsTable.tmpl b/tools/make-keyboard-text/res/src/com/android/inputmethod/keyboard/internal/KeyboardTextsTable.tmpl index b25bfb204..2b5494fa5 100644 --- a/tools/make-keyboard-text/res/src/com/android/inputmethod/keyboard/internal/KeyboardTextsTable.tmpl +++ b/tools/make-keyboard-text/res/src/com/android/inputmethod/keyboard/internal/KeyboardTextsTable.tmpl @@ -19,6 +19,7 @@ package com.android.inputmethod.keyboard.internal; import com.android.inputmethod.latin.utils.CollectionUtils; import java.util.HashMap; +import java.util.Locale; /** * !!!!! DO NOT EDIT THIS FILE !!!!! @@ -44,19 +45,19 @@ import java.util.HashMap; public final class KeyboardTextsTable { // Name to index map. private static final HashMap<String, Integer> sNameToIndexesMap = CollectionUtils.newHashMap(); - // Language to texts table map. - private static final HashMap<String, String[]> sLanguageToTextsTableMap = + // Locale to texts table map. + private static final HashMap<String, String[]> sLocaleToTextsTableMap = CollectionUtils.newHashMap(); // TODO: Remove this variable after debugging. - // Texts table to language maps. - private static final HashMap<String[], String> sTextsTableToLanguageMap = + // Texts table to locale maps. + private static final HashMap<String[], String> sTextsTableToLocaleMap = CollectionUtils.newHashMap(); public static String getText(final String name, final String[] textsTable) { final Integer indexObj = sNameToIndexesMap.get(name); if (indexObj == null) { - throw new RuntimeException("Unknown text name=" + name + " language=" - + sTextsTableToLanguageMap.get(textsTable)); + throw new RuntimeException("Unknown text name=" + name + " locale=" + + sTextsTableToLocaleMap.get(textsTable)); } final int index = indexObj; final String text = (index < textsTable.length) ? textsTable[index] : null; @@ -64,17 +65,24 @@ public final class KeyboardTextsTable { return text; } // Sanity check. - if (index >= 0 && index < LANGUAGE_DEFAULT.length) { - return LANGUAGE_DEFAULT[index]; + if (index >= 0 && index < TEXTS_DEFAULT.length) { + return TEXTS_DEFAULT[index]; } // Throw exception for debugging purpose. throw new RuntimeException("Illegal index=" + index + " for name=" + name - + " language=" + sTextsTableToLanguageMap.get(textsTable)); + + " locale=" + sTextsTableToLocaleMap.get(textsTable)); } - public static String[] getTextsTable(final String language) { - final String[] textsTable = sLanguageToTextsTableMap.get(language); - return textsTable != null ? textsTable : LANGUAGE_DEFAULT; + public static String[] getTextsTable(final Locale locale) { + final String localeKey = locale.toString(); + if (sLocaleToTextsTableMap.containsKey(localeKey)) { + return sLocaleToTextsTableMap.get(localeKey); + } + final String languageKey = locale.getLanguage(); + if (sLocaleToTextsTableMap.containsKey(languageKey)) { + return sLocaleToTextsTableMap.get(languageKey); + } + return TEXTS_DEFAULT; } private static final String[] NAMES = { @@ -85,16 +93,14 @@ public final class KeyboardTextsTable { private static final String EMPTY = ""; /* Default texts */ - private static final String[] LANGUAGE_DEFAULT = { + private static final String[] TEXTS_DEFAULT = { /* @DEFAULT_TEXTS@ */ }; /* @TEXTS@ */ - // TODO: Use the language + "_" + region representation for the locale string key. - // Currently we are dropping the region from the key. - private static final Object[] LANGUAGES_AND_TEXTS = { + private static final Object[] LOCALES_AND_TEXTS = { // "locale", TEXT_ARRAY, /* numberOfNonNullText/lengthOf_TEXT_ARRAY localeName */ - /* @LANGUAGES_AND_TEXTS@ */ + /* @LOCALES_AND_TEXTS@ */ }; static { @@ -102,11 +108,11 @@ public final class KeyboardTextsTable { sNameToIndexesMap.put(NAMES[index], index); } - for (int i = 0; i < LANGUAGES_AND_TEXTS.length; i += 2) { - final String language = (String)LANGUAGES_AND_TEXTS[i]; - final String[] textsTable = (String[])LANGUAGES_AND_TEXTS[i + 1]; - sLanguageToTextsTableMap.put(language, textsTable); - sTextsTableToLanguageMap.put(textsTable, language); + for (int i = 0; i < LOCALES_AND_TEXTS.length; i += 2) { + final String locale = (String)LOCALES_AND_TEXTS[i]; + final String[] textsTable = (String[])LOCALES_AND_TEXTS[i + 1]; + sLocaleToTextsTableMap.put(locale, textsTable); + sTextsTableToLocaleMap.put(textsTable, locale); } } } diff --git a/tools/make-keyboard-text/res/values-de/donottranslate-more-keys.xml b/tools/make-keyboard-text/res/values-de/donottranslate-more-keys.xml index 94b17243c..0c6d3ad2b 100644 --- a/tools/make-keyboard-text/res/values-de/donottranslate-more-keys.xml +++ b/tools/make-keyboard-text/res/values-de/donottranslate-more-keys.xml @@ -26,7 +26,7 @@ U+00E3: "ã" LATIN SMALL LETTER A WITH TILDE U+00E5: "å" LATIN SMALL LETTER A WITH RING ABOVE U+0101: "ā" LATIN SMALL LETTER A WITH MACRON --> - <string name="morekeys_a">ä,â,à,á,æ,ã,å,ā</string> + <string name="morekeys_a">ä,%,â,à,á,æ,ã,å,ā</string> <!-- U+00E9: "é" LATIN SMALL LETTER E WITH ACUTE U+00E8: "è" LATIN SMALL LETTER E WITH GRAVE U+00EA: "ê" LATIN SMALL LETTER E WITH CIRCUMFLEX @@ -41,13 +41,13 @@ U+0153: "œ" LATIN SMALL LIGATURE OE U+00F8: "ø" LATIN SMALL LETTER O WITH STROKE U+014D: "ō" LATIN SMALL LETTER O WITH MACRON --> - <string name="morekeys_o">ö,ô,ò,ó,õ,œ,ø,ō</string> + <string name="morekeys_o">ö,%,ô,ò,ó,õ,œ,ø,ō</string> <!-- U+00FC: "ü" LATIN SMALL LETTER U WITH DIAERESIS U+00FB: "û" LATIN SMALL LETTER U WITH CIRCUMFLEX U+00F9: "ù" LATIN SMALL LETTER U WITH GRAVE U+00FA: "ú" LATIN SMALL LETTER U WITH ACUTE U+016B: "ū" LATIN SMALL LETTER U WITH MACRON --> - <string name="morekeys_u">ü,û,ù,ú,ū</string> + <string name="morekeys_u">ü,%,û,ù,ú,ū</string> <!-- U+00DF: "ß" LATIN SMALL LETTER SHARP S U+015B: "ś" LATIN SMALL LETTER S WITH ACUTE U+0161: "š" LATIN SMALL LETTER S WITH CARON --> diff --git a/tools/make-keyboard-text/res/values-it/donottranslate-more-keys.xml b/tools/make-keyboard-text/res/values-it/donottranslate-more-keys.xml index 6492553ed..e809f4835 100644 --- a/tools/make-keyboard-text/res/values-it/donottranslate-more-keys.xml +++ b/tools/make-keyboard-text/res/values-it/donottranslate-more-keys.xml @@ -59,4 +59,16 @@ U+00FC: "ü" LATIN SMALL LETTER U WITH DIAERESIS U+016B: "ū" LATIN SMALL LETTER U WITH MACRON --> <string name="morekeys_u">ù,ú,û,ü,ū</string> + <!-- U+00FC: "ü" LATIN SMALL LETTER U WITH DIAERESIS --> + <string name="keyspec_swiss_row1_11">ü</string> + <!-- U+00E8: "è" LATIN SMALL LETTER E WITH GRAVE --> + <string name="morekeys_swiss_row1_11">è</string> + <!-- U+00F6: "ö" LATIN SMALL LETTER O WITH DIAERESIS --> + <string name="keyspec_swiss_row2_10">ö</string> + <!-- U+00E9: "é" LATIN SMALL LETTER E WITH ACUTE --> + <string name="morekeys_swiss_row2_10">é</string> + <!-- U+00E4: "ä" LATIN SMALL LETTER A WITH DIAERESIS --> + <string name="keyspec_swiss_row2_11">ä</string> + <!-- U+00E0: "à" LATIN SMALL LETTER A WITH GRAVE --> + <string name="morekeys_swiss_row2_11">à</string> </resources> diff --git a/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/JarUtils.java b/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/JarUtils.java index a74096e79..c947a63bf 100644 --- a/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/JarUtils.java +++ b/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/JarUtils.java @@ -16,6 +16,7 @@ package com.android.inputmethod.keyboard.tools; +import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; @@ -23,6 +24,7 @@ import java.net.URL; import java.net.URLDecoder; import java.util.ArrayList; import java.util.Enumeration; +import java.util.Locale; import java.util.jar.JarEntry; import java.util.jar.JarFile; @@ -58,7 +60,7 @@ public final class JarUtils { public boolean accept(String dirName, String name); } - public static ArrayList<String> getNameListing(final JarFile jar, final JarFilter filter) { + public static ArrayList<String> getEntryNameListing(final JarFile jar, final JarFilter filter) { final ArrayList<String> result = new ArrayList<String>(); final Enumeration<JarEntry> entries = jar.entries(); while (entries.hasMoreElements()) { @@ -74,12 +76,42 @@ public final class JarUtils { return result; } - public static ArrayList<String> getNameListing(final JarFile jar, final String filterName) { - return getNameListing(jar, new JarFilter() { + public static ArrayList<String> getEntryNameListing(final JarFile jar, + final String filterName) { + return getEntryNameListing(jar, new JarFilter() { @Override public boolean accept(final String dirName, final String name) { return name.equals(filterName); } }); } + + // The locale is taken from string resource jar entry name (values-<locale>/) + // or {@link LocaleUtils#DEFAULT_LOCALE} for the default string resource + // directory (values/). + public static Locale getLocaleFromEntryName(final String jarEntryName) { + final String dirName = jarEntryName.substring(0, jarEntryName.lastIndexOf('/')); + final int pos = dirName.lastIndexOf('/'); + final String parentName = (pos >= 0) ? dirName.substring(pos + 1) : dirName; + final int localePos = parentName.indexOf('-'); + if (localePos < 0) { + // Default resource name. + return LocaleUtils.DEFAULT_LOCALE; + } + final String localeStr = parentName.substring(localePos + 1); + final int regionPos = localeStr.indexOf("-r"); + if (regionPos < 0) { + return LocaleUtils.constructLocaleFromString(localeStr); + } + return LocaleUtils.constructLocaleFromString(localeStr.replace("-r", "_")); + } + + public static void close(final Closeable stream) { + try { + if (stream != null) { + stream.close(); + } + } catch (IOException e) { + } + } } diff --git a/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/LocaleUtils.java b/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/LocaleUtils.java index 9fdc1f607..0dfa37667 100644 --- a/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/LocaleUtils.java +++ b/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/LocaleUtils.java @@ -26,36 +26,142 @@ import java.util.Locale; * for the make-keyboard-text tool. */ public final class LocaleUtils { + public static final Locale DEFAULT_LOCALE = Locale.ROOT; + private static final String DEFAULT_LOCALE_CODE = "DEFAULT"; + public static final String NO_LANGUAGE_LOCALE_CODE = "zz"; + public static final String NO_LANGUAGE_LOCALE_DISPLAY_NAME = "Alphabet"; + private LocaleUtils() { // Intentional empty constructor for utility class. } private static final HashMap<String, Locale> sLocaleCache = new HashMap<String, Locale>(); + private static final int INDEX_LANGUAGE = 0; + private static final int INDEX_SCRIPT = 1; + private static final int INDEX_REGION = 2; + private static final int ELEMENT_LIMIT = INDEX_REGION + 1; + /** * Creates a locale from a string specification. + * + * Locale string is: language(_script)?(_region)? + * where: language := [a-zA-Z]{2,3} + * script := [a-zA-Z]{4} + * region := [a-zA-Z]{2,3}|[0-9]{3} */ public static Locale constructLocaleFromString(final String localeStr) { if (localeStr == null) { return null; } synchronized (sLocaleCache) { - Locale retval = sLocaleCache.get(localeStr); - if (retval != null) { - return retval; + if (sLocaleCache.containsKey(localeStr)) { + return sLocaleCache.get(localeStr); + } + boolean hasRegion = false; + final Locale.Builder builder = new Locale.Builder(); + final String[] localeElements = localeStr.split("_", ELEMENT_LIMIT); + if (localeElements.length > INDEX_LANGUAGE) { + final String text = localeElements[INDEX_LANGUAGE]; + if (isValidLanguage(text)) { + builder.setLanguage(text); + } else { + throw new RuntimeException("Unknown locale format: " + localeStr); + } + } + if (localeElements.length > INDEX_SCRIPT) { + final String text = localeElements[INDEX_SCRIPT]; + if (isValidScript(text)) { + builder.setScript(text); + } else if (isValidRegion(text)) { + builder.setRegion(text); + hasRegion = true; + } else { + throw new RuntimeException("Unknown locale format: " + localeStr); + } } - String[] localeParams = localeStr.split("_", 3); - if (localeParams.length == 1) { - retval = new Locale(localeParams[0]); - } else if (localeParams.length == 2) { - retval = new Locale(localeParams[0], localeParams[1]); - } else if (localeParams.length == 3) { - retval = new Locale(localeParams[0], localeParams[1], localeParams[2]); + if (localeElements.length > INDEX_REGION) { + final String text = localeElements[INDEX_REGION]; + if (!hasRegion && isValidRegion(text)) { + builder.setRegion(text); + } else { + throw new RuntimeException("Unknown locale format: " + localeStr); + } } - if (retval != null) { - sLocaleCache.put(localeStr, retval); + final Locale locale = builder.build(); + sLocaleCache.put(localeStr, locale); + return locale; + } + } + + private static final int MIN_LENGTH_OF_LANGUAGE = 2; + private static final int MAX_LENGTH_OF_LANGUAGE = 2; + private static final int LENGTH_OF_SCRIPT = 4; + private static final int MIN_LENGTH_OF_REGION = 2; + private static final int MAX_LENGTH_OF_REGION = 2; + private static final int LENGTH_OF_AREA_CODE = 3; + + private static boolean isValidLanguage(final String text) { + return isAlphabetSequence(text, MIN_LENGTH_OF_LANGUAGE, MAX_LENGTH_OF_LANGUAGE); + } + + private static boolean isValidScript(final String text) { + return isAlphabetSequence(text, LENGTH_OF_SCRIPT, LENGTH_OF_SCRIPT); + } + + private static boolean isValidRegion(final String text) { + return isAlphabetSequence(text, MIN_LENGTH_OF_REGION, MAX_LENGTH_OF_REGION) + || isDigitSequence(text, LENGTH_OF_AREA_CODE, LENGTH_OF_AREA_CODE); + } + + private static boolean isAlphabetSequence(final String text, final int lower, final int upper) { + final int length = text.length(); + if (length < lower || length > upper) { + return false; + } + for (int index = 0; index < length; index++) { + if (!isAsciiAlphabet(text.charAt(index))) { + return false; } - return retval; } + return true; + } + + private static boolean isDigitSequence(final String text, final int lower, final int upper) { + final int length = text.length(); + if (length < lower || length > upper) { + return false; + } + for (int index = 0; index < length; ++index) { + if (!isAsciiDigit(text.charAt(index))) { + return false; + } + } + return true; + } + + private static boolean isAsciiAlphabet(char c) { + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); + } + + private static boolean isAsciiDigit(char c) { + return c >= '0' && c <= '9'; + } + + public static String getLocaleCode(final Locale locale) { + if (locale == DEFAULT_LOCALE) { + return DEFAULT_LOCALE_CODE; + } + return locale.toString(); + } + + public static String getLocaleDisplayName(final Locale locale) { + if (locale == DEFAULT_LOCALE) { + return DEFAULT_LOCALE_CODE; + } + if (locale.getLanguage().equals(NO_LANGUAGE_LOCALE_CODE)) { + return NO_LANGUAGE_LOCALE_DISPLAY_NAME; + } + return locale.getDisplayName(Locale.ENGLISH); } } diff --git a/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/MoreKeysResources.java b/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/MoreKeysResources.java index 9bb2b38dd..c8cb4acec 100644 --- a/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/MoreKeysResources.java +++ b/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/MoreKeysResources.java @@ -16,10 +16,8 @@ package com.android.inputmethod.keyboard.tools; -import java.io.Closeable; import java.io.File; import java.io.IOException; -import java.io.InputStream; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.io.PrintStream; @@ -28,6 +26,7 @@ import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Locale; +import java.util.TreeMap; import java.util.jar.JarFile; public class MoreKeysResources { @@ -37,21 +36,15 @@ public class MoreKeysResources { private static final String MARK_NAMES = "@NAMES@"; private static final String MARK_DEFAULT_TEXTS = "@DEFAULT_TEXTS@"; private static final String MARK_TEXTS = "@TEXTS@"; - private static final String MARK_LANGUAGES_AND_TEXTS = "@LANGUAGES_AND_TEXTS@"; - private static final String DEFAULT_LANGUAGE_NAME = "DEFAULT"; + private static final String TEXTS_ARRAY_NAME_PREFIX = "TEXTS_"; + private static final String MARK_LOCALES_AND_TEXTS = "@LOCALES_AND_TEXTS@"; private static final String EMPTY_STRING_VAR = "EMPTY"; - private static final String NO_LANGUAGE_CODE = "zz"; - private static final String NO_LANGUAGE_DISPLAY_NAME = "Alphabet"; - private final JarFile mJar; - // Language to string resources map. - private final HashMap<String, StringResourceMap> mResourcesMap = - new HashMap<String, StringResourceMap>(); - // Sorted languages list. The language is taken from string resource directories - // (values-<language>/) or {@link #DEFAULT_LANGUAGE_NAME} for the default string resource - // directory (values/). - private final ArrayList<String> mSortedLanguagesList = new ArrayList<String>(); + // String resources maps sorted by its language. The language is determined from the jar entry + // name by calling {@link JarUtils#getLocaleFromEntryName(String)}. + private final TreeMap<String, StringResourceMap> mResourcesMap = + new TreeMap<String, StringResourceMap>(); // Default string resources map. private final StringResourceMap mDefaultResourceMap; // Histogram of string resource names. This is used to sort {@link #mSortedResourceNames}. @@ -64,22 +57,14 @@ public class MoreKeysResources { public MoreKeysResources(final JarFile jar) { mJar = jar; - final ArrayList<String> resources = JarUtils.getNameListing(jar, TEXT_RESOURCE_NAME); - for (final String name : resources) { - final String dirName = name.substring(0, name.lastIndexOf('/')); - final int pos = dirName.lastIndexOf('/'); - final String parentName = (pos >= 0) ? dirName.substring(pos + 1) : dirName; - final String language = getLanguageFromResDir(parentName); - final InputStream stream = JarUtils.openResource(name); - try { - mResourcesMap.put(language, new StringResourceMap(stream)); - } finally { - close(stream); - } + final ArrayList<String> resourceEntryNames = JarUtils.getEntryNameListing( + jar, TEXT_RESOURCE_NAME); + for (final String entryName : resourceEntryNames) { + final StringResourceMap resMap = new StringResourceMap(entryName); + mResourcesMap.put(LocaleUtils.getLocaleCode(resMap.mLocale), resMap); } - mDefaultResourceMap = mResourcesMap.get(DEFAULT_LANGUAGE_NAME); - mSortedLanguagesList.addAll(mResourcesMap.keySet()); - Collections.sort(mSortedLanguagesList); + mDefaultResourceMap = mResourcesMap.get( + LocaleUtils.getLocaleCode(LocaleUtils.DEFAULT_LOCALE)); // Initialize name histogram and names list. final HashMap<String, Integer> nameHistogram = mNameHistogram; @@ -89,12 +74,12 @@ public class MoreKeysResources { resourceNamesList.add(res.mName); } // Make name histogram. - for (final String language : mResourcesMap.keySet()) { - final StringResourceMap resMap = mResourcesMap.get(language); + for (final String locale : mResourcesMap.keySet()) { + final StringResourceMap resMap = mResourcesMap.get(locale); if (resMap == mDefaultResourceMap) continue; for (final StringResource res : resMap.getResources()) { if (!mDefaultResourceMap.contains(res.mName)) { - throw new RuntimeException(res.mName + " in " + language + throw new RuntimeException(res.mName + " in " + locale + " doesn't have default resource"); } final int histogramValue = nameHistogram.get(res.mName); @@ -118,22 +103,8 @@ public class MoreKeysResources { mSortedResourceNames = resourceNamesList.toArray(new String[resourceNamesList.size()]); } - private static String getLanguageFromResDir(final String dirName) { - final int languagePos = dirName.indexOf('-'); - if (languagePos < 0) { - // Default resource. - return DEFAULT_LANGUAGE_NAME; - } - final String language = dirName.substring(languagePos + 1); - final int countryPos = language.indexOf("-r"); - if (countryPos < 0) { - return language; - } - return language.replace("-r", "_"); - } - public void writeToJava(final String outDir) { - final ArrayList<String> list = JarUtils.getNameListing(mJar, JAVA_TEMPLATE); + final ArrayList<String> list = JarUtils.getEntryNameListing(mJar, JAVA_TEMPLATE); if (list.isEmpty()) { throw new RuntimeException("Can't find java template " + JAVA_TEMPLATE); } @@ -159,8 +130,8 @@ public class MoreKeysResources { } catch (IOException e) { throw new RuntimeException(e); } finally { - close(lnr); - close(ps); + JarUtils.close(lnr); + JarUtils.close(ps); } } @@ -174,8 +145,8 @@ public class MoreKeysResources { dumpDefaultTexts(out); } else if (line.contains(MARK_TEXTS)) { dumpTexts(out); - } else if (line.contains(MARK_LANGUAGES_AND_TEXTS)) { - dumpLanguageMap(out); + } else if (line.contains(MARK_LOCALES_AND_TEXTS)) { + dumpLocalesMap(out); } else { out.println(line); } @@ -196,16 +167,17 @@ public class MoreKeysResources { mDefaultResourceMap.setOutputArraySize(outputArraySize); } - private static String getArrayNameForLanguage(final String language) { - return "LANGUAGE_" + language; + private static String getArrayNameForLocale(final Locale locale) { + return TEXTS_ARRAY_NAME_PREFIX + LocaleUtils.getLocaleCode(locale); } private void dumpTexts(final PrintStream out) { - for (final String language : mSortedLanguagesList) { - final StringResourceMap resMap = mResourcesMap.get(language); + for (final StringResourceMap resMap : mResourcesMap.values()) { + final Locale locale = resMap.mLocale; if (resMap == mDefaultResourceMap) continue; - out.format(" /* Language %s: %s */\n", language, getLanguageDisplayName(language)); - out.format(" private static final String[] " + getArrayNameForLanguage(language) + out.format(" /* Locale %s: %s */\n", + locale, LocaleUtils.getLocaleDisplayName(locale)); + out.format(" private static final String[] " + getArrayNameForLocale(locale) + " = {\n"); final int outputArraySize = dumpTextsInternal(out, resMap); resMap.setOutputArraySize(outputArraySize); @@ -213,28 +185,20 @@ public class MoreKeysResources { } } - private void dumpLanguageMap(final PrintStream out) { - for (final String language : mSortedLanguagesList) { - final StringResourceMap resMap = mResourcesMap.get(language); - final Locale locale = LocaleUtils.constructLocaleFromString(language); - final String languageKeyToDump = locale.getCountry().isEmpty() - ? String.format("\"%s\"", language) - : String.format("\"%s\"", locale.getLanguage()); - out.format(" %s, %-15s /* %3d/%3d %s */\n", - languageKeyToDump, getArrayNameForLanguage(language) + ",", + private void dumpLocalesMap(final PrintStream out) { + for (final StringResourceMap resMap : mResourcesMap.values()) { + final Locale locale = resMap.mLocale; + final String localeStr = LocaleUtils.getLocaleCode(locale); + final String localeToDump = (locale == LocaleUtils.DEFAULT_LOCALE) + ? String.format("\"%s\"", localeStr) + : String.format("\"%s\"%s", localeStr, " ".substring(localeStr.length())); + out.format(" %s, %-12s /* %3d/%3d %s */\n", + localeToDump, getArrayNameForLocale(locale) + ",", resMap.getResources().size(), resMap.getOutputArraySize(), - getLanguageDisplayName(language)); + LocaleUtils.getLocaleDisplayName(locale)); } } - private static String getLanguageDisplayName(final String language) { - final Locale locale = LocaleUtils.constructLocaleFromString(language); - if (locale.getLanguage().equals(NO_LANGUAGE_CODE)) { - return NO_LANGUAGE_DISPLAY_NAME; - } - return locale.getDisplayName(Locale.ENGLISH); - } - private int dumpTextsInternal(final PrintStream out, final StringResourceMap resMap) { final ArrayInitializerFormatter formatter = new ArrayInitializerFormatter(out, 100, " ", mSortedResourceNames); @@ -289,13 +253,4 @@ public class MoreKeysResources { } return sb.toString(); } - - private static void close(final Closeable stream) { - try { - if (stream != null) { - stream.close(); - } - } catch (IOException e) { - } - } } diff --git a/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/StringResourceMap.java b/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/StringResourceMap.java index 4eff8a24b..6a79268e5 100644 --- a/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/StringResourceMap.java +++ b/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/StringResourceMap.java @@ -27,6 +27,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; +import java.util.Locale; import java.util.Map; import javax.xml.parsers.ParserConfigurationException; @@ -34,6 +35,8 @@ import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; public class StringResourceMap { + // Locale of this string resource map. + public final Locale mLocale; // String resource list. private final List<StringResource> mResources; // Name to string resource map. @@ -42,25 +45,31 @@ public class StringResourceMap { // The length of String[] that is created from this {@link StringResourceMap}. The length is // calculated in {@link MoreKeysResources#dumpTexts(OutputStream)} and recorded by // {@link #setOutputArraySize(int)}. The recorded length is used as a part of comment by - // {@link MoreKeysResources#dumpLanguageMap(OutputStream)} via {@link #getOutputArraySize()}. + // {@link MoreKeysResources#dumpLocaleMap(OutputStream)} via {@link #getOutputArraySize()}. private int mOutputArraySize; - public StringResourceMap(final InputStream is) { + public StringResourceMap(final String jarEntryName) { + mLocale = JarUtils.getLocaleFromEntryName(jarEntryName); final StringResourceHandler handler = new StringResourceHandler(); final SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); + final InputStream stream = JarUtils.openResource(jarEntryName); try { final SAXParser parser = factory.newSAXParser(); // In order to get comment tag. parser.setProperty("http://xml.org/sax/properties/lexical-handler", handler); - parser.parse(is, handler); + parser.parse(stream, handler); } catch (ParserConfigurationException e) { + throw new RuntimeException(e.getMessage(), e); } catch (SAXParseException e) { throw new RuntimeException(e.getMessage() + " at line " + e.getLineNumber() - + ", column " + e.getColumnNumber()); + + ", column " + e.getColumnNumber(), e); } catch (SAXException e) { - throw new RuntimeException(e.getMessage()); + throw new RuntimeException(e.getMessage(), e); } catch (IOException e) { + throw new RuntimeException(e.getMessage(), e); + } finally { + JarUtils.close(stream); } mResources = Collections.unmodifiableList(handler.mResources); |