From 915cb1f1116892e7a9a9ae26258a68b585e9f9e2 Mon Sep 17 00:00:00 2001 From: phasefx Date: Mon, 3 Oct 2005 18:57:04 +0000 Subject: [PATCH] store/retrieve objects in files git-svn-id: svn://svn.open-ils.org/ILS/trunk@1887 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- .../chrome/content/evergreen/util/file.js | 140 ++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 Open-ILS/xul/staff_client/chrome/content/evergreen/util/file.js diff --git a/Open-ILS/xul/staff_client/chrome/content/evergreen/util/file.js b/Open-ILS/xul/staff_client/chrome/content/evergreen/util/file.js new file mode 100644 index 0000000000..97726571c1 --- /dev/null +++ b/Open-ILS/xul/staff_client/chrome/content/evergreen/util/file.js @@ -0,0 +1,140 @@ +dump('entering util/file.js\n'); + +if (typeof util == 'undefined') util = {}; +util.file = function (mw,G,fname) { + + this.mw = mw; this.G = G; + + this.dirService = Components.classes["@mozilla.org/file/directory_service;1"]. + getService( Components.interfaces.nsIProperties ); + + if (fname) this.get(fname); + + return this; +}; + +util.file.prototype = { + + 'name' : '', + '_file' : null, + '_input_stream' : null, + '_output_stream' : null, + + 'get' : function( fname ) { + try { + if (!fname) { fname = this.name; } else { this.name = fname; } + if (!fname) throw('Must specify a filename.'); + + this._file = this.dirService.get( "AChrom", Components.interfaces.nsIFile ); + this._file.append(this.mw.myPackageDir); + this._file.append("content"); + this._file.append("conf"); + this._file.append(fname); + + this.G.error.sdump('D_FILE',this._file.path); + + return this._file; + + } catch(E) { + this.G.error.sdump('D_ERROR','util.file.get(): ' + E); + throw(E); + } + }, + + 'close' : function() { + try { + if (!this._file) throw('Must .get() a file first.'); + if (this._input_stream) { this._input_stream.close(); this._input_stream = null; } + if (this._output_stream) { this._output_stream.close(); this._output_stream = null; } + + } catch(E) { + this.G.error.sdump('D_ERROR','util.file.close(): ' + E); + throw(E); + } + }, + + 'set_object' : function(obj) { + try { + if (!this._file) throw('Must .get() a file first.'); + if (this._input_stream || this._output_stream) throw('Must .close() first.'); + if (!obj) throw('Must specify an object.'); + + var obj_json; + try { obj_json = js2JSON( obj ); } catch(E) { throw('Could not JSONify the object: ' + E); } + + this._create_output_stream(); + this._output_stream.write( obj_json, String( obj_json ).length ); + this.close(); + + } catch(E) { + this.G.error.sdump('D_ERROR','util.file.set_object(): ' + E); + throw(E); + } + }, + + 'get_object' : function() { + try { + if (!this._file) throw('Must .get() a file first.'); + if (!this._file.exists()) throw('File does not exist.'); + if (this._input_stream || this._output_stream) throw('Must .close() first.'); + + this._create_input_stream(); + var data = this._input_stream(-1); + this.close(); + + var obj; try { obj = JSON2js( data ); } catch(E) { throw('Could not js-ify the JSON: '+E); } + + return obj; + + } catch(E) { + this.G.error.sdump('D_ERROR','util.file.get_object(): ' + E); + throw(E); + } + }, + + '_create_input_stream' : function() { + try { + if (!this._file) throw('Must .get() a file first.'); + if (this._input_stream || this._output_stream) throw('Must .close() first.'); + if (!this._file.exists()) throw('File does not exist.'); + + var f = Components.classes["@mozilla.org/network/file-input-stream;1"] + .createInstance(Components.interfaces.nsIFileInputStream); + f.init(file, 0x01, 0, 0); + this._input_stream = Components.classes["@mozilla.org/scriptableinputstream;1"] + .createInstance(Components.interfaces.nsIScriptableInputStream); + if (f) { + this._input_stream.init(f); + return this._input_stream; + } else { + throw('Could not instantiate input stream.'); + } + + } catch(E) { + this.G.error.sdump('D_ERROR','util.file._create_input_stream(): ' + E); + throw(E); + } + }, + + '_create_output_stream' : function() { + try { + if (!this._file) throw('Must .get() a file first.'); + if (this._input_stream || this._output_stream) throw('Must .close() first.'); + + if (! this._file.exists()) this._file.create( 0, 0640 ); + + this._output_stream = Components.classes["@mozilla.org/network/file-output-stream;1"] + .createInstance(Components.interfaces.nsIFileOutputStream); + this._output_stream.init(this._file, 0x02 | 0x08 | 0x20, 0644, 0); + + return this._output_stream; + + } catch(E) { + this.G.error.sdump('D_ERROR','util.file._create_output_stream(): ' + E); + throw(E); + } + } + +} + +dump('exiting util/file.js\n'); -- 2.43.2