{ "translatorID":"a6ee60df-1ddc-4aae-bb25-45e0537be973", "translatorType":1, "label":"MARC", "creator":"Simon Kornblith, updated for unimarc by Sylvain Machefert", "target":"marc", "minVersion":"1.0.0b3.r1", "maxVersion":"", "priority":100, "inRepository":true, "lastUpdated":"2010-02-04 02:00:00" } function detectImport() { var marcRecordRegexp = /^[0-9]{5}[a-z ]{3}$/ var read = Zotero.read(8); if(marcRecordRegexp.test(read)) { return true; } } //test var fieldTerminator = "\x1E"; var recordTerminator = "\x1D"; var subfieldDelimiter = "\x1F"; /* * CLEANING FUNCTIONS */ // general purpose cleaning function clean(value) { value = value.replace(/^[\s\.\,\/\:;]+/, ''); value = value.replace(/[\s\.\,\/\:;]+$/, ''); value = value.replace(/ +/g, ' '); var char1 = value[0]; var char2 = value[value.length-1]; if((char1 == "[" && char2 == "]") || (char1 == "(" && char2 == ")")) { // chop of extraneous characters return value.substr(1, value.length-2); } return value; } // number extraction function pullNumber(text) { var pullRe = /[0-9]+/; var m = pullRe.exec(text); if(m) { return m[0]; } } // ISBN extraction function pullISBN(text) { var pullRe = /[0-9X\-]+/; var m = pullRe.exec(text); if(m) { return m[0]; } } // corporate author extraction function corpAuthor(author) { return {lastName:author, fieldMode:true}; } // regular author extraction function author(author, type, useComma) { return Zotero.Utilities.cleanAuthor(author, type, useComma); } /* * END CLEANING FUNCTIONS */ var record = function() { this.directory = new Object(); this.leader = ""; this.content = ""; // defaults this.indicatorLength = 2; this.subfieldCodeLength = 2; } // import a binary MARC record into this record record.prototype.importBinary = function(record) { // get directory and leader var directory = record.substr(0, record.indexOf(fieldTerminator)); this.leader = directory.substr(0, 24); var directory = directory.substr(24); // get various data this.indicatorLength = parseInt(this.leader[10], 10); this.subfieldCodeLength = parseInt(this.leader[11], 10); var baseAddress = parseInt(this.leader.substr(12, 5), 10); // get record data var contentTmp = record.substr(baseAddress); // MARC wants one-byte characters, so when we have multi-byte UTF-8 // sequences, add null characters so that the directory shows up right. we // can strip the nulls later. this.content = ""; for(i=0; i 0x00FFFF) { this.content += "\x00\x00\x00"; } else if(contentTmp.charCodeAt(i) > 0x0007FF) { this.content += "\x00\x00"; } else if(contentTmp.charCodeAt(i) > 0x00007F) { this.content += "\x00"; } } // read directory for(var i=0; i this.indicatorLength) { indicator = indicator.substr(0, this.indicatorLength); } else if(indicator.length != this.indicatorLength) { indicator = Zotero.Utilities.lpad(indicator, " ", this.indicatorLength); } // add terminator value = indicator+value+fieldTerminator; // add field to directory if(!this.directory[field]) { this.directory[field] = new Array(); } this.directory[field].push([this.content.length, value.length]); // add field to record this.content += value; } // get all fields with a certain field number record.prototype.getField = function(field) { field = parseInt(field, 10); var fields = new Array(); // make sure fields exist if(!this.directory[field]) { return fields; } // get fields for(var i in this.directory[field]) { var location = this.directory[field][i]; // add to array, replacing null characters fields.push([this.content.substr(location[0], this.indicatorLength), this.content.substr(location[0]+this.indicatorLength, location[1]-this.indicatorLength-1).replace(/\x00/g, "")]); } return fields; } // get subfields from a field record.prototype.getFieldSubfields = function(tag) { // returns a two-dimensional array of values var fields = this.getField(tag); var returnFields = new Array(); for(var i in fields) { returnFields[i] = new Object(); var subfields = fields[i][1].split(subfieldDelimiter); if (subfields.length == 1) { returnFields[i]["?"] = fields[i][1]; } else { for(var j in subfields) { if(subfields[j]) { var subfieldIndex = subfields[j].substr(0, this.subfieldCodeLength-1); if(!returnFields[i][subfieldIndex]) { returnFields[i][subfieldIndex] = subfields[j].substr(this.subfieldCodeLength-1); } } } } } return returnFields; } // add field to DB record.prototype._associateDBField = function(item, fieldNo, part, fieldName, execMe, arg1, arg2) { var field = this.getFieldSubfields(fieldNo); Zotero.debug('MARC: found '+field.length+' matches for '+fieldNo+part); if(field) { for(var i in field) { var value = false; for(var j=0; j 1) { records[0] = holdOver + records[0]; holdOver = records.pop(); // skip last record, since it's not done for(var i in records) { var newItem = new Zotero.Item(); // create new record var rec = new record(); rec.importBinary(records[i]); rec.translate(newItem); newItem.complete(); } } else { holdOver += text; } } }