-
Notifications
You must be signed in to change notification settings - Fork 0
/
CrossRef.js
146 lines (126 loc) · 4.62 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
{
"translatorID":"11645bd1-0420-45c1-badb-53fb41eeb753",
"translatorType":8,
"label":"CrossRef",
"creator":"Simon Kornblith",
"target":"http://partneraccess.oclc.org/",
"minVersion":"1.0.0b3.r1",
"maxVersion":"",
"priority":90,
"inRepository":true,
"lastUpdated":"2007-09-15 21:00:00"
}
function detectSearch(item) {
if(item.itemType == "journalArticle") {
return true;
}
return false;
}
function fixAuthorCapitalization(string) {
if(string.toUpperCase() == string) {
string = string.toLowerCase().replace(/\b[a-z]/g, function(m) { return m[0].toUpperCase() });
}
return string;
}
function processCrossRef(xmlOutput) {
xmlOutput = xmlOutput.replace(/<\?xml[^>]*\?>/, "");
// parse XML with E4X
try {
var xml = new XML(xmlOutput);
} catch(e) {
return false;
}
// ensure status is valid
if(!xml.doi_record.length()) return false;
// ensure this isn't an error
if(xml.doi_record.crossref.error.length()) {
throw xml.doi_record.crossref.error
return false;
}
if(xml.doi_record[0].crossref.journal.length()) {
var item = new Zotero.Item("journalArticle");
var itemXML = xml.doi_record.crossref.journal;
var refXML = itemXML.journal_article;
var metadataXML = itemXML.journal_metadata;
item.ISSN = itemXML.journal_metadata.issn.toString();
item.publicationTitle = itemXML.journal_metadata.full_title.toString();
item.journalAbbreviation = itemXML.journal_metadata.abbrev_title.toString();
item.volume = itemXML.journal_issue.journal_volume.volume.toString();
item.issue = itemXML.journal_issue.issue.toString();
} else if(xml.doi_record[0].crossref.book.length()) {
var item = new Zotero.Item("book");
var refXML = xml.doi_record[0].crossref.book.book_metadata;
var metadataXML = refXML;
var seriesXML = metadataXML.series_metadata;
item.place = metadataXML.publisher.publisher_place.toString();
} else if(xml.doi_record[0].crossref.conference.length()) {
var item = new Zotero.Item("conferencePaper");
var itemXML = xml.doi_record[0].crossref.conference;
var refXML = itemXML.conference_paper;
var metadataXML = itemXML.proceedingsMetadata;
var seriesXML = metadataXML.series_metadata;
item.publicationTitle = itemXML.proceedings_metadata.proceedings_title.toString();
item.place = itemXML.event_metadata.conference_location.toString();
item.conferenceName = itemXML.event_metadata.conference_name.toString();
}
var contributors = refXML.contributors.children();
if(metadataXML.isbn.length()) item.ISBN = metadataXML.isbn[0].toString();
if(metadataXML.issn.length()) item.ISSN = metadataXML.issn[0].toString();
item.publisher = metadataXML.publisher.publisher_name.toString();
item.edition = metadataXML.edition_number.toString();
if(!item.volume) item.volume = metadataXML.volume.toString();
if(seriesXML && seriesXML.length()) {
if(seriesXML.contributors.length()) {
contributors += seriesXML.contributors.children();
}
item.seriesNumber = seriesXML.series_number.toString();
}
for each(var creatorXML in contributors) {
var creator = {creatorType:"author"};
if(creatorXML.contributor_role == "editor") {
creator.creatorType = "editor";
} else if(creatorXML.contributor_role == "translator") {
creator.creatorType = "translator";
} else if(creatorXML.contributor_role == "chair") {
creator.creatorType = "contributor";
}
if(creatorXML.localName() == "organization") {
creator.fieldMode = 1;
creator.lastName = creatorXML.toString();
} else if(creatorXML.localName() == "person_name") {
creator.firstName = fixAuthorCapitalization(creatorXML.given_name.toString());
creator.lastName = fixAuthorCapitalization(creatorXML.surname.toString());
}
item.creators.push(creator);
}
item.date = refXML.publication_date.year.toString();
if(refXML.publication_date.month.length()) {
item.date = refXML.publication_date.month.toString()+"/"+item.date;
}
if(refXML.pages.length()) {
item.pages = refXML.pages.first_page.toString();
if(refXML.pages.last_page.length()) {
item.pages += "-"+refXML.pages.last_page.toString();
}
}
item.DOI = refXML.doi_data.doi.toString();
item.url = refXML.doi_data.resource.toString();
item.title = refXML.titles.title.toString();
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 {
var co = Zotero.Utilities.createContextObject(item);
}
Zotero.Utilities.HTTP.doGet("http://www.crossref.org/openurl?req_dat=zter:zter321&"+co+"&noredirect=true&format=unixref", function(responseText) {
processCrossRef(responseText);
Zotero.done();
});
Zotero.wait();
}