/*
 * Author: Vertex Logic, Inc
 * Copyright 2005, 2006, 2007 - All rights reserved
 *
 * Vertex Logic, Inc., California, USA 
 *
 * Vertex Logic grants you ("Licensee") a non-exclusive license to use
 * and modify this source and recompile it in
 * accordance with the terms of the Agreement under which this software is bought
 * and provided that (i) this copyright notice appear on all copies of the Software; 
 * (ii) Licensee does not sale the software as is or with modification without
 * the prior consent of Vertex Logic. (iii) Licensee agrees that it does not have any 
 * title or ownership of the Software.
 *
 * The program is provided "as is" without any warranty express or
 * implied, including the warranty of non-infringement and the implied
 * warranties of merchantibility and fitness for a particular purpose.
 * Vertex Logic will not be liable for any damages suffered 
 * by you as a result of using the Program. 
 * In no event will Vertex Logic be liable for any
 * special, indirect or consequential damages or lost profits even if
 * Vertex Logic have been advised of the possibility of their occurrence. 
 * Vertex Logic will not be liable for any third party claims against you.
 */


/**
* Inside Data Source a path points to a Data Set. The path name is used to retrieve the
* actual data.
* Data inside data set is an Object Tree. (Object containing other objects as an array),
* rr, it is an array of Object Trees.
* In case of an array of trees, the array could be available partially. If it is partially available
* then an attribute "size" gives the actual size of the complete data set and "offset" gives the offset of the
* first entry in the complete data set.
*/



Af.XMLToDataSet = Class.create();

Af.XMLToDataSet.prototype = {
   
   initialize: function(xml) {
      this.data = new Object();
      if (systemProps['is_dev_env'] && !is_ie && is_ns) {
         try {
            netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
            } catch (e) {
            ("Permission UniversalBrowserRead denied.");
         }
      }
      
      var nodes = xml.childNodes;
      
      var rootNode = nodes[nodes.length-1];
      
      this.setIdFromTopNode(this.data, rootNode);
      var rootClass = null;
      if (rootNode.attributes != null) {
         var attr = rootNode.attributes.getNamedItem('cls');
         if (attr == null) {
            attr = rootNode.attributes.getNamedItem('class');
         }
         if (attr != null) {
            rootClass = attr.value;
            this.data.__className = rootClass;
         }
      }
      
      this.processXMLNode(this.data, rootNode);
   },
   
   setIdFromTopNode: function(obj, node) {
      var id = null;
      var fullId = null;
      var attr = node.attributes.getNamedItem('UUID');
      if (attr == null) {
         attr = node.attributes.getNamedItem('id');
         if (attr == null) {
            attr = node.attributes.getNamedItem('name');
         }
      }
      if (attr != null) {
         id = attr.value;
      }
      
      attr = node.attributes.getNamedItem('fullId');
      
      if (attr != null) {
         fullId = attr.value;
      }
      
      obj.__id = id;
      
      if (fullId == null)
      {
         var nodes = node.childNodes;
         // this is useful for the list (data set)
         for (var i = 0; i < nodes.length; i++) {
            var node2 = nodes[i];
            if (node2.tagName == 'name' && (
            	  // zb9a5f3132e is encoded name for Node variable.
                  node2.nodeType == zb9a5f3132e.ELEMENT_NODE || node2.nodeType == zb9a5f3132e.DOCUMENT_NODE
               || node2.nodeType == zb9a5f3132e.DOCUMENT_FRAGMENT_NODE)) {
               var a = node2.childNodes;
               if (a.length == 1) {
                  var gchild = a[0];
                  if (gchild.nodeType == zb9a5f3132e.TEXT_NODE) {
                     fullId = gchild.data;
                  }
               }
            }
         }
         
         if (node.tagName != "ObjList") {
            attr = node.attributes.getNamedItem('class');
            if (attr == null) {
               attr = node.attributes.getNamedItem('cls');
            }
            if (attr != null) {
               fullId = fullId + "." + attr.value;
            }
         }
      }
      obj.__fullId = fullId;
      
   },
   
   getIdFromInnerNode: function(node) {
      attr = node.attributes.getNamedItem('id');
      
      if (attr != null) {
         return attr.value;
      }
      
      return null;
   },
   
   processXMLNode: function(data, parent) {
      
      
      var nodes = parent.childNodes;
      
      for (var i = 0; i < nodes.length; i++) {
         var node = nodes[i];
         if (node.nodeType == zb9a5f3132e.TEXT_NODE && node.tagName != null) {
            //debugA(node.tagName + " :" + node.data);
            data[node.tagName] = node.data;
         } else if (node.nodeType == zb9a5f3132e.ELEMENT_NODE || node.nodeType == zb9a5f3132e.DOCUMENT_NODE
            || node.nodeType == zb9a5f3132e.DOCUMENT_FRAGMENT_NODE) {
            
            var a = node.childNodes;
            var attrs = node.attributes;
            if (a.length > 0 && attrs.length == 0) {
               if (data[node.tagName] == null) {
            	   data[node.tagName] = "";
               } else {
            	   data[node.tagName] += "\n";
               }
               for (var j=0; j<a.length; j++) {
	               var gchild = a[j];
	               if (gchild.nodeType == zb9a5f3132e.TEXT_NODE) {
					   data[node.tagName] += gchild.data;
	               }
               }
               continue;
            }
            
            var tagName = nodes[i].tagName;
            var myClass = null;
            var fullId = null;
            var actualFullId = null;
            var assoc = null;
            var attrs = node.attributes;
            if (attrs != null) {
               for (var j = 0; j<attrs.length; j++) {
                  var attr = attrs[j];
                  if (attr.name == 'cls' || attr.name == 'class') {
                     myClass = attr.value;
                  } else if (attr.name == 'atype') {
                     assoc = attr.value;
                  } else if (attr.name == 'fullId') {
                     fullId = attr.value;
                  } else if (attr.name == 'actualFullId') {
                     actualFullId = attr.value;
                  }
               }
            }
            
            if (myClass == null && node.childNodes == null || node.childNodes.length == 0) {
               continue;
            }
            
            
            var id = this.getIdFromInnerNode(node);
            
            if (id == null) {
               if (assoc == 'OO') {
                  id = 0;
               } else {
                  var c = data[tagName];
                  if (c) {
                     id = c.length;
                  } else {
                     id = 0;
                  }
               }
            }
            
            var data2 = null;
            
            if (_useFile) {
               if (fullId == null) {
                  var pid = getFullId(data);
                  fullId = pid + "." + myClass + "." + id;
                  data2 = dtCache.getByFullId(fullId); // look into indenpendent table
                  if (!data2) {
                     data2 = new Object();
                  }
                  data2.__parent = data; // this parent is its primary parent
               } else {
                  data2 = dtCache.getByFullId(fullId); // look into indenpendent table
                  if (!data2) {
                     data2 = dtCache.getByFullIdInDataSets(fullId); // extract the name of the data set from the full id and look into the data set
                     if (!data2) {
                        data2 = new Object();
                        data2.__parent = data;
                        dtCache.addToObjsTable(fullId, data2);
                     }
                  }
               }
            } else {
               data2 = new Object();
               data2.__parent = data;
            }
            
            data2.__className = myClass;
            data2.__id = id;
            data2.__fullId = fullId;
            data2.__atype = assoc;
            if (actualFullId != null) {
               data2.__actualFullId = actualFullId;
            }
            if (assoc == 'OO') {
               data[tagName] = data2;
            } else {
               var c = data[tagName];
               
               if (c) {
                  data2.__index = c.length;
                  c.push(data2);
               } else {
                  c = new Array();
                  c.__parent = data;  // list parent is the parent of the objects held by it
                  c.__className = myClass;// the class of the parent is same as the class of objects held by it
                  data[tagName] = c;
                  data2.__index = c.length;
                  c.push(data2);
               }
            }
            this.processXMLNode(data2, node);
         };
      }
      return data;
   }
   
};

