-
Notifications
You must be signed in to change notification settings - Fork 1
/
CrossRef.js
423 lines (381 loc) · 15.3 KB
/
CrossRef.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
{
"translatorID": "11645bd1-0420-45c1-badb-53fb41eeb753",
"translatorType": 8,
"label": "CrossRef",
"creator": "Simon Kornblith",
"target": "^https?://partneraccess\\.oclc\\.org/",
"minVersion": "2.1.9",
"maxVersion": null,
"priority": 90,
"inRepository": true,
"browserSupport": "gcsv",
"lastUpdated": "2014-09-09 20:05:00"
}
/* CrossRef uses unixref; documentation at http://www.crossref.org/schema/documentation/unixref1.0/unixref.html */
var ns;
/**********************
* Utilitiy Functions *
**********************/
var xmlSerializer = new XMLSerializer();
function innerXML(n) {
return xmlSerializer.serializeToString(n) //outer XML
.replace(/^[^>]*>|<[^<]*$/g, '');
}
var markupRE = /<(\/?)(\w+)[^<>]*>/gi;
var supportedMarkup = ['i', 'b', 'sub', 'sup', 'span', 'sc'];
var transformMarkup = {
'scp': {
open: '<span style="font-variant:small-caps;">',
close: '</span>'
}
};
function removeUnsupportedMarkup(text) {
return text.replace(/<!\[CDATA\[([\s\S]*?)\]\]>/g, '$1') // Remove CDATA markup
.replace(markupRE, function(m, close, name) {
if(supportedMarkup.indexOf(name.toLowerCase()) != -1) {
return m;
}
var newMarkup = transformMarkup[name.toLowerCase()]
if(newMarkup) {
return close ? newMarkup.close : newMarkup.open;
}
return '';
});
}
function detectSearch(item) {
// query: should we make this more forgiving?
if(item.itemType === "journalArticle" || item.DOI) {
return true;
}
return false;
}
function fixAuthorCapitalization(string) {
if(typeof string === "string" && string.toUpperCase() === string) {
string = string.toLowerCase().replace(/\b[a-z]/g, function(m) { return m[0].toUpperCase() });
}
return string;
}
function parseCreators(node, item, typeOverrideMap) {
var contributors = ZU.xpath(node, 'c:contributors/c:organization | c:contributors/c:person_name', ns);
for(var i in contributors) {
var creatorXML = contributors[i];
var creator = {};
var role = creatorXML.getAttribute("contributor_role");
if(typeOverrideMap && typeOverrideMap[role] !== undefined) {
creator.creatorType = typeOverrideMap[role];
} else if(role === "author" || role === "editor" || role === "translator") {
creator.creatorType = role;
} else {
creator.creatorType = "contributor";
}
if(!creator.creatorType) continue;
if(creatorXML.nodeName === "organization") {
creator.fieldMode = 1;
creator.lastName = creatorXML.textContent;
} else if(creatorXML.nodeName === "person_name") {
creator.firstName = fixAuthorCapitalization(ZU.xpathText(creatorXML, 'c:given_name', ns));
creator.lastName = fixAuthorCapitalization(ZU.xpathText(creatorXML, 'c:surname', ns));
if (!creator.firstName) creator.fieldMode = 1;
}
item.creators.push(creator);
}
}
function processCrossRef(xmlOutput) {
// XPath does not give us the ability to use the same XPaths regardless of whether or not
// there is a namespace, so we add an element to make sure that there will always be a
// namespace.
xmlOutput = '<xml xmlns="http://www.example.com/">'+xmlOutput.replace(/<\?xml[^>]*\?>/, "")+"</xml>";
// parse XML with E4X
try {
var parser = new DOMParser();
var doc = parser.parseFromString(xmlOutput, "text/xml");
} catch(e) {
Zotero.debug(e);
return false;
}
// determine appropriate namespace
ns = {"c":"http://www.crossref.org/xschema/1.1", "x":"http://www.example.com/"};
var doiRecords = ZU.xpath(doc, '/x:xml/c:doi_records/c:doi_record', ns);
if(!doiRecords.length) {
ns.c = "http://www.crossref.org/xschema/1.0";
doiRecords = ZU.xpath(doc, '/x:xml/c:doi_records/c:doi_record', ns);
if(!doiRecords.length) {
// this means that the original document was un-namespaced
ns.c = "http://www.example.com/";
doiRecords = ZU.xpath(doc, '/c:xml/c:doi_records/c:doi_record', ns);
if(!doiRecords.length) {
throw new Error("No records found");
return;
}
}
}
var doiRecord = doiRecords[0];
// ensure this isn't an error
var errorString = ZU.xpathText(doiRecord, 'c:crossref/c:error', ns);
if(errorString !== null) {
throw errorString;
return false;
}
var itemXML, item, refXML, metadataXML, seriesXML;
if((itemXML = ZU.xpath(doiRecord, 'c:crossref/c:journal', ns)).length) {
item = new Zotero.Item("journalArticle");
refXML = ZU.xpath(itemXML, 'c:journal_article', ns);
metadataXML = ZU.xpath(itemXML, 'c:journal_metadata', ns);
item.publicationTitle = ZU.xpathText(metadataXML, 'c:full_title[1]', ns);
item.journalAbbreviation = ZU.xpathText(refXML, 'c:abbrev_title[1]', ns);
item.volume = ZU.xpathText(itemXML, 'c:journal_issue/c:journal_volume/c:volume', ns);
item.issue = ZU.xpathText(itemXML, 'c:journal_issue/c:journal_volume/c:issue', ns);
// Sometimes the <issue> tag is not nested inside the volume tag; see 10.1007/BF00938486
if (!item.issue)
item.issue = ZU.xpathText(itemXML, 'c:journal_issue/c:issue', ns);
} else if((itemXML = ZU.xpath(doiRecord, 'c:crossref/c:report-paper', ns)).length) {
// Report Paper
// Example: doi: 10.4271/2010-01-0907
// http://www.crossref.org/openurl/?pid=zter:zter321&url_ver=Z39.88-2004&rft_id=info:doi/10.4271/2010-01-0907&format=unixref&redirect=false
item = new Zotero.Item("report");
refXML = ZU.xpath(itemXML, 'c:report-paper_metadata', ns);
if (refXML.length==0) {
//Example doi: 10.1787/5jzb6vwk338x-en
//http://www.crossref.org/openurl/?pid=zter:zter321&url_ver=Z39.88-2004&&rft_id=info:doi/10.1787/5jzb6vwk338x-en&noredirect=true&format=unixref
refXML = ZU.xpath(itemXML, 'c:report-paper_series_metadata', ns);
seriesXML = ZU.xpath(refXML, 'c:series_metadata', ns);
}
metadataXML = refXML;
item.reportNumber = ZU.xpathText(refXML, 'c:publisher_item/c:item_number', ns);
if (!item.reportNumber) item.reportNumber = ZU.xpathText(refXML, 'c:volume', ns);
item.institution = ZU.xpathText(refXML, 'c:publisher/c:publisher_name', ns);
item.place = ZU.xpathText(refXML, 'c:publisher/c:publisher_place', ns);
} else if((itemXML = ZU.xpath(doiRecord, 'c:crossref/c:book', ns)).length) {
// Book chapter
// Example: doi: 10.1017/CCOL0521858429.016
// http://www.crossref.org/openurl/?pid=zter:zter321&url_ver=Z39.88-2004&rft_id=info:doi/10.1017/CCOL0521858429.016&format=unixref&redirect=false
// Reference book entry
// Example: doi: 10.1002/14651858.CD002966.pub3
// http://www.crossref.org/openurl/?pid=zter:zter321&url_ver=Z39.88-2004&rft_id=info:doi/10.1002/14651858.CD002966.pub3&format=unixref&redirect=false
// Entire edite book. This should _not_ be imported as bookSection
// Example: doi: 10.4135/9781446200957
// http://www.crossref.org/openurl/?pid=zter:zter321&url_ver=Z39.88-2004&&rft_id=info:doi/10.4135/9781446200957&noredirect=true&format=unixref
var bookType = itemXML[0].hasAttribute("book_type") ? itemXML[0].getAttribute("book_type") : null;
var componentType = ZU.xpathText(itemXML[0], 'c:content_item/@component_type', ns);
//is this an entry in a reference book?
var isReference = ["reference", "other"].indexOf(bookType) !== -1
&& ["chapter", "reference_entry"].indexOf(componentType) !==-1;
//for items that are entry in reference books OR edited book types that have some type of a chapter entry.
if((bookType === "edited_book" && componentType) || isReference) {
item = new Zotero.Item("bookSection");
refXML = ZU.xpath(itemXML, 'c:content_item', ns);
if(isReference) {
metadataXML = ZU.xpath(itemXML, 'c:book_metadata', ns);
if(!metadataXML.length) metadataXML = ZU.xpath(itemXML, 'c:book_series_metadata', ns);
item.bookTitle = ZU.xpathText(metadataXML, 'c:titles[1]/c:title[1]', ns);
item.seriesTitle = ZU.xpathText(metadataXML, 'c:series_metadata/c:titles[1]/c:title[1]', ns);
var metadataSeriesXML = ZU.xpath(metadataXML, 'c:series_metadata', ns);
if (metadataSeriesXML.length) parseCreators(metadataSeriesXML, item, {"editor":"seriesEditor"});
} else {
metadataXML = ZU.xpath(itemXML, 'c:book_series_metadata', ns);
if(!metadataXML.length) metadataXML = ZU.xpath(itemXML, 'c:book_metadata', ns);
item.bookTitle = ZU.xpathText(metadataXML, 'c:series_metadata/c:titles[1]/c:title[1]', ns);
if(!item.bookTitle) item.bookTitle = ZU.xpathText(metadataXML, 'c:titles[1]/c:title[1]', ns);
}
// Handle book authors
parseCreators(metadataXML, item, {"author":"bookAuthor"});
// Book
} else {
item = new Zotero.Item("book");
refXML = ZU.xpath(itemXML, 'c:book_metadata', ns);
//Sometimes book data is in book_series_metadata
// doi: 10.1007/978-1-4419-9164-5
//http://www.crossref.org/openurl/?pid=zter:zter321&url_ver=Z39.88-2004&rft_id=info:doi/10.1007/978-1-4419-9164-5&format=unixref&redirect=false
if (!refXML.length) refXML = ZU.xpath(itemXML, 'c:book_series_metadata', ns);
metadataXML = refXML;
seriesXML = ZU.xpath(refXML, 'c:series_metadata', ns);
}
item.place = ZU.xpathText(metadataXML, 'c:publisher/c:publisher_place', ns);
} else if((itemXML = ZU.xpath(doiRecord, 'c:crossref/c:standard', ns)).length) {
item = new Zotero.Item("report");
refXML = ZU.xpath(itemXML, 'c:standard_metadata', ns);
metadataXML = ZU.xpath(itemXML, 'c:standard_metadata', ns);
} else if((itemXML = ZU.xpath(doiRecord, 'c:crossref/c:conference', ns)).length) {
item = new Zotero.Item("conferencePaper");
refXML = ZU.xpath(itemXML, 'c:conference_paper', ns);
metadataXML = ZU.xpath(itemXML, 'c:proceedings_metadata', ns);
seriesXML = ZU.xpath(metadataXML, 'c:proceedings_metadata', ns);
item.publicationTitle = ZU.xpathText(metadataXML, 'c:publisher/c:proceedings_title', ns);
item.place = ZU.xpathText(metadataXML, 'c:event_metadata/c:conference_location', ns);
item.conferenceName = ZU.xpathText(metadataXML, 'c:event_metadata/c:conference_name', ns);
}
else if((itemXML = ZU.xpath(doiRecord, 'c:crossref/c:database', ns)).length) {
item = new Zotero.Item("report"); //should be dataset
refXML = ZU.xpath(itemXML, 'c:dataset', ns);
metadataXML = ZU.xpath(itemXML, 'c:database_metadata', ns);
}
item.abstractNote = ZU.xpathText(refXML, 'c:description', ns);
item.language = ZU.xpathText(metadataXML, './@language', ns);
item.ISBN = ZU.xpathText(metadataXML, 'c:isbn', ns);
item.ISSN = ZU.xpathText(metadataXML, 'c:issn', ns);
item.publisher = ZU.xpathText(metadataXML, 'c:publisher/c:publisher_name', ns);
item.edition = ZU.xpathText(metadataXML, 'c:edition_number', ns);
if(!item.volume) item.volume = ZU.xpathText(metadataXML, 'c:volume', ns);
parseCreators(refXML, item, (item.itemType == 'bookSection' ? {"editor": null} : "author") );
if(seriesXML && seriesXML.length) {
parseCreators(seriesXML, item, {"editor":"seriesEditor"});
item.series = ZU.xpathText(seriesXML, 'c:titles[1]/c:title[1]', ns);
item.seriesNumber = ZU.xpathText(seriesXML, 'c:series_number', ns);
item.reportType = ZU.xpathText(seriesXML, 'c:titles[1]/c:title[1]', ns);
}
//prefer article to journal metadata and print to other dates
var pubDateNode = ZU.xpath(refXML, 'c:publication_date[@media_type="print"]', ns);
if(!pubDateNode.length) pubDateNode = ZU.xpath(refXML, 'c:publication_date', ns);
if(!pubDateNode.length) pubDateNode = ZU.xpath(metadataXML, 'c:publication_date[@media_type="print"]', ns);
if(!pubDateNode.length) pubDateNode = ZU.xpath(metadataXML, 'c:publication_date', ns);
//dataset
if(!pubDateNode.length) pubDateNode = ZU.xpath(refXML, 'c:database_date/c:publication_date', ns);
if(!pubDateNode.length) pubDateNode = ZU.xpath(metaXML, 'c:database_date/c:publication_date', ns);
if(pubDateNode.length) {
var year = ZU.xpathText(pubDateNode[0], 'c:year', ns);
var month = ZU.xpathText(pubDateNode[0], 'c:month', ns);
var day = ZU.xpathText(pubDateNode[0], 'c:day', ns);
if(year) {
if(month) {
if(day) {
item.date = year+"-"+month+"-"+day;
} else {
item.date = month+"/"+year
}
} else {
item.date = year;
}
}
}
var pages = ZU.xpath(refXML, 'c:pages[1]', ns);
if(pages.length) {
item.pages = ZU.xpathText(pages, 'c:first_page[1]', ns);
var lastPage = ZU.xpathText(pages, 'c:last_page[1]', ns);
if(lastPage) item.pages += "-"+lastPage;
}
item.DOI = ZU.xpathText(refXML, 'c:doi_data/c:doi', ns);
item.url = ZU.xpathText(refXML, 'c:doi_data/c:resource', ns);
var title = ZU.xpath(refXML, 'c:titles[1]/c:title[1]', ns)[0];
if(title) {
item.title = ZU.trimInternal(
removeUnsupportedMarkup(innerXML(title))
);
var subtitle = ZU.xpath(refXML, 'c:titles[1]/c:subtitle[1]', ns)[0];
if(subtitle) {
item.title += ': ' + ZU.trimInternal(
removeUnsupportedMarkup(innerXML(subtitle))
);
}
}
//Zotero.debug(JSON.stringify(item, null, 4));
//check if there are potential issues with character encoding and try to fix it
//e.g. 10.1057/9780230391116.0016 (en dash in title is presented as <control><control>â)
for(var field in item) {
if(typeof item[field] != 'string') continue;
//check for control characters that should never be in strings from CrossRef
if(/[\u007F-\u009F]/.test(item[field])) {
item[field] = decodeURIComponent(escape(item[field]));
}
}
item.complete();
return true;
}
function doSearch(item) {
if(item.contextObject) {
var co = item.contextObject;
if(co.indexOf("url_ver=") == -1) {
co = "url_ver=Z39.88-2004&"+co;
}
} else if(item.DOI) {
var co = "url_ver=Z39.88-2004&&rft_id=info:doi/"+ZU.cleanDOI(item.DOI.toString());
} else {
var co = Zotero.Utilities.createContextObject(item);
}
Zotero.Utilities.HTTP.doGet("http://www.crossref.org/openurl/?pid=zter:zter321&"+co+"&noredirect=true&format=unixref", function(responseText) {
processCrossRef(responseText);
Zotero.done();
});
Zotero.wait();
}
/** BEGIN TEST CASES **/
var testCases = [
{
"type": "search",
"input": {
"DOI":"10.1017/CCOL0521858429.016"
},
"items": [
{
"itemType": "bookSection",
"creators": [
{
"creatorType": "editor",
"firstName": "John",
"lastName": "Rodden"
},
{
"creatorType": "author",
"firstName": "Christopher",
"lastName": "Hitchens"
}
],
"notes": [],
"tags": [],
"seeAlso": [],
"attachments": [],
"bookTitle": "The Cambridge Companion to George Orwell",
"place": "Cambridge",
"ISBN": "9781139001472",
"publisher": "Cambridge University Press",
"pages": "201-207",
"date": "2007",
"DOI": "10.1017/CCOL0521858429.016",
"url": "http://universitypublishingonline.org/ref/id/companions/CBO9781139001472A019",
"title": "Why Orwell still matters",
"libraryCatalog": "CrossRef"
}
]
},
{
"type": "search",
"input": {
"DOI":"10.1057/9780230391116.0016"
},
"items": [
{
"itemType": "bookSection",
"creators": [
{
"creatorType": "editor",
"firstName": "Claus-Christian W.",
"lastName": "Szejnmann"
},
{
"creatorType": "editor",
"firstName": "Maiken",
"lastName": "Umbach"
},
{
"creatorType": "author",
"firstName": "Oliver",
"lastName": "Werner"
}
],
"notes": [],
"tags": [],
"seeAlso": [],
"attachments": [],
"bookTitle": "Heimat, Region, and Empire",
"ISBN": "9780230391116",
"publisher": "Palgrave Macmillan",
"language": "en",
"date": "2012-10-17",
"DOI": "10.1017/CCOL0521858429.016",
"url": "http://www.palgraveconnect.com/doifinder/10.1057/9780230391116.0016",
"title": "Conceptions, Competences and Limits of German Regional Planning during the Four Year Plan, 1936–1940",
"libraryCatalog": "CrossRef"
}
]
}
]
/** END TEST CASES **/