/** * * SWX ActionScript API * Copyright © 2007 Aral Balkan. http://aralbalkan.com * Part of the SWX (SWF Data Exchange Format) project. http://swxformat.org * * Released under the open source MIT license. * http://opensource.org/licenses/mit-license.php * */ import org.swxformat.*; import mx.utils.Delegate; class org.swxformat.SWX { // Constants public static var POST:String = "POST"; public static var GET:String = "GET"; // Static vars private static var json:String = ''; private static var instance:SWX = null; // Private instance vars private var _gateway:String = null; private var _testGateway:String = null; private var _encoding:String = SWX.POST; private var _resultObj:Object = null; private var _resultFn:Function = null; private var _progressObj:Object = null; private var _progressFn:Function = null; private var _debug:Boolean = false; // // Instance methods // // Accessor method public function getInstance():SWX { if (instance == null) { instance = new SWX(); } return instance; } // The test gateway is used when your Flash movie // is run from the standalone player or from the Flash IDE. public function set testGateway (gatewayUrl:String) { _testGateway = gatewayUrl; } public function set gateway (gatewayUrl:String) { _gateway = gatewayUrl; if (_testGateway == null) _testGateway = gatewayUrl; } // Set the HTTP encoding type public function set encoding (method:String) { if (method != SWX.POST && method != SWX.GET) { throw (new Error("SWX: Invalid encoding method ("+method+"). Valid methods are POST and GET.")); } else { _encoding = method; } } public function set debug (state:Boolean) { _debug = state; } // Set global result handler public function set result (resultHandler:Object) { _resultObj = resultHandler.obj; _resultFn = resultHandler.fn; } // Set global progress handler public function set progress (progressHandler:Object) { _progressObj = progressHandler.obj; _progressFn = progressHandler.fn; } // Call remote method public function call (serviceClass:String, method:String, args:Object, resultObj:Object, resultFn:Function, progressObj:Object, progressFn:Function, debug:Boolean) { debug = (debug == undefined) ? _debug : debug; var externalAsset:ExternalAsset = new ExternalAsset(null, _gateway, {serviceClass:serviceClass, method:method, arguments: args, debug:debug}, _encoding); externalAsset.addEventListener(ExternalAsset.LOAD, Delegate.create(resultObj, resultFn)); externalAsset.addEventListener(ExternalAsset.PROGRESS, Delegate.create(progressObj, progressFn)); var loadManager:LoadManager = LoadManager.getInstance(); var callId:Number = loadManager.prepare(externalAsset); // Prepare the data to be loaded (serialize in JSON format.) SWX.prepare(loadManager.getAssetClip(callId)); // Add the latest call to the queue. loadManager.addToQueue(externalAsset); } // Cancel data call with passed ID. public function cancel (callId:Number) { LoadManager.getInstance().cancel(callId); } /** * Converts the arguments array (if any) to JSON * * Usage: * import org.swxformat.*; * Swx.prepare(dataHolder); */ static function prepare(dataHolder:MovieClip):Void { json = ""; if (dataHolder.arguments != undefined) { if (dataHolder.arguments instanceof Array) { arrayToJson(dataHolder.arguments); dataHolder.arguments = json; } else { trace("SWX Error in Swx.prepare(): args must be an array."); } } else { trace("SWX: No arguments array found. No arguments will be sent."); } } // Converts an object to JSON static function objectToJson(data:Object) { json += "{"; for (var i:String in data) { json += '"' + i + '":'; var x = data[i]; // Note: check for Array first since everything is an Object. if (x instanceof Array) { arrayToJson(x); } else if (x instanceof Object) { objectToJson(x); } else if (typeof x == "string") { stringToJson(x); } else { // Other simple data type, add as-is json += x; } json += ", "; } json = json.substr(0, json.length-2); json += "}"; } // Converts an array to JSON static function arrayToJson(data:Array) { json += "["; var len:Number = data.length; for (var i = 0; i < len; i++) { var x = data[i]; // Note: check for Array first since everything is an Object. if (x instanceof Array) { arrayToJson(x); } else if (x instanceof Object) { objectToJson(x); } else if (typeof x == "string") { stringToJson(x); } else { // Other simple data type, add as-is json += x; } json += ", "; } json = json.substr(0, json.length-2); json += "]"; } static function stringToJson(data:String) { // From JSON.as by Trannie Carter // http://json.org/json.as var l:Number = data.length; json += '"'; for (var i:Number = 0; i < l; i += 1) { var c = data.charAt(i); if (c >= ' ') { if (c == '\\' || c == '"' || c == "'") { json += '\\'; } json += c; } else { switch (c) { case '\b': json += '\\b'; break; case '\f': json += '\\f'; break; case '\n': json += '\\n'; break; case '\r': json += '\\r'; break; case '\t': json += '\\t'; break; default: c = c.charCodeAt(); json += '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16); } } } json += '"'; } // Private constructor - do not use. private function SWX() { // Do not instantiate this class manually. Use its static methods. } }