org.openxava.util
Class Strings

java.lang.Object
  extended by org.openxava.util.Strings

public class Strings
extends java.lang.Object

Utilities to work with String.

Author:
Javier Paniza

Constructor Summary
Strings()
           
 
Method Summary
static java.lang.String change(java.lang.String string, java.util.Map toChange)
          In the sent string changes the strings in toChange map for its values.
static java.lang.String change(java.lang.String string, java.lang.String original, java.lang.String newString)
          Change in string original by newString.
static java.lang.String changeSeparatorsBySpaces(java.lang.String string)
          Changes \n, \r and \t by space.
static java.lang.String firstLower(java.lang.String s)
          Returns a string like the sent one but with the first letter in lowercase.
static java.lang.String firstToken(java.lang.String string, java.lang.String delim)
           
static java.lang.String firstUpper(java.lang.String s)
          Returns a string like the sent one but with the first letter in uppercase.
static java.lang.String fix(java.lang.String string, int length, Align align)
          Fix the length of the string filling with spaces if needed.
static java.lang.String fix(java.lang.String string, int length, Align align, char fillCharacter)
          Fix the length of the string filling with the specified character if needed.
static java.lang.String javaIdentifierToNaturalLabel(java.lang.String name)
          Convert a string with a Java identifier in label natural for a human.
static java.lang.String lastToken(java.lang.String string)
           
static java.lang.String lastToken(java.lang.String string, java.lang.String delim)
           
static java.lang.String noFirstToken(java.lang.String string, java.lang.String delim)
          All string but without first token.
static java.lang.String noLastToken(java.lang.String string)
          All string but without last token.
static java.lang.String noLastToken(java.lang.String string, java.lang.String delim)
          All string but without last token.
static java.lang.String noLastTokenWithoutLastDelim(java.lang.String string, java.lang.String delim)
          All string but without last token.
static java.lang.String removeAccents(java.lang.String value)
          Change from a vowel with an accent, to vowel with no accent If you send "Camión" it returns "Camion"
static java.lang.Object removeXSS(java.lang.Object notSafeValue)
           
static java.lang.String removeXSS(java.lang.String notSafeValue)
          A key function of any application filtering process will be the removal of possible dangerous special characters.
static java.lang.String repeat(int count, java.lang.String string)
          Creates a string from repeating another string.
static java.lang.String spaces(int count)
          Creates a string with the specified blank spaces.
static java.lang.String[] toArray(java.lang.String list)
          Converts a list of comma separated elements in a string array.
static java.lang.String[] toArray(java.lang.String list, java.lang.String separator)
          Converts a list of elements separated by a arbitrary character in a string array.
static java.lang.String toCharSet(java.lang.String original, java.lang.String charSet)
          Translate to the charset specified.
static java.util.Collection<java.lang.String> toCollection(java.lang.String list)
          Converts a list of comma separated elements in a string collection.
static java.util.Collection<java.lang.String> toCollection(java.lang.String list, java.lang.String separator)
          Converts a list of elements separated by a arbitrary character in a string collection.
static java.util.List<java.lang.String> toList(java.lang.String list)
          Converts a list of comma separated elements in a string List.
static java.util.List<java.lang.String> toList(java.lang.String list, java.lang.String separator)
          Converts a list of elements separated by a arbitrary character in a string List.
static java.lang.Object toObject(java.lang.Class type, java.lang.String string)
          Converts a string a object of the specified type.
static java.util.Set<java.lang.String> toSet(java.lang.String list)
          Converts a list of comma separated elements in a string set.
static java.util.Set<java.lang.String> toSet(java.lang.String list, java.lang.String separator)
          Converts a list of elements separated by a arbitrary character in a string set.
static java.util.Set<java.lang.String> toSetNullByPass(java.lang.String list)
          Converts a list of comma separated elements in a string set.
static java.lang.String toString(java.util.Collection collection)
          Converts a collection of objects in a string of comma separated elements.
static java.lang.String toString(java.util.Collection collection, java.lang.String separator)
          Converts a collection of objects in string of elements separated by a arbitrary character .
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Strings

public Strings()
Method Detail

changeSeparatorsBySpaces

public static java.lang.String changeSeparatorsBySpaces(java.lang.String string)
Changes \n, \r and \t by space.

Parameters:
string - Not null

toCharSet

public static java.lang.String toCharSet(java.lang.String original,
                                         java.lang.String charSet)
                                  throws java.io.UnsupportedEncodingException
Translate to the charset specified.

Parameters:
original - Original string
charSet - Charset to traslate to, for example, UTF-8, or ISO-8859-1
Returns:
The string translated
Throws:
java.io.UnsupportedEncodingException - If charset is not supported

spaces

public static java.lang.String spaces(int count)
Creates a string with the specified blank spaces.

Parameters:
count - Quantity of spaces
Returns:
Not null.

fix

public static java.lang.String fix(java.lang.String string,
                                   int length,
                                   Align align)
Fix the length of the string filling with spaces if needed.

Returns the sent string but with the specified length.
It fills or cuts as it needs.

Parameters:
string - Can be null, in which case empty string is assumed
length - Character count of result string
align - Not null
Returns:
Not null

fix

public static java.lang.String fix(java.lang.String string,
                                   int length,
                                   Align align,
                                   char fillCharacter)
Fix the length of the string filling with the specified character if needed.

Returns the sent string but with the specified length.
It fills or cuts as it needs.

Parameters:
string - Can be null, in which case empty string is assumed
length - Character count of result string
align - Not null
fillCharacter - Character used to fill
Returns:
Not null

repeat

public static java.lang.String repeat(int count,
                                      java.lang.String string)
Creates a string from repeating another string.

Parameters:
count - Times to repeat
string - String to repeat
Returns:
Not null

toArray

public static final java.lang.String[] toArray(java.lang.String list)
Converts a list of comma separated elements in a string array.

For example, the list Angel, Manolo, Antonia is converted to a array of 3 elements with this 3 names without comman nor spaces.

Parameters:
list - String with the list. If null return a empty string
Returns:
Not null, including the case list == null.

toArray

public static final java.lang.String[] toArray(java.lang.String list,
                                               java.lang.String separator)
Converts a list of elements separated by a arbitrary character in a string array.

For example, the list Angel : Manolo : Antonia is converted to a array of 3 elements with this 3 names without colon (for example) nor spaces.

Parameters:
list - String with the list. If null return a empty string
separator - The character used as separator.
Returns:
Not null, including the case list == null.

toCollection

public static final java.util.Collection<java.lang.String> toCollection(java.lang.String list)
Converts a list of comma separated elements in a string collection.

For example, the list Angel, Manolo, Antonia is converted to a collection of 3 elements with this 3 names without comman nor spaces.

Parameters:
list - String with the list. If null return a empty string
Returns:
Not null, including the case list == null.

toCollection

public static final java.util.Collection<java.lang.String> toCollection(java.lang.String list,
                                                                        java.lang.String separator)
Converts a list of elements separated by a arbitrary character in a string collection.

For example, the list Angel : Manolo : Antonia is converted to a collection of 3 elements with this 3 names without colon (for example) nor spaces.

Parameters:
list - String with the list. If null return a empty string
separator - The character used as separator.
Returns:
Not null, including the case list == null.

toList

public static final java.util.List<java.lang.String> toList(java.lang.String list)
Converts a list of comma separated elements in a string List.

For example, the list Angel, Manolo, Antonia is converted to a List of 3 elements with this 3 names without comma nor spaces.

Parameters:
list - String with the list. If null return a empty string
Returns:
Not null, including the case list == null.
Since:
4.3

toList

public static final java.util.List<java.lang.String> toList(java.lang.String list,
                                                            java.lang.String separator)
Converts a list of elements separated by a arbitrary character in a string List.

For example, the list Angel : Manolo : Antonia is converted to a List of 3 elements with this 3 names without colon (for example) nor spaces.

Parameters:
list - String with the list. If null return a empty string
separator - The character used as separator.
Returns:
Not null, including the case list == null.
Since:
4.3

toSet

public static final java.util.Set<java.lang.String> toSet(java.lang.String list)
Converts a list of comma separated elements in a string set.

For example, the list Angel, Manolo, Antonia is converted to a set of 3 elements with this 3 names without comman nor spaces.

Parameters:
list - String with the list. If null return a empty string
Returns:
Not null, including the case list == null.
Since:
4.1

toSetNullByPass

public static final java.util.Set<java.lang.String> toSetNullByPass(java.lang.String list)
Converts a list of comma separated elements in a string set.

For example, the list Angel, Manolo, Antonia is converted to a set of 3 elements with this 3 names without comman nor spaces.

Parameters:
list - String with the list. If null return a empty string
Returns:
Null in the case list == null.
Since:
4.1

toSet

public static final java.util.Set<java.lang.String> toSet(java.lang.String list,
                                                          java.lang.String separator)
Converts a list of elements separated by a arbitrary character in a string set.

For example, the list Angel : Manolo : Antonia is converted to a set of 3 elements with this 3 names without colon (for example) nor spaces.

Parameters:
list - String with the list. If null return a empty string
separator - The character used as separator.
Returns:
Not null, including the case list == null.
Since:
4.1

toString

public static final java.lang.String toString(java.util.Collection collection)
Converts a collection of objects in a string of comma separated elements.

For example, a collection of 3 elements with 3 names is converted to the string Angel, Manolo, Antonia

Parameters:
collection - Collection with the elements. If null return a empty string
Returns:
Not null, including the case list == null.

toString

public static final java.lang.String toString(java.util.Collection collection,
                                              java.lang.String separator)
Converts a collection of objects in string of elements separated by a arbitrary character .

For example, a collection of 3 elements with this 3 names is converted to a string of 3 elements with this 3 names and colon (for example).

Parameters:
collection - A collection of objects. If null return a empty string
separator - The character used as separator.
Returns:
Not null, including the case list == null.

toObject

public static final java.lang.Object toObject(java.lang.Class type,
                                              java.lang.String string)
Converts a string a object of the specified type.

Supports all primitive type plus its wrappers except char and void.
Support String and BigDecimal too.

If there is conversion error or is a type not supporte return null or the default value for the type (zero for numeric).

Parameters:
type - The type of returned object (can be a primitive type in this case return its wrapper). Not null
string - String with data to convert. Can be null, in this case return a default value.

firstUpper

public static java.lang.String firstUpper(java.lang.String s)
Returns a string like the sent one but with the first letter in uppercase.

If null is sent null is returned.


firstLower

public static java.lang.String firstLower(java.lang.String s)
Returns a string like the sent one but with the first letter in lowercase.

If null is sent null is returned.


change

public static java.lang.String change(java.lang.String string,
                                      java.util.Map toChange)
In the sent string changes the strings in toChange map for its values.

Parameters:
string - Not null
toChagne - Not null

change

public static java.lang.String change(java.lang.String string,
                                      java.lang.String original,
                                      java.lang.String newString)
Change in string original by newString.

Parameters:
string - String in which we make the changes. Can be null
original - String to search. Not null
newString - New value to put in place of original. Not null
Returns:
The changed string, if the sent string is null a null is returned

lastToken

public static java.lang.String lastToken(java.lang.String string)
Returns:
If string if null or have no tokens returns empty string.

lastToken

public static java.lang.String lastToken(java.lang.String string,
                                         java.lang.String delim)
Returns:
If string if null or have no tokens returns empty string.

noLastToken

public static java.lang.String noLastToken(java.lang.String string)
All string but without last token.

A trim is applied to the result.

Returns:
If string if null or have no tokens returns empty string.

noLastToken

public static java.lang.String noLastToken(java.lang.String string,
                                           java.lang.String delim)
All string but without last token.

Includes the last delim.

Returns:
If string if null or have no tokens returns empty string.

noFirstToken

public static java.lang.String noFirstToken(java.lang.String string,
                                            java.lang.String delim)
All string but without first token.

Includes the delim.

Returns:
If string if null or have no tokens returns empty string.

noLastTokenWithoutLastDelim

public static java.lang.String noLastTokenWithoutLastDelim(java.lang.String string,
                                                           java.lang.String delim)
All string but without last token.

It does not include the last delim.

Returns:
If string if null or have no tokens returns empty string.

firstToken

public static java.lang.String firstToken(java.lang.String string,
                                          java.lang.String delim)
Returns:
If string if null or have no tokens returns empty string.

removeXSS

public static java.lang.String removeXSS(java.lang.String notSafeValue)
A key function of any application filtering process will be the removal of possible dangerous special characters.

Returns:
new safe string

removeXSS

public static java.lang.Object removeXSS(java.lang.Object notSafeValue)
Parameters:
notSafeValue -
Returns:
Safe Object

javaIdentifierToNaturalLabel

public static java.lang.String javaIdentifierToNaturalLabel(java.lang.String name)
Convert a string with a Java identifier in label natural for a human.

If you send "firstName" it returns "First name".
If you send "CustomerOrder" it returns "Customer order".


removeAccents

public static java.lang.String removeAccents(java.lang.String value)
Change from a vowel with an accent, to vowel with no accent If you send "Camión" it returns "Camion"

Since:
v4m6