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  *
 29  * @namespace A SAX Parser
 30  * @name cc.saxParser
 31  */
 32 cc.SAXParser = cc.Class.extend(/** @lends cc.saxParser# */{
 33     _parser: null,
 34     _isSupportDOMParser: null,
 35 
 36     ctor: function () {
 37         if (window.DOMParser) {
 38             this._isSupportDOMParser = true;
 39             this._parser = new DOMParser();
 40         } else {
 41             this._isSupportDOMParser = false;
 42         }
 43     },
 44 
 45     parse : function(xmlTxt){
 46         return this._parseXML(xmlTxt);
 47     },
 48 
 49     _parseXML: function (textxml) {
 50         // get a reference to the requested corresponding xml file
 51         var xmlDoc;
 52         if (this._isSupportDOMParser) {
 53             xmlDoc = this._parser.parseFromString(textxml, "text/xml");
 54         } else {
 55             // Internet Explorer (untested!)
 56             xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
 57             xmlDoc.async = "false";
 58             xmlDoc.loadXML(textxml);
 59         }
 60         return xmlDoc;
 61     }
 62 
 63 });
 64 
 65 /**
 66  *
 67  * @namespace A plist Parser
 68  * @name cc.plistParser
 69  */
 70 cc.PlistParser = cc.SAXParser.extend(/** @lends cc.plistParser# */{
 71 
 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 
 83         // Get first real node
 84         var node = null;
 85         for (var i = 0, len = plist.childNodes.length; i < len; i++) {
 86             node = plist.childNodes[i];
 87             if (node.nodeType == 1)
 88                 break;
 89         }
 90         xmlDoc = null;
 91         return this._parseNode(node);
 92     },
 93 
 94     _parseNode: function (node) {
 95         var data = null, tagName = node.tagName;
 96         if(tagName == "dict"){
 97             data = this._parseDict(node);
 98         }else if(tagName == "array"){
 99             data = this._parseArray(node);
100         }else if(tagName == "string"){
101             if (node.childNodes.length == 1)
102                 data = node.firstChild.nodeValue;
103             else {
104                 //handle Firefox's 4KB nodeValue limit
105                 data = "";
106                 for (var i = 0; i < node.childNodes.length; i++)
107                     data += node.childNodes[i].nodeValue;
108             }
109         }else if(tagName == "false"){
110             data = false;
111         }else if(tagName == "true"){
112             data = true;
113         }else if(tagName == "real"){
114             data = parseFloat(node.firstChild.nodeValue);
115         }else if(tagName == "integer"){
116             data = parseInt(node.firstChild.nodeValue, 10);
117         }
118         return data;
119     },
120 
121     _parseArray: function (node) {
122         var data = [];
123         for (var i = 0, len = node.childNodes.length; i < len; i++) {
124             var child = node.childNodes[i];
125             if (child.nodeType != 1)
126                 continue;
127             data.push(this._parseNode(child));
128         }
129         return data;
130     },
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 
140             // Grab the key, next noe should be the value
141             if (child.tagName == 'key')
142                 key = child.firstChild.nodeValue;
143             else
144                 data[key] = this._parseNode(child);                 // Parse the value node
145         }
146         return data;
147     }
148 
149 });