home *** CD-ROM | disk | FTP | other *** search
/ One Click 21 (Special) / OC021.iso / Interface / it.dig / scripts / __Packages / mx / data / binding / FieldAccessor.as < prev    next >
Encoding:
Text File  |  2006-07-04  |  16.5 KB  |  513 lines

  1. class mx.data.binding.FieldAccessor
  2. {
  3.    var component;
  4.    var property;
  5.    var parentObj;
  6.    var fieldName;
  7.    var m_location;
  8.    var type;
  9.    var index;
  10.    var xpath;
  11.    var data;
  12.    static var xmlNodeFactory = new XML();
  13.    function FieldAccessor(component, property, parentObj, fieldName, type, index, parentField)
  14.    {
  15.       this.component = component;
  16.       this.property = property;
  17.       this.parentObj = parentObj;
  18.       this.fieldName = fieldName;
  19.       if(component == parentObj)
  20.       {
  21.          this.m_location = undefined;
  22.       }
  23.       else if(parentField.m_location == undefined)
  24.       {
  25.          this.m_location = fieldName;
  26.       }
  27.       else
  28.       {
  29.          this.m_location = parentField.m_location + "." + fieldName;
  30.       }
  31.       this.type = type;
  32.       this.index = index;
  33.    }
  34.    function getValue()
  35.    {
  36.       var _loc2_ = this.getFieldData();
  37.       if(_loc2_ == null && this.type.value != undefined)
  38.       {
  39.          var _loc3_ = new mx.data.binding.TypedValue(this.type.value,"String");
  40.          _loc3_.getDefault = true;
  41.          this.component.getField(this.fieldName).setAnyTypedValue(_loc3_);
  42.          _loc2_ = _loc3_.value;
  43.       }
  44.       if(this.isXML(_loc2_) && _loc2_.childNodes.length == 1 && _loc2_.firstChild.nodeType == 3)
  45.       {
  46.          return _loc2_.firstChild.nodeValue;
  47.       }
  48.       return _loc2_;
  49.    }
  50.    function setValue(newValue, newTypedValue)
  51.    {
  52.       if(newTypedValue.getDefault)
  53.       {
  54.          newTypedValue.value = newValue;
  55.       }
  56.       else
  57.       {
  58.          if(this.xpath != null)
  59.          {
  60.             var _loc5_ = this.getFieldData();
  61.             if(_loc5_ != null)
  62.             {
  63.                mx.data.binding.FieldAccessor.setXMLData(_loc5_,newValue);
  64.             }
  65.             else
  66.             {
  67.                _global.__dataLogger.logData(this.component,"Can\'t assign to \'<property>:<xpath>\' because there is no element at the given path",this);
  68.             }
  69.          }
  70.          else if(this.isXML(this.parentObj))
  71.          {
  72.             if(this.type.category == "attribute")
  73.             {
  74.                this.parentObj.attributes[this.fieldName] = newValue;
  75.             }
  76.             else if(this.type.category != "array")
  77.             {
  78.                _loc5_ = this.getOrCreateFieldData();
  79.                mx.data.binding.FieldAccessor.setXMLData(_loc5_,newValue);
  80.             }
  81.          }
  82.          else
  83.          {
  84.             if(this.parentObj == null)
  85.             {
  86.                _global.__dataLogger.logData(this.component,"Can\'t set field \'<property>/<location>\' because the field doesn\'t exist",this);
  87.             }
  88.             this.parentObj[this.fieldName] = newValue;
  89.          }
  90.          this.component.propertyModified(this.property,this.xpath == null && this.parentObj == this.component,newTypedValue.type);
  91.       }
  92.    }
  93.    static function isActionScriptPath(str)
  94.    {
  95.       var _loc2_ = str.toLowerCase();
  96.       var _loc3_ = "0123456789abcdefghijklmnopqrstuvwxyz_.";
  97.       var _loc1_ = 0;
  98.       while(_loc1_ < _loc2_.length)
  99.       {
  100.          if(-1 == _loc3_.indexOf(_loc2_.charAt(_loc1_)))
  101.          {
  102.             return false;
  103.          }
  104.          _loc1_ = _loc1_ + 1;
  105.       }
  106.       return true;
  107.    }
  108.    static function createFieldAccessor(component, property, location, type, mustExist)
  109.    {
  110.       if(mustExist && component[property] == null)
  111.       {
  112.          _global.__dataLogger.logData(component,"Warning: property \'<property>\' does not exist",{property:property});
  113.          return null;
  114.       }
  115.       var _loc5_ = new mx.data.binding.FieldAccessor(component,property,component,property,type,null,null);
  116.       if(location == null)
  117.       {
  118.          return _loc5_;
  119.       }
  120.       var _loc7_ = null;
  121.       if(location.indices != null)
  122.       {
  123.          _loc7_ = location.indices;
  124.          location = location.path;
  125.       }
  126.       if(typeof location == "string")
  127.       {
  128.          if(_loc7_ != null)
  129.          {
  130.             _global.__dataLogger.logData(component,"Warning: ignoring index values for property \'<property>\', path \'<location>\'",{property:property,location:location});
  131.          }
  132.          if(!mx.data.binding.FieldAccessor.isActionScriptPath(String(location)))
  133.          {
  134.             _loc5_.xpath = location;
  135.             return _loc5_;
  136.          }
  137.          location = location.split(".");
  138.       }
  139.       if(location instanceof Array)
  140.       {
  141.          var _loc3_ = undefined;
  142.          var _loc11_ = 0;
  143.          _loc3_ = 0;
  144.          while(_loc3_ < location.length)
  145.          {
  146.             var _loc2_ = null;
  147.             var _loc4_ = location[_loc3_];
  148.             if(_loc4_ == "[n]")
  149.             {
  150.                if(_loc7_ == null)
  151.                {
  152.                   _global.__dataLogger.logData(component,"Error: indices for <property>:<location> are null, but [n] appears in the location.",{property:property,location:location});
  153.                   return null;
  154.                }
  155.                _loc2_ = _loc7_[_loc11_++];
  156.                if(_loc2_ == null)
  157.                {
  158.                   _global.__dataLogger.logData(component,"Error: not enough index values for <property>:<location>",{property:property,location:location});
  159.                   return null;
  160.                }
  161.             }
  162.             _loc5_ = _loc5_.getChild(_loc4_,_loc2_,mustExist);
  163.             _loc3_ = _loc3_ + 1;
  164.          }
  165.          if(mustExist && _loc5_.getValue() == null)
  166.          {
  167.             _global.__dataLogger.logData(component,"Warning: field <property>:<m_location> does not exist, or is null",_loc5_);
  168.          }
  169.          return _loc5_;
  170.       }
  171.       trace("unrecognized location: " + mx.data.binding.ObjectDumper.toString(location));
  172.       return null;
  173.    }
  174.    function getFieldAccessor()
  175.    {
  176.       return this;
  177.    }
  178.    function getChild(childName, index, mustExist)
  179.    {
  180.       if(childName == ".")
  181.       {
  182.          return this;
  183.       }
  184.       var _loc2_ = this.getOrCreateFieldData(mustExist);
  185.       if(_loc2_ == null)
  186.       {
  187.          return null;
  188.       }
  189.       var _loc4_ = mx.data.binding.FieldAccessor.findElementType(this.type,childName);
  190.       return new mx.data.binding.FieldAccessor(this.component,this.property,_loc2_,childName,_loc4_,index,this);
  191.    }
  192.    function getOrCreateFieldData(mustExist)
  193.    {
  194.       var _loc3_ = this.getFieldData();
  195.       if(_loc3_ == null)
  196.       {
  197.          if(mustExist)
  198.          {
  199.             _global.__dataLogger.logData(this.component,"Warning: field <property>:<m_location> does not exist",this);
  200.          }
  201.          else
  202.          {
  203.             this.setupComplexField();
  204.             _loc3_ = this.getFieldData();
  205.          }
  206.       }
  207.       return _loc3_;
  208.    }
  209.    function evaluateSubPath(obj, type)
  210.    {
  211.       var path = type.path;
  212.       if(mx.data.binding.FieldAccessor.isActionScriptPath(path))
  213.       {
  214.          var tokens = path.split(".");
  215.          var i = 0;
  216.          while(i < tokens.length)
  217.          {
  218.             var token = tokens[i];
  219.             if(this.isXML(obj))
  220.             {
  221.                obj = obj.firstChild;
  222.                while(obj != null)
  223.                {
  224.                   if(mx.data.binding.FieldAccessor.toLocalName(obj.nodeName) == token)
  225.                   {
  226.                      break;
  227.                   }
  228.                   obj = obj.nextSibling;
  229.                }
  230.             }
  231.             else
  232.             {
  233.                obj = obj[token];
  234.             }
  235.             if(obj == null)
  236.             {
  237.                _global.__dataLogger.logData(this.component,"Warning: path \'<path>\' evaluates to null, at \'<token>\' in <t.property>:<t.m_location>",{path:path,token:token,t:this});
  238.                break;
  239.             }
  240.             i++;
  241.          }
  242.       }
  243.       else if(this.isXML(obj))
  244.       {
  245.          if(path.charAt(0) != "/")
  246.          {
  247.             path = "/" + path;
  248.          }
  249.          path = mx.data.binding.FieldAccessor.toLocalName(obj.nodeName) + path;
  250.          var category = type.category == null ? (type.elements.length <= 0 ? "simple" : "complex") : type.category;
  251.          if(category == "simple" || category == "attribute")
  252.          {
  253.             obj = eval("obj" + mx.xpath.XPathAPI.getEvalString(obj,path));
  254.          }
  255.          else if(category == "complex")
  256.          {
  257.             obj = mx.xpath.XPathAPI.selectSingleNode(obj,path);
  258.          }
  259.          else if(category == "array")
  260.          {
  261.             obj = mx.xpath.XPathAPI.selectNodeList(obj,path);
  262.          }
  263.       }
  264.       else
  265.       {
  266.          _global.__dataLogger.logData(this.component,"Error: path \'<path>\' is an XPath. It cannot be applied to non-XML data <t.property>:<t.m_location>",{path:path,t:this});
  267.       }
  268.       return obj;
  269.    }
  270.    function getFieldData()
  271.    {
  272.       if(this.xpath != null)
  273.       {
  274.          var _loc4_ = this.parentObj[this.fieldName].firstChild;
  275.          while(_loc4_ != null && _loc4_.nodeType != 1)
  276.          {
  277.             _loc4_ = _loc4_.nextSibling;
  278.          }
  279.          var _loc10_ = mx.xpath.XPathAPI.selectSingleNode(_loc4_,this.xpath);
  280.          return _loc10_;
  281.       }
  282.       if(this.isXML(this.parentObj))
  283.       {
  284.          if(this.type.path != null)
  285.          {
  286.             return this.evaluateSubPath(this.parentObj,this.type);
  287.          }
  288.          if(this.type.category == "attribute")
  289.          {
  290.             var _loc5_ = this.parentObj.attributes;
  291.             for(var _loc8_ in _loc5_)
  292.             {
  293.                if(mx.data.binding.FieldAccessor.toLocalName(_loc8_) == this.fieldName)
  294.                {
  295.                   return _loc5_[_loc8_];
  296.                }
  297.             }
  298.             return undefined;
  299.          }
  300.          var _loc3_ = this.parentObj.firstChild;
  301.          if(this.type.category == "array")
  302.          {
  303.             var _loc6_ = new Array();
  304.             while(_loc3_ != null)
  305.             {
  306.                if(mx.data.binding.FieldAccessor.toLocalName(_loc3_.nodeName) == this.fieldName)
  307.                {
  308.                   _loc6_.push(_loc3_);
  309.                }
  310.                _loc3_ = _loc3_.nextSibling;
  311.             }
  312.             return _loc6_;
  313.          }
  314.          while(_loc3_ != null)
  315.          {
  316.             if(mx.data.binding.FieldAccessor.toLocalName(_loc3_.nodeName) == this.fieldName)
  317.             {
  318.                return _loc3_;
  319.             }
  320.             _loc3_ = _loc3_.nextSibling;
  321.          }
  322.          return null;
  323.       }
  324.       if(this.fieldName == "[n]")
  325.       {
  326.          var _loc7_ = undefined;
  327.          if(this.index.component != null)
  328.          {
  329.             var _loc9_ = this.index.component.getField(this.index.property,this.index.location);
  330.             _loc7_ = _loc9_.getAnyTypedValue(["Number"]);
  331.             _loc7_ = _loc7_.value;
  332.          }
  333.          else
  334.          {
  335.             _loc7_ = this.index.constant;
  336.          }
  337.          var _loc11_ = Number(_loc7_);
  338.          if(typeof _loc7_ == "undefined")
  339.          {
  340.             _global.__dataLogger.logData(this.component,"Error: index specification \'<index>\' was not supplied, or incorrect, for <t.property>:<t.m_location>",{index:_loc11_,t:this});
  341.             return null;
  342.          }
  343.          if(_loc11_.toString() == "NaN")
  344.          {
  345.             _global.__dataLogger.logData(this.component,"Error: index value \'<index>\' for <t.property>:<t.m_location> is not a number",{index:_loc11_,t:this});
  346.             return null;
  347.          }
  348.          if(!(this.parentObj instanceof Array))
  349.          {
  350.             _global.__dataLogger.logData(this.component,"Error: indexed field <property>:<m_location> is not an array",this);
  351.             return null;
  352.          }
  353.          if(_loc11_ < 0 || _loc11_ >= this.parentObj.length)
  354.          {
  355.             _global.__dataLogger.logData(this.component,"Error: index \'<index>\' for <t.property>:<t.m_location> is out of bounds",{index:_loc11_,t:this});
  356.             return null;
  357.          }
  358.          _global.__dataLogger.logData(this.component,"Accessing item [<index>] of <t.property>:<t.m_location>",{index:_loc11_,t:this});
  359.          return this.parentObj[_loc11_];
  360.       }
  361.       if(this.type.path != null)
  362.       {
  363.          return this.evaluateSubPath(this.parentObj,this.type);
  364.       }
  365.       return this.parentObj[this.fieldName];
  366.    }
  367.    static function setXMLData(obj, newValue)
  368.    {
  369.       while(obj.hasChildNodes())
  370.       {
  371.          obj.firstChild.removeNode();
  372.       }
  373.       var _loc2_ = mx.data.binding.FieldAccessor.xmlNodeFactory.createTextNode(newValue);
  374.       obj.appendChild(_loc2_);
  375.    }
  376.    function setupComplexField()
  377.    {
  378.       var _loc2_ = undefined;
  379.       if(this.isXML(this.parentObj))
  380.       {
  381.          _loc2_ = mx.data.binding.FieldAccessor.xmlNodeFactory.createElement(this.fieldName);
  382.          this.parentObj.appendChild(_loc2_);
  383.       }
  384.       else if(this.dataIsXML())
  385.       {
  386.          this.parentObj[this.fieldName] = new XML();
  387.       }
  388.       else
  389.       {
  390.          this.parentObj[this.fieldName] = new Object();
  391.       }
  392.    }
  393.    static function findElementType(type, name)
  394.    {
  395.       var _loc1_ = 0;
  396.       while(_loc1_ < type.elements.length)
  397.       {
  398.          if(type.elements[_loc1_].name == name)
  399.          {
  400.             return type.elements[_loc1_].type;
  401.          }
  402.          _loc1_ = _loc1_ + 1;
  403.       }
  404.       return null;
  405.    }
  406.    function isXML(obj)
  407.    {
  408.       return obj instanceof XMLNode;
  409.    }
  410.    function dataIsXML()
  411.    {
  412.       return this.type.name == "XML";
  413.    }
  414.    static function accessField(component, fieldName, desiredTypes)
  415.    {
  416.       var _loc1_ = undefined;
  417.       _loc1_ = desiredTypes[fieldName];
  418.       if(_loc1_ == null)
  419.       {
  420.          _loc1_ = desiredTypes.dflt;
  421.       }
  422.       if(_loc1_ == null)
  423.       {
  424.          _loc1_ = desiredTypes;
  425.       }
  426.       var _loc4_ = component.createField("data",[fieldName]);
  427.       var _loc2_ = _loc4_.getAnyTypedValue([_loc1_]);
  428.       return _loc2_.value;
  429.    }
  430.    static function ExpandRecord(obj, objectType, desiredTypes)
  431.    {
  432.       var _loc4_ = new Object();
  433.       mx.data.binding.ComponentMixins.initComponent(_loc4_);
  434.       _loc4_.data = obj;
  435.       _loc4_.__schema = {elements:[{name:"data",type:objectType}]};
  436.       var _loc2_ = new Object();
  437.       if(objectType.elements.length > 0)
  438.       {
  439.          var _loc3_ = 0;
  440.          while(_loc3_ < objectType.elements.length)
  441.          {
  442.             var _loc10_ = objectType.elements[_loc3_].name;
  443.             _loc2_[_loc10_] = mx.data.binding.FieldAccessor.accessField(_loc4_,_loc10_,desiredTypes);
  444.             _loc3_ = _loc3_ + 1;
  445.          }
  446.       }
  447.       else if(obj instanceof XML || obj instanceof XMLNode)
  448.       {
  449.          if(obj.childNodes.length == 1 && obj.firstChild.nodeType == 3)
  450.          {
  451.             return obj.firstChild.nodeValue;
  452.          }
  453.          var _loc5_ = obj.lastChild;
  454.          while(_loc5_ != null)
  455.          {
  456.             _loc10_ = mx.data.binding.FieldAccessor.toLocalName(_loc5_.nodeName);
  457.             if(_loc10_ != null && _loc2_[_loc10_] == null)
  458.             {
  459.                _loc2_[_loc10_] = mx.data.binding.FieldAccessor.accessField(_loc4_,_loc10_,desiredTypes);
  460.             }
  461.             _loc5_ = _loc5_.previousSibling;
  462.          }
  463.          for(_loc10_ in obj.attributes)
  464.          {
  465.             if(_loc2_[_loc10_] != null)
  466.             {
  467.                _global.__dataLogger.logData(null,"Warning: attribute \'<name>\' has same name as an element, in XML object <obj>",{name:_loc10_,obj:obj});
  468.             }
  469.             _loc2_[_loc10_] = mx.data.binding.FieldAccessor.accessField(_loc4_,_loc10_,desiredTypes);
  470.          }
  471.       }
  472.       else
  473.       {
  474.          if(typeof obj != "object")
  475.          {
  476.             return obj;
  477.          }
  478.          for(_loc10_ in obj)
  479.          {
  480.             _loc2_[_loc10_] = mx.data.binding.FieldAccessor.accessField(_loc4_,_loc10_,desiredTypes);
  481.          }
  482.       }
  483.       return _loc2_;
  484.    }
  485.    static function wrapArray(theArray, itemType, desiredTypes)
  486.    {
  487.       var _loc4_ = {getItemAt:function(index)
  488.       {
  489.          if(index < 0 || index >= this.data.length)
  490.          {
  491.             return undefined;
  492.          }
  493.          var _loc2_ = this.data[index];
  494.          if(_loc2_ == undefined)
  495.          {
  496.             return undefined;
  497.          }
  498.          var _loc3_ = mx.data.binding.FieldAccessor.ExpandRecord(_loc2_,this.type,desiredTypes);
  499.          return _loc3_;
  500.       },getItemID:function(index)
  501.       {
  502.          return index;
  503.       },data:theArray,type:itemType,length:theArray.length};
  504.       return _loc4_;
  505.    }
  506.    static function toLocalName(nodeName)
  507.    {
  508.       var _loc1_ = nodeName.split(":");
  509.       var _loc2_ = _loc1_[_loc1_.length - 1];
  510.       return _loc2_;
  511.    }
  512. }
  513.