-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml.js
More file actions
170 lines (151 loc) · 5.71 KB
/
xml.js
File metadata and controls
170 lines (151 loc) · 5.71 KB
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
function getComponents() {
// Get components chosen
var components = []; //COMPONENTS IS AN ARRAY
// TODO: Go through objects selected and populate components array
// USING SAMPLE FOR NOW
// ---------------
// CREATING SAMPLE
// var sampleName = "camera";
var sampleKeys = {};
// sampleKeys["option1"] = 2.0
// var sampleComponent = [sampleName, sampleKeys];
var sampleName = "audio";
var sampleComponent2 = [sampleName, sampleKeys];
// ---------------
// Adding component with settings to components array
// components.push(sampleComponent);
components.push(sampleComponent2);
// returning the filename and the formed component list where each component is [cName, cKeys]
return components
}
function sampleMakeXML() {
// CREATING XML
// Start creating XML
var doc = document.implementation.createDocument(pipename, 'xml')
// Create element
var tag = document.createElementNS('tagId', 'tag');
// Set parameters in tag
tag.setAttribute('opt1', 'abc');
tag.setAttribute('opt2', 'def');
doc.documentElement.appendChild(tag);
// Create element 2
var tag2 = document.createElementNS('tagId', 'tag2');
// Set parameters in tag
tag2.setAttribute('opt1', 'abc');
tag2.setAttribute('opt2', 'def');
doc.documentElement.appendChild(tag2);
// Create a child of an element
var tagChild = document.createElementNS('tagChildId', 'tagChild');
// Set parameters in tag
tagChild.setAttribute('opt1', 1.0);
tagChild.setAttribute('opt2', 2.0);
tag.appendChild(tagChild);
console.log(doc);
}
function createXMLElem(tagName, options, nestable=true) {
// Create element
var tag;
if(nestable) {
tag = document.createElementNS('tagId', tagName);
// Set parameters in tag
Object.keys(options).forEach(function(key) {
tag.setAttribute(key, options[key]);
})
}
else {
// TODO: Implement elements of type <load name="ssigraphic" />
tag = document.createElement(tagName);
}
return tag
}
// A function that generates XML from various inputs got from the UI
function makePipe() {
// Get map
map = getJsonMap();
// Get filename
var fName = document.getElementById("pipename").value;
var components = getComponents();
// console.log(fName);
// Printing components list
// var arrayLength = components.length;
// for (var i = 0; i < arrayLength; i++) {
// console.log(i+" -> "+components[i]);
// }
// Start creating XML
var pipe = document.implementation.createDocument('', 'pipeline');
// Register block
// TODO: FLAGS
var comment = document.createComment("BEGIN REGISTER BLOCK");
pipe.documentElement.appendChild(comment);
var register = createXMLElem("register", {});
pipe.documentElement.appendChild(register);
// Loop through components and register them
arrayLength = components.length;
for (var i = 0; i < arrayLength; i++) {
var currentCompName = (components[i][0]);
var currentCompProps = (map[currentCompName]);
var currentCompLoads = currentCompProps[0];
// initialize loads
arrayLen = currentCompLoads.length;
var comment = document.createComment(currentCompName);
register.appendChild(comment);
for (var j = 0; j < arrayLen; j++) {
currLoadStr = (currentCompLoads[j]);
// ------- PARSE STRING TO DOM ELEMENT- "standard" method
var d = document.createElement('div');
d.innerHTML = currLoadStr;
currLoad = (d.firstChild);
// --------
register.appendChild(currLoad);
}
}
var comment = document.createComment("BEGIN SENSOR BLOCK");
pipe.documentElement.appendChild(comment);
// Sensor creation/initialization block
// TODO: FLAGS
arrayLength = components.length;
for (var i = 0; i < arrayLength; i++) {
var currentCompName = (components[i][0]);
var currentCompProps = (map[currentCompName]);
var currentCompSense = currentCompProps[1];
document.createElement('div');
d.innerHTML = currentCompSense;
currSense = d.firstChild
pipe.documentElement.appendChild(currSense);
}
var comment = document.createComment("BEGIN VISUALIZATION BLOCK");
pipe.documentElement.appendChild(comment);
// Visualization block
arrayLength = components.length;
for (var i = 0; i < arrayLength; i++) {
var currentCompName = (components[i][0]);
var currentCompProps = (map[currentCompName]);
var currentCompVis = currentCompProps[2];
document.createElement('div');
d.innerHTML = currentCompVis;
currVis = d.firstChild
pipe.documentElement.appendChild(currVis);
}
// Decorator block
var visualize = createXMLElem("object", {"create": "Decorator", "icon": "true", "title": "Pipeline"});
pipe.documentElement.appendChild(visualize);
exportPipe('export.pipeline', 'text/xml', pipe);
}
function exportPipe(filename, mimeType, pipe) {
// A 'hacky' way to download XML
pipeAsStr = (new XMLSerializer()).serializeToString(pipe);
var link = document.createElement('a');
mimeType = mimeType || 'text/plain';
link.setAttribute('download', filename);
link.setAttribute('href', 'data:' + mimeType + ';charset=utf-8,' + encodeURIComponent(pipeAsStr));
link.click();
}
function Get(yourUrl){
var Httpreq = new XMLHttpRequest(); // a new request
Httpreq.open("GET",yourUrl,false);
Httpreq.send(null);
return Httpreq.responseText;
}
function getJsonMap() {
return JSON.parse(Get("https://raw.githubusercontent.com/avaid96/mudcat/master/map.json"));
}