home *** CD-ROM | disk | FTP | other *** search
/ Mobiclic 149 / MOBICLIC149.ISO / pc / DATA / DSS149 / DSS149_03 / DSS149_03.swf / scripts / dss149_03 / ObjectInitializer.as < prev   
Text File  |  2012-11-21  |  4KB  |  123 lines

  1. package dss149_03
  2. {
  3.    public class ObjectInitializer
  4.    {
  5.        
  6.       
  7.       private var _instance = null;
  8.       
  9.       private var _object = null;
  10.       
  11.       private var _initObject = null;
  12.       
  13.       public function ObjectInitializer(instance:*, object:Object, initObject:* = null)
  14.       {
  15.          super();
  16.          this._instance = instance;
  17.          this._object = object;
  18.          this._initObject = initObject;
  19.          this.initialize();
  20.       }
  21.       
  22.       private function destroy() : void
  23.       {
  24.          this._instance = null;
  25.          this._object = null;
  26.          this._initObject = null;
  27.       }
  28.       
  29.       private function initialize() : void
  30.       {
  31.          if(this._initObject == null)
  32.          {
  33.             this.destroy();
  34.             return;
  35.          }
  36.          if(this._initObject is XML)
  37.          {
  38.             this.fromXML(this._initObject);
  39.             this.destroy();
  40.          }
  41.          else if(this._initObject is XMLList)
  42.          {
  43.             this.fromXML(this._initObject[0]);
  44.             this.destroy();
  45.          }
  46.          else if(this._initObject is Object)
  47.          {
  48.             this.fromObject(this._initObject);
  49.             this.destroy();
  50.          }
  51.       }
  52.       
  53.       private function fromObject(obj:*) : void
  54.       {
  55.          var prop:* = null;
  56.          for(prop in obj as Object)
  57.          {
  58.             this._object[prop] = obj[prop];
  59.          }
  60.       }
  61.       
  62.       private function fromXML(xml:XML) : void
  63.       {
  64.          var type:String = null;
  65.          var node:XML = null;
  66.          var prop:String = null;
  67.          var list:XMLList = xml.Param;
  68.          for each(node in list)
  69.          {
  70.             if(node.hasOwnProperty("@type"))
  71.             {
  72.                type = String(node.@type);
  73.             }
  74.             else if(this._instance[node.@nom] !== undefined)
  75.             {
  76.                if(this._instance[node.@nom] is Boolean)
  77.                {
  78.                   type = "boolean";
  79.                }
  80.                else if(this._instance[node.@nom] is Number)
  81.                {
  82.                   type = "number";
  83.                }
  84.                else if(this._instance[node.@nom] is int)
  85.                {
  86.                   type = "int";
  87.                }
  88.                else if(this._instance[node.@nom] is String)
  89.                {
  90.                   type = "string";
  91.                }
  92.                else if(this._instance[node.@nom] is Array)
  93.                {
  94.                   type = "array";
  95.                }
  96.             }
  97.             else
  98.             {
  99.                type = "string";
  100.             }
  101.             switch(type)
  102.             {
  103.                case "boolean":
  104.                   this._object[node.@nom] = this._instance[node.@nom] = String(node.@valeur) == "true" ? true : false;
  105.                   break;
  106.                case "number":
  107.                   this._object[node.@nom] = this._instance[node.@nom] = Number(node.@valeur);
  108.                   break;
  109.                case "int":
  110.                   this._object[node.@nom] = this._instance[node.@nom] = int(node.@valeur);
  111.                   break;
  112.                case "array":
  113.                   this._object[node.@nom] = this._instance[node.@nom] = String(node.@valeur).split(",");
  114.                   break;
  115.                case "string":
  116.                   this._object[node.@nom] = this._instance[node.@nom] = String(node.@valeur);
  117.                   break;
  118.             }
  119.          }
  120.       }
  121.    }
  122. }
  123.