CCSAXParser.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2011-2012 cocos2d-x.org
  4. Copyright (c) 2013-2014 Chukong Technologies 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. * @name cc.saxParser
  26. * @extends cc.Class
  27. */
  28. cc.SAXParser = cc.Class.extend(/** @lends cc.saxParser# */{
  29. _parser: null,
  30. _isSupportDOMParser: null,
  31. /**
  32. * Constructor of cc.SAXParser
  33. */
  34. ctor: function () {
  35. if (window.DOMParser) {
  36. this._isSupportDOMParser = true;
  37. this._parser = new DOMParser();
  38. } else {
  39. this._isSupportDOMParser = false;
  40. }
  41. },
  42. /**
  43. * @function
  44. * @param {String} xmlTxt
  45. * @return {Document}
  46. */
  47. parse : function(xmlTxt){
  48. return this._parseXML(xmlTxt);
  49. },
  50. _parseXML: function (textxml) {
  51. // get a reference to the requested corresponding xml file
  52. var xmlDoc;
  53. if (this._isSupportDOMParser) {
  54. xmlDoc = this._parser.parseFromString(textxml, "text/xml");
  55. } else {
  56. // Internet Explorer (untested!)
  57. xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  58. xmlDoc.async = "false";
  59. xmlDoc.loadXML(textxml);
  60. }
  61. return xmlDoc;
  62. }
  63. });
  64. /**
  65. *
  66. * cc.plistParser is a singleton object for parsing plist files
  67. * @class
  68. * @name cc.plistParser
  69. * @extends cc.SAXParser
  70. */
  71. cc.PlistParser = cc.SAXParser.extend(/** @lends cc.plistParser# */{
  72. /**
  73. * parse a xml string as plist object.
  74. * @param {String} xmlTxt plist xml contents
  75. * @return {*} plist object
  76. */
  77. parse : function (xmlTxt) {
  78. var xmlDoc = this._parseXML(xmlTxt);
  79. var plist = xmlDoc.documentElement;
  80. if (plist.tagName != 'plist')
  81. throw "Not a plist file!";
  82. // Get first real node
  83. var node = null;
  84. for (var i = 0, len = plist.childNodes.length; i < len; i++) {
  85. node = plist.childNodes[i];
  86. if (node.nodeType == 1)
  87. break;
  88. }
  89. xmlDoc = null;
  90. return this._parseNode(node);
  91. },
  92. _parseNode: function (node) {
  93. var data = null, tagName = node.tagName;
  94. if(tagName == "dict"){
  95. data = this._parseDict(node);
  96. }else if(tagName == "array"){
  97. data = this._parseArray(node);
  98. }else if(tagName == "string"){
  99. if (node.childNodes.length == 1)
  100. data = node.firstChild.nodeValue;
  101. else {
  102. //handle Firefox's 4KB nodeValue limit
  103. data = "";
  104. for (var i = 0; i < node.childNodes.length; i++)
  105. data += node.childNodes[i].nodeValue;
  106. }
  107. }else if(tagName == "false"){
  108. data = false;
  109. }else if(tagName == "true"){
  110. data = true;
  111. }else if(tagName == "real"){
  112. data = parseFloat(node.firstChild.nodeValue);
  113. }else if(tagName == "integer"){
  114. data = parseInt(node.firstChild.nodeValue, 10);
  115. }
  116. return data;
  117. },
  118. _parseArray: function (node) {
  119. var data = [];
  120. for (var i = 0, len = node.childNodes.length; i < len; i++) {
  121. var child = node.childNodes[i];
  122. if (child.nodeType != 1)
  123. continue;
  124. data.push(this._parseNode(child));
  125. }
  126. return data;
  127. },
  128. _parseDict: function (node) {
  129. var data = {};
  130. var key = null;
  131. for (var i = 0, len = node.childNodes.length; i < len; i++) {
  132. var child = node.childNodes[i];
  133. if (child.nodeType != 1)
  134. continue;
  135. // Grab the key, next noe should be the value
  136. if (child.tagName == 'key')
  137. key = child.firstChild.nodeValue;
  138. else
  139. data[key] = this._parseNode(child); // Parse the value node
  140. }
  141. return data;
  142. }
  143. });