[Fix] StringHelper.replacePropertiesIn() allow empty string (non-null)

This commit is contained in:
Robert von Burg 2017-11-21 18:03:25 +01:00
parent 3e116b6b34
commit 332724439d
1 changed files with 139 additions and 120 deletions

View File

@ -52,9 +52,22 @@ public class StringHelper {
/**
* Hex char table for fast calculating of hex values
*/
private static final byte[] HEX_CHAR_TABLE = { (byte) '0', (byte) '1', (byte) '2', (byte) '3', (byte) '4',
(byte) '5', (byte) '6', (byte) '7', (byte) '8', (byte) '9', (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd',
(byte) 'e', (byte) 'f' };
private static final byte[] HEX_CHAR_TABLE = { (byte) '0',
(byte) '1',
(byte) '2',
(byte) '3',
(byte) '4',
(byte) '5',
(byte) '6',
(byte) '7',
(byte) '8',
(byte) '9',
(byte) 'a',
(byte) 'b',
(byte) 'c',
(byte) 'd',
(byte) 'e',
(byte) 'f' };
public static String toHexString(byte data) {
return String.format("%02x", data);
@ -80,7 +93,8 @@ public class StringHelper {
return new String(hex, "ASCII"); //$NON-NLS-1$
} catch (UnsupportedEncodingException e) {
String msg = MessageFormat.format("Something went wrong while converting to HEX: {0}", e.getMessage()); //$NON-NLS-1$
String msg = MessageFormat
.format("Something went wrong while converting to HEX: {0}", e.getMessage()); //$NON-NLS-1$
throw new RuntimeException(msg, e);
}
}
@ -99,12 +113,12 @@ public class StringHelper {
* Converts each byte of the given byte array to a HEX value and returns the concatenation of these values
*
* @param raw
* the bytes to convert to String using numbers in hexadecimal
* the bytes to convert to String using numbers in hexadecimal
*
* @return the encoded string
*
* @throws RuntimeException
* if {@link UnsupportedEncodingException} is thrown
* if {@link UnsupportedEncodingException} is thrown
*/
public static String toHexString(byte[] raw) throws RuntimeException {
return toHexString(raw, 0, raw.length);
@ -114,12 +128,12 @@ public class StringHelper {
* Converts each byte of the given byte array to a HEX value and returns the concatenation of these values
*
* @param raw
* the bytes to convert to String using numbers in hexadecimal
* the bytes to convert to String using numbers in hexadecimal
*
* @return the encoded string
*
* @throws RuntimeException
* if {@link UnsupportedEncodingException} is thrown
* if {@link UnsupportedEncodingException} is thrown
*/
public static String toHexString(byte[] raw, int offset, int length) throws RuntimeException {
try {
@ -138,7 +152,8 @@ public class StringHelper {
return new String(hex, "ASCII"); //$NON-NLS-1$
} catch (UnsupportedEncodingException e) {
String msg = MessageFormat.format("Something went wrong while converting to HEX: {0}", e.getMessage()); //$NON-NLS-1$
String msg = MessageFormat
.format("Something went wrong while converting to HEX: {0}", e.getMessage()); //$NON-NLS-1$
throw new RuntimeException(msg, e);
}
}
@ -154,7 +169,7 @@ public class StringHelper {
* Returns a byte array of a given string by converting each character of the string to a number base 16
*
* @param encoded
* the string to convert to a byt string
* the string to convert to a byt string
*
* @return the encoded byte stream
*/
@ -177,7 +192,7 @@ public class StringHelper {
* Generates the MD5 Hash of a string and converts it to a HEX string
*
* @param string
* the string to hash
* the string to hash
*
* @return the hash or null, if an exception was thrown
*/
@ -190,7 +205,7 @@ public class StringHelper {
* Hex String which is printable
*
* @param string
* the string to hash
* the string to hash
*
* @return the hash or null, if an exception was thrown
*/
@ -203,7 +218,7 @@ public class StringHelper {
* a Hex String which is printable
*
* @param bytes
* the bytes to hash
* the bytes to hash
*
* @return the hash or null, if an exception was thrown
*/
@ -215,7 +230,7 @@ public class StringHelper {
* Generates the SHA1 Hash of a string and converts it to a HEX String
*
* @param string
* the string to hash
* the string to hash
*
* @return the hash or null, if an exception was thrown
*/
@ -228,7 +243,7 @@ public class StringHelper {
* Hex String which is printable
*
* @param string
* the string to hash
* the string to hash
*
* @return the hash or null, if an exception was thrown
*/
@ -241,7 +256,7 @@ public class StringHelper {
* a Hex String which is printable
*
* @param bytes
* the bytes to hash
* the bytes to hash
*
* @return the hash or null, if an exception was thrown
*/
@ -253,7 +268,7 @@ public class StringHelper {
* Generates the SHA-256 Hash of a string and converts it to a HEX String
*
* @param string
* the string to hash
* the string to hash
*
* @return the hash or null, if an exception was thrown
*/
@ -266,7 +281,7 @@ public class StringHelper {
* a Hex String which is printable
*
* @param string
* the string to hash
* the string to hash
*
* @return the hash or null, if an exception was thrown
*/
@ -279,7 +294,7 @@ public class StringHelper {
* a Hex String which is printable
*
* @param bytes
* the bytes to hash
* the bytes to hash
*
* @return the hash or null, if an exception was thrown
*/
@ -291,9 +306,9 @@ public class StringHelper {
* Returns the hash of an algorithm
*
* @param algorithm
* the algorithm to use
* the algorithm to use
* @param string
* the string to hash
* the string to hash
*
* @return the hash or null, if an exception was thrown
*/
@ -305,9 +320,9 @@ public class StringHelper {
* Returns the hash of an algorithm
*
* @param algorithm
* the algorithm to use
* the algorithm to use
* @param string
* the string to hash
* the string to hash
*
* @return the hash or null, if an exception was thrown
*/
@ -329,9 +344,9 @@ public class StringHelper {
* Returns the hash of an algorithm
*
* @param algorithm
* the algorithm to use
* the algorithm to use
* @param bytes
* the bytes to hash
* the bytes to hash
*
* @return the hash or null, if an exception was thrown
*/
@ -343,9 +358,9 @@ public class StringHelper {
* Returns the hash of an algorithm
*
* @param algorithm
* the algorithm to use
* the algorithm to use
* @param bytes
* the bytes to hash
* the bytes to hash
*
* @return the hash or null, if an exception was thrown
*/
@ -368,13 +383,14 @@ public class StringHelper {
* options set: adding the char at the beginning or appending it at the end
*
* @param value
* string to normalize
* string to normalize
* @param length
* length string must have
* length string must have
* @param beginning
* add at beginning of value
* add at beginning of value
* @param c
* char to append when appending
* char to append when appending
*
* @return the new string
*/
public static String normalizeLength(String value, int length, boolean beginning, char c) {
@ -386,15 +402,16 @@ public class StringHelper {
* depending on the options set: appending the char at the beginning or the end
*
* @param value
* string to normalize
* string to normalize
* @param length
* length string must have
* length string must have
* @param beginning
* append at beginning of value
* append at beginning of value
* @param shorten
* allow shortening of value
* allow shortening of value
* @param c
* char to append when appending
* char to append when appending
*
* @return the new string
*/
public static String normalizeLength(String value, int length, boolean beginning, boolean shorten, char c) {
@ -430,7 +447,7 @@ public class StringHelper {
* Calls {@link #replacePropertiesIn(Properties, String)}, with {@link System#getProperties()} as input
*
* @return a new string with all defined system properties replaced or if an error occurred the original value is
* returned
* returned
*/
public static String replaceSystemPropertiesIn(String value) {
return replacePropertiesIn(System.getProperties(), value);
@ -442,9 +459,9 @@ public class StringHelper {
* sequence is not in the properties, then the sequence is not replaced
*
* @param properties
* the {@link Properties} in which to get the value
* the {@link Properties} in which to get the value
* @param value
* the value in which to replace any system properties
* the value in which to replace any system properties
*
* @return a new string with all defined properties replaced or if an error occurred the original value is returned
*/
@ -458,11 +475,11 @@ public class StringHelper {
* value of the sequence is not in the properties, then the sequence is not replaced
*
* @param properties
* the {@link Properties} in which to get the value
* the {@link Properties} in which to get the value
* @param prefix
* the prefix to use, for instance use <code>$</code> to replace occurrences of <code>$</code>{...}
* the prefix to use, for instance use <code>$</code> to replace occurrences of <code>$</code>{...}
* @param value
* the value in which to replace any system properties
* the value in which to replace any system properties
*
* @return a new string with all defined properties replaced or if an error occurred the original value is returned
*/
@ -486,7 +503,8 @@ public class StringHelper {
// if no stop found, then break as another sequence should be able to start
if (stop == -1) {
logger.error(MessageFormat.format("Sequence starts at offset {0} but does not end!", pos)); //$NON-NLS-1$
logger.error(
MessageFormat.format("Sequence starts at offset {0} but does not end!", pos)); //$NON-NLS-1$
tmpValue = value;
break;
}
@ -505,10 +523,10 @@ public class StringHelper {
}
// sequence is good, so see if we have a property for it
String property = properties.getProperty(sequence, StringHelper.EMPTY);
String property = properties.getProperty(sequence);
// if no property exists, then log and continue
if (property.isEmpty()) {
if (property == null) {
pos = stop;
// logger.warn("No system property found for sequence " + sequence);
continue;
@ -526,7 +544,7 @@ public class StringHelper {
* replacing all properties with itself
*
* @param properties
* the properties in which the values must have any ${...} replaced by values of the respective key
* the properties in which the values must have any ${...} replaced by values of the respective key
*/
public static void replaceProperties(Properties properties) {
replaceProperties(properties, null);
@ -537,9 +555,9 @@ public class StringHelper {
* {@link Properties} value using {@link StringHelper#replacePropertiesIn(Properties, String)}
*
* @param properties
* the properties in which the values must have any ${...} replaced by values of the respective key
* the properties in which the values must have any ${...} replaced by values of the respective key
* @param altProperties
* if properties does not contain the ${...} key, then try these alternative properties
* if properties does not contain the ${...} key, then try these alternative properties
*/
public static void replaceProperties(Properties properties, Properties altProperties) {
@ -571,9 +589,9 @@ public class StringHelper {
* difference occurs, and the second and third lines contain contexts
*
* @param s1
* the first string
* the first string
* @param s2
* the second string
* the second string
*
* @return the string from which the strings differ with a length of 40 characters within the original strings
*/
@ -612,7 +630,7 @@ public class StringHelper {
* Formats the given number of milliseconds to a time like #h/m/s/ms/us/ns
*
* @param millis
* the number of milliseconds
* the number of milliseconds
*
* @return format the given number of milliseconds to a time like #h/m/s/ms/us/ns
*/
@ -624,7 +642,7 @@ public class StringHelper {
* Formats the given number of nanoseconds to a time like #h/m/s/ms/us/ns
*
* @param nanos
* the number of nanoseconds
* the number of nanoseconds
*
* @return format the given number of nanoseconds to a time like #h/m/s/ms/us/ns
*/
@ -669,7 +687,7 @@ public class StringHelper {
* Simply returns true if the value is null, or empty
*
* @param value
* the value to check
* the value to check
*
* @return true if the value is null, or empty
*/
@ -681,7 +699,7 @@ public class StringHelper {
* Simply returns true if the value is neither null nor empty
*
* @param value
* the value to check
* the value to check
*
* @return true if the value is neither null nor empty
*/
@ -700,16 +718,17 @@ public class StringHelper {
* </p>
*
* @param value
* the value to check
* the value to check
*
* @return true or false, depending on the string value
*
* @throws RuntimeException
* if the value is empty, or not equal to the case insensitive value "true" or "false"
* if the value is empty, or not equal to the case insensitive value "true" or "false"
*/
public static boolean parseBoolean(String value) throws RuntimeException {
if (isEmpty(value))
throw new RuntimeException("Value to parse to boolean is empty! Expected case insensitive true or false"); //$NON-NLS-1$
throw new RuntimeException(
"Value to parse to boolean is empty! Expected case insensitive true or false"); //$NON-NLS-1$
String tmp = value.toLowerCase();
if (tmp.equals(Boolean.TRUE.toString())) {
return true;
@ -744,7 +763,7 @@ public class StringHelper {
* If the value parameter is empty, then a {@link #DASH} is returned, otherwise the value is returned
*
* @param value
* the value
* the value
*
* @return the non-empty value, or a {@link #DASH}
*/