1/* 2* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd. 3* Licensed under the Apache License, Version 2.0 (the "License"); 4* you may not use this file except in compliance with the License. 5* You may obtain a copy of the License at 6* 7* http:// www.apache.org/licenses/LICENSE-2.0 8* 9* Unless required by applicable law or agreed to in writing, software 10* distributed under the License is distributed on an "AS IS" BASIS, 11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12* See the License for the specific language governing permissions and 13* limitations under the License. 14*/ 15 16const { NapiLog } = require("./NapiLog"); 17const { NodeTools, DataType, NodeType } = require("./NodeTools"); 18const re = require("./re") 19 20class ModifyNode { 21} 22 23function getParent(node, dest, parent) { 24 return dest.parent_; 25} 26 27function isNameRepeat(node, name) { 28 for (let i in node.value_) { 29 if (node.value_[i].name_ == name) return true; 30 } 31 return false; 32} 33 34ModifyNode.modifyName = function (files, root, node, name) { 35 let parent = getParent(root, node, null); 36 if (parent == null) { 37 NapiLog.logError("不能改变root节点名称"); 38 return false; 39 } 40 node.name_ = name; 41 return true; 42} 43 44ModifyNode.modifyNodeType = function (files, root, node, type) {//节点类型改变 45 let parent = getParent(root, node, null); 46 if (parent == null) { 47 NapiLog.logError("不能改变root节点类型"); 48 return false; 49 } 50 switch (type) { 51 case NodeType.COPY: 52 case NodeType.INHERIT: 53 case NodeType.REFERENCE: 54 node.ref_ = "unknow"; 55 break; 56 case NodeType.DELETE: 57 break; 58 case NodeType.TEMPLETE: 59 break; 60 } 61 node.nodeType_ = type; 62 return true; 63} 64 65ModifyNode.addChildAttr = function (root, node) { 66 //添加子属性,默认为int8 67 for (let i = 1; i < 256; i++) { 68 let name = "attr_" + i; 69 if (!isNameRepeat(node, name)) { 70 let newAttr = NodeTools.createNewNode(DataType.ATTR, name, NodeTools.createNewNode(DataType.INT8, "", 0)) 71 node.value_.push(newAttr); 72 newAttr.parent_ = node; 73 newAttr.value_.parent_ = newAttr; 74 break; 75 } 76 } 77} 78 79ModifyNode.addChildNode = function (root, node) { 80 //添加子节点,默认为数据类节点(不继承) 81 for (let i = 1; i < 256; i++) { 82 let name = "node_" + i; 83 if (!isNameRepeat(node, name)) { 84 let newNode = NodeTools.createNewNode(DataType.NODE, name, [], NodeType.DATA); 85 node.value_.push(newNode); 86 newNode.parent_ = node; 87 break; 88 } 89 } 90} 91 92ModifyNode.deleteNode = function (node) { 93 //删除子节点 94 let parent = node.parent_; 95 if (parent == null) { 96 NapiLog.logError("不能删除root节点"); 97 return false; 98 } 99 for (let i in parent.value_) { 100 if (parent.value_[i] == node) { 101 parent.value_.splice(i, 1); 102 return; 103 } 104 } 105} 106 107ModifyNode.getInheritList = function (root, node) { 108 //获取可继承列表(模板列表) 109 let ret = [] 110 let parent = getParent(root, node, null); 111 if (parent == null) return ret; 112 for (let i in parent.value_) { 113 //获取同级模板节点 114 let pn = parent.value_[i] 115 if (pn.type_ == DataType.NODE && pn.nodeType_ == NodeType.TEMPLETE) { 116 ret.push(pn); 117 } 118 } 119 let ps = [parent] 120 while (true) { 121 let pp = getParent(root, parent, null); 122 if (pp == null) break; 123 ps.splice(0, 0, pp) 124 } 125 let ptemp = null; 126 for (let i in ps) { 127 if (ps[i].nodeType_ == NodeType.INHERIT) { 128 ptemp = NodeTools.findChildByName(ptemp, ps[i].ref_) 129 } 130 else if (ptemp == null) { 131 ptemp = ps[i] 132 } 133 else { 134 ptemp = NodeTools.findChildByName(ptemp, ps[i].name_) 135 } 136 if (ptemp == null) break; 137 } 138 if (ptemp != null && ptemp != parent) { 139 for (let i in ptemp.value_) { 140 //获取其他同级模板节点 141 let pn = ptemp.value_[i] 142 if (pn.type_ == DataType.NODE && pn.nodeType_ == NodeType.TEMPLETE) { 143 ret.push(pn); 144 } 145 } 146 } 147 148 return ret; 149} 150ModifyNode.getInheritNameList = function (root, node) { 151 let nodes = ModifyNode.getInheritList(root, node); 152 let ret = [] 153 for (i in nodes) { 154 ret.push(nodes[i].name_) 155 } 156 return ret; 157} 158ModifyNode.getInheritTemplete = function (root, node) { 159 let parent = getParent(root, node, null); 160 if (parent == null) 161 return null; 162 let ilist = ModifyNode.getInheritList(root, node); 163 for (let i in ilist) { 164 if (parent.nodeType_ == NodeType.TEMPLETE) { 165 if (parent.ref_ == ilist[i].name_) 166 return ilist[i]; 167 } 168 else if (parent.name_ == ilist[i].name_) { 169 return ilist[i] 170 } 171 } 172 return null; 173} 174 175module.exports = { 176 ModifyNode 177}