1 /* ***** BEGIN LICENSE BLOCK *****
  2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3  *
  4  * The contents of this file are subject to the Mozilla Public License Version
  5  * 1.1 (the "License"); you may not use this file except in compliance with
  6  * the License. You may obtain a copy of the License at
  7  * http://www.mozilla.org/MPL/
  8  *
  9  * Software distributed under the License is distributed on an "AS IS" basis,
 10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 11  * for the specific language governing rights and limitations under the
 12  * License.
 13  *
 14  * The Initial Developer of the Original Code is Fireinput Inc.
 15  *
 16  * Portions created by the Initial Developer are Copyright (C) 2007
 17  * the Initial Developer. All Rights Reserved.
 18  *
 19  * Contributor(s):
 20  *     Olly Ja <ollyja@gmail.com>
 21  *
 22  * Alternatively, the contents of this file may be used under the terms of
 23  * either the GNU General Public License Version 2 or later (the "GPL"), or
 24  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 25  * in which case the provisions of the GPL or the LGPL are applicable instead
 26  * of those above. If you wish to allow use of your version of this file only
 27  * under the terms of either the GPL or the LGPL, and not to allow others to
 28  * use your version of this file under the terms of the MPL, indicate your
 29  * decision by deleting the provisions above and replace them with the notice
 30  * and other provisions required by the GPL or the LGPL. If you do not delete
 31  * the provisions above, a recipient may use your version of this file under
 32  * the terms of any one of the MPL, the GPL or the LGPL.
 33  *
 34  * ***** END LICENSE BLOCK ***** 
 35  */
 36 
 37 // Constants
 38 
 39 var FireinputStream = 
 40 {
 41     loadData: function(url)
 42     {
 43        var lines = this.readLine(url);
 44        return lines; 
 45     },
 46 
 47     readLine: function(url)
 48     {
 49        var ioService = FireinputXPC.getIOService(); 
 50 
 51        var channel;
 52        var stream;
 53        try
 54        {
 55             var uri = ioService.newFileURI(url);
 56             channel = ioService.newChannelFromURI(uri);
 57             stream = channel.open();
 58        }
 59        catch (exc)
 60        {
 61             return null;
 62        }
 63 
 64        try
 65        {
 66           var sis = FireinputXPC.getService("@mozilla.org/scriptableinputstream;1", "nsIScriptableInputStream");
 67           sis.init(stream);
 68 
 69           var segments = [];
 70           for (var count = stream.available(); count; count = stream.available())
 71           {
 72              var segment = sis.read(count);
 73              segments.push(segment);
 74           }
 75           sis.close();
 76           var text = segments.join("");
 77 
 78           // var data = FireinputUnicode.getUnicodeString(text); 
 79           var lines = text.split(/\r\n|\r|\n/);
 80           return lines;
 81         }
 82         catch (exc)
 83         {
 84             stream.close();
 85         }
 86 
 87         return null; 
 88     }, 
 89 
 90     loadDataAsync: function(url, user)
 91     {
 92        var ioService = FireinputXPC.getIOService(); 
 93 
 94        var uri = ioService.newFileURI(url);
 95        var channel = ioService.newChannelFromURI(uri);
 96        function processCallback(line)
 97        {
 98             var caller = user.caller; 
 99             var func =  user.onavailable; 
100             func.call(caller, line); 
101        }
102 
103        function completeCallback()
104        {
105             var caller = user.caller; 
106             var func =  user.oncomplete; 
107             if(func)
108                func.call(caller);
109        }
110        var observer = new StreamObserver(processCallback, completeCallback, DATA_TEXT);
111        channel.asyncOpen(observer, null);
112     },
113 
114     loadXHTMLDataAsync: function(url, user)
115     {
116        var ioService = FireinputXPC.getIOService(); 
117 
118        var uri = ioService.newFileURI(url);
119        var channel = ioService.newChannelFromURI(uri);
120        function processCallback(line)
121        {
122             var caller = user.caller;
123             var func =  user.onavailable;
124             func.call(caller, line);
125        }
126 
127        function completeCallback()
128        {
129             var caller = user.caller;
130             var func =  user.oncomplete;
131             if(func)
132                func.call(caller);
133        }
134        var observer = new StreamObserver(processCallback, completeCallback, DATA_XML);
135        channel.asyncOpen(observer, null);
136     }
137 
138 };
139 
140 // ************************************************************************************************
141 
142 function StreamObserver(processCB, completeCB, dataType)
143 {
144     this.processCB = processCB; 
145     this.completeCB = completeCB; 
146     this.dataType = dataType; 
147     this.data = [];
148 }   
149 
150 StreamObserver.prototype =
151 {
152     onStartRequest: function(request, context)
153     {
154     },
155 
156     onStopRequest: function(request, context, status)
157     {
158        this.completeCB(); 
159     },
160 
161     processXMLData: function(str)
162     {
163        this.processCB(str); 
164     },
165 
166     processData: function(str)
167     {
168        var lines = str.split(/\r\n|\r|\n/);
169        this.data = []; 
170        if(str.lastIndexOf("\n") != 0)
171        {
172           this.data.push(lines.pop()); 
173        }
174    
175        for(var i=lines.length-1; i>=0; i--)
176        {
177        //      var data = FireinputUnicode.getUnicodeString(lines[i]);
178              this.processCB(lines[i]);
179        }
180     },
181 
182     onDataAvailable: function(request, context, inStr, sourceOffset, count)
183     {
184        var sis = FireinputXPC.createInstance("@mozilla.org/scriptableinputstream;1", "nsIScriptableInputStream");
185        sis.init(inStr);
186        if(this.dataType == DATA_XML)
187           this.processXMLData(this.data.join("") + sis.read(count));
188        else 
189           this.processData(this.data.join("") + sis.read(count));
190           
191     }
192 };
193  


syntax highlighted by Code2HTML, v. 0.9.1