home *** CD-ROM | disk | FTP | other *** search
/ One Click 5 / ONCE005.iso / Interface / it.dig / scripts / __Packages / mx / data / binding / FieldAccessor.as < prev    next >
Encoding:
Text File  |  2006-06-12  |  16.6 KB  |  520 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 _loc4_ = this.getFieldData();
  61.             if(_loc4_ != null)
  62.             {
  63.                mx.data.binding.FieldAccessor.setXMLData(_loc4_,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.                _loc4_ = this.getOrCreateFieldData();
  79.                mx.data.binding.FieldAccessor.setXMLData(_loc4_,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 _loc10_ = 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_[_loc10_++];
  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.          if(obj.nodeName == null)
  250.          {
  251.             obj = obj.firstChild;
  252.          }
  253.          else
  254.          {
  255.             path = mx.data.binding.FieldAccessor.toLocalName(obj.nodeName) + path;
  256.          }
  257.          var category = type.category == null ? (type.elements.length <= 0 ? "simple" : "complex") : type.category;
  258.          if(category == "simple" || category == "attribute")
  259.          {
  260.             obj = eval("obj" + mx.xpath.XPathAPI.getEvalString(obj,path));
  261.          }
  262.          else if(category == "complex")
  263.          {
  264.             obj = mx.xpath.XPathAPI.selectSingleNode(obj,path);
  265.          }
  266.          else if(category == "array")
  267.          {
  268.             obj = mx.xpath.XPathAPI.selectNodeList(obj,path);
  269.          }
  270.       }
  271.       else
  272.       {
  273.          _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});
  274.       }
  275.       return obj;
  276.    }
  277.    function getFieldData()
  278.    {
  279.       if(this.xpath != null)
  280.       {
  281.          var _loc4_ = this.parentObj[this.fieldName].firstChild;
  282.          while(_loc4_ != null && _loc4_.nodeType != 1)
  283.          {
  284.             _loc4_ = _loc4_.nextSibling;
  285.          }
  286.          var _loc10_ = mx.xpath.XPathAPI.selectSingleNode(_loc4_,this.xpath);
  287.          return _loc10_;
  288.       }
  289.       if(this.isXML(this.parentObj))
  290.       {
  291.          if(this.type.path != null)
  292.          {
  293.             return this.evaluateSubPath(this.parentObj,this.type);
  294.          }
  295.          if(this.type.category == "attribute")
  296.          {
  297.             var _loc5_ = this.parentObj.attributes;
  298.             for(var _loc8_ in _loc5_)
  299.             {
  300.                if(mx.data.binding.FieldAccessor.toLocalName(_loc8_) == this.fieldName)
  301.                {
  302.                   return _loc5_[_loc8_];
  303.                }
  304.             }
  305.             return undefined;
  306.          }
  307.          var _loc3_ = this.parentObj.firstChild;
  308.          if(this.type.category == "array")
  309.          {
  310.             var _loc6_ = new Array();
  311.             while(_loc3_ != null)
  312.             {
  313.                if(mx.data.binding.FieldAccessor.toLocalName(_loc3_.nodeName) == this.fieldName)
  314.                {
  315.                   _loc6_.push(_loc3_);
  316.                }
  317.                _loc3_ = _loc3_.nextSibling;
  318.             }
  319.             return _loc6_;
  320.          }
  321.          while(_loc3_ != null)
  322.          {
  323.             if(mx.data.binding.FieldAccessor.toLocalName(_loc3_.nodeName) == this.fieldName)
  324.             {
  325.                return _loc3_;
  326.             }
  327.             _loc3_ = _loc3_.nextSibling;
  328.          }
  329.          return null;
  330.       }
  331.       if(this.fieldName == "[n]")
  332.       {
  333.          var _loc7_ = undefined;
  334.          if(this.index.component != null)
  335.          {
  336.             var _loc9_ = this.index.component.getField(this.index.property,this.index.location);
  337.             _loc7_ = _loc9_.getAnyTypedValue(["Number"]);
  338.             _loc7_ = _loc7_.value;
  339.          }
  340.          else
  341.          {
  342.             _loc7_ = this.index.constant;
  343.          }
  344.          var index = Number(_loc7_);
  345.          if(typeof _loc7_ == "undefined")
  346.          {
  347.             _global.__dataLogger.logData(this.component,"Error: index specification \'<index>\' was not supplied, or incorrect, for <t.property>:<t.m_location>",{index:index,t:this});
  348.             return null;
  349.          }
  350.          if(index.toString() == "NaN")
  351.          {
  352.             _global.__dataLogger.logData(this.component,"Error: index value \'<index>\' for <t.property>:<t.m_location> is not a number",{index:index,t:this});
  353.             return null;
  354.          }
  355.          if(!(this.parentObj instanceof Array))
  356.          {
  357.             _global.__dataLogger.logData(this.component,"Error: indexed field <property>:<m_location> is not an array",this);
  358.             return null;
  359.          }
  360.          if(index < 0 || index >= this.parentObj.length)
  361.          {
  362.             _global.__dataLogger.logData(this.component,"Error: index \'<index>\' for <t.property>:<t.m_location> is out of bounds",{index:index,t:this});
  363.             return null;
  364.          }
  365.          _global.__dataLogger.logData(this.component,"Accessing item [<index>] of <t.property>:<t.m_location>",{index:index,t:this});
  366.          return this.parentObj[index];
  367.       }
  368.       if(this.type.path != null)
  369.       {
  370.          return this.evaluateSubPath(this.parentObj,this.type);
  371.       }
  372.       return this.parentObj[this.fieldName];
  373.    }
  374.    static function setXMLData(obj, newValue)
  375.    {
  376.       while(obj.hasChildNodes())
  377.       {
  378.          obj.firstChild.removeNode();
  379.       }
  380.       var _loc2_ = mx.data.binding.FieldAccessor.xmlNodeFactory.createTextNode(newValue);
  381.       obj.appendChild(_loc2_);
  382.    }
  383.    function setupComplexField()
  384.    {
  385.       var _loc2_ = undefined;
  386.       if(this.isXML(this.parentObj))
  387.       {
  388.          _loc2_ = mx.data.binding.FieldAccessor.xmlNodeFactory.createElement(this.fieldName);
  389.          this.parentObj.appendChild(_loc2_);
  390.       }
  391.       else if(this.dataIsXML())
  392.       {
  393.          this.parentObj[this.fieldName] = new XML();
  394.       }
  395.       else
  396.       {
  397.          this.parentObj[this.fieldName] = new Object();
  398.       }
  399.    }
  400.    static function findElementType(type, name)
  401.    {
  402.       var _loc1_ = 0;
  403.       while(_loc1_ < type.elements.length)
  404.       {
  405.          if(type.elements[_loc1_].name == name)
  406.          {
  407.             return type.elements[_loc1_].type;
  408.          }
  409.          _loc1_ = _loc1_ + 1;
  410.       }
  411.       return null;
  412.    }
  413.    function isXML(obj)
  414.    {
  415.       return obj instanceof XMLNode;
  416.    }
  417.    function dataIsXML()
  418.    {
  419.       return this.type.name == "XML";
  420.    }
  421.    static function accessField(component, fieldName, desiredTypes)
  422.    {
  423.       var _loc1_ = undefined;
  424.       _loc1_ = desiredTypes[fieldName];
  425.       if(_loc1_ == null)
  426.       {
  427.          _loc1_ = desiredTypes.dflt;
  428.       }
  429.       if(_loc1_ == null)
  430.       {
  431.          _loc1_ = desiredTypes;
  432.       }
  433.       var _loc4_ = component.createField("data",[fieldName]);
  434.       var _loc2_ = _loc4_.getAnyTypedValue([_loc1_]);
  435.       return _loc2_.value;
  436.    }
  437.    static function ExpandRecord(obj, objectType, desiredTypes)
  438.    {
  439.       var _loc4_ = new Object();
  440.       mx.data.binding.ComponentMixins.initComponent(_loc4_);
  441.       _loc4_.data = obj;
  442.       _loc4_.__schema = {elements:[{name:"data",type:objectType}]};
  443.       var _loc2_ = new Object();
  444.       if(objectType.elements.length > 0)
  445.       {
  446.          var _loc3_ = 0;
  447.          while(_loc3_ < objectType.elements.length)
  448.          {
  449.             var _loc10_ = objectType.elements[_loc3_].name;
  450.             _loc2_[_loc10_] = mx.data.binding.FieldAccessor.accessField(_loc4_,_loc10_,desiredTypes);
  451.             _loc3_ = _loc3_ + 1;
  452.          }
  453.       }
  454.       else if(obj instanceof XML || obj instanceof XMLNode)
  455.       {
  456.          if(obj.childNodes.length == 1 && obj.firstChild.nodeType == 3)
  457.          {
  458.             return obj.firstChild.nodeValue;
  459.          }
  460.          var _loc5_ = obj.lastChild;
  461.          while(_loc5_ != null)
  462.          {
  463.             _loc10_ = mx.data.binding.FieldAccessor.toLocalName(_loc5_.nodeName);
  464.             if(_loc10_ != null && _loc2_[_loc10_] == null)
  465.             {
  466.                _loc2_[_loc10_] = mx.data.binding.FieldAccessor.accessField(_loc4_,_loc10_,desiredTypes);
  467.             }
  468.             _loc5_ = _loc5_.previousSibling;
  469.          }
  470.          for(_loc10_ in obj.attributes)
  471.          {
  472.             if(_loc2_[_loc10_] != null)
  473.             {
  474.                _global.__dataLogger.logData(null,"Warning: attribute \'<name>\' has same name as an element, in XML object <obj>",{name:_loc10_,obj:obj});
  475.             }
  476.             _loc2_[_loc10_] = mx.data.binding.FieldAccessor.accessField(_loc4_,_loc10_,desiredTypes);
  477.          }
  478.       }
  479.       else
  480.       {
  481.          if(typeof obj != "object")
  482.          {
  483.             return obj;
  484.          }
  485.          for(_loc10_ in obj)
  486.          {
  487.             _loc2_[_loc10_] = mx.data.binding.FieldAccessor.accessField(_loc4_,_loc10_,desiredTypes);
  488.          }
  489.       }
  490.       return _loc2_;
  491.    }
  492.    static function wrapArray(theArray, itemType, desiredTypes)
  493.    {
  494.       var _loc4_ = {getItemAt:function(index)
  495.       {
  496.          if(index < 0 || index >= this.data.length)
  497.          {
  498.             return undefined;
  499.          }
  500.          var _loc2_ = this.data[index];
  501.          if(_loc2_ == undefined)
  502.          {
  503.             return undefined;
  504.          }
  505.          var _loc3_ = mx.data.binding.FieldAccessor.ExpandRecord(_loc2_,this.type,desiredTypes);
  506.          return _loc3_;
  507.       },getItemID:function(index)
  508.       {
  509.          return index;
  510.       },data:theArray,type:itemType,length:theArray.length};
  511.       return _loc4_;
  512.    }
  513.    static function toLocalName(nodeName)
  514.    {
  515.       var _loc1_ = nodeName.split(":");
  516.       var _loc2_ = _loc1_[_loc1_.length - 1];
  517.       return _loc2_;
  518.    }
  519. }
  520.