1.改进版,以dto对象来封装
private static void addPath(ProjectTreeDTO root, String path, List<ProjectTablesDTO> tables) {
StringBuilder url = new StringBuilder();
String[] pathArr = path.split("/");
for (String label : pathArr) {
url.append("/").append(label);
boolean flag = true;
List<ProjectTreeDTO> children = root.getChildren();
for (ProjectTreeDTO node : children) {
if (node.getLabel().equals(label)) {
root = node;
flag = false;
break;
}
}
if (flag) {
ProjectTreeDTO newNode = new ProjectTreeDTO(label,null, BdpConstant.DIRECTORY,null,BdpConstant.IMPORT_TABLE,root.getBackupId());
List<ProjectTreeDTO> children2 = new ArrayList<>();
String oldPath = label.substring(1);
List<ProjectTablesDTO> levelTable = tables.stream().filter(u -> u.getImportPath().equals(oldPath)).collect(Collectors.toList());
ProjectTreeDTO tableTree;
for (ProjectTablesDTO table : levelTable) {
tableTree = new ProjectTreeDTO(table.getTableName(), table.getTid(), BdpConstant.FILE, table.getTableType(), null);
children2.add(tableTree);
}
newNode.setChildren(children2);
children.add(newNode);
root = newNode;
}
}
}
public static void createDirTree(ProjectTreeDTO importTableTree,List<ProjectTablesDTO> tables) {
List<String> paths = tables.stream().map(ProjectTablesDTO::getImportPath).collect(Collectors.toList());
List<ProjectTreeDTO> chirldren = new ArrayList<>();
importTableTree.setChildren(chirldren);
for (String path : paths) {
addPath(importTableTree, path,tables);
}
}
public static void main(String[] args) throws Exception {
ProjectTreeDTO parent = new ProjectTreeDTO("导入表",null, BdpConstant.DIRECTORY,null,BdpConstant.IMPORT_TABLE,666L);
List<ProjectTablesDTO> tables = new ArrayList<>();
//todo list.add(),list.add(),list.add()
createDirTree(parent,tables);
System.out.println(parent);
}
2.旧版,以map为来封装数据
public class PathToTreeUtil2 {
private static String getType(String filename, String path) {
if (path.endsWith(filename)) {
return "file";
}
return "folder";
}
private static void addPath(Map<String, Object> root, String path) {
String url = "";
String[] pathArr = path.split("/");
for (String name : pathArr) {
url += "/" + name;
boolean flag = true;
for (Map<String, Object> node : (List<Map<String, Object>>) root.get("content")) {
if (node.get("name").equals(name)) {
root = node;
flag = false;
break;
}
}
if (flag) {
Map<String, Object> newNode = new HashMap<>();
newNode.put("name", name);
newNode.put("type", getType(name, path));
newNode.put("url", url);
newNode.put("content", new ArrayList<HashMap<String, Object>>());
((List<Map<String, Object>>) root.get("content")).add(newNode);
root = newNode;
}
}
}
private static HashMap<String, Object> generate_data(String s) {
String[] paths = s.split(",");
HashMap<String, Object> root = new HashMap<>();
root.put("name", "");
root.put("url", "");
root.put("type", "");
ArrayList<String> arrayList = new ArrayList<>();
root.put("content", arrayList);
for (String path : paths) {
addPath(root, path);
}
return root;
}
/****
* 1. 主分析程序,拿到路径总的字符串,进行分析
* @param path
* @return
*/
public static JSONObject pathToTree(String path) {
Map<String, Object> root = new HashMap<>();
root = generate_data(path);
JSONObject jsonObject = new JSONObject();
jsonObject.put("data", root);
return jsonObject;
}
public static void main(String[] args) throws Exception {
String str = "luo_t/ddr/liu,luo_t/luo/bb,cca/as21/df4";
System.out.println(pathToTree(str).toString());
}
}