All Downloads are FREE. Search and download functionalities are using the official Maven repository.

converter.node.CompressedNode Maven / Gradle / Ivy

There is a newer version: 1.0.1
Show newest version
package converter.node;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.LinkedList;
import java.util.List;

/*
 * 压缩树节点
 * @author liuqiangm
 */
public class CompressedNode {

  private String sourcePath;

  private String targetPath;

  private Object value;

  private List subNodeList = new LinkedList<>();

  public CompressedNode() {
  }

  public JSONObject getTargetJSONObject() {
    return (JSONObject) getTargetJSON("", null);
  }

  public JSONArray getTargetJSONArray() {
    return (JSONArray) getTargetJSON("", null);
  }

  private JSON getTargetJSON(String parentPath, JSON parentNode) {
    if(parentNode == null) {
      parentNode = new JSONObject();
    }
    JSON treeNode = parentNode;
    String currPath = targetPath.substring(parentPath.length());
    if (".".equals(currPath)) { // 对象数组中的对象
      treeNode = new JSONObject();
      for(CompressedNode subCompressedNode : this.subNodeList) {
        subCompressedNode.getTargetJSON(this.getTargetPath(), treeNode);
      }
      ((JSONArray) parentNode).add(treeNode);
    }
    else if("]".equals(currPath)) { // 常量数组中的常量
      ((JSONArray) parentNode).add(this.getValue());
    }
    else {
      String[] splits = currPath.split("\\.");
      JSON preNode = treeNode;
      JSON curNode = null;
      for(int i = 0; i < splits.length; i++) {
        String split = splits[i];
        if(!"".equals(split) && !"[".equals(split)) {

          if(this.value != null) {
            ((JSONObject) preNode).put(split, this.value);
            continue;
          }
          else {
            if(i != splits.length - 1 && "[".equals(splits[i + 1])) {
              curNode = new JSONArray();
            }
            else if(!this.subNodeList.isEmpty()){
              curNode = new JSONObject();
            }
            else {
              curNode = null;
            }
            ((JSONObject) preNode).put(split, curNode);
            preNode = curNode;
          }
        }
      }
      for (CompressedNode subCompressedNode : this.subNodeList) {
        subCompressedNode.getTargetJSON(this.getTargetPath(), preNode);
      }
    }
    return treeNode;
  }

  public String getSourcePath() {
    return sourcePath;
  }

  public void setSourcePath(String sourcePath) {
    this.sourcePath = sourcePath;
  }

  public String getTargetPath() {
    return targetPath;
  }

  public void setTargetPath(String targetPath) {
    this.targetPath = targetPath;
  }

  public Object getValue() {
    return value;
  }

  public void setValue(Object value) {
    this.value = value;
  }

  public List getSubNodeList() {
    return subNodeList;
  }

  public void setSubNodeList(List subNodeList) {
    this.subNodeList = subNodeList;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy