fix import issues

This commit is contained in:
Simon Kornblith 2011-02-09 21:54:38 +00:00
parent 3fd0523b6e
commit a83fbb4fa8

View file

@ -134,17 +134,17 @@ Zotero.Translate.SandboxManager.prototype = {
* scope at all times. Otherwise, our streams might get garbage collected when we allow other code * scope at all times. Otherwise, our streams might get garbage collected when we allow other code
* to run during Zotero.wait(). * to run during Zotero.wait().
*/ */
Zotero.Translate.IO.streamsToKeepOpen = []; Zotero.Translate.IO.maintainedInstances = [];
/******* (Native) Read support *******/ /******* (Native) Read support *******/
Zotero.Translate.IO.Read = function(file, mode) { Zotero.Translate.IO.Read = function(file, mode) {
Zotero.Translate.IO.maintainedInstances.push(this);
this.file = file; this.file = file;
// open file // open file
this._rawStream = Components.classes["@mozilla.org/network/file-input-stream;1"] this._rawStream = Components.classes["@mozilla.org/network/file-input-stream;1"]
.createInstance(Components.interfaces.nsIFileInputStream); .createInstance(Components.interfaces.nsIFileInputStream);
Zotero.Translate.IO.streamsToKeepOpen.push(this._rawStream);
this._rawStream.init(file, 0x01, 0664, 0); this._rawStream.init(file, 0x01, 0664, 0);
// start detecting charset // start detecting charset
@ -166,7 +166,8 @@ Zotero.Translate.IO.Read = function(file, mode) {
if(this._charset) { if(this._charset) {
// BOM found; store its length and go back to the beginning of the file // BOM found; store its length and go back to the beginning of the file
this._bomLength = BOMs[this._charset].length; this._bomLength = BOMs[this._charset].length;
this._seekToStart(); this._rawStream.QueryInterface(Components.interfaces.nsISeekableStream)
.seek(Components.interfaces.nsISeekableStream.NS_SEEK_SET, this._bomLength);
} else { } else {
// look for an XML parse instruction // look for an XML parse instruction
this._bomLength = 0; this._bomLength = 0;
@ -212,7 +213,8 @@ Zotero.Translate.IO.Read = function(file, mode) {
// If we managed to get a charset here, then translators shouldn't be able to override it, // If we managed to get a charset here, then translators shouldn't be able to override it,
// since it's almost certainly correct. Otherwise, we allow override. // since it's almost certainly correct. Otherwise, we allow override.
this._allowCharsetOverride = !!this._charset; this._allowCharsetOverride = !!this._charset;
this._seekToStart(); this._rawStream.QueryInterface(Components.interfaces.nsISeekableStream)
.seek(Components.interfaces.nsISeekableStream.NS_SEEK_SET, this._bomLength);
if(!this._charset) { if(!this._charset) {
// No XML parse instruction or BOM. // No XML parse instruction or BOM.
@ -267,9 +269,6 @@ Zotero.Translate.IO.Read = function(file, mode) {
break; break;
} }
} }
// Seek back to beginning of file
this._seekToStart();
} else { } else {
// No need to auto-detect; user has specified a charset // No need to auto-detect; user has specified a charset
this._charset = charsetPref; this._charset = charsetPref;
@ -291,14 +290,30 @@ Zotero.Translate.IO.Read.prototype = {
"setCharacterSet":"r" "setCharacterSet":"r"
}, },
"_seekToStart":function() { "_openRawStream":function() {
this._rawStream = Components.classes["@mozilla.org/network/file-input-stream;1"]
.createInstance(Components.interfaces.nsIFileInputStream);
this._rawStream.init(this.file, 0x01, 0664, 0);
},
"_seekToStart":function(charset) {
this._rawStream.close();
this._openRawStream();
this._linesExhausted = false;
this._rawStream.QueryInterface(Components.interfaces.nsISeekableStream) this._rawStream.QueryInterface(Components.interfaces.nsISeekableStream)
.seek(Components.interfaces.nsISeekableStream.NS_SEEK_SET, this._bomLength); .seek(Components.interfaces.nsISeekableStream.NS_SEEK_SET, this._bomLength);
this.bytesRead = this._bomLength; this.bytesRead = this._bomLength;
this.inputStream = Components.classes["@mozilla.org/intl/converter-input-stream;1"]
.createInstance(Components.interfaces.nsIConverterInputStream);
this.inputStream.init(this._rawStream, charset, 32768,
Components.interfaces.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER);
}, },
"_readToString":function() { "_readToString":function() {
var str = {}; var str = {};
this.inputStream.QueryInterface(Components.interfaces.nsIUnicharInputStream);
this.inputStream.readString(this.file.fileSize, str); this.inputStream.readString(this.file.fileSize, str);
return str.value; return str.value;
}, },
@ -326,16 +341,9 @@ Zotero.Translate.IO.Read.prototype = {
} }
// seek back to the beginning // seek back to the beginning
this._seekToStart(); this._seekToStart(this._allowCharsetOverride ? this._allowCharsetOverride : this._charset);
if(this._allowCharsetOverride) { if(!_allowCharsetOverride) {
try {
this.inputStream.init(this._rawStream, charset, 65535,
Components.interfaces.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER);
} catch(e) {
throw "Translate: setCharacterSet: text encoding not supported";
}
} else {
Zotero.debug("Translate: setCharacterSet: translate charset override ignored due to BOM or XML parse instruction"); Zotero.debug("Translate: setCharacterSet: translate charset override ignored due to BOM or XML parse instruction");
} }
}, },
@ -345,19 +353,19 @@ Zotero.Translate.IO.Read.prototype = {
if(bytes) { if(bytes) {
// read number of bytes requested // read number of bytes requested
this.inputStream.QueryInterface(Components.interfaces.nsIUnicharInputStream);
var amountRead = this.inputStream.readString(bytes, str); var amountRead = this.inputStream.readString(bytes, str);
if(!amountRead) return false;
this.bytesRead += amountRead;
} else { } else {
// bytes not specified; read a line // bytes not specified; read a line
this.inputStream.QueryInterface(Components.interfaces.nsIUnicharLineInputStream); this.inputStream.QueryInterface(Components.interfaces.nsIUnicharLineInputStream);
var amountRead = this.inputStream.readLine(str); if(this._linesExhausted) return false;
} this._linesExhausted = !this.inputStream.readLine(str);
this.bytesRead += str.value.length+1; // only approximate
if(amountRead) {
this.bytesRead += amountRead;
return str.value;
} else {
return false;
} }
return str.value;
}, },
"_getXML":function() { "_getXML":function() {
@ -369,15 +377,7 @@ Zotero.Translate.IO.Read.prototype = {
}, },
"reset":function(newMode) { "reset":function(newMode) {
this._seekToStart(); this._seekToStart(this._charset);
if(!this.inputStream) {
this.inputStream = Components.classes["@mozilla.org/intl/converter-input-stream;1"]
.createInstance(Components.interfaces.nsIConverterInputStream);
Zotero.Translate.IO.streamsToKeepOpen.push(this.inputStream);
}
this.inputStream.init(this._rawStream, this._charset, 65535,
Components.interfaces.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER);
this._mode = newMode; this._mode = newMode;
if(Zotero.Translate.IO.rdfDataModes.indexOf(this._mode) !== -1 && !this.RDF) { if(Zotero.Translate.IO.rdfDataModes.indexOf(this._mode) !== -1 && !this.RDF) {
@ -386,16 +386,13 @@ Zotero.Translate.IO.Read.prototype = {
}, },
"close":function() { "close":function() {
Zotero.Translate.IO.streamsToKeepOpen.splice(Zotero.Translate.IO.streamsToKeepOpen.indexOf(this._rawStream), 1); var myIndex = Zotero.Translate.IO.maintainedInstances.indexOf(this);
if(this.inputStream) { if(myIndex !== -1) Zotero.Translate.IO.maintainedInstances.splice(myIndex, 1);
Zotero.Translate.IO.streamsToKeepOpen.splice(Zotero.Translate.IO.streamsToKeepOpen.indexOf(this.inputStream), 1);
}
this._rawStream.close(); this._rawStream.close();
} }
} }
Zotero.Translate.IO.Read.prototype.__defineGetter__("contentLength",
Zotero.Translate.IO.String.prototype.__defineGetter__("contentLength",
function() { function() {
return this.file.fileSize; return this.file.fileSize;
}); });
@ -403,9 +400,9 @@ function() {
/******* Write support *******/ /******* Write support *******/
Zotero.Translate.IO.Write = function(file, mode, charset) { Zotero.Translate.IO.Write = function(file, mode, charset) {
Zotero.Translate.IO.maintainedInstances.push(this);
this._rawStream = Components.classes["@mozilla.org/network/file-output-stream;1"] this._rawStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
.createInstance(Components.interfaces.nsIFileOutputStream); .createInstance(Components.interfaces.nsIFileOutputStream);
Zotero.Translate.IO.streamsToKeepOpen.push(this._rawStream);
this._rawStream.init(file, 0x02 | 0x08 | 0x20, 0664, 0); // write, create, truncate this._rawStream.init(file, 0x02 | 0x08 | 0x20, 0664, 0); // write, create, truncate
this._writtenToStream = false; this._writtenToStream = false;
if(mode || charset) this.reset(mode, charset); if(mode || charset) this.reset(mode, charset);
@ -432,7 +429,6 @@ Zotero.Translate.IO.Write.prototype = {
if(!this.outputStream) { if(!this.outputStream) {
this.outputStream = Components.classes["@mozilla.org/intl/converter-output-stream;1"] this.outputStream = Components.classes["@mozilla.org/intl/converter-output-stream;1"]
.createInstance(Components.interfaces.nsIConverterOutputStream); .createInstance(Components.interfaces.nsIConverterOutputStream);
Zotero.Translate.IO.streamsToKeepOpen.push(this.outputStream);
} }
if(charset == "UTF-8xBOM") charset = "UTF-8"; if(charset == "UTF-8xBOM") charset = "UTF-8";
@ -481,10 +477,8 @@ Zotero.Translate.IO.Write.prototype = {
this.write(this.RDF.serialize()); this.write(this.RDF.serialize());
} }
Zotero.Translate.IO.streamsToKeepOpen.splice(Zotero.Translate.IO.streamsToKeepOpen.indexOf(this._rawStream), 1); var myIndex = Zotero.Translate.IO.maintainedInstances.indexOf(this);
if(this.outputStream) { if(myIndex !== -1) Zotero.Translate.IO.maintainedInstances.splice(myIndex, 1);
Zotero.Translate.IO.streamsToKeepOpen.splice(Zotero.Translate.IO.streamsToKeepOpen.indexOf(this.outputStream), 1);
}
this._rawStream.close(); this._rawStream.close();
} }