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 6 http://www.cocos2d-x.org 7 8 Permission is hereby granted, free of charge, to any person obtaining a copy 9 of this software and associated documentation files (the "Software"), to deal 10 in the Software without restriction, including without limitation the rights 11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 copies of the Software, and to permit persons to whom the Software is 13 furnished to do so, subject to the following conditions: 14 15 The above copyright notice and this permission notice shall be included in 16 all copies or substantial portions of the Software. 17 18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 THE SOFTWARE. 25 ****************************************************************************/ 26 27 /** 28 * A SAX Parser 29 * @class 30 * @name cc.saxParser 31 * @extends cc.Class 32 */ 33 cc.SAXParser = cc.Class.extend(/** @lends cc.saxParser# */{ 34 _parser: null, 35 _isSupportDOMParser: null, 36 37 /** 38 * Constructor of cc.SAXParser 39 */ 40 ctor: function () { 41 if (window.DOMParser) { 42 this._isSupportDOMParser = true; 43 this._parser = new DOMParser(); 44 } else { 45 this._isSupportDOMParser = false; 46 } 47 }, 48 49 /** 50 * @function 51 * @param {String} xmlTxt 52 * @return {Document} 53 */ 54 parse : function(xmlTxt){ 55 return this._parseXML(xmlTxt); 56 }, 57 58 _parseXML: function (textxml) { 59 // get a reference to the requested corresponding xml file 60 var xmlDoc; 61 if (this._isSupportDOMParser) { 62 xmlDoc = this._parser.parseFromString(textxml, "text/xml"); 63 } else { 64 // Internet Explorer (untested!) 65 xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); 66 xmlDoc.async = "false"; 67 xmlDoc.loadXML(textxml); 68 } 69 return xmlDoc; 70 } 71 72 }); 73 74 /** 75 * 76 * cc.plistParser is a singleton object for parsing plist files 77 * @class 78 * @name cc.plistParser 79 * @extends cc.SAXParser 80 */ 81 cc.PlistParser = cc.SAXParser.extend(/** @lends cc.plistParser# */{ 82 83 /** 84 * parse a xml string as plist object. 85 * @param {String} xmlTxt plist xml contents 86 * @return {*} plist object 87 */ 88 parse : function (xmlTxt) { 89 var xmlDoc = this._parseXML(xmlTxt); 90 var plist = xmlDoc.documentElement; 91 if (plist.tagName !== 'plist') 92 throw new Error("Not a plist file!"); 93 94 // Get first real node 95 var node = null; 96 for (var i = 0, len = plist.childNodes.length; i < len; i++) { 97 node = plist.childNodes[i]; 98 if (node.nodeType === 1) 99 break; 100 } 101 xmlDoc = null; 102 return this._parseNode(node); 103 }, 104 105 _parseNode: function (node) { 106 var data = null, tagName = node.tagName; 107 if(tagName === "dict"){ 108 data = this._parseDict(node); 109 }else if(tagName === "array"){ 110 data = this._parseArray(node); 111 }else if(tagName === "string"){ 112 if (node.childNodes.length === 1) 113 data = node.firstChild.nodeValue; 114 else { 115 //handle Firefox's 4KB nodeValue limit 116 data = ""; 117 for (var i = 0; i < node.childNodes.length; i++) 118 data += node.childNodes[i].nodeValue; 119 } 120 }else if(tagName === "false"){ 121 data = false; 122 }else if(tagName === "true"){ 123 data = true; 124 }else if(tagName === "real"){ 125 data = parseFloat(node.firstChild.nodeValue); 126 }else if(tagName === "integer"){ 127 data = parseInt(node.firstChild.nodeValue, 10); 128 } 129 return data; 130 }, 131 132 _parseArray: function (node) { 133 var data = []; 134 for (var i = 0, len = node.childNodes.length; i < len; i++) { 135 var child = node.childNodes[i]; 136 if (child.nodeType !== 1) 137 continue; 138 data.push(this._parseNode(child)); 139 } 140 return data; 141 }, 142 143 _parseDict: function (node) { 144 var data = {}; 145 var key = null; 146 for (var i = 0, len = node.childNodes.length; i < len; i++) { 147 var child = node.childNodes[i]; 148 if (child.nodeType !== 1) 149 continue; 150 151 // Grab the key, next noe should be the value 152 if (child.tagName === 'key') 153 key = child.firstChild.nodeValue; 154 else 155 data[key] = this._parseNode(child); // Parse the value node 156 } 157 return data; 158 } 159 160 });