XmlParser.js 14 KB

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