home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / Share / Java / ScriptBuilder / NOSB30_TRIAL.exe / data1.cab / Program_Files / Wddx / wddx.js < prev    next >
Encoding:
JavaScript  |  1998-10-05  |  7.0 KB  |  290 lines

  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. //    WddxSerializer
  4. //
  5. ///////////////////////////////////////////////////////////////////////////
  6.  
  7.  
  8. ///////////////////////////////////////////////////////////////////////////
  9. //     serializeValue() serializes any value that can be serialized
  10. //    returns true/false
  11. function wddxSerializer_serializeValue(obj)
  12. {
  13.     bSuccess = true;
  14.  
  15.     if (typeof(obj) == "string")
  16.     {
  17.         // String value
  18.         this.write("<string>" + obj + "</string>");
  19.     }
  20.     else if (typeof(obj) == "number")
  21.     {
  22.         // Number value
  23.         this.write("<number>" + obj + "</number>");
  24.     }
  25.     else if (typeof(obj) == "boolean")
  26.     {
  27.         // Boolean value
  28.         this.write("<boolean value='" + obj + "'/>");
  29.     }
  30.     else if (typeof(obj) == "object")
  31.     {
  32.         if (obj == null)
  33.         {
  34.             // Null values become empty strings
  35.             this.write("<string></string>");
  36.         }
  37.         else if (typeof(obj.wddxSerialize) == "function")
  38.         {
  39.             // Object knows how to serialize itself
  40.             bSuccess = obj.wddxSerialize(this);
  41.         }
  42.         else if (
  43.             typeof(obj.join) == "function" &&
  44.             typeof(obj.reverse) == "function" &&
  45.             typeof(obj.sort) == "function" &&
  46.             typeof(obj.length) == "number")
  47.         {
  48.             this.write("<array length='" + obj.length + "'>");
  49.             for (var i = 0; bSuccess && i < obj.length; ++i)
  50.             {
  51.                 bSuccess = this.serializeValue(obj[i]);
  52.             }
  53.             this.write("</array>");
  54.         }
  55.         else if (
  56.             typeof(obj.getTimezoneOffset) == "function" &&
  57.             typeof(obj.toGMTString) == "function")
  58.         {
  59.             // Possible Date
  60.             this.write(    "<dateTime>" + 
  61.                 obj.getYear() + "-" + (obj.getMonth() + 1) + "-" + obj.getDate() +
  62.                 "T" + obj.getHours() + ":" + obj.getMinutes() + ":" + obj.getSeconds() +
  63.                 "</dateTime>");
  64.         }
  65.         else
  66.         {
  67.             // Some generic object; treat it as a structure
  68.             this.write("<struct>");
  69.             for (prop in obj)
  70.             {
  71.                 bSuccess = this.serializeVariable(prop, obj[prop]);
  72.                 if (! bSuccess)
  73.                 {
  74.                     break;
  75.                 }
  76.             }
  77.             this.write("</struct>");
  78.         }
  79.     }
  80.     else
  81.     {
  82.         // Error: undefined values or functions
  83.         bSuccess = false;
  84.     }
  85.  
  86.     // Successful serialization
  87.     return bSuccess;
  88. }
  89.  
  90.  
  91. ///////////////////////////////////////////////////////////////////////////
  92. //     serializeVariable() serializes a property of a structure
  93. //    returns true/false
  94. function wddxSerializer_serializeVariable(name, obj)
  95. {
  96.     bSuccess = true;
  97.     
  98.     if (typeof(obj) != "function")
  99.     {
  100.         this.write("<var name='" + name.toUpperCase() + "'>");
  101.         bSuccess = this.serializeValue(obj);
  102.         this.write("</var>");
  103.     }
  104.  
  105.     return bSuccess;
  106. }
  107.  
  108.  
  109. ///////////////////////////////////////////////////////////////////////////
  110. //    write() appends text to the wddxPacket buffer
  111. function wddxSerializer_write(str)
  112. {
  113.     this.wddxPacket += str;
  114. }
  115.  
  116.  
  117. ///////////////////////////////////////////////////////////////////////////
  118. //    serialize() creates a WDDX packet for a given object
  119. //    returns the packet on success or null on failure
  120. function wddxSerializer_serialize(rootObj)
  121. {
  122.     this.wddxPacket = "";
  123.  
  124.     this.write("<wddxPacket version='0.9'><header/><data>");
  125.     bSuccess = this.serializeValue(rootObj);
  126.     this.write("</data></wddxPacket>");
  127.  
  128.     if (bSuccess)
  129.     {
  130.         return this.wddxPacket;
  131.     }
  132.     else
  133.     {    
  134.         return null;
  135.     }
  136. }
  137.  
  138.  
  139. ///////////////////////////////////////////////////////////////////////////
  140. // WddxSerializer() binds the function properties of the object
  141. function WddxSerializer()
  142. {
  143.     this.serialize = wddxSerializer_serialize;
  144.     this.serializeValue = wddxSerializer_serializeValue;
  145.     this.serializeVariable = wddxSerializer_serializeVariable;
  146.     this.write = wddxSerializer_write;
  147. }
  148.  
  149.  
  150. ///////////////////////////////////////////////////////////////////////////
  151. //
  152. //    WddxRecordset
  153. //
  154. ///////////////////////////////////////////////////////////////////////////
  155.  
  156.  
  157. ///////////////////////////////////////////////////////////////////////////
  158. //     getRowCount() returns the number of rows in the recordset
  159. function wddxRecordset_getRowCount()
  160. {
  161.     nRowCount = 0;
  162.     for (col in this)
  163.     {
  164.         if (typeof(this[col]) == "object")
  165.         {
  166.             nRowCount = this[col].length;
  167.             break;
  168.         }
  169.     }
  170.     return nRowCount;
  171. }
  172.  
  173.  
  174. ///////////////////////////////////////////////////////////////////////////
  175. //     addColumn(name) adds a column with that name and length == getRowCount()
  176. function wddxRecordset_addColumn(name)
  177. {
  178.     nLen = this.getRowCount();
  179.     colValue = new Array(nLen);
  180.     for (i = 0; i < nLen; ++i)
  181.     {
  182.         colValue[i] = null;
  183.     }
  184.     this[name] = colValue;
  185. }
  186.  
  187.  
  188. ///////////////////////////////////////////////////////////////////////////
  189. //     addRows() adds n rows to all columns of the recordset
  190. function wddxRecordset_addRows(n)
  191. {
  192.     for (col in this)
  193.     {
  194.         nLen = this[col].length;
  195.         for (i = nLen; i < nLen + n; ++i)
  196.         {
  197.             this[col][i] = null;
  198.         }
  199.     }
  200. }
  201.  
  202.  
  203. ///////////////////////////////////////////////////////////////////////////
  204. //     getField() returns the element in a given (row, col) position
  205. function wddxRecordset_getField(row, col)
  206. {
  207.     return this[col][row];
  208. }
  209.  
  210.  
  211. ///////////////////////////////////////////////////////////////////////////
  212. //     setField() sets the element in a given (row, col) position to value
  213. function wddxRecordset_setField(row, col, value)
  214. {
  215.     this[col][row] = value;
  216. }
  217.  
  218.  
  219. ///////////////////////////////////////////////////////////////////////////
  220. //     wddxSerialize() serializes a recordset
  221. //    returns true/false
  222. function wddxRecordset_wddxSerialize(serializer)
  223. {
  224.     // Create an array of column names
  225.     colNames = new Array();
  226.     i = 0;
  227.     for (col in this)
  228.     {
  229.         if (typeof(this[col]) == "object")
  230.         {
  231.             colNames[i++] = col;
  232.         }
  233.     }
  234.     
  235.     nRows = this.getRowCount();
  236.     
  237.     serializer.write("<recordset rowCount='" + nRows + "' fieldNames='" + colNames.join(",") + "'>");
  238.     
  239.     bSuccess = true;
  240.     for (i = 0; bSuccess && i < colNames.length; ++i)
  241.     {
  242.         name = colNames[i];
  243.         serializer.write("<field name='" + name + "'>");
  244.         
  245.         for (row = 0; bSuccess && row < nRows; ++row)
  246.         {
  247.             bSuccess = serializer.serializeValue(this[name][row]);
  248.         }
  249.         
  250.         serializer.write("</field>");
  251.     }
  252.     
  253.     serializer.write("</recordset>");
  254.     
  255.     return bSuccess;
  256. }
  257.  
  258.  
  259. ///////////////////////////////////////////////////////////////////////////
  260. // WddxRecordset() creates an empty recordset
  261. // WddxRecordset(columns) creates a recordset with these columns
  262. // WddxRecordset(columns, rows) creates a recordset with these columns and some number of rows
  263. function WddxRecordset()
  264. {
  265.     this.getRowCount = wddxRecordset_getRowCount;
  266.     this.addColumn = wddxRecordset_addColumn;
  267.     this.addRows = wddxRecordset_addRows;
  268.     this.getField = wddxRecordset_getField;
  269.     this.setField = wddxRecordset_setField;
  270.     this.wddxSerialize = wddxRecordset_wddxSerialize;
  271.     
  272.     if (WddxRecordset.arguments.length > 0)
  273.     {
  274.         cols = WddxRecordset.arguments[0];
  275.         nLen = WddxRecordset.arguments.length > 1 ? WddxRecordset.arguments[1] : 0;
  276.         
  277.         for (i = 0; i < cols.length; ++i)
  278.         {
  279.              colValue = new Array(nLen);
  280.             for (j = 0; j < nLen; ++j)
  281.             {
  282.                 colValue[j] = null;
  283.             }
  284.         
  285.             this[cols[i]] = colValue;
  286.         }        
  287.     }
  288. }
  289.  
  290.