-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschemify.js
395 lines (343 loc) · 13.9 KB
/
schemify.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
var Schemify = {
initSchema: function () {
var schema = {
"$schema": "http://json-schema.org/draft-04/schema#"
};
return schema;
},
JSONtoJSONSchema: function (obj) {
var schema = this.initSchema();
if (Object.keys(obj).length === 1) {
var title = Object.keys(obj)[0];
schema.title = title;
obj = obj[title];
}
var objType = typeof obj;
schema.type = objType;
if (objType !== "object") {
schema.properties = {};
return schema;
}
var str = Array.isArray(obj) ? "items" : "properties";
schema[str] = {};
(function traverse(obj, result) {
var keys = Object.keys(obj);
keys.map(function (key) {
var subject = obj[key],
type = typeof subject;
//if type is object or array
if (type === "object") {
//if Array
if (Array.isArray(subject)) {
result[key] = {
"type": "array"
};
var arrayType = typeof subject[0],
tempObject = {
"items": {}
};
if (arrayType !== "object") {
tempObject.items.type = arrayType;
result[key].items = tempObject.items;
return true;
} else {
tempObject.items = subject[0];
return traverse(tempObject, result[key]);
}
} else {
result[key] = {
"type": type,
"properties": {}
};
return traverse(subject, result[key].properties);
}
} else { //if leaf type
result[key] = {
"type": type
};
return true;
}
});
})(obj, schema[str]);
return schema;
},
XMLtoJSONSchema: function (xmlText) {
var schema = this.initSchema(),
self = this,
//parse XML tree
root = this.parseXMLTree(xmlText);
schema.title = root.tagName;
schema.type = "object";
schema.properties = {};
var namespace = "";
if (root.attributes.length > 0) {
var obj = {};
for (var j = 0; j < root.attributes.length; j++) {
var attr = root.attributes[j];
//skip xmlns stuff
if (!attr.name.includes("xmlns")) {
obj[attr.name] = {
"type": self.getType(attr.textContent)
}
} else {
if (attr.value.includes("www.w3.org") && attr.name.split(":").length > 1) {
namespace = attr.name.split(":")[1];
}
}
}
schema.attributes = obj;
}
for (var i = 0; i < root.children.length; i++) {
traverseXMLTree(root.children[i], schema["properties"]);
}
function traverseXMLTree(rootNode, parent) {
var children = rootNode.children;
var attributes = rootNode.attributes;
var title = rootNode.tagName;
//check if there is xsi:type... and add attributes
var myAttributes = {};
if (attributes.length > 0) {
for (var j = 0; j < attributes.length; j++) {
var attr = attributes[j];
if (!attr.name.includes("xmlns")) {
//if there is (xsi:type) add the value to title
if (attr.name === namespace + ":type") {
title = title + ":" + attr.value;
} //else {
myAttributes[attr.name] = {
"type": self.getType(attr.textContent)
}
// }
}
}
}
var isArray = parent[title] ? true : false;
if (isArray) { //if array
parent[title]["type"] = "array";
if (parent[title]["properties"]) {
parent[title]["items"] = {
"type": "object",
"properties": parent[title]["properties"],
}
delete parent[title]["properties"];
}
return;
} else {
parent[title] = {
"type": self.getType(rootNode.textContent)
};
}
if (children.length === 0 && attributes.length === 0) {
return;
} else {
//add children
if (children.length !== 0) {
parent[title] = {
"type": "object",
"properties": {}
};
var nestParent = parent[title]["properties"];
// $("#" + resultBox).append(nodeName);
for (var i = 0; i < children.length; i++) {
var child = children[i];
traverseXMLTree(child, nestParent);
}
}
//add attributes
if (attributes.length !== 0) {
parent[title]["attributes"] = myAttributes;
}
}
}
return schema;
},
XSDtoJSONSchema: function (xsdText) {
var self = this;
var schema = this.initSchema();
schema["title"] = "Root",
schema["properties"] = {},
schema["type"] = "object",
schema["attributes"] = {};
var root = this.parseXMLTree(xsdText);
var complexTypes = {};
var ignoreTags = ["any", "anyAttribute", "sequence", "all", "choice", "annotation", "documentation"];
var namespaces = []; //the first entry is default namespace
var schemaAttributes = root.attributes;
var generalElements = root.children;
var globalElements = [];
//filter the namespaces eg.xs,xsd,xsi...
for (var i = 0; i < schemaAttributes.length; i++) {
var attr = schemaAttributes[i];
var splitKey = attr.name.split(":");
if (splitKey.length > 1 && splitKey[0] === "xmlns") {
namespaces.push(splitKey[1]);
if (attr.value.includes("http://www.w3.org") && i !== 0) { //set primary as first
var prim = namespaces.pop();
namespaces.push(namespaces[0]);
namespaces[0] = prim;
}
}
}
// console.log(namespaces);
//filter definitions in root
var definitions = {};
for (var i = 0; i < root.children.length; i++) {
var child = root.children[i];
if (child.attributes.name) {
definitions[child.attributes.name.value] = {};
var res = traverseXSDTree(child, definitions[child.attributes.name.value], child.attributes.name.value);
}
if (child.tagName === namespaces[0] + ":element") {
globalElements.push(child);
}
}
console.log(JSON.stringify(definitions, null, 4));
//set the root
var schemaRoot = globalElements[0];
// console.log(globalElements);
if (globalElements.length > 1) {
console.log("multiple possible roots - first possible element selected");
//if no child elements and primary - select next
var count = globalElements.length - 1;
while (count >= 0) {
schemaRoot = globalElements[count];
if (schemaRoot.children.length !== 0 || !isPrimaryType(schemaRoot.attributes.type.value)) {
break;
}
count--;
}
}
var rootTitle = schemaRoot.attributes.name.value;
schema["title"] = rootTitle;
schemaRoot = definitions[rootTitle];
// console.log(schemaRoot.attributes.name);
var myCount = 0;
//create schema from root
createSchemaFromDefs(schemaRoot, schema);
//create the schema using the calculated definitions
function createSchemaFromDefs(rootObj, target) {
for (var kk = 0; kk < Object.keys(rootObj).length; kk++) {
var key = Object.keys(rootObj)[kk];
var val = rootObj[key];
if (typeof val === "object") {
var tempTarget = target;
target["properties"] = target["properties"] || {};
tempTarget = target["properties"];
if (val["isAttribute"]) { //iff attribute - change the target location
target["attributes"] = target["attributes"] || {};
tempTarget = target["attributes"];
}
if (val["type"] && definitions[val.type]) { //if the type is a definition, choose it as the subject
val = definitions[val.type];
}
if (val["isLeaf"]) {
//if a leaf node - add it to the target
tempTarget[key] = val;
} else {
if (val["isGroupDef"]) {
//if the definition is group - use the parent target (not in a new nested child)
tempTarget = target;
createSchemaFromDefs(val, tempTarget);
} else {
tempTarget[key] = {};
tempTarget[key]["type"] = "object";
createSchemaFromDefs(val, tempTarget[key]);
}
}
} else {
if (key === "type" && definitions[val]) {
if (!definitions[val]["isLeaf"]) {
createSchemaFromDefs(definitions[val], target);
}
}
}
}
}
//traverse the items and add to definitions
function traverseXSDTree(root, result, title) {
var obj = result;
var tagName = getTagName(root.tagName);
if (ignoreTags.indexOf(tagName) > -1 || ((tagName === "complexType" || tagName === "simpleType") && !root.attributes.length) || tagName === "restriction") { //if the tag is
obj = result;
} else {
var tempName = root.attributes.name || root.attributes.ref;
if (tempName) {
if (tagName !== "complexType" && tagName !== "simpleType") {
var rootName = tempName.value;
if (rootName !== title) {
result[rootName] = {};
obj = result[rootName];
}
} else {
obj = result;
}
if (tagName === "group") {
obj["isGroupDef"] = true;
}
}
}
if (root.children.length === 0) {
for (var j = 0; j < root.attributes.length; j++) {
var attr = root.attributes[j];
if (attr.name === "type") {
if (isPrimaryType(attr.value)) {
obj["isLeaf"] = true;
}
obj["type"] = getTagName(attr.value) || attr.value;
} else if (attr.name === "ref") {
obj["type"] = attr.value;
} else {
obj[attr.name] = attr.value;
}
}
if (tagName === "attribute") {
obj["isAttribute"] = true;
}
return result;
}
if (getTagName(root.parentElement.tagName) === "simpleType" && root.attributes.base) {
// console.log(obj);
obj["type"] = getTagName(root.attributes.base.value);
obj["isLeaf"] = true;
// console.log(obj);
return result;
}
for (var i = 0; i < root.children.length; i++) {
var child = root.children[i];
var res = traverseXSDTree(child, obj, "");
}
return result;
}
function getTagName(name) {
var keys = name.split(":");
if (keys.length == 2 && namespaces.indexOf(keys[0] > -1)) {
return keys[1];
}
}
function isPrimaryType(type) {
var keys = type.split(":");
if (keys.length == 2 && namespaces[0] === keys[0]) {
return keys[1];
} else {
return false;
}
}
return schema;
},
parseXMLTree: function (xmlText) {
parser = new DOMParser();
var xmlDoc = parser.parseFromString(xmlText, "text/xml");
// documentElement always represents the root node
var root = xmlDoc.documentElement;
return root;
},
getType: function (text) {
if (text === "true" || text === "false") {
return "boolean";
}
if (!isNaN(Number(text))) {
return "number";
}
return "string";
}
}