-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathschema_processor.js
580 lines (508 loc) · 17.1 KB
/
schema_processor.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/**
* Processes an XML schema, extracting key features and storing them into
* javascript structures.
*
* @author bbpennel
*/
/**
* xsdDocument XML document representaton of the schema document to be processed
* xsdManager schema manager which this processor belongs to
*/
function SchemaProcessor(xsdDocument, xsdManager, schemaUrl, parentNSIndex) {
this.xsd = xsdDocument;
this.xsdManager = xsdManager;
this.schemaUrl = schemaUrl;
this.schemaPath = this.schemaUrl.substring(0, this.schemaUrl.lastIndexOf("/") + 1);
// Object definitions defined at the root level of the schema
this.rootDefinitions = {};
// Local namespace prefix registry
this.localNamespaces = $.extend({}, this.xsdManager.globalNamespaces);
this.extractNamespaces();
if (parentNSIndex !== undefined) {
this.targetNSIndex = parentNSIndex;
this.targetNS = this.xsdManager.getNamespaceUri(parentNSIndex);
} else {
this.targetNS = this.xsd.getAttribute('targetNamespace');
this.targetNSIndex = this.xsdManager.registerNamespace(this.targetNS);
}
// Register the correct default namespace if none was specified, see:
// * https://www.w3.org/TR/xmlschema11-1/#src-include
// * https://www.w3.org/TR/xmlschema11-1/#sec-src-resolve
if (!('' in this.localNamespaces)) {
if (this.xsd.getAttribute('targetNamespace') === null) {
this.localNamespaces[''] = this.targetNS;
} else {
this.localNamespaces[''] = this.xsdManager.xsNS;
}
}
// Root definition for this schema, either an element or a schema object
this.root = null;
};
// Process the schema file to extract its structure into javascript objects
SchemaProcessor.prototype.processSchema = function() {
// Begin constructing the element tree, starting from the schema element
this.root = this.build_schema(this.xsd);
};
SchemaProcessor.prototype.extractNamespaces = function() {
for (var i = 0; i < this.xsd.attributes.length; i++) {
var attr = this.xsd.attributes[i];
if (!attr.specified)
continue;
var namespaceIndex = attr.nodeName.indexOf("xmlns");
if (namespaceIndex == 0){
var namespacePrefix = attr.nodeName.substring(5).replace(":", "");
var namespaceUri = attr.nodeValue;
this.registerNamespace(namespaceUri, namespacePrefix);
}
}
};
SchemaProcessor.prototype.createDefinition = function(node, name) {
if (!name) {
name = node.getAttribute("name");
}
// New root definition
var definition = {
values : [],
type : null,
ns: this.targetNSIndex,
np : true
};
if (name)
definition.name = name;
if (node.localName == "attribute") {
definition.attribute = true;
} else {
if (node.localName == "element")
definition.element = true;
definition.attributes = [];
definition.elements = [];
}
return definition;
};
SchemaProcessor.prototype.addElement = function(node, definition, parentDef) {
if (!parentDef.schema) {
// Store min/max occurs on the the elements parent, as they only apply to this particular relationship
// Root level elements can't have min/max occurs attributes
var minOccurs = node.getAttribute("minOccurs");
var maxOccurs = node.getAttribute("maxOccurs");
if (parentDef && (minOccurs || maxOccurs)) {
var name = node.getAttribute('name');
var indexedNameOrRef = name ? this.indexedDefinitionName(name) : this.extractName(node.getAttribute('ref')).indexedName;
if (!("occurs" in parentDef))
parentDef.occurs = {};
parentDef.occurs[indexedNameOrRef] = {
'min' : minOccurs,
'max' : maxOccurs
};
}
}
// Add this element as a child of the parent, unless it is abstract or already added
if (parentDef != null && node.getAttribute("abstract") != "true"
&& !this.containsChild(parentDef, definition))
parentDef.elements.push(definition);
return definition;
};
SchemaProcessor.prototype.addAttribute = function(node, definition, parentDef) {
// Add the definition to its parents attributes array
if (parentDef != null)
parentDef.attributes.push(definition);
};
SchemaProcessor.prototype.addReference = function(definition, refName) {
var nameParts = this.extractName(refName);
definition.ref = nameParts.indexedName;
};
SchemaProcessor.prototype.addTypeReference = function(definition, refName, mergeMode) {
var nameParts = this.extractName(refName);
if (!definition.typeRef) {
definition.typeRef = [];
}
definition.typeRef.push({indexedName: nameParts.indexedName, mergeMode: mergeMode});
};
// Build the schema tag
SchemaProcessor.prototype.build_schema = function(node) {
var definition = {
elements : [],
ns : this.targetNSIndex,
schema : true,
np : true
};
var self = this;
var children = this.getChildren(node);
for (var i in children) {
var child = children[i];
if (child.localName == 'element') {
var element = this.buildTopLevel(child, definition);
this.addElement(child, element, definition);
} else if (child.localName == 'simpleType' ||
child.localName == 'attribute' || child.localName == 'complexType' ||
child.localName == 'group' || child.localName == 'attributeGroup') {
this.buildTopLevel(child, definition);
}
}
return definition;
};
SchemaProcessor.prototype.buildTopLevel = function(node) {
var name = node.getAttribute("name");
// Root level definitions must have a name attribute
if (!name)
return;
// New root definition
var definition = this.createDefinition(node, name);
this.rootDefinitions[this.indexedDefinitionName(name)] = definition;
return this.build(node, definition);
};
SchemaProcessor.prototype.build = function(node, definition, parentDef) {
return this["build_" + node.localName](node, definition, parentDef);
};
// Build an element definition
// node - element schema node
// parentdefinition - definition of the parent this element will be added to
SchemaProcessor.prototype.build_element = function(node, definition, parentDef) {
var ref = node.getAttribute("ref");
if (ref) {
this.addReference(definition, ref);
} else {
// Build or retrieve the type definition
var type = node.getAttribute("type");
if (type == null) {
var child = this.getChildren(node)[0];
if (child) {
this.build(child, definition);
} else {
var subGroup = node.getAttribute('substitutionGroup');
if (subGroup) {
this.addTypeReference(definition, subGroup, 'extension');
} else {
definition.type = 'anyType';
}
}
} else {
// Check to see if it is a built in type
definition.type = this.getBuiltInType(type, definition);
if (definition.type == null) {
// Was not built in, make a reference to resolve later
this.addTypeReference(definition, type, 'extension');
}
}
}
return definition;
}
SchemaProcessor.prototype.build_attribute = function(node, definition) {
var ref = node.getAttribute("ref");
if (ref) {
this.addReference(definition, ref);
} else {
// Build or retrieve the type definition
var type = node.getAttribute("type");
if (type == null) {
var child = this.getChildren(node)[0];
if (child)
this.build(this.getChildren(node)[0], definition);
else // Fall back to string type if nothing else available
definition.type = "string";
} else {
// Check to see if it is a built in type
definition.type = this.getBuiltInType(type, definition);
if (definition.type == null) {
// Was not built in, make a reference to resolve later
this.addTypeReference(definition, type, 'extension');
}
}
}
return definition;
};
// Process a complexType tag
SchemaProcessor.prototype.build_complexType = function(node, definition, parentDef) {
if (node.getAttribute("mixed") == "true") {
definition.type = "mixed";
}
var children = this.getChildren(node);
for (var i in children) {
var child = children[i];
switch (child.localName) {
case "group" : case "simpleContent" : case "complexContent" : case "choice" :
case "attributeGroup" : case "sequence" : case "all" : case "anyAttribute" :
this.build(child, definition);
break;
case "attribute" :
this.addAttribute(child, this.build_attribute(child,
this.createDefinition(child)), definition);
}
}
};
// Process a simpleType tag
SchemaProcessor.prototype.build_simpleType = function(node, definition) {
var child = this.getChildren(node)[0];
switch (child.localName) {
case "restriction" : case "list" : case "union" :
this.build(child, definition);
}
};
// Process a list tag, which allows for a single tag with multiple values
SchemaProcessor.prototype.build_list = function(node, definition) {
// For the moment, lists will just be treated as free text fields
definition.type = "string";
definition.multivalued = true;
};
// Process a union tag
SchemaProcessor.prototype.build_union = function(node, definition) {
var memberTypes = node.getAttribute('memberTypes');
if (memberTypes) {
memberTypes = memberTypes.split(' ');
for (var i in memberTypes) {
var memberType = memberTypes[i];
definition.type = this.getBuiltInType(memberType, definition);
if (definition.type == null) {
this.addTypeReference(definition, memberType, 'extension');
}
}
}
var self = this;
var children = this.getChildren(node, 'simpleType');
for (var i in children)
self.build_simpleType(children[i], definition);
};
// Process a group tag
SchemaProcessor.prototype.build_group = function(node, definition) {
var ref = node.getAttribute("ref");
if (ref){
this.addTypeReference(definition, ref, 'extension');
return definition;
}
var self = this;
var children = this.getChildren(node);
for (var i in children) {
var child = children[i];
switch (child.localName) {
case "choice" : case "all" : case "sequence" :
this.build(child, definition);
}
}
};
// Process an all tag
SchemaProcessor.prototype.build_all = function(node, definition) {
var self = this;
var children = this.getChildren(node);
for (var i in children) {
var child = children[i];
if (child.localName == "element") {
this.addElement(child, this.build_element(child,
this.createDefinition(child)), definition);
}
}
};
// Process a choice tag
SchemaProcessor.prototype.build_choice = function(node, definition) {
var self = this;
var max = node.getAttribute("maxOccurs");
var choice = {
"elements": [],
"minOccurs": node.getAttribute("minOccurs"),
"maxOccurs": (max == null || (isNaN(max) && "unbounded" != max) || max < 0)? 1 : max
};
var children = this.getChildren(node);
for (var i in children) {
var child = children[i];
switch (child.localName) {
case "choice" : case "group" : case "sequence" : case "any" :
this.build(child, definition);
break;
case "element" :
var element = this.addElement(child, this.build_element(child,
this.createDefinition(child)), definition);
if (element.name)
choice.elements.push(element.ns + ":" + element.name);
else
choice.elements.push(element.ref);
}
}
if (!('choices' in definition))
definition.choices = [];
definition.choices.push(choice);
};
// Process a sequence tag
SchemaProcessor.prototype.build_sequence = function(node, definition) {
var self = this;
var children = this.getChildren(node);
for (var i in children) {
var child = children[i];
switch (child.localName) {
case "choice" : case "group" : case "sequence" : case "any" :
this.build(child, definition);
break;
case "element" :
var element = this.addElement(child, this.build_element(child,
this.createDefinition(child)), definition);
}
}
};
// Process an any tag
SchemaProcessor.prototype.build_any = function(node, definition) {
definition.any = !(node.getAttribute("minOccurs") == "0" && node.getAttribute("maxOccurs") == "0");
};
SchemaProcessor.prototype.build_anyAttribute = function(node, definition) {
definition.anyAttribute = true;
};
// Process a complexContent tag
SchemaProcessor.prototype.build_complexContent = function(node, definition) {
if (node.getAttribute("mixed") == "true") {
definition.type = "mixed";
}
var child = this.getChildren(node)[0];
switch (child.localName) {
case "extension" : case "restriction" :
this.build(child, definition);
}
};
// Process a simpleContent tag
SchemaProcessor.prototype.build_simpleContent = function(node, definition) {
var child = this.getChildren(node)[0];
switch (child.localName) {
case "extension" : case "restriction" :
this.build(child, definition);
}
};
// Process a restriction tag
SchemaProcessor.prototype.build_restriction = function(node, definition) {
var base = node.getAttribute("base");
definition.type = this.getBuiltInType(base, definition);
if (definition.type == null) {
this.addTypeReference(definition, base, 'restriction');
}
var self = this;
var children = this.getChildren(node);
for (var i in children) {
var child = children[i];
switch (child.localName) {
case "group" : case "simpleType" : case "choice" :
case "attributeGroup" : case "sequence" : case "all" : case "anyAttribute":
this.build(child, definition);
break;
case "attribute" :
this.addAttribute(child, this.build_attribute(child,
this.createDefinition(child)), definition);
break;
case "enumeration" :
definition.values.push(child.getAttribute("value"));
}
}
};
// Process an extension tag
SchemaProcessor.prototype.build_extension = function(node, definition) {
var base = node.getAttribute("base");
definition.type = this.getBuiltInType(base, definition);
if (definition.type == null) {
this.addTypeReference(definition, base, 'extension');
}
var self = this;
var children = this.getChildren(node);
for (var i in children) {
var child = children[i];
switch (child.localName) {
case "group" : case "choice" : case "attributeGroup" :
case "sequence" : case "all" : case "anyAttribute":
this.build(child, definition);
break;
case "attribute" :
this.addAttribute(child, this.build_attribute(child,
this.createDefinition(child)), definition);
}
}
};
// Process an attributeGroup tag
SchemaProcessor.prototype.build_attributeGroup = function(node, definition) {
var ref = node.getAttribute("ref");
if (ref){
this.addTypeReference(definition, ref, 'extension');
return definition;
}
var children = this.getChildren(node);
for (var i in children) {
var child = children[i];
switch (child.localName) {
case "attributeGroup" : case "anyAttribute" :
this.build(child, definition);
break;
case "attribute" :
this.addAttribute(child, this.build_attribute(child,
this.createDefinition(child)), definition);
}
}
};
SchemaProcessor.prototype.getBuiltInType = function(type, definition) {
if (definition.type != null)
return definition.type;
var nameParts = this.extractName(type);
if (nameParts.namespaceUri === this.xsdManager.xsNS) {
return nameParts.localName;
}
return null;
};
//Retrieve all the children of node which belong to the schema namespace.
//If they are specified, then the results will be filtered to only include children which
//match the given element name and/or have a name attribute of the given value
//filtered to
SchemaProcessor.prototype.getChildren = function(node, childName, nameAttribute) {
var children = [];
if (!node)
node = this.xsd;
var childNameSpecified = childName !== undefined;
var attributeSpecified = nameAttribute !== undefined;
var childNodes = node.childNodes;
for (var index in childNodes) {
var child = childNodes[index];
if (child.nodeType == 1 && child.namespaceURI == this.xsdManager.xsNS &&
((childNameSpecified && child.localName == childName) || (!childNameSpecified && child.localName != 'annotation')) &&
(!attributeSpecified || child.getAttribute('name') == nameAttribute))
children.push(child);
}
return children;
}
//Namespace aware check to see if a definition already contains parent child element
SchemaProcessor.prototype.containsChild = function(definition, child) {
if (definition.elements) {
var childName = child.ref || child.ns + ":" + child.name;
for (var index in definition.elements) {
var element = definition.elements[index];
var existingName = element.ref || element.ns + ":" + element.name;
if (childName == existingName)
return true;
}
}
return false;
};
SchemaProcessor.prototype.indexedDefinitionName = function(name) {
return this.targetNSIndex + ':' + name;
};
SchemaProcessor.prototype.extractName = function(name) {
if (!name)
return null;
var result = {};
var index = name.indexOf(':');
if (index == -1) {
result['localName'] = name;
result['prefix'] = "";
} else {
result['localName'] = name.substring(index + 1);
result['prefix'] = name.substring(0, index);
}
var namespaceUri = this.localNamespaces[result.prefix];
result['namespaceUri'] = namespaceUri;
result['namespace'] = this.xsdManager.getNamespaceIndex(namespaceUri);
result['indexedName'] = result.namespace + ":" + result.localName;
return result;
};
//Register a namespace, and optionally its prefix, to the schema
SchemaProcessor.prototype.registerNamespace = function(namespaceUri, prefix) {
var namespaceIndex = this.xsdManager.registerNamespace(namespaceUri);
if (prefix !== undefined)
this.localNamespaces[prefix] = namespaceUri;
return namespaceIndex;
};
SchemaProcessor.prototype.getLocalNamespacePrefix = function(namespaceUri) {
for (var prefix in this.localNamespaces) {
if (this.localNamespaces[prefix] == namespaceUri)
return prefix;
}
return null;
};