better handling of invalid JSON objects

This commit is contained in:
Simon Kornblith 2008-09-11 20:53:15 +00:00
parent 7d9493827e
commit f6b1d6e56e

View File

@ -131,34 +131,40 @@ Zotero.Translator = function(file) {
if(!m) { if(!m) {
this.logError("Invalid or missing translator metadata JSON object"); this.logError("Invalid or missing translator metadata JSON object");
} else { } else {
var info = Zotero.JSON.unserialize(m[0]); try {
var haveMetadata = true; var info = Zotero.JSON.unserialize(m[0]);
// make sure we have all the properties } catch(e) {
for each(var property in ["translatorID", "translatorType", "label", "target", "lastUpdated"]) { this.logError("Invalid or missing translator metadata JSON object");
if(info[property] === undefined) {
this.logError('Missing property "'+property+'" in translator metadata JSON object');
haveMetadata = false;
break;
} else {
this[property] = info[property];
}
} }
if(info) {
if(haveMetadata) { var haveMetadata = true;
if(this.translatorType & TRANSLATOR_TYPES["import"]) { // make sure we have all the properties
// compile import regexp to match only file extension for each(var property in ["translatorID", "translatorType", "label", "target", "lastUpdated"]) {
this.importRegexp = this.target ? new RegExp("\\."+this.target+"$", "i") : null; if(info[property] === undefined) {
this.logError('Missing property "'+property+'" in translator metadata JSON object');
haveMetadata = false;
break;
} else {
this[property] = info[property];
}
} }
if(this.translatorType & TRANSLATOR_TYPES["web"]) {
// compile web regexp if(haveMetadata) {
this.webRegexp = this.target ? new RegExp(this.target, "i") : null; if(this.translatorType & TRANSLATOR_TYPES["import"]) {
// compile import regexp to match only file extension
if(!this.target) { this.importRegexp = this.target ? new RegExp("\\."+this.target+"$", "i") : null;
// for translators used on every page, cache code in memory }
var strs = [str.value]; if(this.translatorType & TRANSLATOR_TYPES["web"]) {
var amountRead; // compile web regexp
while(amountRead = cStream.readString(4096, str)) strs.push(str.value); this.webRegexp = this.target ? new RegExp(this.target, "i") : null;
this._code = strs.join("");
if(!this.target) {
// for translators used on every page, cache code in memory
var strs = [str.value];
var amountRead;
while(amountRead = cStream.readString(4096, str)) strs.push(str.value);
this._code = strs.join("");
}
} }
} }
} }