XmlParser.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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 + `/${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) {
  265. for (const n of this.rawNodes) {
  266. callback(new NodeObject(n));
  267. }
  268. return this;
  269. }
  270. eachDeep(callback) {
  271. const deep = (nodes, route = '') => {
  272. for (const n of nodes) {
  273. const node = new NodeObject(n);
  274. callback(node, route);
  275. if (node.type === NODE && node.value) {
  276. deep(node.value, route + `/${node.name}`);
  277. }
  278. }
  279. }
  280. deep(this.rawNodes);
  281. return this;
  282. }
  283. rawSelect(nodes, selectorObj, callback) {
  284. for (const n of nodes)
  285. if (this.checkNode(n, selectorObj))
  286. callback(n);
  287. return this;
  288. }
  289. select(selector = '', self = false) {
  290. let newRawNodes = [];
  291. if (selector.indexOf('/') >= 0) {
  292. const selectors = selector.split('/');
  293. let res = this;
  294. for (const sel of selectors) {
  295. res = res.select(sel, self);
  296. self = false;
  297. }
  298. newRawNodes = res.rawNodes;
  299. } else {
  300. const selectorObj = this.makeSelectorObj(selector);
  301. if (self) {
  302. this.rawSelect(this.rawNodes, selectorObj, (node) => {
  303. newRawNodes.push(node);
  304. })
  305. } else {
  306. for (const n of this.rawNodes) {
  307. if (n && n[0] === NODE && Array.isArray(n[3])) {
  308. this.rawSelect(n[3], selectorObj, (node) => {
  309. newRawNodes.push(node);
  310. })
  311. }
  312. }
  313. }
  314. }
  315. return new XmlParser(newRawNodes);
  316. }
  317. $$(selector, self) {
  318. return this.select(selector, self);
  319. }
  320. $$self(selector) {
  321. return this.select(selector, true);
  322. }
  323. selectFirst(selector, self) {
  324. const result = this.select(selector, self);
  325. const node = (result.count ? result.rawNodes[0] : null);
  326. return this.toObject(node);
  327. }
  328. $(selector, self) {
  329. return this.selectFirst(selector, self);
  330. }
  331. $self(selector) {
  332. return this.selectFirst(selector, true);
  333. }
  334. toJson(options = {}) {
  335. const {format = false} = options;
  336. if (format)
  337. return JSON.stringify(this.rawNodes, null, 2);
  338. else
  339. return JSON.stringify(this.rawNodes);
  340. }
  341. fromJson(jsonString) {
  342. const parsed = JSON.parse(jsonString);
  343. if (!Array.isArray(parsed))
  344. throw new Error('JSON parse error: root element must be array');
  345. this.rawNodes = parsed;
  346. }
  347. toString(options = {}) {
  348. const {encoding = 'utf-8', format = false, noHeader = false} = options;
  349. let deepType = 0;
  350. let out = '';
  351. if (!noHeader)
  352. out += `<?xml version="1.0" encoding="${encoding}"?>`;
  353. const nodesToString = (nodes, depth = 0) => {
  354. let result = '';
  355. const indent = '\n' + ' '.repeat(depth);
  356. let lastType = 0;
  357. for (const n of nodes) {
  358. const node = new NodeObject(n);
  359. let open = '';
  360. let body = '';
  361. let close = '';
  362. if (node.type === NODE) {
  363. if (!node.name)
  364. break;
  365. let attrs = '';
  366. const nodeAttrs = node.attrs();
  367. if (nodeAttrs) {
  368. for (const [attrName, attrValue] of nodeAttrs) {
  369. if (typeof(attrValue) === 'string')
  370. attrs += ` ${attrName}="${attrValue}"`;
  371. else
  372. if (attrValue)
  373. attrs += ` ${attrName}`;
  374. }
  375. }
  376. open = (format && lastType !== TEXT ? indent : '');
  377. open += `<${node.name}${attrs}>`;
  378. if (node.value)
  379. body = nodesToString(node.value, depth + 2);
  380. close = (format && deepType && deepType !== TEXT ? indent : '');
  381. close += `</${node.name}>`;
  382. } else if (node.type === TEXT) {
  383. body = node.value || '';
  384. } else if (node.type === CDATA) {
  385. body = (format && lastType !== TEXT ? indent : '');
  386. body += `<![CDATA[${node.value || ''}]]>`;
  387. } else if (node.type === COMMENT) {
  388. body = (format && lastType !== TEXT ? indent : '');
  389. body += `<!--${node.value || ''}-->`;
  390. }
  391. result += `${open}${body}${close}`;
  392. lastType = node.type;
  393. }
  394. deepType = lastType;
  395. return result;
  396. }
  397. out += nodesToString(this.rawNodes);
  398. return out;
  399. }
  400. fromString(xmlString, options = {}) {
  401. const {
  402. lowerCase = false,
  403. whiteSpace = false,
  404. pickNode = false,
  405. } = options;
  406. const parsed = [];
  407. const root = this.createNode('root', null, parsed);//fake node
  408. let node = root;
  409. let route = '';
  410. let routeStack = [];
  411. let ignoreNode = false;
  412. const onStartNode = (tag, tail, singleTag, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
  413. if (tag == '?xml')
  414. return;
  415. if (!ignoreNode && pickNode) {
  416. route += `/${tag}`;
  417. ignoreNode = !pickNode(route);
  418. }
  419. let newNode = node;
  420. if (!ignoreNode)
  421. newNode = this.createNode(tag);
  422. routeStack.push({tag, route, ignoreNode, node: newNode});
  423. if (ignoreNode)
  424. return;
  425. if (tail && tail.trim() !== '') {
  426. const parsedAttrs = sax.getAttrsSync(tail, lowerCase);
  427. const attrs = new Map();
  428. for (const attr of parsedAttrs.values()) {
  429. attrs.set(attr.fn, attr.value);
  430. }
  431. if (attrs.size)
  432. newNode.attrs(attrs);
  433. }
  434. if (!node.value)
  435. node.value = [];
  436. node.value.push(newNode.raw);
  437. node = newNode;
  438. };
  439. const onEndNode = (tag, tail, singleTag, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
  440. if (routeStack.length && routeStack[routeStack.length - 1].tag === tag) {
  441. routeStack.pop();
  442. if (routeStack.length) {
  443. const last = routeStack[routeStack.length - 1];
  444. route = last.route;
  445. ignoreNode = last.ignoreNode;
  446. node = last.node;
  447. } else {
  448. route = '';
  449. ignoreNode = false;
  450. node = root;
  451. }
  452. }
  453. }
  454. const onTextNode = (text, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
  455. if (ignoreNode || (pickNode && !pickNode(`${route}/*TEXT`)))
  456. return;
  457. if (!whiteSpace && text.trim() == '')
  458. return;
  459. if (!node.value)
  460. node.value = [];
  461. node.value.push(this.createText(text).raw);
  462. };
  463. const onCdata = (tagData, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
  464. if (ignoreNode || (pickNode && !pickNode(`${route}/*CDATA`)))
  465. return;
  466. if (!node.value)
  467. node.value = [];
  468. node.value.push(this.createCdata(tagData).raw);
  469. }
  470. const onComment = (tagData, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
  471. if (ignoreNode || (pickNode && !pickNode(`${route}/*COMMENT`)))
  472. return;
  473. if (!node.value)
  474. node.value = [];
  475. node.value.push(this.createComment(tagData).raw);
  476. }
  477. sax.parseSync(xmlString, {
  478. onStartNode, onEndNode, onTextNode, onCdata, onComment, lowerCase
  479. });
  480. this.rawNodes = parsed;
  481. }
  482. }
  483. module.exports = XmlParser;