Skip to content

Commit

Permalink
Address warnings in StringUtils class (openhab#3845)
Browse files Browse the repository at this point in the history
Signed-off-by: Kai Kreuzer <[email protected]>
  • Loading branch information
kaikreuzer authored Oct 15, 2023
1 parent 7281dca commit 14f8490
Showing 1 changed file with 21 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
*
* @author Leo Siepel - Initial contribution
*/

@NonNullByDefault
public class StringUtils {

Expand Down Expand Up @@ -65,7 +64,7 @@ public class StringUtils {
/**
* Simple method to escape XML special characters in String.
* There are five XML special characters which needs to be escaped:
*
*
* <pre>
* & - &amp;
* < - &lt;
Expand All @@ -80,14 +79,10 @@ public class StringUtils {
public static @Nullable String escapeXml(@Nullable String str) {
if (str == null || str.isEmpty()) {
return str;
} else {
return str.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;").replace("\"", "&quot;")
.replace("'", "&apos;");
}

str = str.replace("&", "&amp;");
str = str.replace("<", "&lt;");
str = str.replace(">", "&gt;");
str = str.replace("\"", "&quot;");
str = str.replace("'", "&apos;");
return str;
}

/**
Expand Down Expand Up @@ -122,7 +117,7 @@ public class StringUtils {
* "openHAB_is_cool" => "Openhab_Is_Cool"
* "foobar_Example" => "Foobar_Example"
* </pre>
*
*
* @param str the String to capitalize, may be null
* @return the capitalized String, may be null
*/
Expand Down Expand Up @@ -155,7 +150,7 @@ public class StringUtils {
* "openHAB is cool" => "OpenHAB Is Cool"
* "foobar Example" => "Foobar Example"
* </pre>
*
*
* @param str the String to capitalize, may be null
* @return the capitalized String, may be null
*/
Expand All @@ -180,29 +175,26 @@ public class StringUtils {

/**
* Pads the string from the left
*
*
* <pre>
* padLeft("9", 4, "0") => "0009"
* padLeft("3112", 12, "*") => "********3112"
* padLeft("openHAB", 4, "*") => "openHAB"
* </pre>
*
*
* @param str the String to pad, may be null
* @param minSize the minimum String size to return
* @param padString the String to add when padding
* @return the padded String
*/
public static String padLeft(@Nullable String str, int minSize, String padString) {
if (str == null) {
str = "";
}

return String.format("%" + minSize + "s", str).replace(" ", padString);
String paddedString = str == null ? "" : str;
return String.format("%" + minSize + "s", paddedString).replace(" ", padString);
}

/**
* Creates a random string
*
*
* @param length the length of the String to return
* @param charset the characters to use to create the String
* @return the random String
Expand All @@ -220,7 +212,7 @@ public static String getRandomString(int length, String charset) {

/**
* Creates a random string with [A-Zaz] characters
*
*
* @param length the length of the String to return
* @return the random String
*/
Expand All @@ -230,7 +222,7 @@ public static String getRandomAlphabetic(int length) {

/**
* Creates a random string with only hex characters
*
*
* @param length the length of the String to return
* @return the random String
*/
Expand All @@ -240,7 +232,7 @@ public static String getRandomHex(int length) {

/**
* Creates a random string with [A-Za-z0-9] characters
*
*
* @param length the length of the String to return
* @return the random String
*/
Expand All @@ -250,7 +242,7 @@ public static String getRandomAlphanumeric(int length) {

/**
* Splits the string by character type into an array
*
*
* <pre>
* "ab de fg" => "ab", " ", "de", " ", "fg";
* "ab de fg" => "ab", " ", "de", " ", "fg";
Expand All @@ -259,7 +251,7 @@ public static String getRandomAlphanumeric(int length) {
* "fooBar" => "foo", "Bar"
* "OHRules" => "OH", "Rules"
* </pre>
*
*
* @param str the input String to split, may be null
* @return the splitted String
*/
Expand Down Expand Up @@ -296,27 +288,24 @@ public static String[] splitByCharacterType(@Nullable String str) {
/**
* Simple method to un escape XML special characters in String.
* There are five XML Special characters which needs to be escaped:
*
*
* <pre>
* & => &amp;
* < => &lt;
* > => &gt;
* " => &quot;
* ' => &apos;
* </pre>
*
*
* @param input the input xml as String to unescape, may be null
* @return the unescaped xml as String, may be null
*/
public static @Nullable String unEscapeXml(@Nullable String str) {
if (str == null || str.isEmpty()) {
return str;
} else {
return str.replace("&amp;", "&").replace("&lt;", "<").replace("&gt;", ">").replace("&quot;", "\"")
.replace("&apos;", "'");
}
str = str.replace("&amp;", "&");
str = str.replace("&lt;", "<");
str = str.replace("&gt;", ">");
str = str.replace("&quot;", "\"");
str = str.replace("&apos;", "'");
return str;
}
}

0 comments on commit 14f8490

Please sign in to comment.