From 4e9d03fce4d5b40480b6a4d56dff25f15a2eacb5 Mon Sep 17 00:00:00 2001 From: nick Date: Wed, 17 Apr 2024 13:09:54 +0300 Subject: [PATCH] tbel: encodeURI() decodeURI() comments 2 --- .../thingsboard/script/api/tbel/TbUtils.java | 44 +++++++++---------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/TbUtils.java b/common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/TbUtils.java index 209df886b5..283044b279 100644 --- a/common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/TbUtils.java +++ b/common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/TbUtils.java @@ -43,7 +43,6 @@ import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.Optional; import java.util.Set; import java.util.regex.Matcher; @@ -612,22 +611,6 @@ public class TbUtils { return BigDecimal.valueOf(value).setScale(precision, RoundingMode.HALF_UP).floatValue(); } - private static boolean isHexadecimal(String value) { - return value != null && (value.contains("0x") || value.contains("0X")); - } - - private static String prepareNumberString(String value) { - if (value != null) { - value = value.trim(); - if (isHexadecimal(value)) { - value = value.replace("0x", ""); - value = value.replace("0X", ""); - } - value = value.replace(",", "."); - } - return value; - } - public static ExecutionHashMap toFlatMap(ExecutionContext ctx, Map json) { return toFlatMap(ctx, json, new ArrayList<>(), true); } @@ -646,23 +629,22 @@ public class TbUtils { return map; } + public static String encodeURI(String uri) { String encoded = URLEncoder.encode(uri, StandardCharsets.UTF_8); - Optional encodedMdnOpt = Optional.of(encoded); for (var entry : mdnEncodingReplacements.entrySet()) { - encodedMdnOpt = Optional.of(encodedMdnOpt.get().replaceAll(entry.getKey(), entry.getValue())); + encoded = encoded.replaceAll(entry.getKey(), entry.getValue()); } - return encodedMdnOpt.orElse(null); + return encoded; } public static String decodeURI(String uri) { ArrayList allKeys = new ArrayList<>(mdnEncodingReplacements.keySet()); Collections.reverse(allKeys); - Optional encodedMdnOpt = Optional.of(uri); for (String strKey : allKeys) { - encodedMdnOpt = Optional.of(encodedMdnOpt.get().replaceAll(mdnEncodingReplacements.get(strKey), strKey)); + uri = uri.replaceAll(mdnEncodingReplacements.get(strKey), strKey); } - return encodedMdnOpt.map(s -> URLDecoder.decode(s, StandardCharsets.UTF_8)).orElse(null); + return URLDecoder.decode(uri, StandardCharsets.UTF_8); } private static void parseRecursive(Object json, Map map, List excludeList, String path, boolean pathInKey) { @@ -705,6 +687,22 @@ public class TbUtils { } } + private static boolean isHexadecimal(String value) { + return value != null && (value.contains("0x") || value.contains("0X")); + } + + private static String prepareNumberString(String value) { + if (value != null) { + value = value.trim(); + if (isHexadecimal(value)) { + value = value.replace("0x", ""); + value = value.replace("0X", ""); + } + value = value.replace(",", "."); + } + return value; + } + private static boolean isValidRadix(String value, int radix) { for (int i = 0; i < value.length(); i++) { if (i == 0 && value.charAt(i) == '-') {