Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2020 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package com.fizzgate.fizz.input;
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.noear.snack.ONode;
import com.fizzgate.constants.CommonConstants;
import com.fizzgate.fizz.StepContext;
import com.fizzgate.fizz.exception.FizzRuntimeException;
import com.fizzgate.fizz.function.FuncExecutor;
import com.fizzgate.fizz.function.IFunc;
import com.fizzgate.global_resource.GlobalResourceService;
import com.fizzgate.util.MapUtil;
/**
*
* @author Francis Dong
*
*/
public class PathMapping {
private static final String GLOBAL_RESOURCE_PREFIX = "g.";
private static List typeList = Arrays.asList("Integer", "int", "Boolean", "boolean", "Float", "float",
"Double", "double", "String", "string", "Long", "long", "Number", "number");
public static ONode toONode(Object obj) {
ONode o = null;
synchronized (obj) {
o = ONode.loadObj(obj);
}
return o;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void setByPath(ONode target, String path, Object obj, boolean supportMultiLevels) {
if (CommonConstants.WILDCARD_STAR.equals(path)) {
if (obj instanceof ONode) {
ONode node = (ONode) obj;
if(node.isObject()) {
target.setAll(node);
}
} else if (obj instanceof Map) {
target.setAll((Map) obj);
}
} else {
String[] keys = path.split("\\.");
if (!supportMultiLevels) {
keys = new String[] { path };
}
ONode cur = target;
for (int i = 0; i < keys.length - 1; i++) {
cur = cur.getOrNew(keys[i]);
}
if ((obj instanceof ONode && ((ONode) obj).isArray()) || obj instanceof Collection
|| (obj instanceof ONode && ((ONode) obj).isObject()) || obj instanceof Map) {
ONode subNode = cur.getOrNew(keys[keys.length - 1]);
if ((obj instanceof ONode && ((ONode) obj).isArray()) || obj instanceof Collection) {
if (subNode.isArray()) {
if (obj instanceof ONode) {
subNode.addAll((ONode) obj);
} else if (obj instanceof Collection) {
subNode.addAll((Collection) obj);
}
} else {
subNode.fill(obj);
}
} else {
if (subNode.isObject()) {
if (obj instanceof ONode) {
ONode node = (ONode) obj;
if (node.isObject()) {
subNode.setAll(node);
}
} else if (obj instanceof Map) {
subNode.setAll((Map) obj);
}
} else {
subNode.fill(obj);
}
}
} else {
cur.set(keys[keys.length - 1], obj);
}
}
}
public static Map transformToMap(ONode ctxNode, Map rules, boolean supportMultiLevels) {
ONode target = transform(ctxNode, rules, supportMultiLevels);
return target.toObject(Map.class);
}
@SuppressWarnings("unchecked")
public static ONode transform(ONode ctxNode, Map rules, boolean supportMultiLevels) {
ONode target = ONode.load(new HashMap());
if (rules.isEmpty()) {
return target;
}
Map rs = new HashMap<>();
Map types = new HashMap<>();
for (Entry entry : rules.entrySet()) {
if (entry.getValue() instanceof String) {
String val = (String) entry.getValue();
Optional optType = typeList.stream().filter(s -> val.startsWith(s + " ")).findFirst();
if (optType.isPresent()) {
rs.put(entry.getKey(), val.substring(optType.get().length() + 1));
types.put(entry.getKey(), optType.get());
} else {
rs.put(entry.getKey(), val);
}
} else if (entry.getValue() instanceof List) {
List