From e4571faf39ce644e39234c4044821dbeea7a7920 Mon Sep 17 00:00:00 2001 From: Book Pauk Date: Mon, 7 Nov 2022 21:13:15 +0700 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=20=D0=BD?= =?UTF-8?q?=D0=B0=D0=B4=20XmlParser?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/core/xml/XmlParser.js | 96 +++++++++++++++++++++++++++++++++++- 1 file changed, 95 insertions(+), 1 deletion(-) diff --git a/server/core/xml/XmlParser.js b/server/core/xml/XmlParser.js index e1c3a0d..38a3855 100644 --- a/server/core/xml/XmlParser.js +++ b/server/core/xml/XmlParser.js @@ -1,3 +1,5 @@ +const sax = require('./sax'); + //node types const NODE = 1; const TEXT = 2; @@ -159,6 +161,24 @@ class NodeObject extends NodeBase { callback(new NodeObject(n)); } } + + eachDeep(callback) { + if (this.type !== NODE || !this.raw[3]) + return; + + const deep = (nodes, route = '') => { + for (const n of nodes) { + const node = new NodeObject(n); + callback(node, route); + + if (node.type === NODE && node.value) { + deep(node.value, route + `/${node.name}`); + } + } + } + + deep(this.raw[3]); + } } class XmlParser extends NodeBase { @@ -264,6 +284,21 @@ class XmlParser extends NodeBase { } } + eachDeep(callback) { + const deep = (nodes, route = '') => { + for (const n of nodes) { + const node = new NodeObject(n); + callback(node, route); + + if (node.type === NODE && node.value) { + deep(node.value, route + `/${node.name}`); + } + } + } + + deep(this.rawNodes); + } + rawSelect(nodes, selectorObj, callback) { for (const n of nodes) if (this.checkNode(n, selectorObj)) @@ -332,7 +367,66 @@ class XmlParser extends NodeBase { this.rawNodes = parsed; } - toString() { + toString(options = {}) { + const {encoding = 'utf-8', format = false} = options; + + let deepType = 0; + let out = ''; + if (this.count < 2) + out += ``; + + const nodesToString = (nodes, depth = 0) => { + let result = ''; + let lastType = 0; + + for (const n of nodes) { + const node = new NodeObject(n); + lastType = node.type; + + let open = ''; + let body = ''; + let close = ''; + + if (node.type === NODE) { + if (!node.name) + break; + + let attrs = ''; + + if (node.attrs) { + for (const [attrName, attrValue] of node.attrs) { + attrs += ` ${attrName}="${attrValue}"`; + } + } + + open = `<${node.name}${attrs}>`; + + if (node.value) + body = nodesToString(node.value, depth + 2); + close = ``; + + if (format) { + open = '\n' + ' '.repeat(depth) + open; + close = (deepType === NODE ? ' '.repeat(depth) : '') + close + '\n'; + } + } else if (node.type === TEXT) { + body = node.value; + } else if (node.type === CDATA) { + // + } else if (node.type === COMMENT) { + // + } + + result += `${open}${body}${close}`; + } + + deepType = lastType; + return result; + } + + out += nodesToString(this.rawNodes); + + return out; } fromSrtring() {