org.ofbiz.base.util
Class StringUtil

java.lang.Object
  extended by org.ofbiz.base.util.StringUtil

public class StringUtil
extends java.lang.Object

Misc String Utility Functions


Nested Class Summary
static class StringUtil.HtmlEncoder
           
static class StringUtil.HtmlEncodingMapWrapper<K>
          A simple Map wrapper class that will do HTML encoding.
static interface StringUtil.SimpleEncoder
           
static class StringUtil.StringWrapper
          A super-lightweight object to wrap a String object.
static class StringUtil.XmlEncoder
           
 
Field Summary
static Encoder defaultWebEncoder
           
static Validator defaultWebValidator
           
static boolean esapiCanonicalizeStrict
          OWASP ESAPI canonicalize strict flag; setting false so we only get warnings about double encoding, etc; can be set to true for exceptions and more security
static StringUtil.SimpleEncoder htmlEncoder
           
static java.lang.String module
           
static StringUtil.SimpleEncoder xmlEncoder
           
 
Constructor Summary
StringUtil()
           
 
Method Summary
static java.lang.String addToNumberString(java.lang.String numberString, long addAmount)
          Add the number to the string, keeping (padding to min of original length)
static java.lang.String checkStringForHtmlSafeOnly(java.lang.String valueName, java.lang.String value, java.util.List<java.lang.String> errorMessageList)
          Uses a white-list approach to check for safe HTML.
static java.lang.String checkStringForHtmlStrictNone(java.lang.String valueName, java.lang.String value, java.util.List<java.lang.String> errorMessageList)
          Uses a black-list approach for necessary characters for HTML.
static java.lang.String cleanHexString(java.lang.String str)
           
static java.lang.String cleanUpPathPrefix(java.lang.String prefix)
          Make sure the string starts with a forward slash but does not end with one; converts back-slashes to forward-slashes; if in String is null or empty, returns zero length string.
static java.lang.String collapseCharacter(java.lang.String str, char c)
          Remove/collapse multiple characters
static java.lang.String collapseNewlines(java.lang.String str)
          Remove/collapse multiple newline characters
static java.lang.String collapseSpaces(java.lang.String str)
          Remove/collapse multiple spaces
static int convertChar(char c)
           
static
<K,V> java.util.Map<K,V>
createMap(java.util.List<K> keys, java.util.List<V> values)
          Create a Map from a List of keys and a List of values
static char[] encodeInt(int i, int j, char[] digestChars)
           
static byte[] fromHexString(java.lang.String str)
           
static java.lang.String fromHtmlToSpecialChars(java.lang.String html, boolean doubleQuotes, boolean singleQuotes, boolean insertBR)
           
static StringUtil.SimpleEncoder getEncoder(java.lang.String type)
           
static java.lang.String htmlSpecialChars(java.lang.String html)
           
static java.lang.String htmlSpecialChars(java.lang.String html, boolean doubleQuotes, boolean singleQuotes, boolean insertBR)
          Deprecated. Use StringUtil.htmlEncoder instead.
static java.lang.String internString(java.lang.String value)
           
static java.lang.String join(java.util.List<?> list, java.lang.String delim)
          Creates a single string from a List of strings seperated by a delimiter.
static StringUtil.StringWrapper makeStringWrapper(java.lang.String theString)
           
static java.lang.String mapToStr(java.util.Map<? extends java.lang.Object,? extends java.lang.Object> map)
          Creates an encoded String from a Map of name/value pairs (MUST BE STRINGS!)
static java.lang.String padNumberString(java.lang.String numberString, int targetMinLength)
           
static java.util.List<java.lang.String> quoteStrList(java.util.List<java.lang.String> list)
          Encloses each of a List of Strings in quotes.
static java.lang.String removeNonNumeric(java.lang.String str)
          Removes all non-numbers from str
static java.lang.String removeNumeric(java.lang.String str)
          Removes all numbers from str
static java.lang.String removeRegex(java.lang.String str, java.lang.String regex)
           
static java.lang.String removeSpaces(java.lang.String str)
          Removes all spaces from a string
static java.lang.String replaceString(java.lang.String mainString, java.lang.String oldString, java.lang.String newString)
          Replaces all occurrences of oldString in mainString with newString
static java.util.List<java.lang.String> split(java.lang.String str, java.lang.String delim)
          Splits a String on a delimiter into a List of Strings.
static java.util.Map<java.lang.String,java.lang.String> strToMap(java.lang.String str)
          Creates a Map from an encoded name/value pair string
static java.util.Map<java.lang.String,java.lang.String> strToMap(java.lang.String str, boolean trim)
          Creates a Map from an encoded name/value pair string
static java.lang.String toHexString(byte[] bytes)
           
static java.util.List<java.lang.String> toList(java.lang.String s)
          Reads a String version of a List (should contain only strings) and creates a new List
static java.util.Map<java.lang.String,java.lang.String> toMap(java.lang.String s)
          Reads a String version of a Map (should contain only strings) and creates a new Map
static java.util.Set<java.lang.String> toSet(java.lang.String s)
          Reads a String version of a Set (should contain only strings) and creates a new Set
static StringUtil.StringWrapper wrapString(java.lang.String theString)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

module

public static final java.lang.String module

esapiCanonicalizeStrict

public static final boolean esapiCanonicalizeStrict
OWASP ESAPI canonicalize strict flag; setting false so we only get warnings about double encoding, etc; can be set to true for exceptions and more security

See Also:
Constant Field Values

defaultWebEncoder

public static final Encoder defaultWebEncoder

defaultWebValidator

public static final Validator defaultWebValidator

htmlEncoder

public static final StringUtil.SimpleEncoder htmlEncoder

xmlEncoder

public static final StringUtil.SimpleEncoder xmlEncoder
Constructor Detail

StringUtil

public StringUtil()
Method Detail

getEncoder

public static StringUtil.SimpleEncoder getEncoder(java.lang.String type)

internString

public static java.lang.String internString(java.lang.String value)

replaceString

public static java.lang.String replaceString(java.lang.String mainString,
                                             java.lang.String oldString,
                                             java.lang.String newString)
Replaces all occurrences of oldString in mainString with newString

Parameters:
mainString - The original string
oldString - The string to replace
newString - The string to insert in place of the old
Returns:
mainString with all occurrences of oldString replaced by newString

join

public static java.lang.String join(java.util.List<?> list,
                                    java.lang.String delim)
Creates a single string from a List of strings seperated by a delimiter.

Parameters:
list - a list of strings to join
delim - the delimiter character(s) to use. (null value will join with no delimiter)
Returns:
a String of all values in the list seperated by the delimiter

split

public static java.util.List<java.lang.String> split(java.lang.String str,
                                                     java.lang.String delim)
Splits a String on a delimiter into a List of Strings.

Parameters:
str - the String to split
delim - the delimiter character(s) to join on (null will split on whitespace)
Returns:
a list of Strings

quoteStrList

public static java.util.List<java.lang.String> quoteStrList(java.util.List<java.lang.String> list)
Encloses each of a List of Strings in quotes.

Parameters:
list - List of String(s) to quote.

strToMap

public static java.util.Map<java.lang.String,java.lang.String> strToMap(java.lang.String str,
                                                                        boolean trim)
Creates a Map from an encoded name/value pair string

Parameters:
str - The string to decode and format
trim - Trim whitespace off fields
Returns:
a Map of name/value pairs

strToMap

public static java.util.Map<java.lang.String,java.lang.String> strToMap(java.lang.String str)
Creates a Map from an encoded name/value pair string

Parameters:
str - The string to decode and format
Returns:
a Map of name/value pairs

mapToStr

public static java.lang.String mapToStr(java.util.Map<? extends java.lang.Object,? extends java.lang.Object> map)
Creates an encoded String from a Map of name/value pairs (MUST BE STRINGS!)

Parameters:
map - The Map of name/value pairs
Returns:
String The encoded String

toMap

public static java.util.Map<java.lang.String,java.lang.String> toMap(java.lang.String s)
Reads a String version of a Map (should contain only strings) and creates a new Map

Parameters:
s - String value of a Map ({n1=v1, n2=v2})
Returns:
new Map

toList

public static java.util.List<java.lang.String> toList(java.lang.String s)
Reads a String version of a List (should contain only strings) and creates a new List

Parameters:
s - String value of a Map ({n1=v1, n2=v2})
Returns:
new List

toSet

public static java.util.Set<java.lang.String> toSet(java.lang.String s)
Reads a String version of a Set (should contain only strings) and creates a new Set

Parameters:
s - String value of a Map ({n1=v1, n2=v2})
Returns:
new List

createMap

public static <K,V> java.util.Map<K,V> createMap(java.util.List<K> keys,
                                                 java.util.List<V> values)
Create a Map from a List of keys and a List of values

Parameters:
keys - List of keys
values - List of values
Returns:
Map of combined lists
Throws:
java.lang.IllegalArgumentException - When either List is null or the sizes do not equal

cleanUpPathPrefix

public static java.lang.String cleanUpPathPrefix(java.lang.String prefix)
Make sure the string starts with a forward slash but does not end with one; converts back-slashes to forward-slashes; if in String is null or empty, returns zero length string.


removeSpaces

public static java.lang.String removeSpaces(java.lang.String str)
Removes all spaces from a string


toHexString

public static java.lang.String toHexString(byte[] bytes)

cleanHexString

public static java.lang.String cleanHexString(java.lang.String str)

fromHexString

public static byte[] fromHexString(java.lang.String str)

convertChar

public static int convertChar(char c)

encodeInt

public static char[] encodeInt(int i,
                               int j,
                               char[] digestChars)

removeNonNumeric

public static java.lang.String removeNonNumeric(java.lang.String str)
Removes all non-numbers from str


removeNumeric

public static java.lang.String removeNumeric(java.lang.String str)
Removes all numbers from str


removeRegex

public static java.lang.String removeRegex(java.lang.String str,
                                           java.lang.String regex)
Parameters:
str -
regex - Removes all matches of regex from a str

addToNumberString

public static java.lang.String addToNumberString(java.lang.String numberString,
                                                 long addAmount)
Add the number to the string, keeping (padding to min of original length)

Returns:
the new value

padNumberString

public static java.lang.String padNumberString(java.lang.String numberString,
                                               int targetMinLength)

checkStringForHtmlStrictNone

public static java.lang.String checkStringForHtmlStrictNone(java.lang.String valueName,
                                                            java.lang.String value,
                                                            java.util.List<java.lang.String> errorMessageList)
Uses a black-list approach for necessary characters for HTML. Does not allow various characters (after canonicalization), including "<", ">", "&" (if not followed by a space), and "%" (if not followed by a space).

Parameters:
value -
errorMessageList -

checkStringForHtmlSafeOnly

public static java.lang.String checkStringForHtmlSafeOnly(java.lang.String valueName,
                                                          java.lang.String value,
                                                          java.util.List<java.lang.String> errorMessageList)
Uses a white-list approach to check for safe HTML. Based on the ESAPI validator configured in the antisamy-esapi.xml file.

Parameters:
value -
errorMessageList -
Returns:
String with updated value if needed for safer HTML.

htmlSpecialChars

public static java.lang.String htmlSpecialChars(java.lang.String html,
                                                boolean doubleQuotes,
                                                boolean singleQuotes,
                                                boolean insertBR)
Deprecated. Use StringUtil.htmlEncoder instead.

Translates various HTML characters in a string so that the string can be displayed in a browser safely

This function is useful in preventing user-supplied text from containing HTML markup, such as in a message board or guest book application. The optional arguments doubleQuotes and singleQuotes allow the control of the substitution of the quote characters. The default is to translate them with the HTML equivalent.

The translations performed are:
  1. '&' (ampersand) becomes '&'
  2. '"' (double quote) becomes '"' when doubleQuotes is true.
  3. ''' (single quote) becomes ''' when singleQuotes is true.
  4. '<' (less than) becomes '<'
  5. '>' (greater than) becomes '>'
  6. \n (Carriage Return) becomes '<br>gt;'


htmlSpecialChars

public static java.lang.String htmlSpecialChars(java.lang.String html)

fromHtmlToSpecialChars

public static java.lang.String fromHtmlToSpecialChars(java.lang.String html,
                                                      boolean doubleQuotes,
                                                      boolean singleQuotes,
                                                      boolean insertBR)

collapseNewlines

public static java.lang.String collapseNewlines(java.lang.String str)
Remove/collapse multiple newline characters

Parameters:
str - string to collapse newlines in
Returns:
the converted string

collapseSpaces

public static java.lang.String collapseSpaces(java.lang.String str)
Remove/collapse multiple spaces

Parameters:
str - string to collapse spaces in
Returns:
the converted string

collapseCharacter

public static java.lang.String collapseCharacter(java.lang.String str,
                                                 char c)
Remove/collapse multiple characters

Parameters:
str - string to collapse characters in
c - character to collapse
Returns:
the converted string

wrapString

public static StringUtil.StringWrapper wrapString(java.lang.String theString)

makeStringWrapper

public static StringUtil.StringWrapper makeStringWrapper(java.lang.String theString)