XmlParser.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. const sax = require('./sax');
  2. //node types
  3. const NODE = 1;
  4. const TEXT = 2;
  5. const CDATA = 3;
  6. const COMMENT = 4;
  7. const name2type = {
  8. 'NODE': NODE,
  9. 'TEXT': TEXT,
  10. 'CDATA': CDATA,
  11. 'COMMENT': COMMENT,
  12. };
  13. const type2name = {
  14. [NODE]: 'NODE',
  15. [TEXT]: 'TEXT',
  16. [CDATA]: 'CDATA',
  17. [COMMENT]: 'COMMENT',
  18. };
  19. class NodeBase {
  20. makeSelectorObj(selectorString) {
  21. const result = {all: false, before: false, type: 0, name: ''};
  22. if (selectorString === '') {
  23. result.before = true;
  24. } else if (selectorString === '*') {
  25. result.all = true;
  26. } else if (selectorString[0] === '*') {
  27. const typeName = selectorString.substring(1);
  28. result.type = name2type[typeName];
  29. if (!result.type)
  30. throw new Error(`Unknown selector type: ${typeName}`);
  31. } else {
  32. result.name = selectorString;
  33. }
  34. return result;
  35. }
  36. checkNode(rawNode, selectorObj) {
  37. return selectorObj.all || selectorObj.before
  38. || (selectorObj.type && rawNode[0] === selectorObj.type)
  39. || (rawNode[0] === NODE && rawNode[1] === selectorObj.name);
  40. }
  41. findNodeIndex(nodes, selectorObj) {
  42. for (let i = 0; i < nodes.length; i++)
  43. if (this.checkNode(nodes[i], selectorObj))
  44. return i;
  45. }
  46. rawAdd(nodes, rawNode, selectorObj) {
  47. if (selectorObj.all) {
  48. nodes.push(rawNode);
  49. } else if (selectorObj.before) {
  50. nodes.unshift(rawNode);
  51. } else {
  52. const index = this.findNodeIndex(nodes, selectorObj);
  53. if (index >= 0)
  54. nodes.splice(index, 0, rawNode);
  55. else
  56. nodes.push(rawNode);
  57. }
  58. }
  59. rawRemove(nodes, selectorObj) {
  60. if (selectorObj.before)
  61. return;
  62. for (let i = nodes.length - 1; i >= 0; i--) {
  63. if (this.checkNode(nodes[i], selectorObj))
  64. nodes.splice(i, 1);
  65. }
  66. }
  67. }
  68. class NodeObject extends NodeBase {
  69. constructor(rawNode) {
  70. super();
  71. if (rawNode)
  72. this.raw = rawNode;
  73. else
  74. this.raw = [];
  75. }
  76. get type() {
  77. return this.raw[0] || null;
  78. }
  79. get name() {
  80. if (this.type === NODE)
  81. return this.raw[1] || null;
  82. return null;
  83. }
  84. set name(value) {
  85. if (this.type === NODE)
  86. this.raw[1] = value;
  87. }
  88. get attrs() {
  89. if (this.type === NODE && Array.isArray(this.raw[2]))
  90. return new Map(this.raw[2]);
  91. return null;
  92. }
  93. set attrs(value) {
  94. if (this.type === NODE)
  95. if (value && value.size)
  96. this.raw[2] = Array.from(value);
  97. else
  98. this.raw[2] = null;
  99. }
  100. get value() {
  101. switch (this.type) {
  102. case NODE:
  103. return this.raw[3] || null;
  104. case TEXT:
  105. case CDATA:
  106. case COMMENT:
  107. return this.raw[1] || null;
  108. }
  109. return null;
  110. }
  111. add(node, after = '*') {
  112. if (this.type !== NODE)
  113. return;
  114. const selectorObj = this.makeSelectorObj(after);
  115. if (!Array.isArray(this.raw[3]))
  116. this.raw[3] = [];
  117. this.rawAdd(this.raw[3], node.raw, selectorObj);
  118. }
  119. remove(selector = '') {
  120. if (this.type !== NODE || !this.raw[3])
  121. return;
  122. const selectorObj = this.makeSelectorObj(selector);
  123. this.rawRemove(this.raw[3], selectorObj);
  124. if (!this.raw[3].length)
  125. this.raw[3] = null;
  126. }
  127. each(callback) {
  128. if (this.type !== NODE || !this.raw[3])
  129. return;
  130. for (const n of this.raw[3]) {
  131. callback(new NodeObject(n));
  132. }
  133. }
  134. eachDeep(callback) {
  135. if (this.type !== NODE || !this.raw[3])
  136. return;
  137. const deep = (nodes, route = '') => {
  138. for (const n of nodes) {
  139. const node = new NodeObject(n);
  140. callback(node, route);
  141. if (node.type === NODE && node.value) {
  142. deep(node.value, route + `/${node.name}`);
  143. }
  144. }
  145. }
  146. deep(this.raw[3]);
  147. }
  148. }
  149. class XmlParser extends NodeBase {
  150. constructor(rawNodes = []) {
  151. super();
  152. this.NODE = NODE;
  153. this.TEXT = TEXT;
  154. this.CDATA = CDATA;
  155. this.COMMENT = COMMENT;
  156. this.rawNodes = rawNodes;
  157. }
  158. get count() {
  159. return this.rawNodes.length;
  160. }
  161. toObject(node) {
  162. return new NodeObject(node);
  163. }
  164. newParser(nodes) {
  165. return new XmlParser(nodes);
  166. }
  167. checkType(type) {
  168. if (!type2name[type])
  169. throw new Error(`Invalid type: ${type}`);
  170. }
  171. createTypedNode(type, nameOrValue, attrs = null, value = null) {
  172. this.checkType(type);
  173. switch (type) {
  174. case NODE:
  175. if (!nameOrValue || typeof(nameOrValue) !== 'string')
  176. throw new Error('Node name must be non-empty string');
  177. return new NodeObject([type, nameOrValue, attrs, value]);
  178. case TEXT:
  179. case CDATA:
  180. case COMMENT:
  181. if (typeof(nameOrValue) !== 'string')
  182. throw new Error('Node value must be of type string');
  183. return new NodeObject([type, nameOrValue]);
  184. }
  185. }
  186. createNode(name, attrs = null, value = null) {
  187. return this.createTypedNode(NODE, name, attrs, value);
  188. }
  189. createText(value = null) {
  190. return this.createTypedNode(TEXT, value);
  191. }
  192. createCdata(value = null) {
  193. return this.createTypedNode(CDATA, value);
  194. }
  195. createComment(value = null) {
  196. return this.createTypedNode(COMMENT, value);
  197. }
  198. add(node, after = '*') {
  199. const selectorObj = this.makeSelectorObj(after);
  200. for (const n of this.rawNodes) {
  201. if (n && n[0] === NODE) {
  202. if (!Array.isArray(n[3]))
  203. n[3] = [];
  204. this.rawAdd(n[3], node.raw, selectorObj);
  205. }
  206. }
  207. }
  208. addRoot(node, after = '*') {
  209. const selectorObj = this.makeSelectorObj(after);
  210. this.rawAdd(this.rawNodes, node.raw, selectorObj);
  211. }
  212. remove(selector = '') {
  213. const selectorObj = this.makeSelectorObj(selector);
  214. for (const n of this.rawNodes) {
  215. if (n && n[0] === NODE && Array.isArray(n[3])) {
  216. this.rawRemove(n[3], selectorObj);
  217. if (!n[3].length)
  218. n[3] = null;
  219. }
  220. }
  221. }
  222. removeRoot(selector = '') {
  223. const selectorObj = this.makeSelectorObj(selector);
  224. this.rawRemove(this.rawNodes, selectorObj);
  225. }
  226. each(callback) {
  227. for (const n of this.rawNodes) {
  228. callback(new NodeObject(n));
  229. }
  230. }
  231. eachDeep(callback) {
  232. const deep = (nodes, route = '') => {
  233. for (const n of nodes) {
  234. const node = new NodeObject(n);
  235. callback(node, route);
  236. if (node.type === NODE && node.value) {
  237. deep(node.value, route + `/${node.name}`);
  238. }
  239. }
  240. }
  241. deep(this.rawNodes);
  242. }
  243. rawSelect(nodes, selectorObj, callback) {
  244. for (const n of nodes)
  245. if (this.checkNode(n, selectorObj))
  246. callback(n);
  247. }
  248. select(selector = '', self = false) {
  249. let newRawNodes = [];
  250. if (selector.indexOf('/') >= 0) {
  251. const selectors = selector.split('/');
  252. let res = this;
  253. for (const sel of selectors) {
  254. res = res.select(sel, self);
  255. self = false;
  256. }
  257. newRawNodes = res.rawNodes;
  258. } else {
  259. const selectorObj = this.makeSelectorObj(selector);
  260. if (self) {
  261. this.rawSelect(this.rawNodes, selectorObj, (node) => {
  262. newRawNodes.push(node);
  263. })
  264. } else {
  265. for (const n of this.rawNodes) {
  266. if (n && n[0] === NODE && Array.isArray(n[3])) {
  267. this.rawSelect(n[3], selectorObj, (node) => {
  268. newRawNodes.push(node);
  269. })
  270. }
  271. }
  272. }
  273. }
  274. return new XmlParser(newRawNodes);
  275. }
  276. s(selector, self) {
  277. return this.select(selector, self);
  278. }
  279. selectFirst(selector, self) {
  280. const result = this.select(selector, self);
  281. const node = (result.count ? result.rawNodes[0] : null);
  282. return this.toObject(node);
  283. }
  284. sf(selector, self) {
  285. return this.selectFirst(selector, self);
  286. }
  287. toJson(format = false) {
  288. if (format)
  289. return JSON.stringify(this.rawNodes, null, 2);
  290. else
  291. return JSON.stringify(this.rawNodes);
  292. }
  293. fromJson(jsonString) {
  294. const parsed = JSON.parse(jsonString);
  295. if (!Array.isArray(parsed))
  296. throw new Error('JSON parse error: root element must be array');
  297. this.rawNodes = parsed;
  298. }
  299. toString(options = {}) {
  300. const {encoding = 'utf-8', format = false} = options;
  301. let deepType = 0;
  302. let out = '';
  303. if (this.count < 2)
  304. out += `<?xml version="1.0" encoding="${encoding}"?>`;
  305. const nodesToString = (nodes, depth = 0) => {
  306. let result = '';
  307. let lastType = 0;
  308. for (const n of nodes) {
  309. const node = new NodeObject(n);
  310. lastType = node.type;
  311. let open = '';
  312. let body = '';
  313. let close = '';
  314. if (node.type === NODE) {
  315. if (!node.name)
  316. break;
  317. let attrs = '';
  318. if (node.attrs) {
  319. for (const [attrName, attrValue] of node.attrs) {
  320. attrs += ` ${attrName}="${attrValue}"`;
  321. }
  322. }
  323. open = `<${node.name}${attrs}>`;
  324. if (node.value)
  325. body = nodesToString(node.value, depth + 2);
  326. close = `</${node.name}>`;
  327. if (format) {
  328. open = '\n' + ' '.repeat(depth) + open;
  329. close = (deepType === NODE ? ' '.repeat(depth) : '') + close + '\n';
  330. }
  331. } else if (node.type === TEXT) {
  332. body = node.value;
  333. } else if (node.type === CDATA) {
  334. //
  335. } else if (node.type === COMMENT) {
  336. //
  337. }
  338. result += `${open}${body}${close}`;
  339. }
  340. deepType = lastType;
  341. return result;
  342. }
  343. out += nodesToString(this.rawNodes);
  344. return out;
  345. }
  346. fromSrtring() {
  347. }
  348. }
  349. module.exports = XmlParser;