No Description

XMLDocumentCB.js 12KB

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