Author: ppalaga
Date: 2012-07-10 08:17:37 -0400 (Tue, 10 Jul 2012)
New Revision: 8780
Modified:
epp/portal/branches/EPP_5_2_Branch/component/common/src/main/java/org/exoplatform/commons/utils/I18N.java
epp/portal/branches/EPP_5_2_Branch/component/common/src/test/java/org/exoplatform/commons/utils/TestI18N.java
Log:
Bug 794469 - I18N locale identifier parsing code does not support all valid cases in Java
7
Modified:
epp/portal/branches/EPP_5_2_Branch/component/common/src/main/java/org/exoplatform/commons/utils/I18N.java
===================================================================
---
epp/portal/branches/EPP_5_2_Branch/component/common/src/main/java/org/exoplatform/commons/utils/I18N.java 2012-07-09
04:34:49 UTC (rev 8779)
+++
epp/portal/branches/EPP_5_2_Branch/component/common/src/main/java/org/exoplatform/commons/utils/I18N.java 2012-07-10
12:17:37 UTC (rev 8780)
@@ -129,14 +129,26 @@
}
else
{
- for (int i = index + 1;i < s.length();i++)
+ final String variant;
+ if ("ja_JP_JP_#u-ca-japanese".equals(s))
{
- if (!isLetter(s.charAt(i)))
+ variant = "JP";
+ }
+ else if ("th_TH_TH_#u-nu-thai".equals(s))
+ {
+ variant = "TH";
+ }
+ else
+ {
+ for (int i = index + 1;i < s.length();i++)
{
- throw new IllegalArgumentException();
+ if (!isLetter(s.charAt(i)))
+ {
+ throw new IllegalArgumentException("Invalid Java Locale
identifier: '" + s + "'");
+ }
}
+ variant = s.substring(index + 1);
}
- String variant = s.substring(index + 1);
return new Locale(lang, country, variant);
}
}
Modified:
epp/portal/branches/EPP_5_2_Branch/component/common/src/test/java/org/exoplatform/commons/utils/TestI18N.java
===================================================================
---
epp/portal/branches/EPP_5_2_Branch/component/common/src/test/java/org/exoplatform/commons/utils/TestI18N.java 2012-07-09
04:34:49 UTC (rev 8779)
+++
epp/portal/branches/EPP_5_2_Branch/component/common/src/test/java/org/exoplatform/commons/utils/TestI18N.java 2012-07-10
12:17:37 UTC (rev 8780)
@@ -98,12 +98,82 @@
{
for (Locale expected : Locale.getAvailableLocales())
{
- String s = expected.toString();
- Locale parsed = I18N.parseJavaIdentifier(s);
- assertEquals(expected, parsed);
+ if (isJava6Compatible(expected))
+ {
+ String s = expected.toString();
+ Locale parsed = I18N.parseJavaIdentifier(s);
+ assertEquals(expected, parsed);
+ }
}
}
+ /**
+ * Tests if it is possible to construct the given locale using means
+ * available in Java 6 only.
+ *
+ * The given locale instance is considered Java 6 compatible if and only if
+ * no script, calendar, etc. fields are set that are there in the
+ * {@link Locale} class since Java 7 only. However, there are two hard-coded
+ * exceptions to this rule, for which this method returns {@code true}: (i)
+ * "ja_JP_JP_#u-ca-japanese" and (ii) "th_TH_TH_#u-nu-thai". These
two
+ * exceptions are there due to the fact that new Locale("ja",
"JP",
+ * "JP").toString() and new Locale("th", "TH",
"TH").toString() return
+ * "ja_JP_JP_#u-ca-japanese" and "th_TH_TH_#u-nu-thai"
respectively in Java
+ * 7; see <a
+ *
href="http://docs.oracle.com/javase/7/docs/api/java/util/Locale.html...
+ * 7 Locale, Compatibility Section</a>.
+ *
+ * The result is computed through comparing the expected Java 6 locale ID
+ * (e.g. "en_GB") with the result of {@link Locale#toString()}. If the
output
+ * of of {@link Locale#toString()} matches the expected ID (i.e. something of
+ * the form {language}[_[{country}][_{variant}]]) or if
+ * {@link Locale#toString()} equals one of "ja_JP_JP_#u-ca-japanese" or
+ * "th_TH_TH_#u-nu-thai" then {@code true} is returned. Otherwise this
method
+ * returns {@code false}.
+ *
+ * @param locale
+ * @return {@code true} or {@code false}
+ */
+ private static boolean isJava6Compatible(Locale locale)
+ {
+ StringBuilder localeIdBuilder = new StringBuilder(16);
+ localeIdBuilder.append(locale.getLanguage());
+
+ String country = locale.getCountry();
+ boolean hasCountry = country != null && country.length() > 0;
+ String variant = locale.getVariant();
+ boolean hasVariant = variant != null && variant.length() > 0;
+ if (hasCountry || hasVariant)
+ {
+ localeIdBuilder.append('_');
+ if (hasCountry)
+ {
+ localeIdBuilder.append(country);
+ }
+ if (hasVariant)
+ {
+ localeIdBuilder.append('_').append(variant);
+ }
+ }
+ String expectedJava6LocaleId = localeIdBuilder.toString();
+ String foundLocaleId = locale.toString();
+ if ("ja_JP_JP".equals(expectedJava6LocaleId)
+ && "ja_JP_JP_#u-ca-japanese".equals(foundLocaleId))
+ {
+ return true;
+ }
+ else if ("th_TH_TH".equals(expectedJava6LocaleId)
+ && "th_TH_TH_#u-nu-thai".equals(foundLocaleId))
+ {
+ return true;
+ }
+ else
+ {
+ return expectedJava6LocaleId.equals(foundLocaleId);
+ }
+
+ }
+
private void assertNotJavaIdentifier(String s)
{
try