diff options
author | Artur Signell <artur@vaadin.com> | 2014-12-08 20:13:20 +0200 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2014-12-10 22:10:39 +0000 |
commit | 405262d805f68979c8aa4c1dc25614d7bf9e80cf (patch) | |
tree | 4432c4d3eadc82136f1e530e8204aa3b33466cc6 /shared | |
parent | 60221e96255eddfaaffb87ecd1b7b282fb85e6de (diff) | |
download | vaadin-framework-405262d805f68979c8aa4c1dc25614d7bf9e80cf.tar.gz vaadin-framework-405262d805f68979c8aa4c1dc25614d7bf9e80cf.zip |
Split and capitalize header captions by default
Only done on the server side where we have property ids to generate the default
headers from. On the client side you must always specify the header.
Change-Id: Ic743fb3f52517116193b022cfdd2df7dea8dd487
Diffstat (limited to 'shared')
-rw-r--r-- | shared/src/com/vaadin/shared/util/SharedUtil.java | 136 | ||||
-rw-r--r-- | shared/tests/src/com/vaadin/shared/util/SharedUtilTests.java | 66 |
2 files changed, 187 insertions, 15 deletions
diff --git a/shared/src/com/vaadin/shared/util/SharedUtil.java b/shared/src/com/vaadin/shared/util/SharedUtil.java index 7276f418fa..b40d8f03bb 100644 --- a/shared/src/com/vaadin/shared/util/SharedUtil.java +++ b/shared/src/com/vaadin/shared/util/SharedUtil.java @@ -60,4 +60,140 @@ public class SharedUtil implements Serializable { */ public static final String SIZE_PATTERN = "^(-?\\d*(?:\\.\\d+)?)(%|px|em|rem|ex|in|cm|mm|pt|pc)?$"; + /** + * Splits a camelCaseString into an array of words with the casing + * preserved. + * + * @since 7.4 + * @param camelCaseString + * The input string in camelCase format + * @return An array with one entry per word in the input string + */ + public static String[] splitCamelCase(String camelCaseString) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < camelCaseString.length(); i++) { + char c = camelCaseString.charAt(i); + if (Character.isUpperCase(c) && isWordComplete(camelCaseString, i)) { + sb.append(' '); + } + sb.append(c); + } + return sb.toString().split(" "); + } + + private static boolean isWordComplete(String camelCaseString, int i) { + if (i == 0) { + // Word can't end at the beginning + return false; + } else if (!Character.isUpperCase(camelCaseString.charAt(i - 1))) { + // Word ends if previous char wasn't upper case + return true; + } else if (i + 1 < camelCaseString.length() + && !Character.isUpperCase(camelCaseString.charAt(i + 1))) { + // Word ends if next char isn't upper case + return true; + } else { + return false; + } + } + + /** + * Converts a camelCaseString to a human friendly format (Camel case + * string). + * <p> + * In general splits words when the casing changes but also handles special + * cases such as consecutive upper case characters. Examples: + * <p> + * {@literal MyBeanContainer} becomes {@literal My Bean Container} + * {@literal AwesomeURLFactory} becomes {@literal Awesome URL Factory} + * {@literal SomeUriAction} becomes {@literal Some Uri Action} + * + * @since 7.4 + * @param camelCaseString + * The input string in camelCase format + * @return A human friendly version of the input + */ + public static String camelCaseToHumanFriendly(String camelCaseString) { + String[] parts = splitCamelCase(camelCaseString); + for (int i = 0; i < parts.length; i++) { + parts[i] = capitalize(parts[i]); + } + return join(parts, " "); + } + + private static boolean isAllUpperCase(String string) { + for (int i = 0; i < string.length(); i++) { + char c = string.charAt(i); + if (!Character.isUpperCase(c) && !Character.isDigit(c)) { + return false; + } + } + return true; + } + + /** + * Joins the words in the input array together into a single string by + * inserting the separator string between each word. + * + * @since 7.4 + * @param parts + * The array of words + * @param separator + * The separator string to use between words + * @return The constructed string of words and separators + */ + public static String join(String[] parts, String separator) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < parts.length; i++) { + sb.append(parts[i]); + sb.append(separator); + } + return sb.substring(0, sb.length() - 1); + } + + /** + * Capitalizes the first character in the given string + * + * @since 7.4 + * @param string + * The string to capitalize + * @return The capitalized string + */ + public static String capitalize(String string) { + if (string == null) { + return null; + } + + if (string.length() <= 1) { + return string.toUpperCase(); + } + + return string.substring(0, 1).toUpperCase() + string.substring(1); + } + + /** + * Converts a property id to a human friendly format. Handles nested + * properties by only considering the last part, e.g. "address.streetName" + * is equal to "streetName" for this method. + * + * @since 7.4 + * @param propertyId + * The propertyId to format + * @return A human friendly version of the property id + */ + public static String propertyIdToHumanFriendly(Object propertyId) { + String string = propertyId.toString(); + if (string.isEmpty()) { + return ""; + } + + // For nested properties, only use the last part + int dotLocation = string.lastIndexOf('.'); + if (dotLocation > 0 && dotLocation < string.length() - 1) { + string = string.substring(dotLocation + 1); + } + + return camelCaseToHumanFriendly(string); + } + } diff --git a/shared/tests/src/com/vaadin/shared/util/SharedUtilTests.java b/shared/tests/src/com/vaadin/shared/util/SharedUtilTests.java index b593032bd6..208d4ca7c7 100644 --- a/shared/tests/src/com/vaadin/shared/util/SharedUtilTests.java +++ b/shared/tests/src/com/vaadin/shared/util/SharedUtilTests.java @@ -1,43 +1,79 @@ package com.vaadin.shared.util; -import org.junit.Before; -import org.junit.Test; - import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; -public class SharedUtilTests { - - private SharedUtil sut; +import org.junit.Assert; +import org.junit.Test; - @Before - public void setup() { - sut = new SharedUtil(); - } +public class SharedUtilTests { @Test public void trailingSlashIsTrimmed() { - assertThat(sut.trimTrailingSlashes("/path/"), is("/path")); + assertThat(SharedUtil.trimTrailingSlashes("/path/"), is("/path")); } @Test public void noTrailingSlashForTrimming() { - assertThat(sut.trimTrailingSlashes("/path"), is("/path")); + assertThat(SharedUtil.trimTrailingSlashes("/path"), is("/path")); } @Test public void trailingSlashesAreTrimmed() { - assertThat(sut.trimTrailingSlashes("/path///"), is("/path")); + assertThat(SharedUtil.trimTrailingSlashes("/path///"), is("/path")); } @Test public void emptyStringIsHandled() { - assertThat(sut.trimTrailingSlashes(""), is("")); + assertThat(SharedUtil.trimTrailingSlashes(""), is("")); } @Test public void rootSlashIsTrimmed() { - assertThat(sut.trimTrailingSlashes("/"), is("")); + assertThat(SharedUtil.trimTrailingSlashes("/"), is("")); } + @Test + public void camelCaseToHumanReadable() { + Assert.assertEquals("First Name", + SharedUtil.camelCaseToHumanFriendly("firstName")); + Assert.assertEquals("First Name", + SharedUtil.camelCaseToHumanFriendly("first name")); + Assert.assertEquals("First Name2", + SharedUtil.camelCaseToHumanFriendly("firstName2")); + Assert.assertEquals("First", + SharedUtil.camelCaseToHumanFriendly("first")); + Assert.assertEquals("First", + SharedUtil.camelCaseToHumanFriendly("First")); + Assert.assertEquals("Some XYZ Abbreviation", + SharedUtil.camelCaseToHumanFriendly("SomeXYZAbbreviation")); + + // Javadoc examples + Assert.assertEquals("My Bean Container", + SharedUtil.camelCaseToHumanFriendly("MyBeanContainer")); + Assert.assertEquals("Awesome URL Factory", + SharedUtil.camelCaseToHumanFriendly("AwesomeURLFactory")); + Assert.assertEquals("Some Uri Action", + SharedUtil.camelCaseToHumanFriendly("SomeUriAction")); + + } + + @Test + public void splitCamelCase() { + assertCamelCaseSplit("firstName", "first", "Name"); + assertCamelCaseSplit("fooBar", "foo", "Bar"); + assertCamelCaseSplit("fooBar", "foo", "Bar"); + assertCamelCaseSplit("fBar", "f", "Bar"); + assertCamelCaseSplit("FBar", "F", "Bar"); + assertCamelCaseSplit("MYCdi", "MY", "Cdi"); + assertCamelCaseSplit("MyCDIUI", "My", "CDIUI"); + assertCamelCaseSplit("MyCDIUITwo", "My", "CDIUI", "Two"); + assertCamelCaseSplit("first name", "first", "name"); + + } + + private void assertCamelCaseSplit(String camelCaseString, String... parts) { + String[] splitParts = SharedUtil.splitCamelCase(camelCaseString); + Assert.assertArrayEquals(parts, splitParts); + } } |