diff options
6 files changed, 83 insertions, 23 deletions
diff --git a/client/src/com/vaadin/client/ui/csslayout/CssLayoutConnector.java b/client/src/com/vaadin/client/ui/csslayout/CssLayoutConnector.java index a0ef1fbb3e..bef506b492 100644 --- a/client/src/com/vaadin/client/ui/csslayout/CssLayoutConnector.java +++ b/client/src/com/vaadin/client/ui/csslayout/CssLayoutConnector.java @@ -32,6 +32,7 @@ import com.vaadin.shared.ui.Connect; import com.vaadin.shared.ui.LayoutClickRpc; import com.vaadin.shared.ui.csslayout.CssLayoutServerRpc; import com.vaadin.shared.ui.csslayout.CssLayoutState; +import com.vaadin.shared.util.SharedUtil; import com.vaadin.ui.CssLayout; /** @@ -160,13 +161,7 @@ public class CssLayoutConnector extends AbstractLayoutConnector { * @return A string converted to camelcase */ private static final String makeCamelCase(String cssProperty) { - // TODO this might be cleaner to implement with regexp - while (cssProperty.contains("-")) { - int indexOf = cssProperty.indexOf("-"); - cssProperty = cssProperty.substring(0, indexOf) - + String.valueOf(cssProperty.charAt(indexOf + 1)) - .toUpperCase() + cssProperty.substring(indexOf + 2); - } + cssProperty = SharedUtil.dashSeparatedToCamelCase(cssProperty); if ("float".equals(cssProperty)) { if (BrowserInfo.get().isIE()) { return "styleFloat"; diff --git a/server/src/com/vaadin/data/util/MethodProperty.java b/server/src/com/vaadin/data/util/MethodProperty.java index 853f68b711..83279afa53 100644 --- a/server/src/com/vaadin/data/util/MethodProperty.java +++ b/server/src/com/vaadin/data/util/MethodProperty.java @@ -26,6 +26,7 @@ import java.util.logging.Level; import java.util.logging.Logger; import com.vaadin.data.Property; +import com.vaadin.shared.util.SharedUtil; import com.vaadin.util.SerializerHelper; /** @@ -189,11 +190,7 @@ public class MethodProperty<T> extends AbstractProperty<T> { // Assure that the first letter is upper cased (it is a common // mistake to write firstName, not FirstName). - if (Character.isLowerCase(beanPropertyName.charAt(0))) { - final char[] buf = beanPropertyName.toCharArray(); - buf[0] = Character.toUpperCase(buf[0]); - beanPropertyName = new String(buf); - } + beanPropertyName = SharedUtil.capitalize(beanPropertyName); // Find the get method getMethod = null; @@ -534,8 +531,7 @@ public class MethodProperty<T> extends AbstractProperty<T> { */ static Method initGetterMethod(String propertyName, final Class<?> beanClass) throws NoSuchMethodException { - propertyName = propertyName.substring(0, 1).toUpperCase() - + propertyName.substring(1); + propertyName = SharedUtil.capitalize(propertyName); Method getMethod = null; try { @@ -772,4 +768,5 @@ public class MethodProperty<T> extends AbstractProperty<T> { private static final Logger getLogger() { return Logger.getLogger(MethodProperty.class.getName()); } + } diff --git a/server/src/com/vaadin/data/util/NestedMethodProperty.java b/server/src/com/vaadin/data/util/NestedMethodProperty.java index 0493812861..29fe62f845 100644 --- a/server/src/com/vaadin/data/util/NestedMethodProperty.java +++ b/server/src/com/vaadin/data/util/NestedMethodProperty.java @@ -26,6 +26,7 @@ import java.util.List; import com.vaadin.data.Property; import com.vaadin.data.util.MethodProperty.MethodException; +import com.vaadin.shared.util.SharedUtil; /** * Nested accessor based property for a bean. @@ -164,11 +165,8 @@ public class NestedMethodProperty<T> extends AbstractProperty<T> { try { // Assure that the first letter is upper cased (it is a common // mistake to write firstName, not FirstName). - if (Character.isLowerCase(lastSimplePropertyName.charAt(0))) { - final char[] buf = lastSimplePropertyName.toCharArray(); - buf[0] = Character.toUpperCase(buf[0]); - lastSimplePropertyName = new String(buf); - } + lastSimplePropertyName = SharedUtil + .capitalize(lastSimplePropertyName); setMethod = lastClass.getMethod("set" + lastSimplePropertyName, new Class[] { type }); diff --git a/shared/src/com/vaadin/shared/util/SharedUtil.java b/shared/src/com/vaadin/shared/util/SharedUtil.java index bc5d87b9f5..d363d767be 100644 --- a/shared/src/com/vaadin/shared/util/SharedUtil.java +++ b/shared/src/com/vaadin/shared/util/SharedUtil.java @@ -149,11 +149,12 @@ public class SharedUtil implements Serializable { sb.append(parts[i]); sb.append(separator); } - return sb.substring(0, sb.length() - 1); + return sb.substring(0, sb.length() - separator.length()); } /** - * Capitalizes the first character in the given string + * Capitalizes the first character in the given string in a way suitable for + * use in code (methods, properties etc) * * @since 7.4 * @param string @@ -239,4 +240,29 @@ public class SharedUtil implements Serializable { return uri; } + /** + * Converts a dash ("-") separated string into camelCase. + * <p> + * Examples: + * <p> + * {@literal foo} becomes {@literal foo} {@literal foo-bar} becomes + * {@literal fooBar} {@literal foo--bar} becomes {@literal fooBar} + * + * @since + * @param dashSeparated + * The dash separated string to convert + * @return a camelCase version of the input string + */ + public static String dashSeparatedToCamelCase(String dashSeparated) { + if (dashSeparated == null) { + return null; + } + String[] parts = dashSeparated.split("-"); + for (int i = 1; i < parts.length; i++) { + parts[i] = capitalize(parts[i]); + } + + return join(parts, ""); + } + } diff --git a/shared/tests/src/com/vaadin/shared/util/SharedUtilTests.java b/shared/tests/src/com/vaadin/shared/util/SharedUtilTests.java index 208d4ca7c7..e77501a446 100644 --- a/shared/tests/src/com/vaadin/shared/util/SharedUtilTests.java +++ b/shared/tests/src/com/vaadin/shared/util/SharedUtilTests.java @@ -3,6 +3,8 @@ package com.vaadin.shared.util; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; +import java.util.Locale; + import org.junit.Assert; import org.junit.Test; @@ -76,4 +78,44 @@ public class SharedUtilTests { String[] splitParts = SharedUtil.splitCamelCase(camelCaseString); Assert.assertArrayEquals(parts, splitParts); } + + @Test + public void join() { + String s1 = "foo-bar-baz"; + String s2 = "foo--bar"; + + Assert.assertEquals("foobarbaz", SharedUtil.join(s1.split("-"), "")); + Assert.assertEquals("foo!bar!baz", SharedUtil.join(s1.split("-"), "!")); + Assert.assertEquals("foo!!bar!!baz", + SharedUtil.join(s1.split("-"), "!!")); + + Assert.assertEquals("foo##bar", SharedUtil.join(s2.split("-"), "#")); + } + + @Test + public void dashSeparatedToCamelCase() { + Assert.assertEquals(null, SharedUtil.dashSeparatedToCamelCase(null)); + Assert.assertEquals("", SharedUtil.dashSeparatedToCamelCase("")); + Assert.assertEquals("foo", SharedUtil.dashSeparatedToCamelCase("foo")); + Assert.assertEquals("fooBar", + SharedUtil.dashSeparatedToCamelCase("foo-bar")); + Assert.assertEquals("fooBar", + SharedUtil.dashSeparatedToCamelCase("foo--bar")); + Assert.assertEquals("fooBarBaz", + SharedUtil.dashSeparatedToCamelCase("foo-bar-baz")); + Assert.assertEquals("fooBarBaz", + SharedUtil.dashSeparatedToCamelCase("foo-Bar-Baz")); + } + + @Test + public void methodUppercaseWithTurkishLocale() { + Locale defaultLocale = Locale.getDefault(); + try { + Locale.setDefault(new Locale("tr", "TR")); + Assert.assertEquals("Integer", SharedUtil.capitalize("integer")); + } finally { + Locale.setDefault(defaultLocale); + } + } + } diff --git a/uitest/src/com/vaadin/tests/themes/valo/StringGenerator.java b/uitest/src/com/vaadin/tests/themes/valo/StringGenerator.java index 7e5cc0f691..bffd94137c 100644 --- a/uitest/src/com/vaadin/tests/themes/valo/StringGenerator.java +++ b/uitest/src/com/vaadin/tests/themes/valo/StringGenerator.java @@ -15,6 +15,8 @@ */ package com.vaadin.tests.themes.valo; +import com.vaadin.shared.util.SharedUtil; + public class StringGenerator { static String[] strings = new String[] { "lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "quid", "securi", "etiam", "tamquam", "eu", @@ -25,8 +27,8 @@ public class StringGenerator { if (++stringCount >= strings.length) { stringCount = 0; } - return capitalize ? strings[stringCount].substring(0, 1).toUpperCase() - + strings[stringCount].substring(1) : strings[stringCount]; + return capitalize ? SharedUtil.capitalize(strings[stringCount]) + : strings[stringCount]; } } |