-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.js
90 lines (71 loc) · 2.15 KB
/
schema.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
// define constants
var TNAME = 0; // datatype name
var TTYPE = 1; // base type
var TOPTS = 2; // type options
var TDESC = 3; // type description
var FIELDS = 4; // list of fields
var FTAG = 0; // element id
var FNAME = 1; // element name
var EDESC = 2; // description for enumerated types
var FTYPE = 2; // datatype of field
var FOPTS = 3; // field options
var FDESC = 4; // field description
function getSchema() {
var url = 'https://raw.githubusercontent.com/romanojd/jaen/working/schema/openc2.jaen'
var schema = getDataUrl(url);
return schema;
}
function getSchemaTypes(jaen) {
var type_list = jaen['types'];
var types = {};
for (var i = 0; i < type_list.length; i++) {
var type = type_list[i];
// get the type name
var typename = type[TNAME];
// get the type
var datatype = type[TTYPE];
// get the field fields
Logger.log(type[TNAME]);
if (datatype == 'Enumerated') {
var fields = getSchemaEnumFields(type[FIELDS]);
} else {
var fields = getSchemaTypeFields(type[FIELDS]);
}
var obj = {'tname': type[TNAME],
'ttype': type[TTYPE],
'topts': type[TOPTS],
'tdesc': type[TDESC],
'fields': fields};
// add the type object to the json object
types[typename] = obj;
}
return types;
}
function getSchemaTypeFields(schema_fields) {
var fields = [];
if (schema_fields !== undefined) {
for (var i = 0; i < schema_fields.length; i++) {
var field = schema_fields[i];
var obj = {'ftag': field[FTAG],
'fname': field[FNAME],
'ftype': field[FTYPE],
'fopts': field[FOPTS],
'fdesc': field[FDESC]};
fields[i] = obj;
}
}
return fields;
}
function getSchemaEnumFields(schema_fields) {
var fields = [];
if (schema_fields !== undefined) {
for (var i = 0; i < schema_fields.length; i++) {
var field = schema_fields[i];
var obj = {'ftag': field[FTAG],
'fname': field[FNAME],
'edesc': field[EDESC]};
fields[i] = obj;
}
}
return fields;
}