📄 wddx.js
字号:
/////////////////////////////////////////////////////////////////////////////// Filename: wddx.js//// Authors: Simeon Simeonov (simeons@allaire.com)// Nate Weiss (nweiss@icesinc.com)//// Last Modified: October 19, 1999//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// WddxSerializer////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// serializeValue() serializes any value that can be serialized// returns true/falsefunction wddxSerializer_serializeValue(obj){ var bSuccess = true; var val; if (obj == null) { // Null value this.write("<null/>"); } else if (typeof(val = obj.valueOf()) == "string") { // String value this.serializeString(val); } else if (typeof(val = obj.valueOf()) == "number") { // Distinguish between numbers and date-time values if ( typeof(obj.getTimezoneOffset) == "function" && typeof(obj.toGMTString) == "function") { // Possible Date // Note: getYear() fix is from David Flanagan's // "JS: The Definitive Guide". This code is Y2K safe. this.write("<dateTime>" + (obj.getYear() < 1000 ? 1900+obj.getYear() : obj.getYear()) + "-" + (obj.getMonth() + 1) + "-" + obj.getDate() + "T" + obj.getHours() + ":" + obj.getMinutes() + ":" + obj.getSeconds()); if (this.useTimezoneInfo) { this.write(this.timezoneString); } this.write("</dateTime>"); } else { // Number value this.write("<number>" + val + "</number>"); } } else if (typeof(val = obj.valueOf()) == "boolean") { // Boolean value this.write("<boolean value='" + val + "'/>"); } else if (typeof(obj) == "object") { 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") { // Possible Array this.write("<array length='" + obj.length + "'>"); for (var i = 0; bSuccess && i < obj.length; ++i) { bSuccess = this.serializeValue(obj[i]); } this.write("</array>"); } else { // Some generic object; treat it as a structure // Use the wddxSerializationType property as a guide as to its type if (typeof(obj.wddxSerializationType) == 'string') { this.write('<struct type="'+ obj.wddxSerializationType +'">') } else { this.write("<struct>"); } for (var prop in obj) { if (prop != 'wddxSerializationType') { bSuccess = this.serializeVariable(prop, obj[prop]); if (! bSuccess) { break; } } } this.write("</struct>"); } } else { // Error: undefined values or functions bSuccess = false; } // Successful serialization return bSuccess;}///////////////////////////////////////////////////////////////////////////// serializeAttr() serializes an attribute (such as a var tag) using JavaScript // functionality available in NS 3.0 and abovefunction wddxSerializer_serializeAttr(s){ for (var i = 0; i < s.length; ++i) { this.write(this.at[s.charAt(i)]); }}///////////////////////////////////////////////////////////////////////////// serializeAttrOld() serializes a string using JavaScript functionality// available in IE 3.0. We don't support special characters for IE3, so// just throw the unencoded text and hope for the bestfunction wddxSerializer_serializeAttrOld(s){ this.write(s);}///////////////////////////////////////////////////////////////////////////// serializeString() serializes a string using JavaScript functionality// available in NS 3.0 and abovefunction wddxSerializer_serializeString(s){ this.write("<string>"); for (var i = 0; i < s.length; ++i) { this.write(this.et[s.charAt(i)]); } this.write("</string>");}///////////////////////////////////////////////////////////////////////////// serializeStringOld() serializes a string using JavaScript functionality// available in IE 3.0function wddxSerializer_serializeStringOld(s){ this.write("<string><![CDATA["); pos = s.indexOf("]]>"); if (pos != -1) { startPos = 0; while (pos != -1) { this.write(s.substring(startPos, pos) + "]]>]]><![CDATA["); startPos = pos + 3; if (startPos < s.length) { pos = s.indexOf("]]>", startPos); } else { // Work around bug in indexOf() // "" will be returned instead of -1 if startPos > length pos = -1; } } this.write(s.substring(startPos, s.length)); } else { this.write(s); } this.write("]]></string>");}///////////////////////////////////////////////////////////////////////////// serializeVariable() serializes a property of a structure// returns true/falsefunction wddxSerializer_serializeVariable(name, obj){ var bSuccess = true; if (typeof(obj) != "function") { this.write("<var name='"); this.preserveVarCase ? this.serializeAttr(name) : this.serializeAttr(name.toLowerCase()); this.write("'>"); bSuccess = this.serializeValue(obj); this.write("</var>"); } return bSuccess;}///////////////////////////////////////////////////////////////////////////// write() appends text to the wddxPacket bufferfunction wddxSerializer_write(str){ this.wddxPacket += str;}///////////////////////////////////////////////////////////////////////////// serialize() creates a WDDX packet for a given object// returns the packet on success or null on failurefunction wddxSerializer_serialize(rootObj){ this.wddxPacket = ""; this.write("<wddxPacket version='1.0'><header/><data>"); var bSuccess = this.serializeValue(rootObj); this.write("</data></wddxPacket>"); if (bSuccess) { return this.wddxPacket; } else { return null; }}///////////////////////////////////////////////////////////////////////////// WddxSerializer() binds the function properties of the objectfunction WddxSerializer(){ // Compatibility section if (navigator.appVersion != "" && navigator.appVersion.indexOf("MSIE 3.") == -1) { // Character encoding table // Encoding table for strings (CDATA) var et = new Array(); // Numbers to characters table and // characters to numbers table var n2c = new Array(); var c2n = new Array(); // Encoding table for attributes (i.e. var=str) var at = new Array(); for (var i = 0; i < 256; ++i) { // Build a character from octal code var d1 = Math.floor(i/64); var d2 = Math.floor((i%64)/8); var d3 = i%8; var c = eval("\"\\" + d1.toString(10) + d2.toString(10) + d3.toString(10) + "\""); // Modify character-code conversion tables n2c[i] = c; c2n[c] = i; // Modify encoding table if (i < 32 && i != 9 && i != 10 && i != 13) { // Control characters that are not tabs, newlines, and carriage returns // Create a two-character hex code representation var hex = i.toString(16); if (hex.length == 1) { hex = "0" + hex; } et[n2c[i]] = "<char code='" + hex + "'/>"; // strip control chars from inside attrs at[n2c[i]] = ""; } else if (i < 128) { // Low characters that are not special control characters et[n2c[i]] = n2c[i]; // attr table at[n2c[i]] = n2c[i]; } else { // High characters et[n2c[i]] = "&#x" + i.toString(16) + ";"; at[n2c[i]] = "&#x" + i.toString(16) + ";"; } } // Special escapes for CDATA encoding et["<"] = "<"; et[">"] = ">"; et["&"] = "&"; // Special escapes for attr encoding at["<"] = "<"; at[">"] = ">"; at["&"] = "&"; at["'"] = "'"; at["\""] = """; // Store tables this.n2c = n2c; this.c2n = c2n; this.et = et; this.at = at; // The browser is not MSIE 3.x this.serializeString = wddxSerializer_serializeString; this.serializeAttr = wddxSerializer_serializeAttr; } else { // The browser is most likely MSIE 3.x, it is NS 2.0 compatible this.serializeString = wddxSerializer_serializeStringOld; this.serializeAttr = wddxSerializer_serializeAttrOld; } // Setup timezone information var tzOffset = (new Date()).getTimezoneOffset(); // Invert timezone offset to convert local time to UTC time if (tzOffset >= 0) { this.timezoneString = '-'; } else { this.timezoneString = '+'; } this.timezoneString += Math.floor(Math.abs(tzOffset) / 60) + ":" + (Math.abs(tzOffset) % 60); // Common properties this.preserveVarCase = false; this.useTimezoneInfo = true; // Common functions this.serialize = wddxSerializer_serialize; this.serializeValue = wddxSerializer_serializeValue; this.serializeVariable = wddxSerializer_serializeVariable; this.write = wddxSerializer_write;}/////////////////////////////////////////////////////////////////////////////// WddxRecordset/////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -