XmlParser.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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. attrs(key, value) {
  89. if (this.type !== NODE)
  90. return null;
  91. let map = null;
  92. if (key instanceof Map) {
  93. map = key;
  94. this.raw[2] = Array.from(map);
  95. } else if (Array.isArray(this.raw[2])) {
  96. map = new Map(this.raw[2]);
  97. if (key) {
  98. map.set(key, value);
  99. this.raw[2] = Array.from(map);
  100. }
  101. }
  102. return map;
  103. }
  104. get value() {
  105. switch (this.type) {
  106. case NODE:
  107. return this.raw[3] || null;
  108. case TEXT:
  109. case CDATA:
  110. case COMMENT:
  111. return this.raw[1] || null;
  112. }
  113. return null;
  114. }
  115. set value(v) {
  116. switch (this.type) {
  117. case NODE:
  118. this.raw[3] = v;
  119. break;
  120. case TEXT:
  121. case CDATA:
  122. case COMMENT:
  123. this.raw[1] = v;
  124. }
  125. }
  126. add(node, after = '*') {
  127. if (this.type !== NODE)
  128. return;
  129. const selectorObj = this.makeSelectorObj(after);
  130. if (!Array.isArray(this.raw[3]))
  131. this.raw[3] = [];
  132. if (Array.isArray(node)) {
  133. for (const node_ of node)
  134. this.rawAdd(this.raw[3], node_.raw, selectorObj);
  135. } else {
  136. this.rawAdd(this.raw[3], node.raw, selectorObj);
  137. }
  138. return this;
  139. }
  140. remove(selector = '') {
  141. if (this.type !== NODE || !this.raw[3])
  142. return;
  143. const selectorObj = this.makeSelectorObj(selector);
  144. this.rawRemove(this.raw[3], selectorObj);
  145. if (!this.raw[3].length)
  146. this.raw[3] = null;
  147. return this;
  148. }
  149. each(callback) {
  150. if (this.type !== NODE || !this.raw[3])
  151. return;
  152. for (const n of this.raw[3]) {
  153. callback(new NodeObject(n));
  154. }
  155. return this;
  156. }
  157. eachDeep(callback) {
  158. if (this.type !== NODE || !this.raw[3])
  159. return;
  160. const deep = (nodes, route = '') => {
  161. for (const n of nodes) {
  162. const node = new NodeObject(n);
  163. callback(node, route);
  164. if (node.type === NODE && node.value) {
  165. deep(node.value, `${route}${route ? '/' : ''}${node.name}`);
  166. }
  167. }
  168. }
  169. deep(this.raw[3]);
  170. return this;
  171. }
  172. }
  173. class XmlParser extends NodeBase {
  174. constructor(rawNodes = []) {
  175. super();
  176. this.NODE = NODE;
  177. this.TEXT = TEXT;
  178. this.CDATA = CDATA;
  179. this.COMMENT = COMMENT;
  180. this.rawNodes = rawNodes;
  181. }
  182. get count() {
  183. return this.rawNodes.length;
  184. }
  185. toObject(node) {
  186. return new NodeObject(node);
  187. }
  188. newParser(nodes) {
  189. return new XmlParser(nodes);
  190. }
  191. checkType(type) {
  192. if (!type2name[type])
  193. throw new Error(`Invalid type: ${type}`);
  194. }
  195. createTypedNode(type, nameOrValue, attrs = null, value = null) {
  196. this.checkType(type);
  197. switch (type) {
  198. case NODE:
  199. if (!nameOrValue || typeof(nameOrValue) !== 'string')
  200. throw new Error('Node name must be non-empty string');
  201. return new NodeObject([type, nameOrValue, attrs, value]);
  202. case TEXT:
  203. case CDATA:
  204. case COMMENT:
  205. if (typeof(nameOrValue) !== 'string')
  206. throw new Error('Node value must be of type string');
  207. return new NodeObject([type, nameOrValue]);
  208. }
  209. }
  210. createNode(name, attrs = null, value = null) {
  211. return this.createTypedNode(NODE, name, attrs, value);
  212. }
  213. createText(value = null) {
  214. return this.createTypedNode(TEXT, value);
  215. }
  216. createCdata(value = null) {
  217. return this.createTypedNode(CDATA, value);
  218. }
  219. createComment(value = null) {
  220. return this.createTypedNode(COMMENT, value);
  221. }
  222. add(node, after = '*') {
  223. const selectorObj = this.makeSelectorObj(after);
  224. for (const n of this.rawNodes) {
  225. if (n && n[0] === NODE) {
  226. if (!Array.isArray(n[3]))
  227. n[3] = [];
  228. if (Array.isArray(node)) {
  229. for (const node_ of node)
  230. this.rawAdd(n[3], node_.raw, selectorObj);
  231. } else {
  232. this.rawAdd(n[3], node.raw, selectorObj);
  233. }
  234. }
  235. }
  236. return this;
  237. }
  238. addRoot(node, after = '*') {
  239. const selectorObj = this.makeSelectorObj(after);
  240. if (Array.isArray(node)) {
  241. for (const node_ of node)
  242. this.rawAdd(this.rawNodes, node_.raw, selectorObj);
  243. } else {
  244. this.rawAdd(this.rawNodes, node.raw, selectorObj);
  245. }
  246. return this;
  247. }
  248. remove(selector = '') {
  249. const selectorObj = this.makeSelectorObj(selector);
  250. for (const n of this.rawNodes) {
  251. if (n && n[0] === NODE && Array.isArray(n[3])) {
  252. this.rawRemove(n[3], selectorObj);
  253. if (!n[3].length)
  254. n[3] = null;
  255. }
  256. }
  257. return this;
  258. }
  259. removeRoot(selector = '') {
  260. const selectorObj = this.makeSelectorObj(selector);
  261. this.rawRemove(this.rawNodes, selectorObj);
  262. return this;
  263. }
  264. each(callback, self = false) {
  265. if (self) {
  266. for (const n of this.rawNodes) {
  267. callback(new NodeObject(n));
  268. }
  269. } else {
  270. for (const n of this.rawNodes) {
  271. if (n[0] === NODE && n[3])
  272. callback(new NodeObject(n[3]));
  273. }
  274. }
  275. return this;
  276. }
  277. eachDeep(callback, self = false) {
  278. const deep = (nodes, route = '') => {
  279. for (const n of nodes) {
  280. const node = new NodeObject(n);
  281. callback(node, route);
  282. if (node.type === NODE && node.value) {
  283. deep(node.value, `${route}${route ? '/' : ''}${node.name}`);
  284. }
  285. }
  286. }
  287. if (self) {
  288. deep(this.rawNodes);
  289. } else {
  290. for (const n of this.rawNodes) {
  291. if (n[0] === NODE && n[3])
  292. deep(n[3]);
  293. }
  294. }
  295. return this;
  296. }
  297. eachDeepSelf(callback) {
  298. return this.eachDeep(callback, true);
  299. }
  300. rawSelect(nodes, selectorObj, callback) {
  301. for (const n of nodes)
  302. if (this.checkNode(n, selectorObj))
  303. callback(n);
  304. return this;
  305. }
  306. select(selector = '', self = false) {
  307. let newRawNodes = [];
  308. if (selector.indexOf('/') >= 0) {
  309. const selectors = selector.split('/');
  310. let res = this;
  311. for (const sel of selectors) {
  312. res = res.select(sel, self);
  313. self = false;
  314. }
  315. newRawNodes = res.rawNodes;
  316. } else {
  317. const selectorObj = this.makeSelectorObj(selector);
  318. if (self) {
  319. this.rawSelect(this.rawNodes, selectorObj, (node) => {
  320. newRawNodes.push(node);
  321. })
  322. } else {
  323. for (const n of this.rawNodes) {
  324. if (n && n[0] === NODE && Array.isArray(n[3])) {
  325. this.rawSelect(n[3], selectorObj, (node) => {
  326. newRawNodes.push(node);
  327. })
  328. }
  329. }
  330. }
  331. }
  332. return new XmlParser(newRawNodes);
  333. }
  334. $$(selector, self) {
  335. return this.select(selector, self);
  336. }
  337. $$self(selector) {
  338. return this.select(selector, true);
  339. }
  340. selectFirst(selector, self) {
  341. const result = this.select(selector, self);
  342. const node = (result.count ? result.rawNodes[0] : null);
  343. return this.toObject(node);
  344. }
  345. $(selector, self) {
  346. return this.selectFirst(selector, self);
  347. }
  348. $self(selector) {
  349. return this.selectFirst(selector, true);
  350. }
  351. toJson(options = {}) {
  352. const {format = false} = options;
  353. if (format)
  354. return JSON.stringify(this.rawNodes, null, 2);
  355. else
  356. return JSON.stringify(this.rawNodes);
  357. }
  358. fromJson(jsonString) {
  359. const parsed = JSON.parse(jsonString);
  360. if (!Array.isArray(parsed))
  361. throw new Error('JSON parse error: root element must be array');
  362. this.rawNodes = parsed;
  363. }
  364. toString(options = {}) {
  365. const {encoding = 'utf-8', format = false, noHeader = false} = options;
  366. let deepType = 0;
  367. let out = '';
  368. if (!noHeader)
  369. out += `<?xml version="1.0" encoding="${encoding}"?>`;
  370. const nodesToString = (nodes, depth = 0) => {
  371. let result = '';
  372. const indent = '\n' + ' '.repeat(depth);
  373. let lastType = 0;
  374. for (const n of nodes) {
  375. const node = new NodeObject(n);
  376. let open = '';
  377. let body = '';
  378. let close = '';
  379. if (node.type === NODE) {
  380. if (!node.name)
  381. break;
  382. let attrs = '';
  383. const nodeAttrs = node.attrs();
  384. if (nodeAttrs) {
  385. for (const [attrName, attrValue] of nodeAttrs) {
  386. if (typeof(attrValue) === 'string')
  387. attrs += ` ${attrName}="${attrValue}"`;
  388. else
  389. if (attrValue)
  390. attrs += ` ${attrName}`;
  391. }
  392. }
  393. open = (format && lastType !== TEXT ? indent : '');
  394. open += `<${node.name}${attrs}>`;
  395. if (node.value)
  396. body = nodesToString(node.value, depth + 2);
  397. close = (format && deepType && deepType !== TEXT ? indent : '');
  398. close += `</${node.name}>`;
  399. } else if (node.type === TEXT) {
  400. body = node.value || '';
  401. } else if (node.type === CDATA) {
  402. body = (format && lastType !== TEXT ? indent : '');
  403. body += `<![CDATA[${node.value || ''}]]>`;
  404. } else if (node.type === COMMENT) {
  405. body = (format && lastType !== TEXT ? indent : '');
  406. body += `<!--${node.value || ''}-->`;
  407. }
  408. result += `${open}${body}${close}`;
  409. lastType = node.type;
  410. }
  411. deepType = lastType;
  412. return result;
  413. }
  414. out += nodesToString(this.rawNodes);
  415. return out;
  416. }
  417. fromString(xmlString, options = {}) {
  418. const {
  419. lowerCase = false,
  420. whiteSpace = false,
  421. pickNode = false,
  422. } = options;
  423. const parsed = [];
  424. const root = this.createNode('root', null, parsed);//fake node
  425. let node = root;
  426. let route = '';
  427. let routeStack = [];
  428. let ignoreNode = false;
  429. const onStartNode = (tag, tail, singleTag, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
  430. if (tag == '?xml')
  431. return;
  432. if (!ignoreNode && pickNode) {
  433. route += `/${tag}`;
  434. ignoreNode = !pickNode(route);
  435. }
  436. let newNode = node;
  437. if (!ignoreNode)
  438. newNode = this.createNode(tag);
  439. routeStack.push({tag, route, ignoreNode, node: newNode});
  440. if (ignoreNode)
  441. return;
  442. if (tail && tail.trim() !== '') {
  443. const parsedAttrs = sax.getAttrsSync(tail, lowerCase);
  444. const attrs = new Map();
  445. for (const attr of parsedAttrs.values()) {
  446. attrs.set(attr.fn, attr.value);
  447. }
  448. if (attrs.size)
  449. newNode.attrs(attrs);
  450. }
  451. if (!node.value)
  452. node.value = [];
  453. node.value.push(newNode.raw);
  454. node = newNode;
  455. };
  456. const onEndNode = (tag, tail, singleTag, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
  457. if (routeStack.length && routeStack[routeStack.length - 1].tag === tag) {
  458. routeStack.pop();
  459. if (routeStack.length) {
  460. const last = routeStack[routeStack.length - 1];
  461. route = last.route;
  462. ignoreNode = last.ignoreNode;
  463. node = last.node;
  464. } else {
  465. route = '';
  466. ignoreNode = false;
  467. node = root;
  468. }
  469. }
  470. }
  471. const onTextNode = (text, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
  472. if (ignoreNode || (pickNode && !pickNode(`${route}/*TEXT`)))
  473. return;
  474. if (!whiteSpace && text.trim() == '')
  475. return;
  476. if (!node.value)
  477. node.value = [];
  478. node.value.push(this.createText(text).raw);
  479. };
  480. const onCdata = (tagData, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
  481. if (ignoreNode || (pickNode && !pickNode(`${route}/*CDATA`)))
  482. return;
  483. if (!node.value)
  484. node.value = [];
  485. node.value.push(this.createCdata(tagData).raw);
  486. }
  487. const onComment = (tagData, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
  488. if (ignoreNode || (pickNode && !pickNode(`${route}/*COMMENT`)))
  489. return;
  490. if (!node.value)
  491. node.value = [];
  492. node.value.push(this.createComment(tagData).raw);
  493. }
  494. sax.parseSync(xmlString, {
  495. onStartNode, onEndNode, onTextNode, onCdata, onComment, lowerCase
  496. });
  497. this.rawNodes = parsed;
  498. }
  499. }
  500. module.exports = XmlParser;