home *** CD-ROM | disk | FTP | other *** search
/ Computer Active 2010 August / CA08.iso / Multimedija / shufflr.air / ShufflrClient.swf / scripts / mx / utils / RPCObjectUtil.as < prev    next >
Encoding:
Text File  |  2010-06-23  |  15.4 KB  |  492 lines

  1. package mx.utils
  2. {
  3.    import flash.utils.Dictionary;
  4.    import flash.utils.describeType;
  5.    import flash.utils.getQualifiedClassName;
  6.    import flash.xml.XMLNode;
  7.    import mx.core.mx_internal;
  8.    
  9.    use namespace mx_internal;
  10.    
  11.    public class RPCObjectUtil
  12.    {
  13.       mx_internal static const VERSION:String = "3.5.0.12683";
  14.       
  15.       private static var defaultToStringExcludes:Array = ["password","credentials"];
  16.       
  17.       private static var refCount:int = 0;
  18.       
  19.       private static var CLASS_INFO_CACHE:Object = {};
  20.       
  21.       public function RPCObjectUtil()
  22.       {
  23.          super();
  24.       }
  25.       
  26.       private static function recordMetadata(param1:XMLList) : Object
  27.       {
  28.          var prop:XML = null;
  29.          var propName:String = null;
  30.          var metadataList:XMLList = null;
  31.          var metadata:Object = null;
  32.          var md:XML = null;
  33.          var mdName:String = null;
  34.          var argsList:XMLList = null;
  35.          var value:Object = null;
  36.          var arg:XML = null;
  37.          var existing:Object = null;
  38.          var argKey:String = null;
  39.          var argValue:String = null;
  40.          var existingArray:Array = null;
  41.          var properties:XMLList = param1;
  42.          var result:Object = null;
  43.          try
  44.          {
  45.             for each(prop in properties)
  46.             {
  47.                propName = prop.attribute("name").toString();
  48.                metadataList = prop.metadata;
  49.                if(metadataList.length() > 0)
  50.                {
  51.                   if(result == null)
  52.                   {
  53.                      result = {};
  54.                   }
  55.                   metadata = {};
  56.                   result[propName] = metadata;
  57.                   for each(md in metadataList)
  58.                   {
  59.                      mdName = md.attribute("name").toString();
  60.                      argsList = md.arg;
  61.                      value = {};
  62.                      for each(arg in argsList)
  63.                      {
  64.                         argKey = arg.attribute("key").toString();
  65.                         if(argKey != null)
  66.                         {
  67.                            argValue = arg.attribute("value").toString();
  68.                            value[argKey] = argValue;
  69.                         }
  70.                      }
  71.                      existing = metadata[mdName];
  72.                      if(existing != null)
  73.                      {
  74.                         if(existing is Array)
  75.                         {
  76.                            existingArray = existing as Array;
  77.                         }
  78.                         else
  79.                         {
  80.                            existingArray = [];
  81.                         }
  82.                         existingArray.push(value);
  83.                         existing = existingArray;
  84.                      }
  85.                      else
  86.                      {
  87.                         existing = value;
  88.                      }
  89.                      metadata[mdName] = existing;
  90.                   }
  91.                }
  92.             }
  93.          }
  94.          catch(e:Error)
  95.          {
  96.          }
  97.          return result;
  98.       }
  99.       
  100.       public static function toString(param1:Object, param2:Array = null, param3:Array = null) : String
  101.       {
  102.          if(param3 == null)
  103.          {
  104.             param3 = defaultToStringExcludes;
  105.          }
  106.          refCount = 0;
  107.          return internalToString(param1,0,null,param2,param3);
  108.       }
  109.       
  110.       private static function internalToString(param1:Object, param2:int = 0, param3:Dictionary = null, param4:Array = null, param5:Array = null) : String
  111.       {
  112.          var str:String = null;
  113.          var classInfo:Object = null;
  114.          var properties:Array = null;
  115.          var id:Object = null;
  116.          var isArray:Boolean = false;
  117.          var prop:* = undefined;
  118.          var j:int = 0;
  119.          var value:Object = param1;
  120.          var indent:int = param2;
  121.          var refs:Dictionary = param3;
  122.          var namespaceURIs:Array = param4;
  123.          var exclude:Array = param5;
  124.          var type:String = value == null ? "null" : typeof value;
  125.          switch(type)
  126.          {
  127.             case "boolean":
  128.             case "number":
  129.                return value.toString();
  130.             case "string":
  131.                return "\"" + value.toString() + "\"";
  132.             case "object":
  133.                if(value is Date)
  134.                {
  135.                   return value.toString();
  136.                }
  137.                if(value is XMLNode)
  138.                {
  139.                   return value.toString();
  140.                }
  141.                if(value is Class)
  142.                {
  143.                   return "(" + getQualifiedClassName(value) + ")";
  144.                }
  145.                classInfo = getClassInfo(value,exclude,{
  146.                   "includeReadOnly":true,
  147.                   "uris":namespaceURIs
  148.                });
  149.                properties = classInfo.properties;
  150.                str = "(" + classInfo.name + ")";
  151.                if(refs == null)
  152.                {
  153.                   refs = new Dictionary(true);
  154.                }
  155.                id = refs[value];
  156.                if(id != null)
  157.                {
  158.                   str += "#" + int(id);
  159.                   return str;
  160.                }
  161.                if(value != null)
  162.                {
  163.                   str += "#" + refCount.toString();
  164.                   refs[value] = refCount;
  165.                   ++refCount;
  166.                }
  167.                isArray = value is Array;
  168.                indent += 2;
  169.                j = 0;
  170.                while(j < properties.length)
  171.                {
  172.                   str = newline(str,indent);
  173.                   prop = properties[j];
  174.                   if(isArray)
  175.                   {
  176.                      str += "[";
  177.                   }
  178.                   str += prop.toString();
  179.                   if(isArray)
  180.                   {
  181.                      str += "] ";
  182.                   }
  183.                   else
  184.                   {
  185.                      str += " = ";
  186.                   }
  187.                   try
  188.                   {
  189.                      str += internalToString(value[prop],indent,refs,namespaceURIs,exclude);
  190.                   }
  191.                   catch(e:Error)
  192.                   {
  193.                      str += "?";
  194.                   }
  195.                   j++;
  196.                }
  197.                indent -= 2;
  198.                return str;
  199.                break;
  200.             case "xml":
  201.                return value.toString();
  202.             default:
  203.                return "(" + type + ")";
  204.          }
  205.       }
  206.       
  207.       private static function getCacheKey(param1:Object, param2:Array = null, param3:Object = null) : String
  208.       {
  209.          var _loc5_:uint = 0;
  210.          var _loc6_:String = null;
  211.          var _loc7_:String = null;
  212.          var _loc8_:String = null;
  213.          var _loc4_:String = getQualifiedClassName(param1);
  214.          if(param2 != null)
  215.          {
  216.             _loc5_ = 0;
  217.             while(_loc5_ < param2.length)
  218.             {
  219.                _loc6_ = param2[_loc5_] as String;
  220.                if(_loc6_ != null)
  221.                {
  222.                   _loc4_ += _loc6_;
  223.                }
  224.                _loc5_++;
  225.             }
  226.          }
  227.          if(param3 != null)
  228.          {
  229.             for(_loc7_ in param3)
  230.             {
  231.                _loc4_ += _loc7_;
  232.                _loc8_ = param3[_loc7_] as String;
  233.                if(_loc8_ != null)
  234.                {
  235.                   _loc4_ += _loc8_;
  236.                }
  237.             }
  238.          }
  239.          return _loc4_;
  240.       }
  241.       
  242.       private static function newline(param1:String, param2:int = 0) : String
  243.       {
  244.          var _loc3_:* = param1;
  245.          _loc3_ += "\n";
  246.          var _loc4_:int = 0;
  247.          while(_loc4_ < param2)
  248.          {
  249.             _loc3_ += " ";
  250.             _loc4_++;
  251.          }
  252.          return _loc3_;
  253.       }
  254.       
  255.       public static function getClassInfo(param1:Object, param2:Array = null, param3:Object = null) : Object
  256.       {
  257.          var propertyNames:Array;
  258.          var isArray:Boolean;
  259.          var excludeObject:Object;
  260.          var dynamic:Boolean;
  261.          var n:int = 0;
  262.          var i:int = 0;
  263.          var result:Object = null;
  264.          var cacheKey:String = null;
  265.          var className:String = null;
  266.          var classAlias:String = null;
  267.          var properties:XMLList = null;
  268.          var prop:XML = null;
  269.          var metadataInfo:Object = null;
  270.          var classInfo:XML = null;
  271.          var numericIndex:Boolean = false;
  272.          var p:String = null;
  273.          var pi:Number = NaN;
  274.          var uris:Array = null;
  275.          var uri:String = null;
  276.          var qName:QName = null;
  277.          var j:int = 0;
  278.          var obj:Object = param1;
  279.          var excludes:Array = param2;
  280.          var options:Object = param3;
  281.          if(options == null)
  282.          {
  283.             options = {
  284.                "includeReadOnly":true,
  285.                "uris":null,
  286.                "includeTransient":true
  287.             };
  288.          }
  289.          propertyNames = [];
  290.          dynamic = false;
  291.          if(typeof obj == "xml")
  292.          {
  293.             className = "XML";
  294.             properties = obj.text();
  295.             if(properties.length())
  296.             {
  297.                propertyNames.push("*");
  298.             }
  299.             properties = obj.attributes();
  300.          }
  301.          else
  302.          {
  303.             classInfo = describeType(obj);
  304.             className = classInfo.@name.toString();
  305.             classAlias = classInfo.@alias.toString();
  306.             dynamic = classInfo.@isDynamic.toString() == "true";
  307.             if(options.includeReadOnly)
  308.             {
  309.                properties = classInfo..accessor.(@access != "writeonly") + classInfo..variable;
  310.             }
  311.             else
  312.             {
  313.                properties = classInfo..accessor.(@access == "readwrite") + classInfo..variable;
  314.             }
  315.             numericIndex = false;
  316.          }
  317.          if(!dynamic)
  318.          {
  319.             cacheKey = getCacheKey(obj,excludes,options);
  320.             result = CLASS_INFO_CACHE[cacheKey];
  321.             if(result != null)
  322.             {
  323.                return result;
  324.             }
  325.          }
  326.          result = {};
  327.          result["name"] = className;
  328.          result["alias"] = classAlias;
  329.          result["properties"] = propertyNames;
  330.          result["dynamic"] = dynamic;
  331.          result["metadata"] = metadataInfo = recordMetadata(properties);
  332.          excludeObject = {};
  333.          if(excludes)
  334.          {
  335.             n = int(excludes.length);
  336.             i = 0;
  337.             while(i < n)
  338.             {
  339.                excludeObject[excludes[i]] = 1;
  340.                i++;
  341.             }
  342.          }
  343.          isArray = className == "Array";
  344.          if(dynamic)
  345.          {
  346.             for(p in obj)
  347.             {
  348.                if(excludeObject[p] != 1)
  349.                {
  350.                   if(isArray)
  351.                   {
  352.                      pi = parseInt(p);
  353.                      if(isNaN(pi))
  354.                      {
  355.                         propertyNames.push(new QName("",p));
  356.                      }
  357.                      else
  358.                      {
  359.                         propertyNames.push(pi);
  360.                      }
  361.                   }
  362.                   else
  363.                   {
  364.                      propertyNames.push(new QName("",p));
  365.                   }
  366.                }
  367.             }
  368.             numericIndex = isArray && !isNaN(Number(p));
  369.          }
  370.          if(!(className == "Object" || isArray))
  371.          {
  372.             if(className == "XML")
  373.             {
  374.                n = int(properties.length());
  375.                i = 0;
  376.                while(i < n)
  377.                {
  378.                   p = properties[i].name();
  379.                   if(excludeObject[p] != 1)
  380.                   {
  381.                      propertyNames.push(new QName("","@" + p));
  382.                   }
  383.                   i++;
  384.                }
  385.             }
  386.             else
  387.             {
  388.                n = int(properties.length());
  389.                uris = options.uris;
  390.                i = 0;
  391.                for(; i < n; i++)
  392.                {
  393.                   prop = properties[i];
  394.                   p = prop.@name.toString();
  395.                   uri = prop.@uri.toString();
  396.                   if(excludeObject[p] != 1)
  397.                   {
  398.                      if(!(!options.includeTransient && internalHasMetadata(metadataInfo,p,"Transient")))
  399.                      {
  400.                         if(uris != null)
  401.                         {
  402.                            if(uris.length == 1 && uris[0] == "*")
  403.                            {
  404.                               qName = new QName(uri,p);
  405.                               try
  406.                               {
  407.                                  obj[qName];
  408.                                  propertyNames.push();
  409.                               }
  410.                               catch(e:Error)
  411.                               {
  412.                               }
  413.                            }
  414.                            else
  415.                            {
  416.                               j = 0;
  417.                               while(j < uris.length)
  418.                               {
  419.                                  uri = uris[j];
  420.                                  if(prop.@uri.toString() == uri)
  421.                                  {
  422.                                     qName = new QName(uri,p);
  423.                                     try
  424.                                     {
  425.                                        obj[qName];
  426.                                        propertyNames.push(qName);
  427.                                     }
  428.                                     catch(e:Error)
  429.                                     {
  430.                                     }
  431.                                  }
  432.                                  j++;
  433.                               }
  434.                            }
  435.                         }
  436.                         else if(uri.length == 0)
  437.                         {
  438.                            qName = new QName(uri,p);
  439.                            try
  440.                            {
  441.                               obj[qName];
  442.                               propertyNames.push(qName);
  443.                            }
  444.                            catch(e:Error)
  445.                            {
  446.                            }
  447.                            continue;
  448.                         }
  449.                      }
  450.                   }
  451.                }
  452.             }
  453.          }
  454.          propertyNames.sort(Array.CASEINSENSITIVE | (numericIndex ? Array.NUMERIC : 0));
  455.          i = 0;
  456.          while(i < propertyNames.length - 1)
  457.          {
  458.             if(propertyNames[i].toString() == propertyNames[i + 1].toString())
  459.             {
  460.                propertyNames.splice(i,1);
  461.                i--;
  462.             }
  463.             i++;
  464.          }
  465.          if(!dynamic)
  466.          {
  467.             cacheKey = getCacheKey(obj,excludes,options);
  468.             CLASS_INFO_CACHE[cacheKey] = result;
  469.          }
  470.          return result;
  471.       }
  472.       
  473.       private static function internalHasMetadata(param1:Object, param2:String, param3:String) : Boolean
  474.       {
  475.          var _loc4_:Object = null;
  476.          if(param1 != null)
  477.          {
  478.             _loc4_ = param1[param2];
  479.             if(_loc4_ != null)
  480.             {
  481.                if(_loc4_[param3] != null)
  482.                {
  483.                   return true;
  484.                }
  485.             }
  486.          }
  487.          return false;
  488.       }
  489.    }
  490. }
  491.  
  492.