public class TextUtils
extends java.lang.Object
In current version this class provides methods for converting a String into its numeric value of various types (Integer, Float, Byte, Double, Long, Short).
There are several methods for retrieving Exception stacktrace as a String in full or shortened version. Shortened version of the stacktrace will contain concise information focusing on specific package or subpackage while removing long parts of irrelevant stacktrace. This could be very useful for logging in web-based architecture where stacktrace may contain long parts of server provided classes trace that could be eliminated with the methods of this class while retaining important parts of the stacktrace relating to user's packages. Also the same utility (starting from version 1.5.0.3) allows to filter and shorten stacktrace as a string the same way as the stacktrace extracted from exception. So, essentially stacktraces could be filtered "on the fly" at run time or later on from any text source such as log.
Also this class provides methods that work with textual representation of versions.
Valid version is a String of the following format:
X[.X[.X[...]]]
where X is a zero or positive integer not larger than 2147483647. Leading or trailing white spaces in this string are permitted and are ignored. Examples of valid versions are: "1.6", "58", " 7.34.17 " etc. (Note that last example contains both leading and trailing white spaces and it is still a valid version)
Another useful feature is parsing String to time interval. It parses Strings with numerical value and optional time unit suffix (for example string "38s" will be parsed as 38 seconds, "24m" - 24 minutes "4h" - 4 hours, "3d" - 3 days and "45" as 45 milliseconds.) This method may be very useful for parsing time interval properties such as timeouts or waiting periods from configuration files.
Also in this class there is a method that converts String to preserve indentation formatting for html without use of escape characters. It converts a String in such a way that its spaces are not modified by HTML renderer i.e. it replaces regular space characters with non-breaking spaces known as ' ' but they look in your source as regular space ' ' and not as ' ' It also replaces new line character with '<br>'.
Note that this class has a loose dependency on slf4J library. If in the project some other compatible logging library is present (such as Log4J) this class will still work without any ill effects
| Modifier and Type | Field and Description |
|---|---|
protected static java.lang.String |
DAYS_SUFFIX |
protected static java.util.concurrent.TimeUnit |
DEFAULT_TIMEOUT_TIME_UNIT |
protected static java.lang.String |
HOURS_SUFFIX |
protected static java.lang.String |
MINUTES_SUFFIX |
protected static java.lang.String |
SECONDS_SUFFIX |
| Constructor and Description |
|---|
TextUtils() |
| Modifier and Type | Method and Description |
|---|---|
static int |
compareVersions(java.lang.String ver1,
java.lang.String ver2)
This method compares 2 Strings as versions
|
static int |
compareVersions(java.lang.String ver1,
Version ver2)
This method compares a String that represents a version to a Version
|
static int |
compareVersions(Version ver1,
java.lang.String ver2)
This method compares a Version to a String that represents a version
|
static int |
compareVersions(Version ver1,
Version ver2)
This method compares two Version instances
|
static java.lang.String |
formatStringToPreserveIndentationForHtml(java.lang.String rawText)
This method converts a String in such a way that its spaces are not modified by HTML renderer i.e.
|
static java.lang.String |
getRelevantPackage()
This a getter method for global value of relevant package prefix property
|
static java.lang.String |
getStacktrace(java.lang.CharSequence stacktrace)
This method retrieves a stacktrace in shortened format from source stactrace provided as
CharSequence. |
static java.lang.String |
getStacktrace(java.lang.CharSequence stacktrace,
java.lang.String relevantPackage)
This method retrieves a stacktrace in shortened format from source stactrace provided as
CharSequence. |
static java.lang.String |
getStacktrace(java.lang.Throwable e)
This method retrieves a stacktrace from
Throwable as a String in shortened format. |
static java.lang.String |
getStacktrace(java.lang.Throwable e,
boolean cutTBS)
This method retrieves a stacktrace from
Throwable as a String in full or shortened format. |
static java.lang.String |
getStacktrace(java.lang.Throwable e,
boolean cutTBS,
java.lang.String relevantPackage)
This method retrieves a stacktrace from
Throwable as a String in full or shortened format. |
static java.lang.String |
getStacktrace(java.lang.Throwable e,
java.lang.String relevantPackage)
This method retrieves a stacktrace from
Throwable as a String in shortened format. |
static java.lang.Byte |
parseStringToByte(java.lang.CharSequence num,
byte defaultValue,
java.lang.String nullOrEmptyStringErrorMessage,
java.lang.String numberFormatErrorMessage)
This method parses a String to Numeric value.
|
static double |
parseStringToDouble(java.lang.CharSequence num,
double defaultValue,
java.lang.String nullOrEmptyStringErrorMessage,
java.lang.String numberFormatErrorMessage)
This method parses a String to Numeric value.
|
static float |
parseStringToFloat(java.lang.CharSequence num,
float defaultValue,
java.lang.String nullOrEmptyStringErrorMessage,
java.lang.String numberFormatErrorMessage)
This method parses a String to Numeric value.
|
static int |
parseStringToInt(java.lang.CharSequence num,
int defaultValue,
java.lang.String nullOrEmptyStringErrorMessage,
java.lang.String numberFormatErrorMessage)
This method parses a String to its Numeric value.
|
static long |
parseStringToLong(java.lang.CharSequence num,
long defaultValue,
java.lang.String nullOrEmptyStringErrorMessage,
java.lang.String numberFormatErrorMessage)
This method parses a String to Numeric value.
|
static java.lang.Short |
parseStringToShort(java.lang.CharSequence num,
short defaultValue,
java.lang.String nullOrEmptyStringErrorMessage,
java.lang.String numberFormatErrorMessage)
This method parses a String to Numeric value.
|
static TimeInterval |
parseStringToTimeInterval(java.lang.String valueStr)
This method parses String value into
TimeInterval. |
static TimeInterval |
parsingStringToTimeInterval(java.lang.String valueStr)
Deprecated.
Use
parseStringToTimeInterval(String) instead. The new method the same as this one except the name change.
This method is left for backwards compatibility only and will be removed in future versions. |
static void |
setRelevantPackage(java.lang.String relevantPackage)
This is a setter method for relevant package prefix property for method
getStacktrace(Throwable, boolean) Once the value has been set
the convenience method getStacktrace(Throwable, boolean) could be used instead of method
getStacktrace(Throwable, boolean, String) |
protected static final java.util.concurrent.TimeUnit DEFAULT_TIMEOUT_TIME_UNIT
protected static final java.lang.String SECONDS_SUFFIX
protected static final java.lang.String MINUTES_SUFFIX
protected static final java.lang.String HOURS_SUFFIX
protected static final java.lang.String DAYS_SUFFIX
public static int compareVersions(java.lang.String ver1,
java.lang.String ver2)
throws InvalidVersionFormatException
ver1 - String that contains version for comparisonver2 - String that contains version for comparisonInvalidVersionFormatException - if any of the String instances is not a valid Versionpublic static int compareVersions(Version ver1, java.lang.String ver2) throws InvalidVersionFormatException
ver1 - Version for comparisonver2 - String that contains version for comparisonInvalidVersionFormatException - if the String parameter is not a valid Versionpublic static int compareVersions(java.lang.String ver1,
Version ver2)
throws InvalidVersionFormatException
ver1 - String that contains version for comparisonver2 - Version for comparisonInvalidVersionFormatException - if the String parameter is not a valid Versionpublic static int compareVersions(Version ver1, Version ver2)
ver1 - Version for comparisonver2 - Version for comparisonpublic static int parseStringToInt(java.lang.CharSequence num,
int defaultValue,
java.lang.String nullOrEmptyStringErrorMessage,
java.lang.String numberFormatErrorMessage)
nullOrEmptyStringErrorMessage or numberFormatErrorMessage parameters are null or empty
Strings than the correlating error will not be printed.num - CharSequence to be parseddefaultValue - value that will be returned by this method if parsing of the String failednullOrEmptyStringErrorMessage - String that holds an error message that will printed into log if parameter num is null or blanknumberFormatErrorMessage - String that holds an error message that will printed into log if parameter num is not in appropriate formatpublic static float parseStringToFloat(java.lang.CharSequence num,
float defaultValue,
java.lang.String nullOrEmptyStringErrorMessage,
java.lang.String numberFormatErrorMessage)
nullOrEmptyStringErrorMessage or numberFormatErrorMessage parameters are null or empty Strings than the
correlating error will not be printed.num - CharSequence to be parseddefaultValue - value that will be returned by this method if parsing of the String failednullOrEmptyStringErrorMessage - String that holds an error message that will printed into log if parameter num is null or blanknumberFormatErrorMessage - String that holds an error message that will printed into log if parameter num is not in appropriate formatpublic static java.lang.Byte parseStringToByte(java.lang.CharSequence num,
byte defaultValue,
java.lang.String nullOrEmptyStringErrorMessage,
java.lang.String numberFormatErrorMessage)
nullOrEmptyStringErrorMessage or numberFormatErrorMessage parameters are null or empty Strings than the
correlating error will not be printed.num - CharSequence to be parseddefaultValue - value that will be returned by this method if parsing of the String failednullOrEmptyStringErrorMessage - String that holds an error message that will printed into log if parameter num is null or blanknumberFormatErrorMessage - String that holds an error message that will printed into log if parameter num is not in appropriate formatpublic static double parseStringToDouble(java.lang.CharSequence num,
double defaultValue,
java.lang.String nullOrEmptyStringErrorMessage,
java.lang.String numberFormatErrorMessage)
nullOrEmptyStringErrorMessage or numberFormatErrorMessage parameters are null or empty Strings than the
correlating error will not be printed.num - CharSequence to be parseddefaultValue - value that will be returned by this method if parsing of the String failednullOrEmptyStringErrorMessage - String that holds an error message that will printed into log if parameter num is null or blanknumberFormatErrorMessage - String that holds an error message that will printed into log if parameter num is not in appropriate formatpublic static long parseStringToLong(java.lang.CharSequence num,
long defaultValue,
java.lang.String nullOrEmptyStringErrorMessage,
java.lang.String numberFormatErrorMessage)
nullOrEmptyStringErrorMessage or numberFormatErrorMessage parameters are null or empty Strings than the
correlating error will not be printed.num - CharSequence to be parseddefaultValue - value that will be returned by this method if parsing of the String failednullOrEmptyStringErrorMessage - String that holds an error message that will printed into log if parameter num is null or blanknumberFormatErrorMessage - String that holds an error message that will printed into log if parameter num is not in appropriate formatpublic static java.lang.Short parseStringToShort(java.lang.CharSequence num,
short defaultValue,
java.lang.String nullOrEmptyStringErrorMessage,
java.lang.String numberFormatErrorMessage)
nullOrEmptyStringErrorMessage or numberFormatErrorMessage parameters are null or empty Strings than the
correlating error will not be printed.num - CharSequence to be parseddefaultValue - value that will be returned by this method if parsing of the String failednullOrEmptyStringErrorMessage - String that holds an error message that will printed into log if parameter num is null or blanknumberFormatErrorMessage - String that holds an error message that will printed into log if parameter num is not in appropriate formatpublic static TimeInterval parseStringToTimeInterval(java.lang.String valueStr) throws java.lang.IllegalArgumentException
TimeInterval. This method supports time interval suffixes "s"
for seconds, "m" for minutes, "h" for hours, and "d" for days. Suffix is case insensitive.
If String parameter contains no suffix the default is milliseconds. So for example string "38s" will be parsed
as 38 seconds, "24m" - 24 minutes "4h" - 4 hours, "3d" - 3 days and "45" as 45 milliseconds. If the string parses
to a negative numerical value or 0 or the string is not a valid numerical value then IllegalArgumentException
is thrown. Note that it is very convenient to extract time value from TimeInterval, See methods
TimeInterval.toMillis(), TimeInterval.toSeconds(), TimeInterval.toMinutes(),
TimeInterval.toHours(), TimeInterval.toDays().
methodInvokingInterval property that you need to set for 5 days. So in order to
set the miliseconds value you will need to calculate that 5 days is 432000000 milliseconds (obviously not an
impossible task but annoying and error prone) and then anyone else who sees the value 432000000 will have to
calculate it back to 5 days which is frustrating. But using this method you will have a property value set to
"5d" and invoking the code
long milliseconds = TextUtils.parseStringToTimeInterval("5d").toMillis();
valueStr - String value to parse to TimeIntervalTimeInterval parsed from the Stringjava.lang.IllegalArgumentException - if parsed value has invalid suffix, invalid numeric value or negative numeric value or 0public static TimeInterval parsingStringToTimeInterval(java.lang.String valueStr) throws java.lang.IllegalArgumentException
parseStringToTimeInterval(String) instead. The new method the same as this one except the name change.
This method is left for backwards compatibility only and will be removed in future versions.valueStr - String value to parse to TimeIntervalTimeInterval parsed from the Stringjava.lang.IllegalArgumentException - if parsed value has invalid suffix, invalid numeric value or negative numeric value or 0public static java.lang.String getStacktrace(java.lang.Throwable e,
boolean cutTBS,
java.lang.String relevantPackage)
This method retrieves a stacktrace from Throwable as a String in full or shortened format. Shortened format skips the lines in the
stacktrace that do not start with a configurable package prefix and replaces them with "..." line. The stacktrace is viewed as consisting
possibly of several parts. If stacktrace contains "caused by" or "Suppressed" section, each such section for the purposes of
this utility is called "Singular stacktrace". For example the stacktrace bellow contains 2 singular stacktraces: First is 4 top lines and the
second starting from the line "Caused by: ..." and to the end.
The way this method shortens the stacktrace is as follows. Each "singular" stacktraces are analyzed and shortened separately. For each singular
stacktrace the error message is always printed. Then all the lines that follow are printed even if they do not start with prefix specified by
relevantPackage. Once the first line with the prefix is found this line and all immediately following lines that start with the relevant
package prefix are printed as well. The first line that does not start with the prefix after a section of the lines that did is also printed.
But all the following lines that do not start with the prefix are skipped and replaced with a single line "...". If at some point within the
stacktrace a line that starts with the prefix is encountered again this line and all the following line that start with the prefix + one
following line that does not start with the prefix are printed in. And so on. Here is an example: Assume that exception above was passed as a
parameter to this method and parameter relevantPackage is set to "com.plain.analytics.v2.utils.test." which means that the lines starting with
that prefix are the important or "relevant" lines. (Also the parameter cutTBS set to true which means that stacktrace should be
shortened at all. In this case the result of this method should be as follows:
Note that the first singular stacktrace is printed in full because all the lines start with the required prefix. The second singular stacktrace
prints the first 7 lines because at first all the lines are printed until the first line with relevant prefix is found, and then all the lines
with the prefix (one in our case) are printed + plus one following line without the prefix. And then the second line without the prefix (3d
from the bottom) is skipped and replaced with line "...". But then again we encounter a line with the prefix which is printed and finally the
last line is printed because it is the first line without prefix following the one with the prefix. In this particular example only one line
was skipped over which is not very much, but for web-based environments for the long stacktraces that contain long traces of server related
classes this method could be very effective in removing irrelevant lines and leaving only application related lines making log files more
concise and clear.
setRelevantPackage(String) and getRelevantPackage()). Using method
setRelevantPackage(String) to set the value will preset the value of relevant package prefix for all calls for which parameter
relevantPackage is null. In fact there is a convinience method getStacktrace(Throwable, boolean) that invokes this method with
parameter relevantPackage set to null and relies on the globally set property through method setRelevantPackage(String).
However if the global property was not set and parameter relevantPackage was left null then the method will return stacktrace in full as
if the parameter cutTBS was set to falsee - Throwable from which stacktrace should be retrievedcutTBS - boolean that specifies if stacktrace should be shortened. The stacktrace should be shortened if this flag is set to true.
Note that if this parameter set to false the stacktrace will be printed in full and parameter relevantPackage becomes
irrelevant.relevantPackage - String that contains the prefix specifying which lines are relevant. It is recommended to be in the following format
"packag_name1.[package_name2.[...]]." In the example above it should be "com.plain.analytics.v2.utils.test.".public static java.lang.String getStacktrace(java.lang.Throwable e,
boolean cutTBS)
Throwable as a String in full or shortened format. This is convenience method that invokes
method getStacktrace(Throwable, boolean, String) with last parameter as null. It relies on relevant package prefix to have
been set by method setRelevantPackage(String). There are several ways to pre-invoke method setRelevantPackage(String):setRelevantPackage(String) which will appropriately initialize the package prefix and enable the use of this method
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass" value="com.mgnt.utils.TextUtils"/>
<property name="targetMethod" value="setRelevantPackage"/>
<property name="arguments" value="com.plain."/>
</bean>
e - Throwable from which stacktrace should be retrievedcutTBS - boolean flag that specifies if stacktrace should be shortened or not. It is shortened if the flag value is truegetStacktrace(Throwable, boolean, String)public static java.lang.String getStacktrace(java.lang.Throwable e)
Throwable as a String in shortened format. This is convenience method that invokes method
getStacktrace(Throwable, boolean, String) with second parameter set to 'true' and last parameter as null. It relies on
relevant package prefix to have been set by method setRelevantPackage(String). There are several ways to pre-invoke method setRelevantPackage(String):setRelevantPackage(String) which will appropriately initialize the package prefix and enable the use of this method
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass" value="com.mgnt.utils.TextUtils"/>
<property name="targetMethod" value="setRelevantPackage"/>
<property name="arguments" value="com.plain."/>
</bean>
e - Throwable from which stacktrace should be retrievedgetStacktrace(Throwable, boolean, String)public static java.lang.String getStacktrace(java.lang.Throwable e,
java.lang.String relevantPackage)
Throwable as a String in shortened format. This is convenience method that invokes method
getStacktrace(Throwable, boolean, String) with second parameter set to 'true'.e - Throwable from which stacktrace should be retrievedrelevantPackage - String that contains the prefix specifying which lines are relevant. It is recommended to be in the following format
"packag_name1.[package_name2.[...]]."getStacktrace(Throwable, boolean, String)public static java.lang.String getStacktrace(java.lang.CharSequence stacktrace,
java.lang.String relevantPackage)
CharSequence.
Since this method receives stacktrace as a CharSequence, it is assumed that it is always desirable to get
shortened format since the full stacktrace is already available as a CharSequence. To shorten the original
stacktrace this method processes the stacktrace exactly as described in method
getStacktrace(Throwable, boolean, String).stacktrace - CharSequence that holds full stacktrace textrelevantPackage - String that contains the prefix specifying which lines are relevant. It is
recommended to be in the following format "packag_name1.[package_name2.[...]]." (Again for full explanation
on how this parameter (and the entire method) works see the method getStacktrace(Throwable, boolean, String).getStacktrace(Throwable, boolean, String)public static java.lang.String getStacktrace(java.lang.CharSequence stacktrace)
CharSequence.
This is convenience method that works the same way as method
getStacktrace(CharSequence, String) with second parameter set to null. It relies on relevant
package prefix to have been set by method setRelevantPackage(String). There are several options to
pre-set this value. For detailed explanation of these options see method getStacktrace(Throwable, boolean).
Since this method receives stacktrace as a CharSequence, it is assumed that it is always desirable to get
shortened format since the full stacktrace is already available as a CharSequencestacktrace - CharSequence that holds full stacktrace textgetStacktrace(Throwable, boolean)public static java.lang.String getRelevantPackage()
public static void setRelevantPackage(java.lang.String relevantPackage)
getStacktrace(Throwable, boolean) Once the value has been set
the convenience method getStacktrace(Throwable, boolean) could be used instead of method
getStacktrace(Throwable, boolean, String)relevantPackage - String that contains the prefix specifying which lines are relevant. It is recommended to be in the following format
"packag_name1.[package_name2.[...]]."getStacktrace(Throwable, boolean, String)public static java.lang.String formatStringToPreserveIndentationForHtml(java.lang.String rawText)
This is non-indented line
This is 2 spaces indented line
This is 4 spaces indented line
This is non-indented line This is 2 spaces indented line This is 4 spaces indented line
This is non-indented line<br>
This is 2 spaces indented line<br>
This is 4 spaces indented line
This is non-indented line
This is 2 spaces indented line
This is 4 spaces indented line
This is non-indented line<br>
This is 2 spaces indented line<br>
This is 4 spaces indented line<br>
rawText - to be converted