|
| 1 | +const WriteEncoder = require('./encoder'); |
| 2 | +const logger = require('../logger'); |
| 3 | + |
| 4 | +class ProtocolTreeNode { |
| 5 | + constructor(tag, attributes = null, children = null, data = null) { |
| 6 | + this.tag = tag; |
| 7 | + this.attributes = attributes || {}; |
| 8 | + this.children = children || []; |
| 9 | + this.data = data; |
| 10 | + Object.keys(this.attributes).forEach(key => { |
| 11 | + if (this.attributes[key] === undefined || this.attributes[key] === null) { |
| 12 | + delete this.attributes[key]; |
| 13 | + } |
| 14 | + }); |
| 15 | + this.children = this.children.map(child => { |
| 16 | + return new ProtocolTreeNode(child.tag, child.attributes, child.children, child.data); |
| 17 | + }); |
| 18 | + } |
| 19 | + |
| 20 | + toBuffer() { |
| 21 | + try { |
| 22 | + const bytes = new WriteEncoder().protocolTreeNodeToBytes(this); |
| 23 | + // console.log('bytes', this.tag, this.attributes, Buffer.from(bytes).toString('hex')); |
| 24 | + return Buffer.from(bytes); |
| 25 | + } catch (e) { |
| 26 | + logger.info(`Node 序列化 buffer 失败${e.message}`); |
| 27 | + logger.info(`Node 序列化 buffer 失败${e.stack}`); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + toString() { |
| 32 | + let out = `<${this.tag}`; |
| 33 | + if (this.attributes) { |
| 34 | + Object.keys(this.attributes).forEach(key => { |
| 35 | + const val = this.attributes[key]; |
| 36 | + out += ` ${key}="${val}"`; |
| 37 | + }); |
| 38 | + } |
| 39 | + out += '>\n'; |
| 40 | + if (this.data) { |
| 41 | + if (this.data instanceof Buffer) { |
| 42 | + out += this.data.toString('utf8'); |
| 43 | + } else { |
| 44 | + try { |
| 45 | + out += this.data; |
| 46 | + } catch (e) { |
| 47 | + try { |
| 48 | + out += this.data.toString(); |
| 49 | + } catch (err) { |
| 50 | + out += this.data.toString('hex'); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + out += `\nHEX3:${Buffer.from(this.data || '').toString('hex')}\n`; |
| 55 | + } |
| 56 | + |
| 57 | + for (let i = 0; i < this.children.length; i++) { |
| 58 | + const c = this.children[i]; |
| 59 | + try { |
| 60 | + out += c.toString(); |
| 61 | + } catch (e) { |
| 62 | + console.error(e); |
| 63 | + out += '[ENCODED DATA]\n'; |
| 64 | + } |
| 65 | + } |
| 66 | + out += `</${this.tag}>\n`; |
| 67 | + return out; |
| 68 | + } |
| 69 | + |
| 70 | + toJSON() { |
| 71 | + const json = { |
| 72 | + tag: this.tag, |
| 73 | + props: this.attributes, |
| 74 | + children: [], |
| 75 | + }; |
| 76 | + Object.keys(this.attributes).forEach(key => { |
| 77 | + let value = this.attributes[key]; |
| 78 | + if (value instanceof Buffer) { |
| 79 | + value = value.toString('utf8'); |
| 80 | + this.attributes[key] = value; |
| 81 | + } |
| 82 | + }); |
| 83 | + if (this.data !== null) { |
| 84 | + json.data = this.data; |
| 85 | + // if (json.data instanceof Buffer) { |
| 86 | + // json.data = json.data.toString('utf8'); |
| 87 | + // } |
| 88 | + } |
| 89 | + for (let i = 0; i < this.children.length; i++) { |
| 90 | + const c = this.children[i]; |
| 91 | + try { |
| 92 | + json.children.push(c.toJSON()); |
| 93 | + } catch (e) { |
| 94 | + console.error(e); |
| 95 | + json.children.push('[ENCODED DATA]'); |
| 96 | + } |
| 97 | + } |
| 98 | + return json; |
| 99 | + } |
| 100 | + |
| 101 | + toSimpleJSON() { |
| 102 | + const json = {}; |
| 103 | + json[this.tag] = {}; |
| 104 | + Object.keys(this.attributes).forEach(key => { |
| 105 | + let value = this.attributes[key]; |
| 106 | + if (value instanceof Buffer) { |
| 107 | + value = value.toString('utf8'); |
| 108 | + } |
| 109 | + json[this.tag][key] = value; |
| 110 | + }); |
| 111 | + try { |
| 112 | + for (let i = 0; i < this.children.length; i++) { |
| 113 | + const c = this.children[i].toSimpleJSON(); |
| 114 | + if (!c) continue; |
| 115 | + let tag = Object.keys(c)[0]; |
| 116 | + const child = c[tag]; |
| 117 | + if (typeof this.attributes[tag] !== 'undefined') { |
| 118 | + tag = `${tag}_entity`; |
| 119 | + } |
| 120 | + if (json[this.tag][tag]) { |
| 121 | + if (Array.isArray(json[this.tag][tag])) { |
| 122 | + json[this.tag][tag].push(child); |
| 123 | + } else { |
| 124 | + json[this.tag][tag] = [json[this.tag][tag], child]; |
| 125 | + } |
| 126 | + } else { |
| 127 | + json[this.tag][tag] = child; |
| 128 | + } |
| 129 | + } |
| 130 | + } catch (e) { |
| 131 | + console.error(e); |
| 132 | + } |
| 133 | + if (this.data !== null && typeof this.data !== 'undefined') { |
| 134 | + json[this.tag].entity_value = this.data; |
| 135 | + if (json[this.tag].entity_value instanceof Buffer) { |
| 136 | + json[this.tag].entity_value = json[this.tag].entity_value.toString('utf8'); |
| 137 | + } |
| 138 | + } |
| 139 | + return json || {}; |
| 140 | + } |
| 141 | + |
| 142 | + setData(data) { |
| 143 | + this.data = data; |
| 144 | + } |
| 145 | + |
| 146 | + getData() { |
| 147 | + return this.data; |
| 148 | + } |
| 149 | + |
| 150 | + getChild(identifier) { |
| 151 | + if (typeof identifier === 'number') { |
| 152 | + if (this.children.length > identifier) return this.children[identifier]; |
| 153 | + return null; |
| 154 | + } |
| 155 | + for (let i = 0; i < this.children.length; i++) { |
| 156 | + const c = this.children[i]; |
| 157 | + if (c.tag === identifier) return c; |
| 158 | + } |
| 159 | + return null; |
| 160 | + } |
| 161 | + |
| 162 | + addChild(childNode) { |
| 163 | + this.children.push(childNode); |
| 164 | + } |
| 165 | + |
| 166 | + addChildren(children) { |
| 167 | + children.forEach(child => { |
| 168 | + this.addChild(child); |
| 169 | + }); |
| 170 | + } |
| 171 | + |
| 172 | + hasChildren() { |
| 173 | + return this.children.length > 0; |
| 174 | + } |
| 175 | + |
| 176 | + getAllChildren(tag = null) { |
| 177 | + if (!tag) return this.children; |
| 178 | + return this.children.filter(child => child.tag === tag); |
| 179 | + } |
| 180 | + |
| 181 | + getAttributeValue(key) { |
| 182 | + return this.attributes[key]; |
| 183 | + } |
| 184 | + |
| 185 | + getAttr(key) { |
| 186 | + return this.getAttributeValue(key); |
| 187 | + } |
| 188 | + |
| 189 | + setAttr(key, value) { |
| 190 | + this.setAttribute(key, value); |
| 191 | + } |
| 192 | + |
| 193 | + removeAttr(key) { |
| 194 | + delete this.attributes[key]; |
| 195 | + } |
| 196 | + |
| 197 | + removeAttribute(key) { |
| 198 | + delete this.attributes[key]; |
| 199 | + } |
| 200 | + |
| 201 | + setAttribute(key, value) { |
| 202 | + this.attributes[key] = value; |
| 203 | + } |
| 204 | +} |
| 205 | +module.exports = ProtocolTreeNode; |
0 commit comments