This repository has been archived by the owner on Jun 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
jats4r-parser.js
324 lines (284 loc) · 9.18 KB
/
jats4r-parser.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
// This module contains functions for parsing the XML header, not including
// the xml declaration.
// parse_header() looks at everything up to and including the opening tag,
// and, among other things, makes a list of schema references.
// This returns an array of schema references, each of which is an object with:
// - type - one of 'xml-model-pi', 'doctype-decl', or 'xsd-attrs'
// - is_jats - boolean value: true == jats; false = jats4r
// - ref_type - one of 'dtd', 'rng', 'xsd', or 'schematron'
// - schema - If JATS, the Schema object from the yaml file database.
// - version - If JATS4R, the version number as a string.
if (typeof jats4r == "undefined") jats4r = {};
jats4r.parser = (function() {
function parse_header(contents) {
var results = jats4r.results;
var schema_db = jats4r.jats_schema_db;
var m, i;
// We'll return this:
var schema_refs = [];
// Delete all comments
// (Note that in these regexps we use
// "[\s\S]" instead of ".", to make sure it matches newlines.)
contents = contents.replace(/<\!--([\s\S]*?)-->/g, "");
// Now grab everything up to and including the opening element.
m = contents.match(/^[\s\S]*?<\s*article\s+([\s\S]*?)>/);
if (!m) return schema_refs;
contents = m[0];
// Look for a doctype declaration
// ------------------------------
// This is first, because it should take precedence
var doctype_pub_re =
/<!DOCTYPE\s+\S+\s+PUBLIC\s+('|\")(.*?)('|\")\s+('|\")(.*?)('|\")\s*(\[[\s\S]*?\]\s*)?>/;
if (m = contents.match(doctype_pub_re)) {
var fpi = m[2];
var sysid = m[5];
s = schema_db.get_by_fpi(fpi) || null;
var use_dtd = true;
if (!s) {
results.error("Bad doctype declaration. " +
"Unrecognized public identifier: '" + fpi + "'");
use_dtd = false;
}
else if (s.sysid() != sysid) {
if (s.sysid().endsWith(sysid)) {
results.error(
"<p>Bad doctype declaration: it looks like this article only uses a " +
"partial system identifier: '" + sysid + "'. In order to facilitate reuse, " +
"we recommend " +
"that all documents that use a doctype declaration include full " +
"public and system identifiers.</p>\n"
);
}
else {
results.error(
"<p>Bad doctype declaration: the public and system identifiers don't match.\n" +
"Based on the public identifier of '" + fpi + "', we were expecting a " +
"system identifier of '" + s.sysid() + "'. However, this document " +
"has '" + sysid + "'. JATS4R requires that, when using a doctype declaration, " +
"you should use the full URI of the system identifier.</p>\n"
);
}
}
if (use_dtd) {
schema_refs.push({
type: 'doctype-decl',
is_jats: true,
ref_type: 'dtd',
schema: s,
});
}
}
else {
var doctype_sys_re =
/<!DOCTYPE\s+\S+\s+SYSTEM\s+('|\")(.*?)('|\")\s*(\[[\s\S]*?\]\s*)?>/;
if (m = contents.match(doctype_sys_re)) {
results.error(
"A doctype declaration was found that only contains a SYSTEM identifer. " +
"Documents conforming to JATS4R that use DTDs are required to include " +
"a PUBLIC identifier.");
var sysid = m[2];
s = schema_db.get_by_sysid(sysid);
if (!s) {
results.error("Unrecognized system identifier '" + sysid + "'.");
}
else {
schema_refs.push({
type: 'doctype-decl',
is_jats: true,
ref_type: 'dtd',
schema: s,
});
}
}
}
// Look for <?xml-model?> PIs
// --------------------------
var xml_model_pis = [];
if (m = contents.match(/<\?xml-model\s+([\s\S]*?)\s*\?>/g)) {
for (i = 0; i < m.length; ++i) {
xml_model_pis.push(jats4r.parser.parse_pi(m[i]));
}
}
console.log("xml-model pis: %o", xml_model_pis);
var s;
xml_model_pis.forEach(function(pi) {
if (!pi.href) return;
var schema_ref = {
type: 'xml-model-pi',
};
var schematypens = pi.schematypens ? pi.schematypens : '';
var type = pi.type ? pi.type : '';
/*
// Check JATS4R <?xml-model?> PI
if ( schematypens == "http://purl.oclc.org/dsdl/schematron" &&
(m = pi.href.match(/http:\/\/jats4r\.org\//)) )
{
// We know they're *trying* to use this PI, make sure it's strictly correct:
if (! (m = pi.href.match(/^http:\/\/jats4r\.org\/schema\/(\d+\.\d+)\/jats4r\.sch$/)) ) {
results.error(
"The JATS4R xml-model processing instruction seems to be in the wrong form. " +
"Please check that the href pseudo-attribute is strictly of the form, for example, " +
"<blockquote><pre>http://jats4r.org/schema/1.0/jats4r.sch</pre></blockquote>"
);
}
else {
schema_ref.is_jats = false;
schema_ref.ref_type = 'schematron';
schema_ref.version = m[1];
schema_refs.push(schema_ref);
}
}
*/
// Check for xml-model calling out one of DTD, RNG, or XSD
if (type == "application/xml-dtd") {
s = schema_db.get_by_sysid(pi.href) || null;
if (!s) {
results.error("Bad xml-model processing instruction. " +
"This references a DTD, but the DTD is not one of the recognized JATS " +
"DTD URLs: '" + pi.href + "'");
}
else {
schema_ref.is_jats = true;
schema_ref.ref_type = 'dtd';
schema_ref.schema = s;
schema_refs.push(schema_ref);
}
}
else if (schematypens == "http://relaxng.org/ns/structure/1.0") {
s = schema_db.get_by_rng_uri(pi.href) || null;
if (s) {
schema_ref.is_jats = true;
schema_ref.ref_type = 'rng';
schema_ref.schema = s;
schema_refs.push(schema_ref);
}
}
else if (schematypens == "http://www.w3.org/2001/XMLSchema") {
s = schema_db.get_by_xsd_uri(pi.href) || null;
if (s) {
schema_ref.is_jats = true;
schema_ref.ref_type = 'xsd';
schema_ref.schema = s;
schema_refs.push(schema_ref);
}
}
});
// Look for XSD attributes on the root element
// -------------------------------------------
var xsd_uri = null;
var root_elem_re = /<\s*article\s+([\s\S]*?)>/;
if (m = contents.match(root_elem_re)) {
var root_attrs = parse_attrs(m[1]);
// iterate through the attributes to see if an XSD namespace prefix was set
var xsi_prefix = null;
for (a in root_attrs) {
if (a.startsWith("xmlns:") && root_attrs[a] ==
"http://www.w3.org/2001/XMLSchema-instance")
{
xsi_prefix = a.substr(6);
break;
}
}
// now iterate again to see if a schema instance was specified
for (a in root_attrs) {
if (a = xsi_prefix + ":noNamespaceSchemaLocation") {
xsd_uri = root_attrs[a];
break;
}
}
}
if (xsd_uri) {
s = schema_db.get_by_xsd_uri(xsd_uri) || null;
if (s) {
schema_refs.push({
type: 'xsd-attrs',
is_jats: true,
ref_type: 'xsd',
schema: s,
});
}
}
return schema_refs;
}
// Parse a PI
function parse_pi(str) {
var v;
if (v = str.match(/^<\?\s*\S+\s+([\s\S]*?)\?>/)) {
return parse_attrs(v[1]);
}
else {
return {};
}
}
// Parse out the attributes or pseudo-attributes
function parse_attrs(str) {
var attrs = {};
var pos = 0;
var state = 0;
var strlen = str.length;
var name = "";
var val = "";
var quote_char;
// Each time through the loop, pos should point to the first non-whitespace.
while (pos < strlen) {
c = str.charAt(pos);
var is_white = /\s/.test(c);
// inside the name, looking for whitespace or "="
if (state == 0) {
if (is_white) {
state = 1;
}
else if (c == "=") {
state = 2;
}
else {
name += c;
}
}
// in whitespace after the name, looking for "="
else if (state == 1) {
if (c == "=") {
state = 2;
}
else if (!is_white) {
return attrs;
}
}
// after "=", looking for whitespace or a quote
else if (state == 2) {
if (c == "'" || c == '"') {
quote_char = c;
state = 3;
}
else if (!/\s/.test(c)) {
return attrs;
}
}
// inside the value, looking for quote_char
else if (state == 3) {
if (c == quote_char) {
attrs[name] = val;
state = 4;
}
else {
val += c;
}
}
// After the whole pseudo-attr; skip whitespace
else if (state == 4) {
if (!is_white) {
name = c;
val = "";
state = 0;
}
}
pos++;
}
return attrs;
}
return {
parse_pi: parse_pi,
parse_attrs: parse_attrs,
parse_header: parse_header,
};
})();