XMLDocumentCB.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. // Generated by CoffeeScript 1.12.7
  2. (function() {
  3. var NodeType, WriterState, XMLAttribute, XMLCData, XMLComment, XMLDTDAttList, XMLDTDElement, XMLDTDEntity, XMLDTDNotation, XMLDeclaration, XMLDocType, XMLDocument, 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. NodeType = require('./NodeType');
  7. XMLDocument = require('./XMLDocument');
  8. XMLElement = require('./XMLElement');
  9. XMLCData = require('./XMLCData');
  10. XMLComment = require('./XMLComment');
  11. XMLRaw = require('./XMLRaw');
  12. XMLText = require('./XMLText');
  13. XMLProcessingInstruction = require('./XMLProcessingInstruction');
  14. XMLDeclaration = require('./XMLDeclaration');
  15. XMLDocType = require('./XMLDocType');
  16. XMLDTDAttList = require('./XMLDTDAttList');
  17. XMLDTDEntity = require('./XMLDTDEntity');
  18. XMLDTDElement = require('./XMLDTDElement');
  19. XMLDTDNotation = require('./XMLDTDNotation');
  20. XMLAttribute = require('./XMLAttribute');
  21. XMLStringifier = require('./XMLStringifier');
  22. XMLStringWriter = require('./XMLStringWriter');
  23. WriterState = require('./WriterState');
  24. module.exports = XMLDocumentCB = (function() {
  25. function XMLDocumentCB(options, onData, onEnd) {
  26. var writerOptions;
  27. this.name = "?xml";
  28. this.type = NodeType.Document;
  29. options || (options = {});
  30. writerOptions = {};
  31. if (!options.writer) {
  32. options.writer = new XMLStringWriter();
  33. } else if (isPlainObject(options.writer)) {
  34. writerOptions = options.writer;
  35. options.writer = new XMLStringWriter();
  36. }
  37. this.options = options;
  38. this.writer = options.writer;
  39. this.writerOptions = this.writer.filterOptions(writerOptions);
  40. this.stringify = new XMLStringifier(options);
  41. this.onDataCallback = onData || function() {};
  42. this.onEndCallback = onEnd || function() {};
  43. this.currentNode = null;
  44. this.currentLevel = -1;
  45. this.openTags = {};
  46. this.documentStarted = false;
  47. this.documentCompleted = false;
  48. this.root = null;
  49. }
  50. XMLDocumentCB.prototype.createChildNode = function(node) {
  51. var att, attName, attributes, child, i, len, ref1, ref2;
  52. switch (node.type) {
  53. case NodeType.CData:
  54. this.cdata(node.value);
  55. break;
  56. case NodeType.Comment:
  57. this.comment(node.value);
  58. break;
  59. case NodeType.Element:
  60. attributes = {};
  61. ref1 = node.attribs;
  62. for (attName in ref1) {
  63. if (!hasProp.call(ref1, attName)) continue;
  64. att = ref1[attName];
  65. attributes[attName] = att.value;
  66. }
  67. this.node(node.name, attributes);
  68. break;
  69. case NodeType.Dummy:
  70. this.dummy();
  71. break;
  72. case NodeType.Raw:
  73. this.raw(node.value);
  74. break;
  75. case NodeType.Text:
  76. this.text(node.value);
  77. break;
  78. case NodeType.ProcessingInstruction:
  79. this.instruction(node.target, node.value);
  80. break;
  81. default:
  82. throw new Error("This XML node type is not supported in a JS object: " + node.constructor.name);
  83. }
  84. ref2 = node.children;
  85. for (i = 0, len = ref2.length; i < len; i++) {
  86. child = ref2[i];
  87. this.createChildNode(child);
  88. if (child.type === NodeType.Element) {
  89. this.up();
  90. }
  91. }
  92. return this;
  93. };
  94. XMLDocumentCB.prototype.dummy = function() {
  95. return this;
  96. };
  97. XMLDocumentCB.prototype.node = function(name, attributes, text) {
  98. var ref1;
  99. if (name == null) {
  100. throw new Error("Missing node name.");
  101. }
  102. if (this.root && this.currentLevel === -1) {
  103. throw new Error("Document can only have one root node. " + this.debugInfo(name));
  104. }
  105. this.openCurrent();
  106. name = getValue(name);
  107. if (attributes == null) {
  108. attributes = {};
  109. }
  110. attributes = getValue(attributes);
  111. if (!isObject(attributes)) {
  112. ref1 = [attributes, text], text = ref1[0], attributes = ref1[1];
  113. }
  114. this.currentNode = new XMLElement(this, name, attributes);
  115. this.currentNode.children = false;
  116. this.currentLevel++;
  117. this.openTags[this.currentLevel] = this.currentNode;
  118. if (text != null) {
  119. this.text(text);
  120. }
  121. return this;
  122. };
  123. XMLDocumentCB.prototype.element = function(name, attributes, text) {
  124. var child, i, len, oldValidationFlag, ref1, root;
  125. if (this.currentNode && this.currentNode.type === NodeType.DocType) {
  126. this.dtdElement.apply(this, arguments);
  127. } else {
  128. if (Array.isArray(name) || isObject(name) || isFunction(name)) {
  129. oldValidationFlag = this.options.noValidation;
  130. this.options.noValidation = true;
  131. root = new XMLDocument(this.options).element('TEMP_ROOT');
  132. root.element(name);
  133. this.options.noValidation = oldValidationFlag;
  134. ref1 = root.children;
  135. for (i = 0, len = ref1.length; i < len; i++) {
  136. child = ref1[i];
  137. this.createChildNode(child);
  138. if (child.type === NodeType.Element) {
  139. this.up();
  140. }
  141. }
  142. } else {
  143. this.node(name, attributes, text);
  144. }
  145. }
  146. return this;
  147. };
  148. XMLDocumentCB.prototype.attribute = function(name, value) {
  149. var attName, attValue;
  150. if (!this.currentNode || this.currentNode.children) {
  151. throw new Error("att() can only be used immediately after an ele() call in callback mode. " + this.debugInfo(name));
  152. }
  153. if (name != null) {
  154. name = getValue(name);
  155. }
  156. if (isObject(name)) {
  157. for (attName in name) {
  158. if (!hasProp.call(name, attName)) continue;
  159. attValue = name[attName];
  160. this.attribute(attName, attValue);
  161. }
  162. } else {
  163. if (isFunction(value)) {
  164. value = value.apply();
  165. }
  166. if (this.options.keepNullAttributes && (value == null)) {
  167. this.currentNode.attribs[name] = new XMLAttribute(this, name, "");
  168. } else if (value != null) {
  169. this.currentNode.attribs[name] = new XMLAttribute(this, name, value);
  170. }
  171. }
  172. return this;
  173. };
  174. XMLDocumentCB.prototype.text = function(value) {
  175. var node;
  176. this.openCurrent();
  177. node = new XMLText(this, value);
  178. this.onData(this.writer.text(node, this.writerOptions, this.currentLevel + 1), this.currentLevel + 1);
  179. return this;
  180. };
  181. XMLDocumentCB.prototype.cdata = function(value) {
  182. var node;
  183. this.openCurrent();
  184. node = new XMLCData(this, value);
  185. this.onData(this.writer.cdata(node, this.writerOptions, this.currentLevel + 1), this.currentLevel + 1);
  186. return this;
  187. };
  188. XMLDocumentCB.prototype.comment = function(value) {
  189. var node;
  190. this.openCurrent();
  191. node = new XMLComment(this, value);
  192. this.onData(this.writer.comment(node, this.writerOptions, this.currentLevel + 1), this.currentLevel + 1);
  193. return this;
  194. };
  195. XMLDocumentCB.prototype.raw = function(value) {
  196. var node;
  197. this.openCurrent();
  198. node = new XMLRaw(this, value);
  199. this.onData(this.writer.raw(node, this.writerOptions, this.currentLevel + 1), this.currentLevel + 1);
  200. return this;
  201. };
  202. XMLDocumentCB.prototype.instruction = function(target, value) {
  203. var i, insTarget, insValue, len, node;
  204. this.openCurrent();
  205. if (target != null) {
  206. target = getValue(target);
  207. }
  208. if (value != null) {
  209. value = getValue(value);
  210. }
  211. if (Array.isArray(target)) {
  212. for (i = 0, len = target.length; i < len; i++) {
  213. insTarget = target[i];
  214. this.instruction(insTarget);
  215. }
  216. } else if (isObject(target)) {
  217. for (insTarget in target) {
  218. if (!hasProp.call(target, insTarget)) continue;
  219. insValue = target[insTarget];
  220. this.instruction(insTarget, insValue);
  221. }
  222. } else {
  223. if (isFunction(value)) {
  224. value = value.apply();
  225. }
  226. node = new XMLProcessingInstruction(this, target, value);
  227. this.onData(this.writer.processingInstruction(node, this.writerOptions, this.currentLevel + 1), this.currentLevel + 1);
  228. }
  229. return this;
  230. };
  231. XMLDocumentCB.prototype.declaration = function(version, encoding, standalone) {
  232. var node;
  233. this.openCurrent();
  234. if (this.documentStarted) {
  235. throw new Error("declaration() must be the first node.");
  236. }
  237. node = new XMLDeclaration(this, version, encoding, standalone);
  238. this.onData(this.writer.declaration(node, this.writerOptions, this.currentLevel + 1), this.currentLevel + 1);
  239. return this;
  240. };
  241. XMLDocumentCB.prototype.doctype = function(root, pubID, sysID) {
  242. this.openCurrent();
  243. if (root == null) {
  244. throw new Error("Missing root node name.");
  245. }
  246. if (this.root) {
  247. throw new Error("dtd() must come before the root node.");
  248. }
  249. this.currentNode = new XMLDocType(this, pubID, sysID);
  250. this.currentNode.rootNodeName = root;
  251. this.currentNode.children = false;
  252. this.currentLevel++;
  253. this.openTags[this.currentLevel] = this.currentNode;
  254. return this;
  255. };
  256. XMLDocumentCB.prototype.dtdElement = function(name, value) {
  257. var node;
  258. this.openCurrent();
  259. node = new XMLDTDElement(this, name, value);
  260. this.onData(this.writer.dtdElement(node, this.writerOptions, this.currentLevel + 1), this.currentLevel + 1);
  261. return this;
  262. };
  263. XMLDocumentCB.prototype.attList = function(elementName, attributeName, attributeType, defaultValueType, defaultValue) {
  264. var node;
  265. this.openCurrent();
  266. node = new XMLDTDAttList(this, elementName, attributeName, attributeType, defaultValueType, defaultValue);
  267. this.onData(this.writer.dtdAttList(node, this.writerOptions, this.currentLevel + 1), this.currentLevel + 1);
  268. return this;
  269. };
  270. XMLDocumentCB.prototype.entity = function(name, value) {
  271. var node;
  272. this.openCurrent();
  273. node = new XMLDTDEntity(this, false, name, value);
  274. this.onData(this.writer.dtdEntity(node, this.writerOptions, this.currentLevel + 1), this.currentLevel + 1);
  275. return this;
  276. };
  277. XMLDocumentCB.prototype.pEntity = function(name, value) {
  278. var node;
  279. this.openCurrent();
  280. node = new XMLDTDEntity(this, true, name, value);
  281. this.onData(this.writer.dtdEntity(node, this.writerOptions, this.currentLevel + 1), this.currentLevel + 1);
  282. return this;
  283. };
  284. XMLDocumentCB.prototype.notation = function(name, value) {
  285. var node;
  286. this.openCurrent();
  287. node = new XMLDTDNotation(this, name, value);
  288. this.onData(this.writer.dtdNotation(node, this.writerOptions, this.currentLevel + 1), this.currentLevel + 1);
  289. return this;
  290. };
  291. XMLDocumentCB.prototype.up = function() {
  292. if (this.currentLevel < 0) {
  293. throw new Error("The document node has no parent.");
  294. }
  295. if (this.currentNode) {
  296. if (this.currentNode.children) {
  297. this.closeNode(this.currentNode);
  298. } else {
  299. this.openNode(this.currentNode);
  300. }
  301. this.currentNode = null;
  302. } else {
  303. this.closeNode(this.openTags[this.currentLevel]);
  304. }
  305. delete this.openTags[this.currentLevel];
  306. this.currentLevel--;
  307. return this;
  308. };
  309. XMLDocumentCB.prototype.end = function() {
  310. while (this.currentLevel >= 0) {
  311. this.up();
  312. }
  313. return this.onEnd();
  314. };
  315. XMLDocumentCB.prototype.openCurrent = function() {
  316. if (this.currentNode) {
  317. this.currentNode.children = true;
  318. return this.openNode(this.currentNode);
  319. }
  320. };
  321. XMLDocumentCB.prototype.openNode = function(node) {
  322. var att, chunk, name, ref1;
  323. if (!node.isOpen) {
  324. if (!this.root && this.currentLevel === 0 && node.type === NodeType.Element) {
  325. this.root = node;
  326. }
  327. chunk = '';
  328. if (node.type === NodeType.Element) {
  329. this.writerOptions.state = WriterState.OpenTag;
  330. chunk = this.writer.indent(node, this.writerOptions, this.currentLevel) + '<' + node.name;
  331. ref1 = node.attribs;
  332. for (name in ref1) {
  333. if (!hasProp.call(ref1, name)) continue;
  334. att = ref1[name];
  335. chunk += this.writer.attribute(att, this.writerOptions, this.currentLevel);
  336. }
  337. chunk += (node.children ? '>' : '/>') + this.writer.endline(node, this.writerOptions, this.currentLevel);
  338. this.writerOptions.state = WriterState.InsideTag;
  339. } else {
  340. this.writerOptions.state = WriterState.OpenTag;
  341. chunk = this.writer.indent(node, this.writerOptions, this.currentLevel) + '<!DOCTYPE ' + node.rootNodeName;
  342. if (node.pubID && node.sysID) {
  343. chunk += ' PUBLIC "' + node.pubID + '" "' + node.sysID + '"';
  344. } else if (node.sysID) {
  345. chunk += ' SYSTEM "' + node.sysID + '"';
  346. }
  347. if (node.children) {
  348. chunk += ' [';
  349. this.writerOptions.state = WriterState.InsideTag;
  350. } else {
  351. this.writerOptions.state = WriterState.CloseTag;
  352. chunk += '>';
  353. }
  354. chunk += this.writer.endline(node, this.writerOptions, this.currentLevel);
  355. }
  356. this.onData(chunk, this.currentLevel);
  357. return node.isOpen = true;
  358. }
  359. };
  360. XMLDocumentCB.prototype.closeNode = function(node) {
  361. var chunk;
  362. if (!node.isClosed) {
  363. chunk = '';
  364. this.writerOptions.state = WriterState.CloseTag;
  365. if (node.type === NodeType.Element) {
  366. chunk = this.writer.indent(node, this.writerOptions, this.currentLevel) + '</' + node.name + '>' + this.writer.endline(node, this.writerOptions, this.currentLevel);
  367. } else {
  368. chunk = this.writer.indent(node, this.writerOptions, this.currentLevel) + ']>' + this.writer.endline(node, this.writerOptions, this.currentLevel);
  369. }
  370. this.writerOptions.state = WriterState.None;
  371. this.onData(chunk, this.currentLevel);
  372. return node.isClosed = true;
  373. }
  374. };
  375. XMLDocumentCB.prototype.onData = function(chunk, level) {
  376. this.documentStarted = true;
  377. return this.onDataCallback(chunk, level + 1);
  378. };
  379. XMLDocumentCB.prototype.onEnd = function() {
  380. this.documentCompleted = true;
  381. return this.onEndCallback();
  382. };
  383. XMLDocumentCB.prototype.debugInfo = function(name) {
  384. if (name == null) {
  385. return "";
  386. } else {
  387. return "node: <" + name + ">";
  388. }
  389. };
  390. XMLDocumentCB.prototype.ele = function() {
  391. return this.element.apply(this, arguments);
  392. };
  393. XMLDocumentCB.prototype.nod = function(name, attributes, text) {
  394. return this.node(name, attributes, text);
  395. };
  396. XMLDocumentCB.prototype.txt = function(value) {
  397. return this.text(value);
  398. };
  399. XMLDocumentCB.prototype.dat = function(value) {
  400. return this.cdata(value);
  401. };
  402. XMLDocumentCB.prototype.com = function(value) {
  403. return this.comment(value);
  404. };
  405. XMLDocumentCB.prototype.ins = function(target, value) {
  406. return this.instruction(target, value);
  407. };
  408. XMLDocumentCB.prototype.dec = function(version, encoding, standalone) {
  409. return this.declaration(version, encoding, standalone);
  410. };
  411. XMLDocumentCB.prototype.dtd = function(root, pubID, sysID) {
  412. return this.doctype(root, pubID, sysID);
  413. };
  414. XMLDocumentCB.prototype.e = function(name, attributes, text) {
  415. return this.element(name, attributes, text);
  416. };
  417. XMLDocumentCB.prototype.n = function(name, attributes, text) {
  418. return this.node(name, attributes, text);
  419. };
  420. XMLDocumentCB.prototype.t = function(value) {
  421. return this.text(value);
  422. };
  423. XMLDocumentCB.prototype.d = function(value) {
  424. return this.cdata(value);
  425. };
  426. XMLDocumentCB.prototype.c = function(value) {
  427. return this.comment(value);
  428. };
  429. XMLDocumentCB.prototype.r = function(value) {
  430. return this.raw(value);
  431. };
  432. XMLDocumentCB.prototype.i = function(target, value) {
  433. return this.instruction(target, value);
  434. };
  435. XMLDocumentCB.prototype.att = function() {
  436. if (this.currentNode && this.currentNode.type === NodeType.DocType) {
  437. return this.attList.apply(this, arguments);
  438. } else {
  439. return this.attribute.apply(this, arguments);
  440. }
  441. };
  442. XMLDocumentCB.prototype.a = function() {
  443. if (this.currentNode && this.currentNode.type === NodeType.DocType) {
  444. return this.attList.apply(this, arguments);
  445. } else {
  446. return this.attribute.apply(this, arguments);
  447. }
  448. };
  449. XMLDocumentCB.prototype.ent = function(name, value) {
  450. return this.entity(name, value);
  451. };
  452. XMLDocumentCB.prototype.pent = function(name, value) {
  453. return this.pEntity(name, value);
  454. };
  455. XMLDocumentCB.prototype.not = function(name, value) {
  456. return this.notation(name, value);
  457. };
  458. return XMLDocumentCB;
  459. })();
  460. }).call(this);