XMLDocumentCB.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. // Generated by CoffeeScript 1.12.7
  2. (function() {
  3. var XMLAttribute, XMLCData, XMLComment, XMLDTDAttList, XMLDTDElement, XMLDTDEntity, XMLDTDNotation, XMLDeclaration, XMLDocType, XMLDocumentCB, XMLElement, XMLProcessingInstruction, XMLRaw, XMLStringWriter, XMLStringifier, XMLText, getValue, isFunction, isObject, isPlainObject, ref,
  4. hasProp = {}.hasOwnProperty;
  5. ref = require('./Utility'), isObject = ref.isObject, isFunction = ref.isFunction, isPlainObject = ref.isPlainObject, getValue = ref.getValue;
  6. XMLElement = require('./XMLElement');
  7. XMLCData = require('./XMLCData');
  8. XMLComment = require('./XMLComment');
  9. XMLRaw = require('./XMLRaw');
  10. XMLText = require('./XMLText');
  11. XMLProcessingInstruction = require('./XMLProcessingInstruction');
  12. XMLDeclaration = require('./XMLDeclaration');
  13. XMLDocType = require('./XMLDocType');
  14. XMLDTDAttList = require('./XMLDTDAttList');
  15. XMLDTDEntity = require('./XMLDTDEntity');
  16. XMLDTDElement = require('./XMLDTDElement');
  17. XMLDTDNotation = require('./XMLDTDNotation');
  18. XMLAttribute = require('./XMLAttribute');
  19. XMLStringifier = require('./XMLStringifier');
  20. XMLStringWriter = require('./XMLStringWriter');
  21. module.exports = XMLDocumentCB = (function() {
  22. function XMLDocumentCB(options, onData, onEnd) {
  23. var writerOptions;
  24. this.name = "?xml";
  25. options || (options = {});
  26. if (!options.writer) {
  27. options.writer = new XMLStringWriter(options);
  28. } else if (isPlainObject(options.writer)) {
  29. writerOptions = options.writer;
  30. options.writer = new XMLStringWriter(writerOptions);
  31. }
  32. this.options = options;
  33. this.writer = options.writer;
  34. this.stringify = new XMLStringifier(options);
  35. this.onDataCallback = onData || function() {};
  36. this.onEndCallback = onEnd || function() {};
  37. this.currentNode = null;
  38. this.currentLevel = -1;
  39. this.openTags = {};
  40. this.documentStarted = false;
  41. this.documentCompleted = false;
  42. this.root = null;
  43. }
  44. XMLDocumentCB.prototype.node = function(name, attributes, text) {
  45. var ref1, ref2;
  46. if (name == null) {
  47. throw new Error("Missing node name.");
  48. }
  49. if (this.root && this.currentLevel === -1) {
  50. throw new Error("Document can only have one root node. " + this.debugInfo(name));
  51. }
  52. this.openCurrent();
  53. name = getValue(name);
  54. if (attributes === null && (text == null)) {
  55. ref1 = [{}, null], attributes = ref1[0], text = ref1[1];
  56. }
  57. if (attributes == null) {
  58. attributes = {};
  59. }
  60. attributes = getValue(attributes);
  61. if (!isObject(attributes)) {
  62. ref2 = [attributes, text], text = ref2[0], attributes = ref2[1];
  63. }
  64. this.currentNode = new XMLElement(this, name, attributes);
  65. this.currentNode.children = false;
  66. this.currentLevel++;
  67. this.openTags[this.currentLevel] = this.currentNode;
  68. if (text != null) {
  69. this.text(text);
  70. }
  71. return this;
  72. };
  73. XMLDocumentCB.prototype.element = function(name, attributes, text) {
  74. if (this.currentNode && this.currentNode instanceof XMLDocType) {
  75. return this.dtdElement.apply(this, arguments);
  76. } else {
  77. return this.node(name, attributes, text);
  78. }
  79. };
  80. XMLDocumentCB.prototype.attribute = function(name, value) {
  81. var attName, attValue;
  82. if (!this.currentNode || this.currentNode.children) {
  83. throw new Error("att() can only be used immediately after an ele() call in callback mode. " + this.debugInfo(name));
  84. }
  85. if (name != null) {
  86. name = getValue(name);
  87. }
  88. if (isObject(name)) {
  89. for (attName in name) {
  90. if (!hasProp.call(name, attName)) continue;
  91. attValue = name[attName];
  92. this.attribute(attName, attValue);
  93. }
  94. } else {
  95. if (isFunction(value)) {
  96. value = value.apply();
  97. }
  98. if (!this.options.skipNullAttributes || (value != null)) {
  99. this.currentNode.attributes[name] = new XMLAttribute(this, name, value);
  100. }
  101. }
  102. return this;
  103. };
  104. XMLDocumentCB.prototype.text = function(value) {
  105. var node;
  106. this.openCurrent();
  107. node = new XMLText(this, value);
  108. this.onData(this.writer.text(node, this.currentLevel + 1), this.currentLevel + 1);
  109. return this;
  110. };
  111. XMLDocumentCB.prototype.cdata = function(value) {
  112. var node;
  113. this.openCurrent();
  114. node = new XMLCData(this, value);
  115. this.onData(this.writer.cdata(node, this.currentLevel + 1), this.currentLevel + 1);
  116. return this;
  117. };
  118. XMLDocumentCB.prototype.comment = function(value) {
  119. var node;
  120. this.openCurrent();
  121. node = new XMLComment(this, value);
  122. this.onData(this.writer.comment(node, this.currentLevel + 1), this.currentLevel + 1);
  123. return this;
  124. };
  125. XMLDocumentCB.prototype.raw = function(value) {
  126. var node;
  127. this.openCurrent();
  128. node = new XMLRaw(this, value);
  129. this.onData(this.writer.raw(node, this.currentLevel + 1), this.currentLevel + 1);
  130. return this;
  131. };
  132. XMLDocumentCB.prototype.instruction = function(target, value) {
  133. var i, insTarget, insValue, len, node;
  134. this.openCurrent();
  135. if (target != null) {
  136. target = getValue(target);
  137. }
  138. if (value != null) {
  139. value = getValue(value);
  140. }
  141. if (Array.isArray(target)) {
  142. for (i = 0, len = target.length; i < len; i++) {
  143. insTarget = target[i];
  144. this.instruction(insTarget);
  145. }
  146. } else if (isObject(target)) {
  147. for (insTarget in target) {
  148. if (!hasProp.call(target, insTarget)) continue;
  149. insValue = target[insTarget];
  150. this.instruction(insTarget, insValue);
  151. }
  152. } else {
  153. if (isFunction(value)) {
  154. value = value.apply();
  155. }
  156. node = new XMLProcessingInstruction(this, target, value);
  157. this.onData(this.writer.processingInstruction(node, this.currentLevel + 1), this.currentLevel + 1);
  158. }
  159. return this;
  160. };
  161. XMLDocumentCB.prototype.declaration = function(version, encoding, standalone) {
  162. var node;
  163. this.openCurrent();
  164. if (this.documentStarted) {
  165. throw new Error("declaration() must be the first node.");
  166. }
  167. node = new XMLDeclaration(this, version, encoding, standalone);
  168. this.onData(this.writer.declaration(node, this.currentLevel + 1), this.currentLevel + 1);
  169. return this;
  170. };
  171. XMLDocumentCB.prototype.doctype = function(root, pubID, sysID) {
  172. this.openCurrent();
  173. if (root == null) {
  174. throw new Error("Missing root node name.");
  175. }
  176. if (this.root) {
  177. throw new Error("dtd() must come before the root node.");
  178. }
  179. this.currentNode = new XMLDocType(this, pubID, sysID);
  180. this.currentNode.rootNodeName = root;
  181. this.currentNode.children = false;
  182. this.currentLevel++;
  183. this.openTags[this.currentLevel] = this.currentNode;
  184. return this;
  185. };
  186. XMLDocumentCB.prototype.dtdElement = function(name, value) {
  187. var node;
  188. this.openCurrent();
  189. node = new XMLDTDElement(this, name, value);
  190. this.onData(this.writer.dtdElement(node, this.currentLevel + 1), this.currentLevel + 1);
  191. return this;
  192. };
  193. XMLDocumentCB.prototype.attList = function(elementName, attributeName, attributeType, defaultValueType, defaultValue) {
  194. var node;
  195. this.openCurrent();
  196. node = new XMLDTDAttList(this, elementName, attributeName, attributeType, defaultValueType, defaultValue);
  197. this.onData(this.writer.dtdAttList(node, this.currentLevel + 1), this.currentLevel + 1);
  198. return this;
  199. };
  200. XMLDocumentCB.prototype.entity = function(name, value) {
  201. var node;
  202. this.openCurrent();
  203. node = new XMLDTDEntity(this, false, name, value);
  204. this.onData(this.writer.dtdEntity(node, this.currentLevel + 1), this.currentLevel + 1);
  205. return this;
  206. };
  207. XMLDocumentCB.prototype.pEntity = function(name, value) {
  208. var node;
  209. this.openCurrent();
  210. node = new XMLDTDEntity(this, true, name, value);
  211. this.onData(this.writer.dtdEntity(node, this.currentLevel + 1), this.currentLevel + 1);
  212. return this;
  213. };
  214. XMLDocumentCB.prototype.notation = function(name, value) {
  215. var node;
  216. this.openCurrent();
  217. node = new XMLDTDNotation(this, name, value);
  218. this.onData(this.writer.dtdNotation(node, this.currentLevel + 1), this.currentLevel + 1);
  219. return this;
  220. };
  221. XMLDocumentCB.prototype.up = function() {
  222. if (this.currentLevel < 0) {
  223. throw new Error("The document node has no parent.");
  224. }
  225. if (this.currentNode) {
  226. if (this.currentNode.children) {
  227. this.closeNode(this.currentNode);
  228. } else {
  229. this.openNode(this.currentNode);
  230. }
  231. this.currentNode = null;
  232. } else {
  233. this.closeNode(this.openTags[this.currentLevel]);
  234. }
  235. delete this.openTags[this.currentLevel];
  236. this.currentLevel--;
  237. return this;
  238. };
  239. XMLDocumentCB.prototype.end = function() {
  240. while (this.currentLevel >= 0) {
  241. this.up();
  242. }
  243. return this.onEnd();
  244. };
  245. XMLDocumentCB.prototype.openCurrent = function() {
  246. if (this.currentNode) {
  247. this.currentNode.children = true;
  248. return this.openNode(this.currentNode);
  249. }
  250. };
  251. XMLDocumentCB.prototype.openNode = function(node) {
  252. if (!node.isOpen) {
  253. if (!this.root && this.currentLevel === 0 && node instanceof XMLElement) {
  254. this.root = node;
  255. }
  256. this.onData(this.writer.openNode(node, this.currentLevel), this.currentLevel);
  257. return node.isOpen = true;
  258. }
  259. };
  260. XMLDocumentCB.prototype.closeNode = function(node) {
  261. if (!node.isClosed) {
  262. this.onData(this.writer.closeNode(node, this.currentLevel), this.currentLevel);
  263. return node.isClosed = true;
  264. }
  265. };
  266. XMLDocumentCB.prototype.onData = function(chunk, level) {
  267. this.documentStarted = true;
  268. return this.onDataCallback(chunk, level + 1);
  269. };
  270. XMLDocumentCB.prototype.onEnd = function() {
  271. this.documentCompleted = true;
  272. return this.onEndCallback();
  273. };
  274. XMLDocumentCB.prototype.debugInfo = function(name) {
  275. if (name == null) {
  276. return "";
  277. } else {
  278. return "node: <" + name + ">";
  279. }
  280. };
  281. XMLDocumentCB.prototype.ele = function() {
  282. return this.element.apply(this, arguments);
  283. };
  284. XMLDocumentCB.prototype.nod = function(name, attributes, text) {
  285. return this.node(name, attributes, text);
  286. };
  287. XMLDocumentCB.prototype.txt = function(value) {
  288. return this.text(value);
  289. };
  290. XMLDocumentCB.prototype.dat = function(value) {
  291. return this.cdata(value);
  292. };
  293. XMLDocumentCB.prototype.com = function(value) {
  294. return this.comment(value);
  295. };
  296. XMLDocumentCB.prototype.ins = function(target, value) {
  297. return this.instruction(target, value);
  298. };
  299. XMLDocumentCB.prototype.dec = function(version, encoding, standalone) {
  300. return this.declaration(version, encoding, standalone);
  301. };
  302. XMLDocumentCB.prototype.dtd = function(root, pubID, sysID) {
  303. return this.doctype(root, pubID, sysID);
  304. };
  305. XMLDocumentCB.prototype.e = function(name, attributes, text) {
  306. return this.element(name, attributes, text);
  307. };
  308. XMLDocumentCB.prototype.n = function(name, attributes, text) {
  309. return this.node(name, attributes, text);
  310. };
  311. XMLDocumentCB.prototype.t = function(value) {
  312. return this.text(value);
  313. };
  314. XMLDocumentCB.prototype.d = function(value) {
  315. return this.cdata(value);
  316. };
  317. XMLDocumentCB.prototype.c = function(value) {
  318. return this.comment(value);
  319. };
  320. XMLDocumentCB.prototype.r = function(value) {
  321. return this.raw(value);
  322. };
  323. XMLDocumentCB.prototype.i = function(target, value) {
  324. return this.instruction(target, value);
  325. };
  326. XMLDocumentCB.prototype.att = function() {
  327. if (this.currentNode && this.currentNode instanceof XMLDocType) {
  328. return this.attList.apply(this, arguments);
  329. } else {
  330. return this.attribute.apply(this, arguments);
  331. }
  332. };
  333. XMLDocumentCB.prototype.a = function() {
  334. if (this.currentNode && this.currentNode instanceof XMLDocType) {
  335. return this.attList.apply(this, arguments);
  336. } else {
  337. return this.attribute.apply(this, arguments);
  338. }
  339. };
  340. XMLDocumentCB.prototype.ent = function(name, value) {
  341. return this.entity(name, value);
  342. };
  343. XMLDocumentCB.prototype.pent = function(name, value) {
  344. return this.pEntity(name, value);
  345. };
  346. XMLDocumentCB.prototype.not = function(name, value) {
  347. return this.notation(name, value);
  348. };
  349. return XMLDocumentCB;
  350. })();
  351. }).call(this);