You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2008/06/19 19:52:57 UTC
svn commit: r669600 - in /tapestry/tapestry5/trunk/tapestry-core/src:
main/java/org/apache/tapestry5/internal/services/
main/java/org/apache/tapestry5/internal/util/ site/apt/
test/java/org/apache/tapestry5/internal/util/
Author: hlship
Date: Thu Jun 19 10:52:57 2008
New Revision: 669600
URL: http://svn.apache.org/viewvc?rev=669600&view=rev
Log:
TAPESTRY-1997: PersistentLocale is lower-casing locales
Added:
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/util/LocaleUtils.java
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/util/LocaleUtilsTest.java
Modified:
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/PersistentLocaleImpl.java
tapestry/tapestry5/trunk/tapestry-core/src/site/apt/upgrade.apt
Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/PersistentLocaleImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/PersistentLocaleImpl.java?rev=669600&r1=669599&r2=669600&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/PersistentLocaleImpl.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/PersistentLocaleImpl.java Thu Jun 19 10:52:57 2008
@@ -14,6 +14,7 @@
package org.apache.tapestry5.internal.services;
+import org.apache.tapestry5.internal.util.LocaleUtils;
import org.apache.tapestry5.services.Cookies;
import org.apache.tapestry5.services.PersistentLocale;
@@ -42,7 +43,7 @@
{
String localeCookieValue = getCookieValue();
- return localeCookieValue != null ? new Locale(localeCookieValue) : null;
+ return localeCookieValue != null ? LocaleUtils.toLocale(localeCookieValue) : null;
}
private String getCookieValue()
Added: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/util/LocaleUtils.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/util/LocaleUtils.java?rev=669600&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/util/LocaleUtils.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/util/LocaleUtils.java Thu Jun 19 10:52:57 2008
@@ -0,0 +1,84 @@
+// Copyright 2008 The Apache Software Foundation
+//
+// 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.apache.tapestry5.internal.util;
+
+import java.util.Locale;
+
+/**
+ * Contains code borrowed from <a href="http://commons.apache.org/lang/">commons-lang</a>.
+ */
+public class LocaleUtils
+{
+ /**
+ * <p>Converts a String to a Locale.</p> <p/> <p>This method takes the string format of a locale and creates the
+ * locale object from it.</p> <p/>
+ * <pre>
+ * LocaleUtils.toLocale("en") = new Locale("en", "")
+ * LocaleUtils.toLocale("en_GB") = new Locale("en", "GB")
+ * LocaleUtils.toLocale("en_GB_xxx") = new Locale("en", "GB", "xxx") (#)
+ * </pre>
+ * <p/> <p>(#) The behaviour of the JDK variant constructor changed between JDK1.3 and JDK1.4. In JDK1.3, the
+ * constructor upper cases the variant, in JDK1.4, it doesn't. Thus, the result from getVariant() may vary depending
+ * on your JDK.</p> <p/> <p>This method validates the input strictly. The language code must be lowercase. The
+ * country code must be uppercase. The separator must be an underscore. The length must be correct. </p>
+ *
+ * @param input the locale String to convert, null returns null
+ * @return a Locale, null if null input
+ * @throws IllegalArgumentException if the string is an invalid format
+ */
+ public static Locale toLocale(String input)
+ {
+ if (input == null)
+ return null;
+
+ int len = input.length();
+ if (len != 2 && len != 5 && len < 7)
+ fail(input);
+
+ char ch0 = input.charAt(0);
+ char ch1 = input.charAt(1);
+
+ if (ch0 < 'a' || ch0 > 'z' || ch1 < 'a' || ch1 > 'z')
+ fail(input);
+
+ if (len == 2)
+ return new Locale(input, "");
+
+ if (input.charAt(2) != '_')
+ fail(input);
+
+ char ch3 = input.charAt(3);
+ if (ch3 == '_')
+ return new Locale(input.substring(0, 2), "", input.substring(4));
+
+ char ch4 = input.charAt(4);
+ if (ch3 < 'A' || ch3 > 'Z' || ch4 < 'A' || ch4 > 'Z')
+ fail(input);
+
+ if (len == 5)
+ return new Locale(input.substring(0, 2), input.substring(3, 5));
+
+ if (input.charAt(5) != '_')
+ fail(input);
+
+ return new Locale(input.substring(0, 2), input.substring(3, 5), input.substring(6));
+ }
+
+ private static void fail(String input)
+ {
+ throw new IllegalArgumentException(String.format("Unable to convert '%s' to a Locale instance.", input));
+ }
+
+}
Modified: tapestry/tapestry5/trunk/tapestry-core/src/site/apt/upgrade.apt
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/site/apt/upgrade.apt?rev=669600&r1=669599&r2=669600&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/site/apt/upgrade.apt (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/site/apt/upgrade.apt Thu Jun 19 10:52:57 2008
@@ -12,6 +12,8 @@
You should also check the {{{../release-notes.html}project-wide release notes}} for information
about bugs fixes and other improvements.
+Release 5.0.14
+
Release 5.0.13
As part of {{{https://issues.apache.org/jira/browse/TAPESTRY-2311}TAPESTRY-2311}}, there have been
@@ -20,6 +22,17 @@
Release 5.0.12
+* JavaScript Changes
+
+ Tapestry now organizes JavaScript a bit differently; all JavaScript is at the bottom of the page, just
+ before the \</body\> tag. This applies to both externally loaded scripts, and to per-page dynamically
+ generated JavaScript.
+
+ This can cause some pages to break, those that included inline \<script\> blocks in their templates.
+ You should inject the
+ {{{../apidocs/org/apache/tapestry5/RenderSupport.html}RenderSupport}} environmental and use it
+ to include JavaScript properly.
+
* ReorderProperties annotation
A new annotation,
Added: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/util/LocaleUtilsTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/util/LocaleUtilsTest.java?rev=669600&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/util/LocaleUtilsTest.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/util/LocaleUtilsTest.java Thu Jun 19 10:52:57 2008
@@ -0,0 +1,240 @@
+// Copyright 2008 The Apache Software Foundation
+//
+// 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.apache.tapestry5.internal.util;
+
+import junit.framework.Assert;
+import org.testng.annotations.Test;
+
+import java.util.Locale;
+
+/**
+ * This, too, was adapted from commons-lang code. Could be cleaned up a bit to better take advantage of TestNG.
+ */
+@SuppressWarnings({ "EmptyCatchBlock" })
+public class LocaleUtilsTest extends Assert
+{
+ /**
+ * Pass in a valid language, test toLocale.
+ *
+ * @param language the language string
+ */
+ private void assertValidToLocale(String language)
+ {
+ Locale locale = LocaleUtils.toLocale(language);
+ assertNotNull("valid locale", locale);
+ assertEquals(language, locale.getLanguage());
+ //country and variant are empty
+ assertTrue(locale.getCountry() == null || locale.getCountry().length() == 0);
+ assertTrue(locale.getVariant() == null || locale.getVariant().length() == 0);
+ }
+
+ /**
+ * Pass in a valid language, test toLocale.
+ *
+ * @param localeString to pass to toLocale()
+ * @param language of the resulting Locale
+ * @param country of the resulting Locale
+ */
+ private void assertValidToLocale(String localeString, String language, String country)
+ {
+ Locale locale = LocaleUtils.toLocale(localeString);
+ assertNotNull("valid locale", locale);
+ assertEquals(language, locale.getLanguage());
+ assertEquals(country, locale.getCountry());
+ //variant is empty
+ assertTrue(locale.getVariant() == null || locale.getVariant().length() == 0);
+ }
+
+ /**
+ * Pass in a valid language, test toLocale.
+ *
+ * @param localeString to pass to toLocale()
+ * @param language of the resulting Locale
+ * @param country of the resulting Locale
+ * @param variant of the resulting Locale
+ */
+ private void assertValidToLocale(
+ String localeString, String language,
+ String country, String variant)
+ {
+ Locale locale = LocaleUtils.toLocale(localeString);
+ assertNotNull("valid locale", locale);
+ assertEquals(language, locale.getLanguage());
+ assertEquals(country, locale.getCountry());
+ assertEquals(variant, locale.getVariant());
+
+ }
+
+ @Test
+ public void toLocale_just_language()
+ {
+ assertEquals(null, LocaleUtils.toLocale(null));
+
+ assertValidToLocale("us");
+ assertValidToLocale("fr");
+ assertValidToLocale("de");
+ assertValidToLocale("zh");
+ // Valid format but lang doesnt exist, should make instance anyway
+ assertValidToLocale("qq");
+
+ try
+ {
+ LocaleUtils.toLocale("Us");
+ fail("Should fail if not lowercase");
+ }
+ catch (IllegalArgumentException iae)
+ {
+ }
+ try
+ {
+ LocaleUtils.toLocale("US");
+ fail("Should fail if not lowercase");
+ }
+ catch (IllegalArgumentException iae)
+ {
+ }
+ try
+ {
+ LocaleUtils.toLocale("uS");
+ fail("Should fail if not lowercase");
+ }
+ catch (IllegalArgumentException iae)
+ {
+ }
+ try
+ {
+ LocaleUtils.toLocale("u#");
+ fail("Should fail if not lowercase");
+ }
+ catch (IllegalArgumentException iae)
+ {
+ }
+
+ try
+ {
+ LocaleUtils.toLocale("u");
+ fail("Must be 2 chars if less than 5");
+ }
+ catch (IllegalArgumentException iae)
+ {
+ }
+
+ try
+ {
+ LocaleUtils.toLocale("uuu");
+ fail("Must be 2 chars if less than 5");
+ }
+ catch (IllegalArgumentException iae)
+ {
+ }
+
+ try
+ {
+ LocaleUtils.toLocale("uu_U");
+ fail("Must be 2 chars if less than 5");
+ }
+ catch (IllegalArgumentException iae)
+ {
+ }
+ }
+
+
+ @Test
+ public void toLocale_language_and_country()
+ {
+ assertValidToLocale("us_EN", "us", "EN");
+ //valid though doesnt exist
+ assertValidToLocale("us_ZH", "us", "ZH");
+
+ try
+ {
+ LocaleUtils.toLocale("us-EN");
+ fail("Should fail as not underscore");
+ }
+ catch (IllegalArgumentException iae)
+ {
+ }
+ try
+ {
+ LocaleUtils.toLocale("us_En");
+ fail("Should fail second part not uppercase");
+ }
+ catch (IllegalArgumentException iae)
+ {
+ }
+ try
+ {
+ LocaleUtils.toLocale("us_en");
+ fail("Should fail second part not uppercase");
+ }
+ catch (IllegalArgumentException iae)
+ {
+ }
+ try
+ {
+ LocaleUtils.toLocale("us_eN");
+ fail("Should fail second part not uppercase");
+ }
+ catch (IllegalArgumentException iae)
+ {
+ }
+ try
+ {
+ LocaleUtils.toLocale("uS_EN");
+ fail("Should fail first part not lowercase");
+ }
+ catch (IllegalArgumentException iae)
+ {
+ }
+ try
+ {
+ LocaleUtils.toLocale("us_E3");
+ fail("Should fail second part not uppercase");
+ }
+ catch (IllegalArgumentException iae)
+ {
+ }
+ }
+
+ /**
+ * Test toLocale() method.
+ */
+ @Test
+ public void toLocale_with_variant()
+ {
+ assertValidToLocale("us_EN_A", "us", "EN", "A");
+
+ assertValidToLocale("us_EN_a", "us", "EN", "a");
+ assertValidToLocale("us_EN_SFsafdFDsdfF", "us", "EN", "SFsafdFDsdfF");
+
+ try
+ {
+ LocaleUtils.toLocale("us_EN-a");
+ fail("Should fail as not underscore");
+ }
+ catch (IllegalArgumentException iae)
+ {
+ }
+ try
+ {
+ LocaleUtils.toLocale("uu_UU_");
+ fail("Must be 3, 5 or 7+ in length");
+ }
+ catch (IllegalArgumentException iae)
+ {
+ }
+ }
+
+}