CCSAXParser.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2008-2010 Ricardo Quesada
  4. Copyright (c) 2011 Zynga Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. /**
  23. * a SAX Parser
  24. * @class
  25. * @extends cc.Class
  26. */
  27. cc.SAXParser = cc.Class.extend(/** @lends cc.SAXParser# */{
  28. xmlDoc: null,
  29. _parser: null,
  30. _xmlDict: null,
  31. _isSupportDOMParser: null,
  32. ctor: function () {
  33. this._xmlDict = {};
  34. if (window.DOMParser) {
  35. this._isSupportDOMParser = true;
  36. this._parser = new DOMParser();
  37. } else {
  38. this._isSupportDOMParser = false;
  39. }
  40. },
  41. /**
  42. * parse a xml from a string (xmlhttpObj.responseText)
  43. * @param {String} textxml plist xml contents
  44. * @return {Array} plist object array
  45. */
  46. parse: function (textxml) {
  47. var path = textxml;
  48. textxml = this.getList(textxml);
  49. var xmlDoc = this._parserXML(textxml, path);
  50. var plist = xmlDoc.documentElement;
  51. if (plist.tagName != 'plist')
  52. throw "cocos2d: " + path + " is not a plist file or you forgot to preload the plist file";
  53. // Get first real node
  54. var node = null;
  55. for (var i = 0, len = plist.childNodes.length; i < len; i++) {
  56. node = plist.childNodes[i];
  57. if (node.nodeType == 1)
  58. break;
  59. }
  60. xmlDoc = null;
  61. return this._parseNode(node);
  62. },
  63. /**
  64. * parse a tilemap xml from a string (xmlhttpObj.responseText)
  65. * @param {String} textxml tilemap xml content
  66. * @return {Document} xml document
  67. */
  68. tmxParse: function (textxml, isXMLString) {
  69. if ((isXMLString == null) || (isXMLString === false))
  70. textxml = this.getList(textxml);
  71. return this._parserXML(textxml);
  72. },
  73. _parserXML: function (textxml, path) {
  74. // get a reference to the requested corresponding xml file
  75. var xmlDoc;
  76. if (this._isSupportDOMParser) {
  77. xmlDoc = this._parser.parseFromString(textxml, "text/xml");
  78. } else {
  79. // Internet Explorer (untested!)
  80. xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  81. xmlDoc.async = "false";
  82. xmlDoc.loadXML(textxml);
  83. }
  84. if (xmlDoc == null)
  85. cc.log("cocos2d:xml " + path + " not found!");
  86. return xmlDoc;
  87. },
  88. _parseNode: function (node) {
  89. var data = null;
  90. switch (node.tagName) {
  91. case 'dict':
  92. data = this._parseDict(node);
  93. break;
  94. case 'array':
  95. data = this._parseArray(node);
  96. break;
  97. case 'string':
  98. if (node.childNodes.length == 1)
  99. data = node.firstChild.nodeValue;
  100. else {
  101. //handle Firefox's 4KB nodeValue limit
  102. data = "";
  103. for (var i = 0; i < node.childNodes.length; i++)
  104. data += node.childNodes[i].nodeValue;
  105. }
  106. break;
  107. case 'false':
  108. data = false;
  109. break;
  110. case 'true':
  111. data = true;
  112. break;
  113. case 'real':
  114. data = parseFloat(node.firstChild.nodeValue);
  115. break;
  116. case 'integer':
  117. data = parseInt(node.firstChild.nodeValue, 10);
  118. break;
  119. }
  120. return data;
  121. },
  122. _parseArray: function (node) {
  123. var data = [];
  124. for (var i = 0, len = node.childNodes.length; i < len; i++) {
  125. var child = node.childNodes[i];
  126. if (child.nodeType != 1)
  127. continue;
  128. data.push(this._parseNode(child));
  129. }
  130. return data;
  131. },
  132. _parseDict: function (node) {
  133. var data = {};
  134. var key = null;
  135. for (var i = 0, len = node.childNodes.length; i < len; i++) {
  136. var child = node.childNodes[i];
  137. if (child.nodeType != 1)
  138. continue;
  139. // Grab the key, next noe should be the value
  140. if (child.tagName == 'key')
  141. key = child.firstChild.nodeValue;
  142. else
  143. data[key] = this._parseNode(child); // Parse the value node
  144. }
  145. return data;
  146. },
  147. /**
  148. * Preload plist file
  149. * @param {String} filePath
  150. */
  151. preloadPlist: function (filePath, selector, target) {
  152. var fullfilePath = cc.FileUtils.getInstance().fullPathForFilename(filePath);
  153. if (window.XMLHttpRequest) {
  154. var xmlhttp = new XMLHttpRequest();
  155. if (xmlhttp.overrideMimeType)
  156. xmlhttp.overrideMimeType('text/xml');
  157. }
  158. if (xmlhttp != null) {
  159. var that = this;
  160. xmlhttp.onreadystatechange = function () {
  161. if (xmlhttp.readyState == 4) {
  162. if (xmlhttp.responseText) {
  163. that._xmlDict[fullfilePath] = xmlhttp.responseText;
  164. xmlhttp = null;
  165. cc.doCallback(selector, target);
  166. } else {
  167. cc.doCallback(selector, target, filePath);
  168. }
  169. }
  170. };
  171. // load xml
  172. xmlhttp.open("GET", fullfilePath, true);
  173. xmlhttp.send(null);
  174. } else
  175. throw "cocos2d:Your browser does not support XMLHTTP.";
  176. },
  177. /**
  178. * Unload the preloaded plist from xmlList
  179. * @param {String} filePath
  180. */
  181. unloadPlist: function (filePath) {
  182. if (this._xmlDict[filePath])
  183. delete this._xmlDict[filePath];
  184. },
  185. /**
  186. * get filename from filepath
  187. * @param {String} filePath
  188. * @return {String}
  189. */
  190. getName: function (filePath) {
  191. var startPos = filePath.lastIndexOf("/", filePath.length) + 1;
  192. var endPos = filePath.lastIndexOf(".", filePath.length);
  193. return filePath.substring(startPos, endPos);
  194. },
  195. /**
  196. * get file extension name from filepath
  197. * @param {String} filePath
  198. * @return {String}
  199. */
  200. getExt: function (filePath) {
  201. var startPos = filePath.lastIndexOf(".", filePath.length) + 1;
  202. return filePath.substring(startPos, filePath.length);
  203. },
  204. /**
  205. * get value by key from xmlList
  206. * @param {String} key
  207. * @return {String} xml content
  208. */
  209. getList: function (key) {
  210. if (this._xmlDict != null) {
  211. return this._xmlDict[key];
  212. }
  213. return null;
  214. }
  215. });
  216. /**
  217. * get a singleton SAX parser
  218. * @function
  219. * @return {cc.SAXParser}
  220. */
  221. cc.SAXParser.getInstance = function () {
  222. if (!this._instance) {
  223. this._instance = new cc.SAXParser();
  224. }
  225. return this._instance;
  226. };
  227. cc.SAXParser._instance = null;