store/retrieve objects in files
[Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / evergreen / util / file.js
1 dump('entering util/file.js\n');
2
3 if (typeof util == 'undefined') util = {};
4 util.file = function (mw,G,fname) {
5
6         this.mw = mw; this.G = G;
7
8         this.dirService = Components.classes["@mozilla.org/file/directory_service;1"].
9                 getService( Components.interfaces.nsIProperties );
10
11         if (fname) this.get(fname);
12
13         return this;
14 };
15
16 util.file.prototype = {
17
18         'name' : '',
19         '_file' : null,
20         '_input_stream' : null,
21         '_output_stream' : null,
22
23         'get' : function( fname ) {
24                 try {
25                         if (!fname) { fname = this.name; } else { this.name = fname; }
26                         if (!fname) throw('Must specify a filename.');
27
28                         this._file = this.dirService.get( "AChrom",  Components.interfaces.nsIFile );
29                         this._file.append(this.mw.myPackageDir); 
30                         this._file.append("content"); 
31                         this._file.append("conf"); 
32                         this._file.append(fname);
33         
34                         this.G.error.sdump('D_FILE',this._file.path);
35
36                         return this._file;
37
38                 } catch(E) {
39                         this.G.error.sdump('D_ERROR','util.file.get(): ' + E);
40                         throw(E);
41                 }
42         },
43
44         'close' : function() {
45                 try {
46                         if (!this._file) throw('Must .get() a file first.');
47                         if (this._input_stream) { this._input_stream.close(); this._input_stream = null; }
48                         if (this._output_stream) { this._output_stream.close(); this._output_stream = null; }
49
50                 } catch(E) {
51                         this.G.error.sdump('D_ERROR','util.file.close(): ' + E);
52                         throw(E);
53                 }
54         },
55
56         'set_object' : function(obj) {
57                 try {
58                         if (!this._file) throw('Must .get() a file first.');
59                         if (this._input_stream || this._output_stream) throw('Must .close() first.');
60                         if (!obj) throw('Must specify an object.');
61
62                         var obj_json; 
63                         try { obj_json = js2JSON( obj ); } catch(E) { throw('Could not JSONify the object: ' + E); }
64
65                         this._create_output_stream();
66                         this._output_stream.write( obj_json, String( obj_json ).length );
67                         this.close();
68
69                 } catch(E) {
70                         this.G.error.sdump('D_ERROR','util.file.set_object(): ' + E);
71                         throw(E);
72                 }
73         },
74
75         'get_object' : function() {
76                 try {
77                         if (!this._file) throw('Must .get() a file first.');
78                         if (!this._file.exists()) throw('File does not exist.');
79                         if (this._input_stream || this._output_stream) throw('Must .close() first.');
80                         
81                         this._create_input_stream();
82                         var data = this._input_stream(-1);
83                         this.close();
84         
85                         var obj; try { obj = JSON2js( data ); } catch(E) { throw('Could not js-ify the JSON: '+E); }
86         
87                         return obj;
88
89                 } catch(E) {
90                         this.G.error.sdump('D_ERROR','util.file.get_object(): ' + E);
91                         throw(E);
92                 }
93         },
94
95         '_create_input_stream' : function() {
96                 try {
97                         if (!this._file) throw('Must .get() a file first.');
98                         if (this._input_stream || this._output_stream) throw('Must .close() first.');
99                         if (!this._file.exists()) throw('File does not exist.');
100         
101                         var f = Components.classes["@mozilla.org/network/file-input-stream;1"]
102                                 .createInstance(Components.interfaces.nsIFileInputStream);
103                         f.init(file, 0x01, 0, 0);
104                         this._input_stream = Components.classes["@mozilla.org/scriptableinputstream;1"]
105                                 .createInstance(Components.interfaces.nsIScriptableInputStream);
106                         if (f) {
107                                 this._input_stream.init(f);
108                                 return this._input_stream;
109                         } else {
110                                 throw('Could not instantiate input stream.');
111                         }
112
113                 } catch(E) {
114                         this.G.error.sdump('D_ERROR','util.file._create_input_stream(): ' + E);
115                         throw(E);
116                 }
117         },
118
119         '_create_output_stream' : function() {
120                 try {
121                         if (!this._file) throw('Must .get() a file first.');
122                         if (this._input_stream || this._output_stream) throw('Must .close() first.');
123
124                         if (! this._file.exists()) this._file.create( 0, 0640 );
125
126                         this._output_stream = Components.classes["@mozilla.org/network/file-output-stream;1"]
127                                 .createInstance(Components.interfaces.nsIFileOutputStream);
128                         this._output_stream.init(this._file, 0x02 | 0x08 | 0x20, 0644, 0);
129
130                         return this._output_stream;
131
132                 } catch(E) {
133                         this.G.error.sdump('D_ERROR','util.file._create_output_stream(): ' + E);
134                         throw(E);
135                 }
136         }
137
138 }
139
140 dump('exiting util/file.js\n');