pto-assignments
Version:
Library and CLI tool for parsing USPTO assignment data
190 lines (133 loc) • 5.44 kB
JavaScript
var Assignment = require('./assignment');
var XMLDoc = require('./xml_doc');
var Assignment = module.exports = function(xml) {
// <assignment-record>
this.reel = parseInt(xml.descendantWithPath('assignment-record.reel-no').val, 10);
this.frame = parseInt(xml.descendantWithPath('assignment-record.frame-no').val, 10);
this.lastUpdate = parseDate(xml.descendantWithPath('assignment-record.last-update-date.date').val);
this.purge = xml.descendantWithPath('assignment-record.purge-indicator').val;
this.recorded = parseDate(xml.descendantWithPath('assignment-record.recorded-date.date').val);
this.pages = parseInt(xml.descendantWithPath('assignment-record.page-count').val, 10);
var correspondent = xml.descendantWithPath('assignment-record.correspondent');
this.correspondent = {};
this.correspondent.name = correspondent.descendantWithPath('name').val;
this.correspondent.address = [];
for (var i = 0; i < 4; i++) { // see DTD, maximum of 4 address lines, minimum of 1
var addr = correspondent.descendantWithPath('address-' + i);
if (addr) {
this.correspondent.address.push(addr.val);
}
}
this.description = xml.descendantWithPath('assignment-record.conveyance-text').val;
// <patent-assignors>
var assignors = this.assignors = [];
xml.descendantWithPath('patent-assignors').eachChild(function(child) {
var assignor = {
name: child.descendantWithPath('name').val
};
if (child.descendantWithPath('execution-date.date')) {
assignor.executed = parseDate(child.descendantWithPath('execution-date.date').val);
}
if (child.descendantWithPath('date-acknowledged.date')) { // TODO: rare
assignor.acknowledged = parseDate(child.descendantWithPath('date-acknowledged.date').val);
}
if (child.descendantWithPath('address-1')) {
assignor.address = [];
assignor.address.push(child.descendantWithPath('address-1').val);
}
if (child.descendantWithPath('address-2')) {
assignor.address.push(child.descendantWithPath('address-2').val);
}
assignors.push(assignor);
});
// <patent-assignees>
var assignees = this.assignees = [];
xml.descendantWithPath('patent-assignees').eachChild(function(child) {
var assignee = {
name: child.descendantWithPath('name').val
};
if (child.descendantWithPath('address-1')) {
assignee.address = [];
assignee.address.push(child.descendantWithPath('address-1').val);
}
if (child.descendantWithPath('address-2')) {
assignee.address = assignee.address || [];
assignee.address.push(child.descendantWithPath('address-2').val);
}
if (child.descendantWithPath('city')) {
assignee.city = child.descendantWithPath('city').val;
}
if (child.descendantWithPath('state')) {
assignee.state = child.descendantWithPath('state').val;
}
if (child.descendantWithPath('country-name')) {
assignee.country = child.descendantWithPath('country-name').val;
} else {
assignee.country = 'UNITED STATES';
}
if (child.descendantWithPath('postcode')) {
assignee.postcode = child.descendantWithPath('postcode').val;
}
assignees.push(assignee);
});
// <patent-properties>
var properties = this.properties = [];
xml.descendantWithPath('patent-properties').eachChild(function(child) {
var property = {};
if (child.descendantWithPath('invention-title')) {
property.title = child.descendantWithPath('invention-title').val;
}
property.documents = [];
child.eachChild(function(grandchild) {
if (grandchild.name === 'document-id') {
var doc = {
country: grandchild.descendantWithPath('country').val,
number: grandchild.descendantWithPath('doc-number').val
};
if (grandchild.descendantWithPath('kind')) {
doc.kindCode = grandchild.descendantWithPath('kind').val;
}
if (grandchild.descendantWithPath('name')) {
doc.name = grandchild.descendantWithPath('name').val;
}
if (grandchild.descendantWithPath('date')) {
doc.date = parseDate(grandchild.descendantWithPath('date').val);
}
property.documents.push(doc);
}
});
properties.push(property);
});
};
var XML = module.exports = function(xml, options) {
// Valid XML
if (!xml)
throw Error('Missing argument: an XML string is required as the first parameters');
if ('string' !== typeof(xml))
throw Error('Invalid argument: first argument should be an XML string');
var parser = new XMLDoc(xml);
this.dtd = parser.attribute('dtd-version', {
post: parseFloat
});
this.produced = parser.attribute('date-produced', {
post: parseDate
});
this.actionKeyCode = parser.text('action-key-code');
this.transacted = parser.text('transaction-date.date', {
post: parseDate
});
var assignments = this.assignments = [];
var emptyFile = parser.doc.descendantWithPath('patent-assignments.data-available-code');
if (emptyFile && emptyFile.val === 'N') {
this.assignments = null;
return;
}
parser.set('patent-assignments').eachChild(function(child) {
assignments.push(new Assignment(child));
});
};
function parseDate(str) {
var parts = str.match(/(\d\d\d\d)(\d\d)(\d\d)/).splice(-3);
var month = parseInt(parts[1], 10) - 1;
return +new Date(parts[0], month, parts[2]);
}