|
|
|
@ -18,7 +18,9 @@ package org.thingsboard.script.api.tbel; |
|
|
|
import org.mvel2.ExecutionContext; |
|
|
|
import org.mvel2.ParserConfiguration; |
|
|
|
import org.mvel2.execution.ExecutionArrayList; |
|
|
|
import org.mvel2.execution.ExecutionHashMap; |
|
|
|
import org.mvel2.util.MethodStub; |
|
|
|
import org.thingsboard.server.common.data.StringUtils; |
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.io.UnsupportedEncodingException; |
|
|
|
@ -27,8 +29,13 @@ import java.math.RoundingMode; |
|
|
|
import java.nio.ByteBuffer; |
|
|
|
import java.nio.ByteOrder; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Base64; |
|
|
|
import java.util.Collection; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Set; |
|
|
|
|
|
|
|
public class TbUtils { |
|
|
|
|
|
|
|
@ -89,6 +96,14 @@ public class TbUtils { |
|
|
|
byte[].class))); |
|
|
|
parserConfig.addImport("bytesToHex", new MethodStub(TbUtils.class.getMethod("bytesToHex", |
|
|
|
ExecutionArrayList.class))); |
|
|
|
parserConfig.addImport("toFlatMap", new MethodStub(TbUtils.class.getMethod("toFlatMap", |
|
|
|
Object.class, HashMap.class))); |
|
|
|
parserConfig.addImport("toFlatMap", new MethodStub(TbUtils.class.getMethod("toFlatMap", |
|
|
|
Object.class, HashMap.class, boolean.class))); |
|
|
|
parserConfig.addImport("toFlatMap", new MethodStub(TbUtils.class.getMethod("toFlatMap", |
|
|
|
Object.class, HashMap.class, List.class))); |
|
|
|
parserConfig.addImport("toFlatMap", new MethodStub(TbUtils.class.getMethod("toFlatMap", |
|
|
|
Object.class, HashMap.class, List.class, boolean.class))); |
|
|
|
} |
|
|
|
|
|
|
|
public static String btoa(String input) { |
|
|
|
@ -316,4 +331,53 @@ public class TbUtils { |
|
|
|
} |
|
|
|
return value; |
|
|
|
} |
|
|
|
|
|
|
|
public static void toFlatMap(Object json, HashMap<String, Object> map) { |
|
|
|
toFlatMap(json, map, new ArrayList<>(), true); |
|
|
|
} |
|
|
|
|
|
|
|
public static void toFlatMap(Object json, HashMap<String, Object> map, boolean pathInKey) { |
|
|
|
toFlatMap(json, map, new ArrayList<>(), pathInKey); |
|
|
|
} |
|
|
|
|
|
|
|
public static void toFlatMap(Object json, HashMap<String, Object> map, List<String> excludeList) { |
|
|
|
toFlatMap(json, map, excludeList, true); |
|
|
|
} |
|
|
|
|
|
|
|
public static void toFlatMap(Object json, HashMap<String, Object> map, List<String> excludeList, boolean pathInKey) { |
|
|
|
parseRecursive(json, map, excludeList, "", pathInKey); |
|
|
|
} |
|
|
|
|
|
|
|
private static void parseRecursive(Object json, HashMap<String, Object> map, List<String> excludeList, String path, boolean pathInKey) { |
|
|
|
if (json instanceof Map.Entry) { |
|
|
|
Map.Entry<?, ?> entry = (Map.Entry<?, ?>) json; |
|
|
|
if (StringUtils.isNotBlank(path)) { |
|
|
|
path += "."; |
|
|
|
} |
|
|
|
if (excludeList.contains(entry.getKey())) { |
|
|
|
return; |
|
|
|
} |
|
|
|
path += entry.getKey(); |
|
|
|
json = entry.getValue(); |
|
|
|
} |
|
|
|
if (json instanceof Set || json instanceof List) { |
|
|
|
String arrayPath = path + "."; |
|
|
|
Object[] collection = ((Collection<?>) json).toArray(); |
|
|
|
for (int index = 0; index < collection.length; index++) { |
|
|
|
parseRecursive(collection[index], map, excludeList, arrayPath + index, pathInKey); |
|
|
|
} |
|
|
|
} else if (json instanceof Map) { |
|
|
|
Map<?, ?> node = (Map<?, ?>) json; |
|
|
|
for (Map.Entry<?, ?> entry : node.entrySet()) { |
|
|
|
parseRecursive(entry, map, excludeList, path, pathInKey); |
|
|
|
} |
|
|
|
} else { |
|
|
|
if (pathInKey) { |
|
|
|
map.put(path, json); |
|
|
|
} else { |
|
|
|
String key = path.contains(".") ? path.substring(path.lastIndexOf('.') + 1) : path; |
|
|
|
map.put(key, json); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|