new GwtLocaleImpl[localeSet.size()]);
// sort for deterministic output
Arrays.sort(allLocales);
PrintWriter pw = context.tryCreate(logger, packageName, superClassName);
if (pw != null) {
LocalizedProperties displayNames = new LocalizedProperties();
LocalizedProperties displayNamesManual = new LocalizedProperties();
LocalizedProperties displayNamesOverride = new LocalizedProperties();
ClassLoader classLoader = getClass().getClassLoader();
try {
InputStream str = classLoader.getResourceAsStream(GENERATED_LOCALE_NATIVE_DISPLAY_NAMES);
if (str != null) {
displayNames.load(str, "UTF-8");
}
str = classLoader.getResourceAsStream(MANUAL_LOCALE_NATIVE_DISPLAY_NAMES);
if (str != null) {
displayNamesManual.load(str, "UTF-8");
}
str = classLoader.getResourceAsStream(OVERRIDE_LOCALE_NATIVE_DISPLAY_NAMES);
if (str != null) {
displayNamesOverride.load(str, "UTF-8");
}
} catch (UnsupportedEncodingException e) {
// UTF-8 should always be defined
logger.log(TreeLogger.ERROR, "UTF-8 encoding is not defined", e);
throw new UnableToCompleteException();
} catch (IOException e) {
logger.log(TreeLogger.ERROR, "Exception reading locale display names",
e);
throw new UnableToCompleteException();
}
ClassSourceFileComposerFactory factory = new ClassSourceFileComposerFactory(
packageName, superClassName);
factory.setSuperclass(targetClass.getQualifiedSourceName());
factory.addImport(GWT.class.getCanonicalName());
factory.addImport(JavaScriptObject.class.getCanonicalName());
factory.addImport(HashMap.class.getCanonicalName());
SourceWriter writer = factory.createSourceWriter(context, pw);
writer.println("private static native String getLocaleNativeDisplayName(");
writer.println(" JavaScriptObject nativeDisplayNamesNative,String localeName) /*-{");
writer.println(" return nativeDisplayNamesNative[localeName];");
writer.println("}-*/;");
writer.println();
writer.println("HashMap<String,String> nativeDisplayNamesJava;");
writer.println("private JavaScriptObject nativeDisplayNamesNative;");
writer.println();
writer.println("@Override");
writer.println("public String[] getAvailableLocaleNames() {");
writer.println(" return new String[] {");
boolean hasAnyRtl = false;
for (GwtLocaleImpl possibleLocale : allLocales) {
writer.println(" \""
+ possibleLocale.toString().replaceAll("\"", "\\\"") + "\",");
if (RTL_LOCALES.contains(
possibleLocale.getCanonicalForm().getLanguage())) {
hasAnyRtl = true;
}
}
writer.println(" };");
writer.println("}");
writer.println();
writer.println("@Override");
writer.println("public String getLocaleNativeDisplayName(String localeName) {");
writer.println(" if (GWT.isScript()) {");
writer.println(" if (nativeDisplayNamesNative == null) {");
writer.println(" nativeDisplayNamesNative = loadNativeDisplayNamesNative();");
writer.println(" }");
writer.println(" return getLocaleNativeDisplayName(nativeDisplayNamesNative, localeName);");
writer.println(" } else {");
writer.println(" if (nativeDisplayNamesJava == null) {");
writer.println(" nativeDisplayNamesJava = new HashMap<String, String>();");
{
for (GwtLocaleImpl possibleLocale : allLocales) {
String localeName = possibleLocale.toString();
String displayName = displayNamesOverride.getProperty(localeName);
if (displayName == null) {
displayName = displayNamesManual.getProperty(localeName);
}
if (displayName == null) {
displayName = displayNames.getProperty(localeName);
}
if (displayName != null && displayName.length() != 0) {
localeName = quoteQuotes(localeName);
displayName = quoteQuotes(displayName);
writer.println(" nativeDisplayNamesJava.put(\"" + localeName + "\", \"" + displayName + "\");");
}
}
}
writer.println(" }");
writer.println(" return nativeDisplayNamesJava.get(localeName);");
writer.println(" }");
writer.println("}");
writer.println();
writer.println("@Override");
writer.println("public boolean hasAnyRTL() {");
writer.println(" return " + hasAnyRtl + ";");
writer.println("}");
writer.println();
writer.println("private native JavaScriptObject loadNativeDisplayNamesNative() /*-{");
writer.println(" return {");
{
boolean needComma = false;
for (GwtLocaleImpl possibleLocale : allLocales) {
String localeName = possibleLocale.toString();
String displayName = displayNamesOverride.getProperty(localeName);
if (displayName == null) {
displayName = displayNamesManual.getProperty(localeName);
}
if (displayName == null) {
displayName = displayNames.getProperty(localeName);