home *** CD-ROM | disk | FTP | other *** search
/ Champak 48 / cdrom_image.iso / Games / alex_trax.swf / scripts / __Packages / com / neodelight / wddx / Wddx.as next >
Encoding:
Text File  |  2007-10-01  |  11.4 KB  |  363 lines

  1. class com.neodelight.wddx.Wddx
  2. {
  3.    var et = null;
  4.    var etRev = null;
  5.    var at = null;
  6.    var atRev = null;
  7.    var timezoneString = null;
  8.    var preserveVarCase = null;
  9.    var useTimezoneInfo = null;
  10.    var wddxPacket = null;
  11.    function Wddx()
  12.    {
  13.       this.et = new Object();
  14.       this.etRev = new Object();
  15.       this.at = new Object();
  16.       this.atRev = new Object();
  17.       this.timezoneString = new String();
  18.       this.preserveVarCase = true;
  19.       this.useTimezoneInfo = true;
  20.       var _loc2_ = 0;
  21.       while(_loc2_ < 256)
  22.       {
  23.          if(_loc2_ < 32 && _loc2_ != 9 && _loc2_ != 10 && _loc2_ != 13)
  24.          {
  25.             var _loc3_ = _loc2_.toString(16);
  26.             if(_loc3_.length == 1)
  27.             {
  28.                _loc3_ = "0" + _loc3_;
  29.             }
  30.             this.et[_loc2_] = "<char code=\'" + _loc3_ + "\'/>";
  31.             this.at[_loc2_] = "";
  32.          }
  33.          else if(_loc2_ < 128)
  34.          {
  35.             this.et[_loc2_] = chr(_loc2_);
  36.             this.at[_loc2_] = chr(_loc2_);
  37.          }
  38.          else
  39.          {
  40.             this.et[_loc2_] = "&#x" + _loc2_.toString(16) + ";";
  41.             this.etRev["&#x" + _loc2_.toString(16) + ";"] = chr(_loc2_);
  42.             this.at[_loc2_] = "&#x" + _loc2_.toString(16) + ";";
  43.             this.atRev["&#x" + _loc2_.toString(16) + ";"] = chr(_loc2_);
  44.          }
  45.          _loc2_ = _loc2_ + 1;
  46.       }
  47.       this.et[60] = "<";
  48.       this.et[62] = ">";
  49.       this.et[38] = "&";
  50.       this.etRev["<"] = "<";
  51.       this.etRev[">"] = ">";
  52.       this.etRev["&"] = "&";
  53.       this.at[60] = "<";
  54.       this.at[62] = ">";
  55.       this.at[38] = "&";
  56.       this.at[39] = "'";
  57.       this.at[34] = """;
  58.       this.atRev["<"] = "<";
  59.       this.atRev[">"] = ">";
  60.       this.atRev["&"] = "&";
  61.       this.atRev["'"] = "\'";
  62.       this.atRev["""] = "\"";
  63.       var _loc4_ = new Date().getTimezoneOffset();
  64.       if(_loc4_ >= 0)
  65.       {
  66.          this.timezoneString = "-";
  67.       }
  68.       else
  69.       {
  70.          this.timezoneString = "+";
  71.       }
  72.       this.timezoneString += Math.floor(Math.abs(_loc4_) / 60) + ":" + Math.abs(_loc4_) % 60;
  73.    }
  74.    static function serialize(rootObj)
  75.    {
  76.       var _loc4_ = new com.neodelight.wddx.Wddx();
  77.       var _loc2_ = new XML();
  78.       var _loc3_ = new XML();
  79.       _loc3_.appendChild(_loc2_.createElement("wddxPacket"));
  80.       var _loc1_ = _loc3_.firstChild;
  81.       _loc1_.attributes.version = "1.0";
  82.       _loc1_.appendChild(_loc2_.createElement("header"));
  83.       _loc1_.appendChild(_loc2_.createElement("data"));
  84.       if(_loc4_.serializeValue(rootObj,_loc1_.childNodes[1]))
  85.       {
  86.          return _loc3_.toString();
  87.       }
  88.       return null;
  89.    }
  90.    function serializeValue(obj, node)
  91.    {
  92.       var _loc4_ = true;
  93.       var _loc6_ = obj.valueOf();
  94.       var _loc9_ = null;
  95.       var _loc7_ = new XML();
  96.       if(obj == null)
  97.       {
  98.          node.appendChild(_loc7_.createElement("null"));
  99.       }
  100.       else if(typeof _loc6_ == "string")
  101.       {
  102.          this.serializeString(_loc6_,node);
  103.       }
  104.       else if(typeof _loc6_ == "number")
  105.       {
  106.          if(typeof obj.getTimezoneOffset == "function")
  107.          {
  108.             if(this.useTimezoneInfo)
  109.             {
  110.                _loc9_ = this.timezoneString;
  111.             }
  112.             node.appendChild(_loc7_.createElement("dateTime"));
  113.             node.lastChild.appendChild(_loc7_.createTextNode(obj.getFullYear() + "-" + (obj.getMonth() + 1) + "-" + obj.getDate() + "T" + obj.getHours() + ":" + obj.getMinutes() + ":" + obj.getSeconds() + _loc9_));
  114.          }
  115.          else
  116.          {
  117.             node.appendChild(new XML().createElement("number"));
  118.             node.lastChild.appendChild(new XML().createTextNode(_loc6_));
  119.          }
  120.       }
  121.       else if(typeof _loc6_ == "boolean")
  122.       {
  123.          node.appendChild(_loc7_.createElement("boolean"));
  124.          node.lastChild.attributes.value = _loc6_;
  125.       }
  126.       else if(typeof obj == "object")
  127.       {
  128.          if(typeof obj.wddxSerialize == "function")
  129.          {
  130.             _loc4_ = obj.wddxSerialize(this,node);
  131.          }
  132.          else if(typeof obj.join == "function" && typeof obj.reverse == "function")
  133.          {
  134.             node.appendChild(_loc7_.createElement("array"));
  135.             node.lastChild.attributes.length = obj.length;
  136.             var _loc3_ = 0;
  137.             while(_loc4_ && _loc3_ < obj.length)
  138.             {
  139.                _loc4_ = this.serializeValue(obj[_loc3_],node.lastChild);
  140.                _loc3_ = _loc3_ + 1;
  141.             }
  142.          }
  143.          else
  144.          {
  145.             node.appendChild(_loc7_.createElement("struct"));
  146.             if(typeof obj.wddxSerializationType == "string")
  147.             {
  148.                node.lastChild.attributes.type = obj.wddxSerializationType;
  149.             }
  150.             for(var _loc8_ in obj)
  151.             {
  152.                if(_loc8_ != "wddxSerializationType")
  153.                {
  154.                   _loc4_ = this.serializeVariable(_loc8_,obj[_loc8_],node.lastChild);
  155.                   if(!_loc4_)
  156.                   {
  157.                      break;
  158.                   }
  159.                }
  160.             }
  161.          }
  162.       }
  163.       else
  164.       {
  165.          _loc4_ = false;
  166.       }
  167.       return _loc4_;
  168.    }
  169.    function serializeVariable(vname, obj, node)
  170.    {
  171.       var _loc2_ = true;
  172.       var _loc3_ = new XML();
  173.       if(typeof obj != "function")
  174.       {
  175.          node.appendChild(_loc3_.createElement("var"));
  176.          node.lastChild.attributes.name = !this.preserveVarCase ? this.serializeAttr(vname.toLowerCase()) : this.serializeAttr(vname);
  177.          _loc2_ = this.serializeValue(obj,node.lastChild);
  178.       }
  179.       return _loc2_;
  180.    }
  181.    function serializeString(s, node)
  182.    {
  183.       var _loc3_ = "";
  184.       var _loc6_ = new XML();
  185.       var _loc4_ = s.length;
  186.       node.appendChild(_loc6_.createElement("string"));
  187.       var _loc2_ = 0;
  188.       while(_loc2_ < _loc4_)
  189.       {
  190.          _loc3_ += this.et[ord(s.substring(_loc2_,_loc2_ + 1))];
  191.          _loc2_ = _loc2_ + 1;
  192.       }
  193.       node.lastChild.appendChild(_loc6_.createTextNode(_loc3_));
  194.    }
  195.    function serializeAttr(s)
  196.    {
  197.       var _loc3_ = "";
  198.       var _loc4_ = s.length;
  199.       var _loc2_ = 0;
  200.       while(_loc2_ < _loc4_)
  201.       {
  202.          _loc3_ += this.at[ord(s.substring(_loc2_,_loc2_ + 1))];
  203.          _loc2_ = _loc2_ + 1;
  204.       }
  205.       return _loc3_;
  206.    }
  207.    static function deserialize(xmlString)
  208.    {
  209.       var _loc6_ = new com.neodelight.wddx.Wddx();
  210.       var _loc2_ = new XML(xmlString);
  211.       var _loc4_ = new XML();
  212.       var _loc3_ = new Array();
  213.       var _loc5_ = new Object();
  214.       while(_loc2_.nodeName == null)
  215.       {
  216.          _loc2_ = _loc2_.firstChild;
  217.       }
  218.       _loc4_ = _loc2_;
  219.       if(_loc4_.nodeName.toLowerCase() == "wddxpacket")
  220.       {
  221.          _loc3_ = _loc4_.childNodes;
  222.          var _loc1_ = 0;
  223.          while(_loc3_[_loc1_].nodeName.toLowerCase() != "data" && _loc1_ < _loc3_.length)
  224.          {
  225.             _loc1_ = _loc1_ + 1;
  226.          }
  227.          if(_loc1_ < _loc3_.length)
  228.          {
  229.             _loc5_ = _loc6_.deserializeNode(_loc3_[_loc1_].firstChild);
  230.             return _loc5_;
  231.          }
  232.          return null;
  233.       }
  234.       return null;
  235.    }
  236.    function deserializeNode(node)
  237.    {
  238.       var _loc9_ = node.nodeName.toLowerCase();
  239.       if(_loc9_ == "number")
  240.       {
  241.          var _loc7_ = node.firstChild.nodeValue;
  242.          return Number(_loc7_);
  243.       }
  244.       if(_loc9_ == "boolean")
  245.       {
  246.          _loc7_ = String(node.attributes.value).toLowerCase() == "true";
  247.          return _loc7_;
  248.       }
  249.       if(_loc9_ == "string")
  250.       {
  251.          if(node.childNodes.length > 1)
  252.          {
  253.             _loc7_ = "";
  254.             var _loc4_ = 0;
  255.             _loc4_ = 0;
  256.             while(_loc4_ < node.childNodes.length)
  257.             {
  258.                if(node.childNodes[_loc4_].nodeType == 3)
  259.                {
  260.                   _loc7_ += this.deserializeString(node.childNodes[_loc4_].nodeValue);
  261.                }
  262.                else if(node.childNodes[_loc4_].nodeName == "char")
  263.                {
  264.                   _loc7_ += chr(parseInt(node.childNodes[_loc4_].attributes.code,16));
  265.                }
  266.                _loc4_ = _loc4_ + 1;
  267.             }
  268.          }
  269.          else
  270.          {
  271.             _loc7_ = this.deserializeString(node.firstChild.nodeValue);
  272.          }
  273.          return _loc7_;
  274.       }
  275.       if(_loc9_ == "array")
  276.       {
  277.          _loc7_ = new Array();
  278.          var _loc16_ = 0;
  279.          _loc4_ = 0;
  280.          while(_loc4_ < node.attributes.length)
  281.          {
  282.             _loc7_[_loc4_] = this.deserializeNode(node.childNodes[_loc4_].cloneNode(true));
  283.             _loc4_ = _loc4_ + 1;
  284.          }
  285.          return _loc7_;
  286.       }
  287.       if(_loc9_ == "datetime")
  288.       {
  289.          var _loc10_ = node.firstChild.nodeValue;
  290.          var _loc13_ = _loc10_.indexOf("T");
  291.          var _loc8_ = _loc10_.indexOf("+");
  292.          var _loc11_ = new Array();
  293.          var _loc12_ = new Array();
  294.          var _loc14_ = new Array();
  295.          _loc7_ = new Date();
  296.          if(_loc8_ == -1)
  297.          {
  298.             _loc8_ = _loc10_.lastIndexOf("-");
  299.             if(_loc8_ < _loc13_)
  300.             {
  301.                _loc8_ = -1;
  302.             }
  303.          }
  304.          _loc11_ = _loc10_.slice(0,_loc13_).split("-");
  305.          _loc12_ = _loc10_.slice(_loc13_ + 1,_loc8_).split(":");
  306.          _loc14_ = _loc10_.slice(_loc8_).split(":");
  307.          _loc7_.setFullYear(parseInt(_loc11_[0]),parseInt(_loc11_[1]) - 1,parseInt(_loc11_[2]));
  308.          _loc7_.setHours(parseInt(_loc12_[0]),parseInt(_loc12_[1]));
  309.          if(_loc8_ != -1)
  310.          {
  311.             var _loc15_ = parseInt(_loc14_[0]) * 60 + parseInt(_loc14_[1]);
  312.             _loc7_.setMinutes(_loc7_.getMinutes() - (_loc7_.getTimezoneOffset() + _loc15_));
  313.          }
  314.          return _loc7_;
  315.       }
  316.       if(_loc9_ == "struct")
  317.       {
  318.          _loc7_ = new Object();
  319.          _loc4_ = 0;
  320.          while(_loc4_ < node.childNodes.length)
  321.          {
  322.             if(node.childNodes[_loc4_].nodeName.toLowerCase() == "var")
  323.             {
  324.                _loc7_[this.deserializeAttr(node.childNodes[_loc4_].attributes.name)] = this.deserializeNode(node.childNodes[_loc4_].firstChild);
  325.             }
  326.             _loc4_ = _loc4_ + 1;
  327.          }
  328.          return _loc7_;
  329.       }
  330.       if(_loc9_ == "recordset")
  331.       {
  332.          _loc7_ = new com.neodelight.wddx.WddxRecordSet(node.attributes.fieldNames.split(",").reverse(),parseInt(node.attributes.rowCount));
  333.          _loc4_ = node.childNodes.length - 1;
  334.          while(_loc4_ >= 0)
  335.          {
  336.             if(node.childNodes[_loc4_].nodeName.toLowerCase() == "field")
  337.             {
  338.                var _loc5_ = this.deserializeAttr(node.childNodes[_loc4_].attributes.name);
  339.                _loc7_[_loc5_].wddxSerializationType = "field";
  340.                var _loc3_ = node.childNodes[_loc4_].childNodes.length - 1;
  341.                while(_loc3_ >= 0)
  342.                {
  343.                   _loc7_[_loc5_][_loc3_] = new Object();
  344.                   var _loc6_ = this.deserializeNode(node.childNodes[_loc4_].childNodes[_loc3_]);
  345.                   _loc7_.setField(_loc3_,_loc5_,_loc6_);
  346.                   _loc3_ = _loc3_ - 1;
  347.                }
  348.             }
  349.             _loc4_ = _loc4_ - 1;
  350.          }
  351.          return _loc7_;
  352.       }
  353.    }
  354.    function deserializeAttr(attr)
  355.    {
  356.       return attr;
  357.    }
  358.    function deserializeString(str)
  359.    {
  360.       return str;
  361.    }
  362. }
  363.