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             cc.warn("Not a plist file!");
 93             return {};
 94         }
 95 
 96         // Get first real node
 97         var node = null;
 98         for (var i = 0, len = plist.childNodes.length; i < len; i++) {
 99             node = plist.childNodes[i];
100             if (node.nodeType === 1)
101                 break;
102         }
103         xmlDoc = null;
104         return this._parseNode(node);
105     },
106 
107     _parseNode: function (node) {
108         var data = null, tagName = node.tagName;
109         if(tagName === "dict"){
110             data = this._parseDict(node);
111         }else if(tagName === "array"){
112             data = this._parseArray(node);
113         }else if(tagName === "string"){
114             if (node.childNodes.length === 1)
115                 data = node.firstChild.nodeValue;
116             else {
117                 //handle Firefox's 4KB nodeValue limit
118                 data = "";
119                 for (var i = 0; i < node.childNodes.length; i++)
120                     data += node.childNodes[i].nodeValue;
121             }
122         }else if(tagName === "false"){
123             data = false;
124         }else if(tagName === "true"){
125             data = true;
126         }else if(tagName === "real"){
127             data = parseFloat(node.firstChild.nodeValue);
128         }else if(tagName === "integer"){
129             data = parseInt(node.firstChild.nodeValue, 10);
130         }
131         return data;
132     },
133 
134     _parseArray: function (node) {
135         var data = [];
136         for (var i = 0, len = node.childNodes.length; i < len; i++) {
137             var child = node.childNodes[i];
138             if (child.nodeType !== 1)
139                 continue;
140             data.push(this._parseNode(child));
141         }
142         return data;
143     },
144 
145     _parseDict: function (node) {
146         var data = {};
147         var key = null;
148         for (var i = 0, len = node.childNodes.length; i < len; i++) {
149             var child = node.childNodes[i];
150             if (child.nodeType !== 1)
151                 continue;
152 
153             // Grab the key, next noe should be the value
154             if (child.tagName === 'key')
155                 key = child.firstChild.nodeValue;
156             else
157                 data[key] = this._parseNode(child);                 // Parse the value node
158         }
159         return data;
160     }
161 });
162 
163 cc.saxParser = new cc.SAXParser();
164 /**
165  * A Plist Parser
166  * @type {cc.PlistParser}
167  * @name plistParser
168  * @memberof cc
169  */
170 cc.plistParser = new cc.PlistParser();
171