com.arextest.diff.handler.parse.StringAndCompressParse Maven / Gradle / Ivy
The newest version!
package com.arextest.diff.handler.parse;
import com.arextest.diff.model.TransformConfig.TransformMethod;
import com.arextest.diff.model.log.NodeEntity;
import com.arextest.diff.utils.TransformUtil;
import com.arextest.diff.utils.JacksonHelperUtil;
import com.arextest.diff.utils.ListUti;
import com.arextest.diff.utils.StringUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.NullNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import org.apache.commons.lang3.tuple.MutablePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class StringAndCompressParse {
private static final Logger LOGGER = LoggerFactory.getLogger(StringAndCompressParse.class);
private List currentNode = new ArrayList<>();
private Map, String> original = new HashMap<>();
private boolean nameToLower;
private String pluginJarUrl;
private Map, List> transFormConfigMap;
public Map, String> getOriginal() {
return original;
}
public void setNameToLower(boolean nameToLower) {
this.nameToLower = nameToLower;
}
public void setTransFormConfigMap(
Map, List> transFormConfigMap) {
this.transFormConfigMap = transFormConfigMap;
}
public void setPluginJarUrl(String pluginJarUrl) {
this.pluginJarUrl = pluginJarUrl;
}
public void getJSONParse(Object obj, Object preObj) {
if (obj == null || obj instanceof NullNode) {
return;
}
if (obj instanceof ObjectNode) {
ObjectNode jsonObject = (ObjectNode) obj;
List names = JacksonHelperUtil.getNames(jsonObject);
for (String fieldName : names) {
currentNode.add(new NodeEntity(fieldName, 0));
Object objFieldValue = jsonObject.get(fieldName);
getJSONParse(objFieldValue, obj);
ListUti.removeLast(currentNode);
}
} else if (obj instanceof ArrayNode) {
ArrayNode objArray = (ArrayNode) obj;
for (int i = 0; i < objArray.size(); i++) {
currentNode.add(new NodeEntity(null, i));
Object element = objArray.get(i);
getJSONParse(element, obj);
ListUti.removeLast(currentNode);
}
} else {
String value = ((JsonNode) obj).asText();
// TODO: 2022/9/20 improve the method to speed up
List nodePath = nameToLower ? ListUti.convertToStringList(currentNode).stream()
.map(String::toLowerCase).collect(Collectors.toList())
: ListUti.convertToStringList(currentNode);
MutablePair objectBooleanPair = null;
if (transFormConfigMap != null && transFormConfigMap.containsKey(nodePath)) {
List transformMethodList = this.transFormConfigMap.get(nodePath);
objectBooleanPair = processCompress(value, this.pluginJarUrl, transformMethodList, preObj);
} else {
objectBooleanPair = processStringParse(value, preObj);
}
if (objectBooleanPair.getKey() == null) {
return;
}
if (Objects.equals(objectBooleanPair.getValue(), Boolean.TRUE)) {
getJSONParse(objectBooleanPair.getKey(), preObj);
}
String currentName = getCurrentName(currentNode);
if (preObj instanceof ObjectNode) {
((ObjectNode) preObj).set(currentName, objectBooleanPair.getKey());
original.put(new ArrayList<>(currentNode), value);
} else if (preObj instanceof ArrayNode) {
((ArrayNode) preObj).set(Integer.parseInt(currentName), objectBooleanPair.getKey());
original.put(new ArrayList<>(currentNode), value);
}
}
}
private String getCurrentName(List currentNode) {
NodeEntity nodeEntity = currentNode.get(currentNode.size() - 1);
if (nodeEntity.getNodeName() != null) {
return nodeEntity.getNodeName();
} else {
return String.valueOf(nodeEntity.getIndex());
}
}
/**
* @param value
* @param pluginJarUrl
* @param transformMethodList
* @param preObj
* @return Pair