Package org.ametys.core.util
Class StringUtils
java.lang.Object
org.ametys.core.util.StringUtils
A collection of String management utility methods.
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
Returns a escapedString
value for a CSV cell enclosed in double quotes.static String
Generates a unique String key, based on System.currentTimeMillis()static String
Encrypt a password by using first MD5 Hash and base64 encoding.static String
normalizeStringValue
(String value) Normalize string.static String
sanitizeCsv
(String value) Returns a sanitizedString
value for a CSV column enclosed in double quotes.static String
sanitizeXlsHtml
(String value) Returns a sanitizedString
value for a XLS-HTML column (no double quotes enclosing).static Collection<String>
stringToCollection
(String values) Extract String values from a comma seprated list.static String[]
stringToStringArray
(String values) Extract String values from a comma seprated list.static I18nizableText
toReadableDataSize
(Long size) Transform a size to a readable size for data (bytes, KB, MB, etc.).
-
Method Details
-
stringToCollection
Extract String values from a comma seprated list.- Parameters:
values
- the comma separated list- Returns:
- a collection of String or an empty collection if string is null or empty.
-
stringToStringArray
Extract String values from a comma seprated list.- Parameters:
values
- the comma separated list- Returns:
- an array of String
-
generateKey
Generates a unique String key, based on System.currentTimeMillis()- Returns:
- a unique String value
-
md5Base64
Encrypt a password by using first MD5 Hash and base64 encoding.- Parameters:
password
- The password to be encrypted.- Returns:
- The password encrypted or null if the MD5 is not supported
-
normalizeStringValue
Normalize string. Pass to lower case and remove Unicode accents and diacritics- Parameters:
value
- the value to normalize- Returns:
- the normalized value
-
toReadableDataSize
Transform a size to a readable size for data (bytes, KB, MB, etc.).- Parameters:
size
- The size to transform- Returns:
- An internationalized text with the size and the unit.
-
escapeCsv
Returns a escapedString
value for a CSV cell enclosed in double quotes.Any double quote characters in the value are escaped with another double quote.
If cell value contains a formula (ex: =SOMME(A0:A10)) it could be evaluated by CVS editor.
UsesanitizeCsv(String)
to avoid formula evaluationnull => "" =1+2 => "=1+2" =1+2'" ;,=1+2 => "=1+2'"" ;,=1+2" L'orem ipsut; sit amet, dolor => "L'orem ipsut; sit amet, dolor" =cmd|' /c Calc.exe'!'A1' => "=cmd|' /c Calc.exe'!'A1'"
- Parameters:
value
- the String value for CSV column. Can be null.- Returns:
- the escaped value
-
sanitizeCsv
Returns a sanitizedString
value for a CSV column enclosed in double quotes.Any double quote characters in the value are escaped with another double quote.
If the value starts with '=', '+', '-', '@', newline or TAB, is prepend with a single quote.
null => "" =1+2";=1+2 => "'=1+2"";=1+2" =1+2'" ;,=1+2 => "'=1+2'"" ;,=1+2" L'orem ipsut; sit amet, dolor => "L'orem ipsut; sit amet, dolor" =cmd|' /c Calc.exe'!'A1' => "'=cmd|' /c Calc.exe'!'A1'"
- Parameters:
value
- the untrusted String value for CSV column. Can be null.- Returns:
- the escaped and trusted value
-
sanitizeXlsHtml
Returns a sanitizedString
value for a XLS-HTML column (no double quotes enclosing).If the value starts with '=', '+', '-', '@', newline or TAB, is prepend with a single quote.
null => StringUtils.EMPTY =1+2";=1+2 => '=1+2";=1+2 =1+2'" ;,=1+2 => '=1+2'" ;,=1+2 L'orem ipsut; sit amet, dolor => L'orem ipsut; sit amet, dolor =cmd|' /c Calc.exe'!'A1' => '=cmd|' /c Calc.exe'!'A1
- Parameters:
value
- the untrusted String value for HTML-XLS column. Can be null.- Returns:
- the trusted value
-