123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- /****************************************************************************
- Copyright (c) 2010-2012 cocos2d-x.org
- Copyright (c) 2008-2010 Ricardo Quesada
- Copyright (c) 2011 Zynga Inc.
- http://www.cocos2d-x.org
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- ****************************************************************************/
- /**
- * a SAX Parser
- * @class
- * @extends cc.Class
- */
- cc.SAXParser = cc.Class.extend(/** @lends cc.SAXParser# */{
- xmlDoc: null,
- _parser: null,
- _xmlDict: null,
- _isSupportDOMParser: null,
- ctor: function () {
- this._xmlDict = {};
- if (window.DOMParser) {
- this._isSupportDOMParser = true;
- this._parser = new DOMParser();
- } else {
- this._isSupportDOMParser = false;
- }
- },
- /**
- * parse a xml from a string (xmlhttpObj.responseText)
- * @param {String} textxml plist xml contents
- * @return {Array} plist object array
- */
- parse: function (textxml) {
- var path = textxml;
- textxml = this.getList(textxml);
- var xmlDoc = this._parserXML(textxml, path);
- var plist = xmlDoc.documentElement;
- if (plist.tagName != 'plist')
- throw "cocos2d: " + path + " is not a plist file or you forgot to preload the plist file";
- // Get first real node
- var node = null;
- for (var i = 0, len = plist.childNodes.length; i < len; i++) {
- node = plist.childNodes[i];
- if (node.nodeType == 1)
- break;
- }
- xmlDoc = null;
- return this._parseNode(node);
- },
- /**
- * parse a tilemap xml from a string (xmlhttpObj.responseText)
- * @param {String} textxml tilemap xml content
- * @return {Document} xml document
- */
- tmxParse: function (textxml, isXMLString) {
- if ((isXMLString == null) || (isXMLString === false))
- textxml = this.getList(textxml);
- return this._parserXML(textxml);
- },
- _parserXML: function (textxml, path) {
- // get a reference to the requested corresponding xml file
- var xmlDoc;
- if (this._isSupportDOMParser) {
- xmlDoc = this._parser.parseFromString(textxml, "text/xml");
- } else {
- // Internet Explorer (untested!)
- xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
- xmlDoc.async = "false";
- xmlDoc.loadXML(textxml);
- }
- if (xmlDoc == null)
- cc.log("cocos2d:xml " + path + " not found!");
- return xmlDoc;
- },
- _parseNode: function (node) {
- var data = null;
- switch (node.tagName) {
- case 'dict':
- data = this._parseDict(node);
- break;
- case 'array':
- data = this._parseArray(node);
- break;
- case 'string':
- if (node.childNodes.length == 1)
- data = node.firstChild.nodeValue;
- else {
- //handle Firefox's 4KB nodeValue limit
- data = "";
- for (var i = 0; i < node.childNodes.length; i++)
- data += node.childNodes[i].nodeValue;
- }
- break;
- case 'false':
- data = false;
- break;
- case 'true':
- data = true;
- break;
- case 'real':
- data = parseFloat(node.firstChild.nodeValue);
- break;
- case 'integer':
- data = parseInt(node.firstChild.nodeValue, 10);
- break;
- }
- return data;
- },
- _parseArray: function (node) {
- var data = [];
- for (var i = 0, len = node.childNodes.length; i < len; i++) {
- var child = node.childNodes[i];
- if (child.nodeType != 1)
- continue;
- data.push(this._parseNode(child));
- }
- return data;
- },
- _parseDict: function (node) {
- var data = {};
- var key = null;
- for (var i = 0, len = node.childNodes.length; i < len; i++) {
- var child = node.childNodes[i];
- if (child.nodeType != 1)
- continue;
- // Grab the key, next noe should be the value
- if (child.tagName == 'key')
- key = child.firstChild.nodeValue;
- else
- data[key] = this._parseNode(child); // Parse the value node
- }
- return data;
- },
- /**
- * Preload plist file
- * @param {String} filePath
- */
- preloadPlist: function (filePath, selector, target) {
- var fullfilePath = cc.FileUtils.getInstance().fullPathForFilename(filePath);
- if (window.XMLHttpRequest) {
- var xmlhttp = new XMLHttpRequest();
- if (xmlhttp.overrideMimeType)
- xmlhttp.overrideMimeType('text/xml');
- }
- if (xmlhttp != null) {
- var that = this;
- xmlhttp.onreadystatechange = function () {
- if (xmlhttp.readyState == 4) {
- if (xmlhttp.responseText) {
- that._xmlDict[fullfilePath] = xmlhttp.responseText;
- xmlhttp = null;
- cc.doCallback(selector, target);
- } else {
- cc.doCallback(selector, target, filePath);
- }
- }
- };
- // load xml
- xmlhttp.open("GET", fullfilePath, true);
- xmlhttp.send(null);
- } else
- throw "cocos2d:Your browser does not support XMLHTTP.";
- },
- /**
- * Unload the preloaded plist from xmlList
- * @param {String} filePath
- */
- unloadPlist: function (filePath) {
- if (this._xmlDict[filePath])
- delete this._xmlDict[filePath];
- },
- /**
- * get filename from filepath
- * @param {String} filePath
- * @return {String}
- */
- getName: function (filePath) {
- var startPos = filePath.lastIndexOf("/", filePath.length) + 1;
- var endPos = filePath.lastIndexOf(".", filePath.length);
- return filePath.substring(startPos, endPos);
- },
- /**
- * get file extension name from filepath
- * @param {String} filePath
- * @return {String}
- */
- getExt: function (filePath) {
- var startPos = filePath.lastIndexOf(".", filePath.length) + 1;
- return filePath.substring(startPos, filePath.length);
- },
- /**
- * get value by key from xmlList
- * @param {String} key
- * @return {String} xml content
- */
- getList: function (key) {
- if (this._xmlDict != null) {
- return this._xmlDict[key];
- }
- return null;
- }
- });
- /**
- * get a singleton SAX parser
- * @function
- * @return {cc.SAXParser}
- */
- cc.SAXParser.getInstance = function () {
- if (!this._instance) {
- this._instance = new cc.SAXParser();
- }
- return this._instance;
- };
- cc.SAXParser._instance = null;
|