Class StringUtils
- java.lang.Object
-
- org.jivesoftware.util.StringUtils
-
public final class StringUtils extends Object
Utility class to perform common String manipulation algorithms.
-
-
Field Summary
Fields Modifier and Type Field Description static SystemProperty<Boolean>
XMPP_STREAM_SUPPRESS_NEWLINE_AFTER_XML_DECLARATION
Controls if a newline is added after a generated XML declaration and the 'stream' open tag.static SystemProperty<Boolean>
XMPP_STREAM_SUPPRESS_XML_DECLARATION
Controls if an XML declaration is generated before a 'stream' open tag is sent.
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static String
abbreviate(String str, int maxWidth)
Abbreviates a string to a specified length and then adds an ellipsis if the input is greater than the maxWidth.static String
asUnclosedStream(org.dom4j.Document document)
Formats the XML document, that is required to have a root element named 'stream', as a string.static String
chopAtWord(String string, int length)
Intelligently chops a String at a word boundary (whitespace) that occurs at the specified index in the argument or before.static String
collectionToString(Collection<String> collection)
Returns a collection of Strings as a comma-delimitted list of strings.static boolean
contains(String[] array, String item)
Returns true if the given string is in the given array.static boolean
containsIgnoringCase(String nullableString, String value)
static String
dateToMillis(Date date)
Formats a Date as a fifteen character long String made up of the Date's padded millisecond value.static byte[]
decodeBase32(String data)
Decodes a base32 String using the base32hex profile.static byte[]
decodeBase64(String data)
Deprecated, for removal: This API element is subject to removal in a future version.Use java.util.Base64 instead.static byte[]
decodeHex(String hex)
Turns a hex encoded string into a byte array.static String
encodeBase32(byte[] data)
Encodes a byte array into a base32 String using the base32hex profile.static String
encodeBase32(String data)
Encodes a String as a base32 String using the base32hex profile.static String
encodeBase64(byte[] data)
Deprecated, for removal: This API element is subject to removal in a future version.Use java.util.Base64 instead.static String
encodeBase64(String data)
Deprecated, for removal: This API element is subject to removal in a future version.Use java.util.Base64 instead.static String
encodeHex(byte[] bytes)
Turns an array of bytes into a String representing each byte as an unsigned hex number.static String
escapeForSQL(String string)
Escapes all necessary characters in the String so that it can be used in SQLstatic String
escapeForXML(String string)
Escapes all necessary characters in the String so that it can be used in an XML doc.static String
escapeHTMLTags(String in)
This method takes a string which may contain HTML tags (ie, <b>, <table>, etc) and converts the '<' and '>' characters to their HTML escape sequences.static String
escapeHTMLTags(String in, boolean includeLF)
This method takes a string which may contain HTML tags (ie, <b>, <table>, etc) and converts the '<' and '>' characters to their HTML escape sequences.static String
getElapsedTime(long delta)
Returns a textual representation for the time that has elapsed.static String
getFullElapsedTime(long delta)
Returns a textual representation for the time that has elapsed.static String
getFullElapsedTime(Duration delta)
Returns a textual representation for the time that has elapsed.static String
getTimeFromLong(long diff)
Returns a formatted String from time.static String
hash(byte[] bytes, String algorithm)
Hashes a byte array using the specified algorithm and returns the result as a String of hexadecimal numbers.static String
hash(String data)
Hashes a String using the Md5 algorithm and returns the result as a String of hexadecimal numbers.static String
hash(String data, String algorithm)
Hashes a String using the specified algorithm and returns the result as a String of hexadecimal numbers.static boolean
isBase32(String data)
Validates a string to ensure all its bytes are in the Base32 alphabet.static boolean
isValidEmailAddress(String address)
Returns true if the string passed in is a valid Email address.static Optional<Boolean>
parseBoolean(String value)
Parses a boolean.static Optional<Double>
parseDouble(String value)
static Optional<Integer>
parseInteger(String value)
static Optional<Long>
parseLong(String value)
static String
randomString(int length)
Returns a random String of numbers and letters (lower and upper case) of the specified length.static String
removeXSSCharacters(String input)
Removes characters likely to enable Cross Site Scripting attacks from the provided input string.static String
replace(String line, String oldString, String newString, int[] count)
Replaces all instances of oldString with newString in line.static String
replaceIgnoreCase(String line, String oldString, String newString)
Replaces all instances of oldString with newString in line with the added feature that matches of newString in oldString ignore case.static String
replaceIgnoreCase(String line, String oldString, String newString, int[] count)
Replaces all instances of oldString with newString in line with the added feature that matches of newString in oldString ignore case.static List<String>
shellSplit(CharSequence string)
Simple Java program to tokenize string as a shell would - similar to shlex in Pythonstatic Collection<String>
stringToCollection(String string)
Returns a comma-delimitted list of Strings as a Collection.static String
stripTags(String in)
This method takes a string and strips out all tags except
tags while still leaving the tag body intact.static String[]
toLowerCaseWordArray(String text)
Converts a line of text into an array of lower case words using a BreakIterator.wordInstance().static String
unescapeFromXML(String string)
Unescapes the String by converting XML escape sequences back into normal characters.static String
validateDomainName(String domain)
Returns a valid domain name, possibly as an ACE-encoded IDN (per RFC 3490).static String
wordWrap(String input, int width, Locale locale)
Reformats a string where lines that are longer thanwidth
are split apart at the earliest wordbreak or at maxLength, whichever is sooner.static String
zeroPadString(String string, int length)
Pads the supplied String with 0's to the specified length and returns the result as a new String.
-
-
-
Field Detail
-
XMPP_STREAM_SUPPRESS_XML_DECLARATION
public static final SystemProperty<Boolean> XMPP_STREAM_SUPPRESS_XML_DECLARATION
Controls if an XML declaration is generated before a 'stream' open tag is sent.
-
XMPP_STREAM_SUPPRESS_NEWLINE_AFTER_XML_DECLARATION
public static final SystemProperty<Boolean> XMPP_STREAM_SUPPRESS_NEWLINE_AFTER_XML_DECLARATION
Controls if a newline is added after a generated XML declaration and the 'stream' open tag.
-
-
Method Detail
-
replaceIgnoreCase
public static String replaceIgnoreCase(String line, String oldString, String newString)
Replaces all instances of oldString with newString in line with the added feature that matches of newString in oldString ignore case.- Parameters:
line
- the String to search to perform replacements onoldString
- the String that should be replaced by newStringnewString
- the String that will replace all instances of oldString- Returns:
- a String will all instances of oldString replaced by newString
-
replaceIgnoreCase
public static String replaceIgnoreCase(String line, String oldString, String newString, int[] count)
Replaces all instances of oldString with newString in line with the added feature that matches of newString in oldString ignore case. The count paramater is set to the number of replaces performed.- Parameters:
line
- the String to search to perform replacements onoldString
- the String that should be replaced by newStringnewString
- the String that will replace all instances of oldStringcount
- a value that will be updated with the number of replaces performed.- Returns:
- a String will all instances of oldString replaced by newString
-
replace
public static String replace(String line, String oldString, String newString, int[] count)
Replaces all instances of oldString with newString in line. The count Integer is updated with number of replaces.- Parameters:
line
- the String to search to perform replacements on.oldString
- the String that should be replaced by newString.newString
- the String that will replace all instances of oldString.count
- a single element array that, after running, will contain the number of matching items- Returns:
- a String will all instances of oldString replaced by newString.
-
stripTags
public static String stripTags(String in)
This method takes a string and strips out all tags except
tags while still leaving the tag body intact.- Parameters:
in
- the text to be converted.- Returns:
- the input string with all tags removed.
-
escapeHTMLTags
public static String escapeHTMLTags(String in)
This method takes a string which may contain HTML tags (ie, <b>, <table>, etc) and converts the '<' and '>' characters to their HTML escape sequences. It will also replace LF with <br>.- Parameters:
in
- the text to be converted.- Returns:
- the input string with the characters '<' and '>' replaced with their HTML escape sequences.
-
escapeHTMLTags
public static String escapeHTMLTags(String in, boolean includeLF)
This method takes a string which may contain HTML tags (ie, <b>, <table>, etc) and converts the '<' and '>' characters to their HTML escape sequences.- Parameters:
in
- the text to be converted.includeLF
- set to true to replace \n with
.- Returns:
- the input string with the characters '<' and '>' replaced with their HTML escape sequences.
-
hash
public static String hash(String data)
Hashes a String using the Md5 algorithm and returns the result as a String of hexadecimal numbers. This method is synchronized to avoid excessive MessageDigest object creation. If calling this method becomes a bottleneck in your code, you may wish to maintain a pool of MessageDigest objects instead of using this method.A hash is a one-way function -- that is, given an input, an output is easily computed. However, given the output, the input is almost impossible to compute. This is useful for passwords since we can store the hash and a hacker will then have a very hard time determining the original password.
In Jive, every time a user logs in, we simply take their plain text password, compute the hash, and compare the generated hash to the stored hash. Since it is almost impossible that two passwords will generate the same hash, we know if the user gave us the correct password or not. The only negative to this system is that password recovery is basically impossible. Therefore, a reset password method is used instead.
- Parameters:
data
- the String to compute the hash of.- Returns:
- a hashed version of the passed-in String
-
hash
public static String hash(String data, String algorithm)
Hashes a String using the specified algorithm and returns the result as a String of hexadecimal numbers. This method is synchronized to avoid excessive MessageDigest object creation. If calling this method becomes a bottleneck in your code, you may wish to maintain a pool of MessageDigest objects instead of using this method.A hash is a one-way function -- that is, given an input, an output is easily computed. However, given the output, the input is almost impossible to compute. This is useful for passwords since we can store the hash and a hacker will then have a very hard time determining the original password.
In Jive, every time a user logs in, we simply take their plain text password, compute the hash, and compare the generated hash to the stored hash. Since it is almost impossible that two passwords will generate the same hash, we know if the user gave us the correct password or not. The only negative to this system is that password recovery is basically impossible. Therefore, a reset password method is used instead.
- Parameters:
data
- the String to compute the hash of.algorithm
- the name of the algorithm requested.- Returns:
- a hashed version of the passed-in String
-
hash
public static String hash(byte[] bytes, String algorithm)
Hashes a byte array using the specified algorithm and returns the result as a String of hexadecimal numbers. This method is synchronized to avoid excessive MessageDigest object creation. If calling this method becomes a bottleneck in your code, you may wish to maintain a pool of MessageDigest objects instead of using this method.A hash is a one-way function -- that is, given an input, an output is easily computed. However, given the output, the input is almost impossible to compute. This is useful for passwords since we can store the hash and a hacker will then have a very hard time determining the original password.
In Jive, every time a user logs in, we simply take their plain text password, compute the hash, and compare the generated hash to the stored hash. Since it is almost impossible that two passwords will generate the same hash, we know if the user gave us the correct password or not. The only negative to this system is that password recovery is basically impossible. Therefore, a reset password method is used instead.
- Parameters:
bytes
- the byte array to compute the hash of.algorithm
- the name of the algorithm requested.- Returns:
- a hashed version of the passed-in String
-
encodeHex
public static String encodeHex(byte[] bytes)
Turns an array of bytes into a String representing each byte as an unsigned hex number.- Parameters:
bytes
- an array of bytes to convert to a hex-string- Returns:
- generated hex string
-
decodeHex
public static byte[] decodeHex(String hex)
Turns a hex encoded string into a byte array. It is specifically meant to "reverse" the toHex(byte[]) method.- Parameters:
hex
- a hex encoded String to transform into a byte array.- Returns:
- a byte array representing the hex String[
-
encodeBase64
@Deprecated(forRemoval=true, since="4.9.0") public static String encodeBase64(String data)
Deprecated, for removal: This API element is subject to removal in a future version.Use java.util.Base64 instead.Encodes a String as a base64 String.- Parameters:
data
- a String to encode.- Returns:
- a base64 encoded String.
-
encodeBase64
@Deprecated(forRemoval=true, since="4.9.0") public static String encodeBase64(byte[] data)
Deprecated, for removal: This API element is subject to removal in a future version.Use java.util.Base64 instead.Encodes a byte array into a base64 String.- Parameters:
data
- a byte array to encode.- Returns:
- a base64 encode String.
-
decodeBase64
@Deprecated(forRemoval=true, since="4.9.0") public static byte[] decodeBase64(String data)
Deprecated, for removal: This API element is subject to removal in a future version.Use java.util.Base64 instead.Decodes a base64 String.- Parameters:
data
- a base64 encoded String to decode.- Returns:
- the decoded String.
-
encodeBase32
public static String encodeBase32(String data)
Encodes a String as a base32 String using the base32hex profile.- Parameters:
data
- a String to encode.- Returns:
- a base32 encoded String.
-
encodeBase32
public static String encodeBase32(byte[] data)
Encodes a byte array into a base32 String using the base32hex profile. Implementation is case-insensitive and returns encoded strings in lower case.- Parameters:
data
- a byte array to encode.- Returns:
- a base32 encode String.
-
decodeBase32
public static byte[] decodeBase32(String data)
Decodes a base32 String using the base32hex profile. Implementation is case-insensitive and converts the given string to upper case before decoding.- Parameters:
data
- a base32 encoded String to decode.- Returns:
- the decoded String.
-
isBase32
public static boolean isBase32(String data)
Validates a string to ensure all its bytes are in the Base32 alphabet. Implementation is case-insensitive and converts the given string to upper case before evaluating.- Parameters:
data
- the string to test- Returns:
- True if the given string can be decoded using Base32
-
toLowerCaseWordArray
public static String[] toLowerCaseWordArray(String text)
Converts a line of text into an array of lower case words using a BreakIterator.wordInstance().This method is under the Jive Open Source Software License and was written by Mark Imbriaco.
- Parameters:
text
- a String of text to convert into an array of words- Returns:
- text broken up into an array of words.
-
randomString
public static String randomString(int length)
Returns a random String of numbers and letters (lower and upper case) of the specified length. The method uses a cryptographically strong random number generator as provided bySecureRandom
The specified length must be at least one. If not, the method will return null.
- Parameters:
length
- the desired length of the random String to return.- Returns:
- a random String of numbers and letters of the specified length.
-
chopAtWord
public static String chopAtWord(String string, int length)
Intelligently chops a String at a word boundary (whitespace) that occurs at the specified index in the argument or before. However, if there is a newline character beforelength
, the String will be chopped there. If no newline or whitespace is found instring
up to the indexlength
, the String will chopped atlength
.For example, chopAtWord("This is a nice String", 10) will return "This is a" which is the first word boundary less than or equal to 10 characters into the original String.
- Parameters:
string
- the String to chop.length
- the index instring
to start looking for a whitespace boundary at.- Returns:
- a substring of
string
whose length is less than or equal tolength
, and that is chopped at whitespace.
-
wordWrap
public static String wordWrap(String input, int width, Locale locale)
Reformats a string where lines that are longer thanwidth
are split apart at the earliest wordbreak or at maxLength, whichever is sooner. If the width specified is less than 5 or greater than the input Strings length the string will be returned as is.Please note that this method can be lossy - trailing spaces on wrapped lines may be trimmed.
- Parameters:
input
- the String to reformat.width
- the maximum length of any one line.locale
- the local- Returns:
- a new String with reformatted as needed.
-
escapeForSQL
public static String escapeForSQL(String string)
Escapes all necessary characters in the String so that it can be used in SQL- Parameters:
string
- the string to escape.- Returns:
- the string with appropriate characters escaped.
-
escapeForXML
public static String escapeForXML(String string)
Escapes all necessary characters in the String so that it can be used in an XML doc.- Parameters:
string
- the string to escape.- Returns:
- the string with appropriate characters escaped.
-
unescapeFromXML
public static String unescapeFromXML(String string)
Unescapes the String by converting XML escape sequences back into normal characters.- Parameters:
string
- the string to unescape.- Returns:
- the string with appropriate characters unescaped.
-
zeroPadString
public static String zeroPadString(String string, int length)
Pads the supplied String with 0's to the specified length and returns the result as a new String. For example, if the initial String is "9999" and the desired length is 8, the result would be "00009999". This type of padding is useful for creating numerical values that need to be stored and sorted as character data. Note: the current implementation of this method allows for a maximumlength
of 64.- Parameters:
string
- the original String to pad.length
- the desired length of the new padded String.- Returns:
- a new String padded with the required number of 0's.
-
dateToMillis
public static String dateToMillis(Date date)
Formats a Date as a fifteen character long String made up of the Date's padded millisecond value.- Parameters:
date
- the date to encode- Returns:
- a Date encoded as a String.
-
getFullElapsedTime
public static String getFullElapsedTime(long delta)
Returns a textual representation for the time that has elapsed.- Parameters:
delta
- the elapsed time in milliseconds- Returns:
- textual representation for the time that has elapsed.
-
getFullElapsedTime
public static String getFullElapsedTime(Duration delta)
Returns a textual representation for the time that has elapsed.- Parameters:
delta
- the elapsed time.- Returns:
- textual representation for the time that has elapsed.
-
getElapsedTime
public static String getElapsedTime(long delta)
Returns a textual representation for the time that has elapsed.- Parameters:
delta
- the elapsed time in milliseconds.- Returns:
- textual representation for the time that has elapsed.
-
getTimeFromLong
public static String getTimeFromLong(long diff)
Returns a formatted String from time.- Parameters:
diff
- the amount of elapsed time.- Returns:
- the formatte String.
-
collectionToString
public static String collectionToString(Collection<String> collection)
Returns a collection of Strings as a comma-delimitted list of strings.- Parameters:
collection
- the collection of strings- Returns:
- a String representing the Collection.
-
stringToCollection
public static Collection<String> stringToCollection(String string)
Returns a comma-delimitted list of Strings as a Collection.- Parameters:
string
- the string to split- Returns:
- a Collection representing the String.
-
contains
public static boolean contains(String[] array, String item)
Returns true if the given string is in the given array.- Parameters:
array
- an array of Strings to checkitem
- the item to look for- Returns:
- true if the array contains the item
-
abbreviate
public static String abbreviate(String str, int maxWidth)
Abbreviates a string to a specified length and then adds an ellipsis if the input is greater than the maxWidth. Example input:user1@jivesoftware.com/home
and a maximum length of 20 characters, the abbreviate method will return:user1@jivesoftware.c...
- Parameters:
str
- the String to abbreviate.maxWidth
- the maximum size of the string, minus the ellipsis.- Returns:
- the abbreviated String, or
null
if the string wasnull
.
-
isValidEmailAddress
public static boolean isValidEmailAddress(String address)
Returns true if the string passed in is a valid Email address.- Parameters:
address
- Email address to test for validity.- Returns:
- true if the string passed in is a valid email address.
-
validateDomainName
public static String validateDomainName(String domain)
Returns a valid domain name, possibly as an ACE-encoded IDN (per RFC 3490).- Parameters:
domain
- Proposed domain name- Returns:
- The validated domain name, possibly ACE-encoded
- Throws:
IllegalArgumentException
- The given domain name is not valid
-
removeXSSCharacters
public static String removeXSSCharacters(String input)
Removes characters likely to enable Cross Site Scripting attacks from the provided input string. The characters that are removed from the input string, if present, are:< > " ' % ; ) ( & + -
- Parameters:
input
- the string to be scrubbed- Returns:
- Input without certain characters;
-
containsIgnoringCase
public static boolean containsIgnoringCase(String nullableString, String value)
- Parameters:
nullableString
- the string to checkvalue
- the string to match against- Returns:
true
is nullableString is not null and contains the supplied value, otherwisefalse
-
parseBoolean
public static Optional<Boolean> parseBoolean(String value)
Parses a boolean. Subtly different from Boolean.parseBoolean in that it returnsOptional.empty()
instead offalse
if the supplied value is not "true" or "false" (ignoring case)- Parameters:
value
- Any string value- Returns:
true
,false
orOptional.empty()
-
shellSplit
public static List<String> shellSplit(CharSequence string)
Simple Java program to tokenize string as a shell would - similar to shlex in Python- Parameters:
string
- The string to be split.- Returns:
- The splitted string.
- See Also:
- https://gist.github.com/raymyers/8077031
-
asUnclosedStream
public static String asUnclosedStream(org.dom4j.Document document)
Formats the XML document, that is required to have a root element named 'stream', as a string. This method will strip the closing 'stream' tag from the string prior to returning it.- Parameters:
document
- The document for which to return a string representation- Returns:
- the string-representation of the document
- Throws:
IllegalArgumentException
- when the document's root element is not named 'stream'
-
-