home *** CD-ROM | disk | FTP | other *** search
- ///////////////////////////////////////////////////////////////////////////
- //
- // WddxSerializer
- //
- ///////////////////////////////////////////////////////////////////////////
-
-
- ///////////////////////////////////////////////////////////////////////////
- // serializeValue() serializes any value that can be serialized
- // returns true/false
- function wddxSerializer_serializeValue(obj)
- {
- bSuccess = true;
-
- if (typeof(obj) == "string")
- {
- // String value
- this.write("<string>" + obj + "</string>");
- }
- else if (typeof(obj) == "number")
- {
- // Number value
- this.write("<number>" + obj + "</number>");
- }
- else if (typeof(obj) == "boolean")
- {
- // Boolean value
- this.write("<boolean value='" + obj + "'/>");
- }
- else if (typeof(obj) == "object")
- {
- if (obj == null)
- {
- // Null values become empty strings
- this.write("<string></string>");
- }
- else if (typeof(obj.wddxSerialize) == "function")
- {
- // Object knows how to serialize itself
- bSuccess = obj.wddxSerialize(this);
- }
- else if (
- typeof(obj.join) == "function" &&
- typeof(obj.reverse) == "function" &&
- typeof(obj.sort) == "function" &&
- typeof(obj.length) == "number")
- {
- this.write("<array length='" + obj.length + "'>");
- for (var i = 0; bSuccess && i < obj.length; ++i)
- {
- bSuccess = this.serializeValue(obj[i]);
- }
- this.write("</array>");
- }
- else if (
- typeof(obj.getTimezoneOffset) == "function" &&
- typeof(obj.toGMTString) == "function")
- {
- // Possible Date
- this.write( "<dateTime>" +
- obj.getYear() + "-" + (obj.getMonth() + 1) + "-" + obj.getDate() +
- "T" + obj.getHours() + ":" + obj.getMinutes() + ":" + obj.getSeconds() +
- "</dateTime>");
- }
- else
- {
- // Some generic object; treat it as a structure
- this.write("<struct>");
- for (prop in obj)
- {
- bSuccess = this.serializeVariable(prop, obj[prop]);
- if (! bSuccess)
- {
- break;
- }
- }
- this.write("</struct>");
- }
- }
- else
- {
- // Error: undefined values or functions
- bSuccess = false;
- }
-
- // Successful serialization
- return bSuccess;
- }
-
-
- ///////////////////////////////////////////////////////////////////////////
- // serializeVariable() serializes a property of a structure
- // returns true/false
- function wddxSerializer_serializeVariable(name, obj)
- {
- bSuccess = true;
-
- if (typeof(obj) != "function")
- {
- this.write("<var name='" + name.toUpperCase() + "'>");
- bSuccess = this.serializeValue(obj);
- this.write("</var>");
- }
-
- return bSuccess;
- }
-
-
- ///////////////////////////////////////////////////////////////////////////
- // write() appends text to the wddxPacket buffer
- function wddxSerializer_write(str)
- {
- this.wddxPacket += str;
- }
-
-
- ///////////////////////////////////////////////////////////////////////////
- // serialize() creates a WDDX packet for a given object
- // returns the packet on success or null on failure
- function wddxSerializer_serialize(rootObj)
- {
- this.wddxPacket = "";
-
- this.write("<wddxPacket version='0.9'><header/><data>");
- bSuccess = this.serializeValue(rootObj);
- this.write("</data></wddxPacket>");
-
- if (bSuccess)
- {
- return this.wddxPacket;
- }
- else
- {
- return null;
- }
- }
-
-
- ///////////////////////////////////////////////////////////////////////////
- // WddxSerializer() binds the function properties of the object
- function WddxSerializer()
- {
- this.serialize = wddxSerializer_serialize;
- this.serializeValue = wddxSerializer_serializeValue;
- this.serializeVariable = wddxSerializer_serializeVariable;
- this.write = wddxSerializer_write;
- }
-
-
- ///////////////////////////////////////////////////////////////////////////
- //
- // WddxRecordset
- //
- ///////////////////////////////////////////////////////////////////////////
-
-
- ///////////////////////////////////////////////////////////////////////////
- // getRowCount() returns the number of rows in the recordset
- function wddxRecordset_getRowCount()
- {
- nRowCount = 0;
- for (col in this)
- {
- if (typeof(this[col]) == "object")
- {
- nRowCount = this[col].length;
- break;
- }
- }
- return nRowCount;
- }
-
-
- ///////////////////////////////////////////////////////////////////////////
- // addColumn(name) adds a column with that name and length == getRowCount()
- function wddxRecordset_addColumn(name)
- {
- nLen = this.getRowCount();
- colValue = new Array(nLen);
- for (i = 0; i < nLen; ++i)
- {
- colValue[i] = null;
- }
- this[name] = colValue;
- }
-
-
- ///////////////////////////////////////////////////////////////////////////
- // addRows() adds n rows to all columns of the recordset
- function wddxRecordset_addRows(n)
- {
- for (col in this)
- {
- nLen = this[col].length;
- for (i = nLen; i < nLen + n; ++i)
- {
- this[col][i] = null;
- }
- }
- }
-
-
- ///////////////////////////////////////////////////////////////////////////
- // getField() returns the element in a given (row, col) position
- function wddxRecordset_getField(row, col)
- {
- return this[col][row];
- }
-
-
- ///////////////////////////////////////////////////////////////////////////
- // setField() sets the element in a given (row, col) position to value
- function wddxRecordset_setField(row, col, value)
- {
- this[col][row] = value;
- }
-
-
- ///////////////////////////////////////////////////////////////////////////
- // wddxSerialize() serializes a recordset
- // returns true/false
- function wddxRecordset_wddxSerialize(serializer)
- {
- // Create an array of column names
- colNames = new Array();
- i = 0;
- for (col in this)
- {
- if (typeof(this[col]) == "object")
- {
- colNames[i++] = col;
- }
- }
-
- nRows = this.getRowCount();
-
- serializer.write("<recordset rowCount='" + nRows + "' fieldNames='" + colNames.join(",") + "'>");
-
- bSuccess = true;
- for (i = 0; bSuccess && i < colNames.length; ++i)
- {
- name = colNames[i];
- serializer.write("<field name='" + name + "'>");
-
- for (row = 0; bSuccess && row < nRows; ++row)
- {
- bSuccess = serializer.serializeValue(this[name][row]);
- }
-
- serializer.write("</field>");
- }
-
- serializer.write("</recordset>");
-
- return bSuccess;
- }
-
-
- ///////////////////////////////////////////////////////////////////////////
- // WddxRecordset() creates an empty recordset
- // WddxRecordset(columns) creates a recordset with these columns
- // WddxRecordset(columns, rows) creates a recordset with these columns and some number of rows
- function WddxRecordset()
- {
- this.getRowCount = wddxRecordset_getRowCount;
- this.addColumn = wddxRecordset_addColumn;
- this.addRows = wddxRecordset_addRows;
- this.getField = wddxRecordset_getField;
- this.setField = wddxRecordset_setField;
- this.wddxSerialize = wddxRecordset_wddxSerialize;
-
- if (WddxRecordset.arguments.length > 0)
- {
- cols = WddxRecordset.arguments[0];
- nLen = WddxRecordset.arguments.length > 1 ? WddxRecordset.arguments[1] : 0;
-
- for (i = 0; i < cols.length; ++i)
- {
- colValue = new Array(nLen);
- for (j = 0; j < nLen; ++j)
- {
- colValue[j] = null;
- }
-
- this[cols[i]] = colValue;
- }
- }
- }
-
-