📄 nsxmlrpcclient.js
字号:
continue; array.appendValue(this.parseValue(node).value); } }, parseFault: function(node) { var fault = createInstance(XMLRPCFAULT_CONTRACTID, 'nsIXmlRpcFault'); var cValue = this.parseValue(node); if ((cValue.type != this.STRUCT) || (!cValue.value.hasKey('faultCode')) || (!cValue.value.hasKey('faultString'))) { throw Components.Exception('Invalid fault', null, null, node); } fault.init(cValue.value.getValue('faultCode').data, cValue.value.getValue('faultString').data); return fault; }};/* The XMLWriter class constructor */function XMLWriter(encoding) { if (!encoding) encoding = "UTF-8"; this.data = '<?xml version="1.0" encoding="' + encoding + '"?>';}/* The XMLWriter class def */XMLWriter.prototype = { data: '', startElement: function(element) { this.data += '<' + element + '>'; }, endElement: function(element) { this.data += '</' + element + '>'; }, write: function(text) { for (var i = 0; i < text.length; i++) { var c = text[i]; switch (c) { case '<': this.data += '<'; break; case '&': this.data += '&'; break; default: this.data += c; } } }, markup: function(text) { this.data += text }};/* The Value class contructor */function Value() { this.type = this.STRING; };/* The Value class def */Value.prototype = { INT: nsXmlRpcClient.prototype.INT, BOOLEAN: nsXmlRpcClient.prototype.BOOLEAN, STRING: nsXmlRpcClient.prototype.STRING, DOUBLE: nsXmlRpcClient.prototype.DOUBLE, DATETIME: nsXmlRpcClient.prototype.DATETIME, ARRAY: nsXmlRpcClient.prototype.ARRAY, STRUCT: nsXmlRpcClient.prototype.STRUCT, BASE64: nsXmlRpcClient.prototype.BASE64, _createType: nsXmlRpcClient.prototype.createType, name: null, _value: null, get value() { return this._value; }, set value(val) { // accepts [0-9]+ or x[0-9a-fA-F]+ and returns the character. function entityTrans(substr, code) { return String.fromCharCode("0" + code); } switch (this.type) { case this.STRING: val = val.replace(/&#([0-9]+);/g, entityTrans); val = val.replace(/&#(x[0-9a-fA-F]+);/g, entityTrans); val = val.replace(/</g, '<'); val = val.replace(/>/g, '>'); val = val.replace(/&/g, '&'); this._value.data = val; break; case this.BOOLEAN: this._value.data = (val == 1); break; case this.DATETIME: this._value.data = Date.UTC(val.slice(0, 4), val.slice(4, 6) - 1, val.slice(6, 8), val.slice(9, 11), val.slice(12, 14), val.slice(15)); break; case this.BASE64: this._value.data = base64ToString(val); break; default: this._value.data = val; } }, _type: null, get type() { return this._type; }, set type(type) { this._type = type; if (type == this.BASE64) this._value = this._createType(this.STRING, {}); else this._value = this._createType(type, {}); }, appendValue: function(val) { switch (this.type) { case this.ARRAY: this.value.AppendElement(val); break; case this.STRUCT: this.value.setValue(this.name, val); break; } }};/* * Objects *//* nsXmlRpcClient Module (for XPCOM registration) */var nsXmlRpcClientModule = { registerSelf: function(compMgr, fileSpec, location, type) { compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar); compMgr.registerFactoryLocation(XMLRPCCLIENT_CID, 'XML-RPC Client JS component', XMLRPCCLIENT_CONTRACTID, fileSpec, location, type); compMgr.registerFactoryLocation(XMLRPCFAULT_CID, 'XML-RPC Fault JS component', XMLRPCFAULT_CONTRACTID, fileSpec, location, type); }, getClassObject: function(compMgr, cid, iid) { if (!cid.equals(XMLRPCCLIENT_CID) && !cid.equals(XMLRPCFAULT_CID)) throw Components.results.NS_ERROR_NO_INTERFACE; if (!iid.equals(Components.interfaces.nsIFactory)) throw Components.results.NS_ERROR_NOT_IMPLEMENTED; if (cid.equals(XMLRPCCLIENT_CID)) return nsXmlRpcClientFactory else return nsXmlRpcFaultFactory; }, canUnload: function(compMgr) { return true; }};/* nsXmlRpcClient Class Factory */var nsXmlRpcClientFactory = { createInstance: function(outer, iid) { if (outer != null) throw Components.results.NS_ERROR_NO_AGGREGATION; if (!iid.equals(XMLRPCCLIENT_IID) && !iid.equals(Components.interfaces.nsISupports)) throw Components.results.NS_ERROR_INVALID_ARG; return new nsXmlRpcClient(); }}/* nsXmlRpcFault Class Factory */var nsXmlRpcFaultFactory = { createInstance: function(outer, iid) { if (outer != null) throw Components.results.NS_ERROR_NO_AGGREGATION; if (!iid.equals(XMLRPCFAULT_IID) && !iid.equals(Components.interfaces.nsISupports)) throw Components.results.NS_ERROR_INVALID_ARG; return new nsXmlRpcFault(); }}/* * Functions *//* module initialisation */function NSGetModule(comMgr, fileSpec) { return nsXmlRpcClientModule; }/* Create an instance of the given ContractID, with given interface */function createInstance(contractId, intf) { return Components.classes[contractId] .createInstance(Components.interfaces[intf]);}/* Get a pointer to a service indicated by the ContractID, with given interface */function getService(contractId, intf) { return Components.classes[contractId] .getService(Components.interfaces[intf]);}/* Convert an inputstream to a scriptable inputstream */function toScriptableStream(input) { var SIStream = Components.Constructor( '@mozilla.org/scriptableinputstream;1', 'nsIScriptableInputStream', 'init'); return new SIStream(input);}/* format a Date object into a iso8601 datetime string, UTC time */function iso8601Format(date) { var datetime = date.getUTCFullYear(); var month = String(date.getUTCMonth() + 1); datetime += (month.length == 1 ? '0' + month : month); var day = date.getUTCDate(); datetime += (day < 10 ? '0' + day : day); datetime += 'T'; var hour = date.getUTCHours(); datetime += (hour < 10 ? '0' + hour : hour) + ':'; var minutes = date.getUTCMinutes(); datetime += (minutes < 10 ? '0' + minutes : minutes) + ':'; var seconds = date.getUTCSeconds(); datetime += (seconds < 10 ? '0' + seconds : seconds); return datetime;}/* Convert a stream to Base64, writing it away to a string writer */const BASE64CHUNK = 255; // Has to be dividable by 3!!function streamToBase64(stream, writer) { while (stream.available()) { var data = []; while (data.length < BASE64CHUNK && stream.available()) { var d = stream.read(1).charCodeAt(0); // reading a 0 results in NaN, compensate. data = data.concat(isNaN(d) ? 0 : d); } writer.write(toBase64(data)); }}/* Convert data (an array of integers) to a Base64 string. */const toBase64Table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' + '0123456789+/';const base64Pad = '=';function toBase64(data) { var result = ''; var length = data.length; var i; // Convert every three bytes to 4 ascii characters. for (i = 0; i < (length - 2); i += 3) { result += toBase64Table[data[i] >> 2]; result += toBase64Table[((data[i] & 0x03) << 4) + (data[i+1] >> 4)]; result += toBase64Table[((data[i+1] & 0x0f) << 2) + (data[i+2] >> 6)]; result += toBase64Table[data[i+2] & 0x3f]; } // Convert the remaining 1 or 2 bytes, pad out to 4 characters. if (length%3) { i = length - (length%3); result += toBase64Table[data[i] >> 2]; if ((length%3) == 2) { result += toBase64Table[((data[i] & 0x03) << 4) + (data[i+1] >> 4)]; result += toBase64Table[(data[i+1] & 0x0f) << 2]; result += base64Pad; } else { result += toBase64Table[(data[i] & 0x03) << 4]; result += base64Pad + base64Pad; } } return result;}/* Convert Base64 data to a string */const toBinaryTable = [ -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63, 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1, 0,-1,-1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14, 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1, -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40, 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1];function base64ToString(data) { var result = ''; var leftbits = 0; // number of bits decoded, but yet to be appended var leftdata = 0; // bits decoded, but yet to be appended // Convert one by one. for (var i = 0; i < data.length; i++) { var c = toBinaryTable[data.charCodeAt(i) & 0x7f]; var padding = (data[i] == base64Pad); // Skip illegal characters and whitespace if (c == -1) continue; // Collect data into leftdata, update bitcount leftdata = (leftdata << 6) | c; leftbits += 6; // If we have 8 or more bits, append 8 bits to the result if (leftbits >= 8) { leftbits -= 8; // Append if not padding. if (!padding) result += String.fromCharCode((leftdata >> leftbits) & 0xff); leftdata &= (1 << leftbits) - 1; } } // If there are any bits left, the base64 string was corrupted if (leftbits) throw Components.Exception('Corrupted base64 string'); return result;}if (DEBUG) debug = function(msg) { dump(' -- XML-RPC client -- : ' + msg + '\n'); };else debug = function() {}// vim:sw=4:sr:sta:et:sts:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -