Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

xml-helpers.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /**
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. /**
  18. * contains XML utility functions, some of which are specific to elementtree
  19. */
  20. var fs = require('fs-extra');
  21. var path = require('path');
  22. var _ = require('underscore');
  23. var et = require('elementtree');
  24. var stripBom = require('strip-bom');
  25. /* eslint-disable no-useless-escape */
  26. var ROOT = /^\/([^\/]*)/;
  27. var ABSOLUTE = /^\/([^\/]*)\/(.*)/;
  28. /* eslint-enable no-useless-escape */
  29. module.exports = {
  30. // compare two et.XML nodes, see if they match
  31. // compares tagName, text, attributes and children (recursively)
  32. equalNodes: function (one, two) {
  33. if (one.tag !== two.tag) {
  34. return false;
  35. } else if (one.text.trim() !== two.text.trim()) {
  36. return false;
  37. } else if (one._children.length !== two._children.length) {
  38. return false;
  39. }
  40. if (!attribMatch(one, two)) return false;
  41. for (var i = 0; i < one._children.length; i++) {
  42. if (!module.exports.equalNodes(one._children[i], two._children[i])) {
  43. return false;
  44. }
  45. }
  46. return true;
  47. },
  48. // adds node to doc at selector, creating parent if it doesn't exist
  49. graftXML: function (doc, nodes, selector, after) {
  50. var parent = module.exports.resolveParent(doc, selector);
  51. if (!parent) {
  52. // Try to create the parent recursively if necessary
  53. try {
  54. var parentToCreate = et.XML('<' + path.basename(selector) + '/>');
  55. var parentSelector = path.dirname(selector);
  56. this.graftXML(doc, [parentToCreate], parentSelector);
  57. } catch (e) {
  58. return false;
  59. }
  60. parent = module.exports.resolveParent(doc, selector);
  61. if (!parent) return false;
  62. }
  63. nodes.forEach(function (node) {
  64. // check if child is unique first
  65. if (uniqueChild(node, parent)) {
  66. var children = parent.getchildren();
  67. var insertIdx = after ? findInsertIdx(children, after) : children.length;
  68. // TODO: replace with parent.insert after the bug in ElementTree is fixed
  69. parent.getchildren().splice(insertIdx, 0, node);
  70. }
  71. });
  72. return true;
  73. },
  74. // adds new attributes to doc at selector
  75. // Will only merge if attribute has not been modified already or --force is used
  76. graftXMLMerge: function (doc, nodes, selector, xml) {
  77. var target = module.exports.resolveParent(doc, selector);
  78. if (!target) return false;
  79. // saves the attributes of the original xml before making changes
  80. xml.oldAttrib = _.extend({}, target.attrib);
  81. nodes.forEach(function (node) {
  82. var attributes = node.attrib;
  83. for (var attribute in attributes) {
  84. target.attrib[attribute] = node.attrib[attribute];
  85. }
  86. });
  87. return true;
  88. },
  89. // overwrite all attributes to doc at selector with new attributes
  90. // Will only overwrite if attribute has not been modified already or --force is used
  91. graftXMLOverwrite: function (doc, nodes, selector, xml) {
  92. var target = module.exports.resolveParent(doc, selector);
  93. if (!target) return false;
  94. // saves the attributes of the original xml before making changes
  95. xml.oldAttrib = _.extend({}, target.attrib);
  96. // remove old attributes from target
  97. var targetAttributes = target.attrib;
  98. for (var targetAttribute in targetAttributes) {
  99. delete targetAttributes[targetAttribute];
  100. }
  101. // add new attributes to target
  102. nodes.forEach(function (node) {
  103. var attributes = node.attrib;
  104. for (var attribute in attributes) {
  105. target.attrib[attribute] = node.attrib[attribute];
  106. }
  107. });
  108. return true;
  109. },
  110. // removes node from doc at selector
  111. pruneXML: function (doc, nodes, selector) {
  112. var parent = module.exports.resolveParent(doc, selector);
  113. if (!parent) return false;
  114. nodes.forEach(function (node) {
  115. var matchingKid = findChild(node, parent);
  116. if (matchingKid !== undefined) {
  117. // stupid elementtree takes an index argument it doesn't use
  118. // and does not conform to the python lib
  119. parent.remove(matchingKid);
  120. }
  121. });
  122. return true;
  123. },
  124. // restores attributes from doc at selector
  125. pruneXMLRestore: function (doc, selector, xml) {
  126. var target = module.exports.resolveParent(doc, selector);
  127. if (!target) return false;
  128. if (xml.oldAttrib) {
  129. target.attrib = _.extend({}, xml.oldAttrib);
  130. }
  131. return true;
  132. },
  133. pruneXMLRemove: function (doc, selector, nodes) {
  134. var target = module.exports.resolveParent(doc, selector);
  135. if (!target) return false;
  136. nodes.forEach(function (node) {
  137. var attributes = node.attrib;
  138. for (var attribute in attributes) {
  139. if (target.attrib[attribute]) {
  140. delete target.attrib[attribute];
  141. }
  142. }
  143. });
  144. return true;
  145. },
  146. parseElementtreeSync: function (filename) {
  147. var contents = stripBom(fs.readFileSync(filename, 'utf-8'));
  148. return new et.ElementTree(et.XML(contents));
  149. },
  150. resolveParent: function (doc, selector) {
  151. var parent, tagName, subSelector;
  152. // handle absolute selector (which elementtree doesn't like)
  153. if (ROOT.test(selector)) {
  154. tagName = selector.match(ROOT)[1];
  155. // test for wildcard "any-tag" root selector
  156. if (tagName === '*' || tagName === doc._root.tag) {
  157. parent = doc._root;
  158. // could be an absolute path, but not selecting the root
  159. if (ABSOLUTE.test(selector)) {
  160. subSelector = selector.match(ABSOLUTE)[2];
  161. parent = parent.find(subSelector);
  162. }
  163. } else {
  164. return false;
  165. }
  166. } else {
  167. parent = doc.find(selector);
  168. }
  169. return parent;
  170. }
  171. };
  172. function findChild (node, parent) {
  173. const matches = parent.findall(node.tag);
  174. return matches.find(m => module.exports.equalNodes(node, m));
  175. }
  176. function uniqueChild (node, parent) {
  177. return !findChild(node, parent);
  178. }
  179. // Find the index at which to insert an entry. After is a ;-separated priority list
  180. // of tags after which the insertion should be made. E.g. If we need to
  181. // insert an element C, and the rule is that the order of children has to be
  182. // As, Bs, Cs. After will be equal to "C;B;A".
  183. function findInsertIdx (children, after) {
  184. var childrenTags = children.map(function (child) { return child.tag; });
  185. var afters = after.split(';');
  186. var afterIndexes = afters.map(function (current) { return childrenTags.lastIndexOf(current); });
  187. var foundIndex = _.find(afterIndexes, function (index) { return index !== -1; });
  188. // add to the beginning if no matching nodes are found
  189. return typeof foundIndex === 'undefined' ? 0 : foundIndex + 1;
  190. }
  191. var BLACKLIST = ['platform', 'feature', 'plugin', 'engine'];
  192. var SINGLETONS = ['content', 'author', 'name'];
  193. function mergeXml (src, dest, platform, clobber) {
  194. // Do nothing for blacklisted tags.
  195. if (BLACKLIST.includes(src.tag)) return;
  196. // Handle attributes
  197. Object.getOwnPropertyNames(src.attrib).forEach(function (attribute) {
  198. if (clobber || !dest.attrib[attribute]) {
  199. dest.attrib[attribute] = src.attrib[attribute];
  200. }
  201. });
  202. // Handle text
  203. if (src.text && (clobber || !dest.text)) {
  204. dest.text = src.text;
  205. }
  206. // Handle children
  207. src.getchildren().forEach(mergeChild);
  208. // Handle platform
  209. if (platform) {
  210. src.findall('platform[@name="' + platform + '"]').forEach(function (platformElement) {
  211. platformElement.getchildren().forEach(mergeChild);
  212. });
  213. }
  214. // Handle duplicate preference tags (by name attribute)
  215. removeDuplicatePreferences(dest);
  216. function mergeChild (srcChild) {
  217. var srcTag = srcChild.tag;
  218. var destChild = new et.Element(srcTag);
  219. var foundChild;
  220. var query = srcTag + '';
  221. var shouldMerge = true;
  222. if (BLACKLIST.includes(srcTag)) return;
  223. if (SINGLETONS.includes(srcTag)) {
  224. foundChild = dest.find(query);
  225. if (foundChild) {
  226. destChild = foundChild;
  227. dest.remove(destChild);
  228. }
  229. } else {
  230. // Check for an exact match and if you find one don't add
  231. var mergeCandidates = dest.findall(query)
  232. .filter(function (foundChild) {
  233. return foundChild && textMatch(srcChild, foundChild) && attribMatch(srcChild, foundChild);
  234. });
  235. if (mergeCandidates.length > 0) {
  236. destChild = mergeCandidates[0];
  237. dest.remove(destChild);
  238. shouldMerge = false;
  239. }
  240. }
  241. mergeXml(srcChild, destChild, platform, clobber && shouldMerge);
  242. dest.append(destChild);
  243. }
  244. function removeDuplicatePreferences (xml) {
  245. // reduce preference tags to a hashtable to remove dupes
  246. var prefHash = xml.findall('preference[@name][@value]').reduce(function (previousValue, currentValue) {
  247. previousValue[ currentValue.attrib.name ] = currentValue.attrib.value;
  248. return previousValue;
  249. }, {});
  250. // remove all preferences
  251. xml.findall('preference[@name][@value]').forEach(function (pref) {
  252. xml.remove(pref);
  253. });
  254. // write new preferences
  255. Object.keys(prefHash).forEach(function (key) {
  256. var element = et.SubElement(xml, 'preference');
  257. element.set('name', key);
  258. element.set('value', this[key]);
  259. }, prefHash);
  260. }
  261. }
  262. // Expose for testing.
  263. module.exports.mergeXml = mergeXml;
  264. function textMatch (elm1, elm2) {
  265. var text1 = elm1.text ? elm1.text.replace(/\s+/, '') : '';
  266. var text2 = elm2.text ? elm2.text.replace(/\s+/, '') : '';
  267. return (text1 === '' || text1 === text2);
  268. }
  269. function attribMatch (one, two) {
  270. var oneAttribKeys = Object.keys(one.attrib);
  271. var twoAttribKeys = Object.keys(two.attrib);
  272. if (oneAttribKeys.length !== twoAttribKeys.length) {
  273. return false;
  274. }
  275. for (var i = 0; i < oneAttribKeys.length; i++) {
  276. var attribName = oneAttribKeys[i];
  277. if (one.attrib[attribName] !== two.attrib[attribName]) {
  278. return false;
  279. }
  280. }
  281. return true;
  282. }