nicholas@2224
|
1 function Specification() {
|
nicholas@2224
|
2 // Handles the decoding of the project specification XML into a simple JavaScript Object.
|
nicholas@2224
|
3
|
nicholas@2224
|
4 this.interface = null;
|
nicholas@2224
|
5 this.projectReturn = "null";
|
nicholas@2224
|
6 this.randomiseOrder = null;
|
nicholas@2224
|
7 this.testPages = null;
|
nicholas@2224
|
8 this.pages = [];
|
nicholas@2224
|
9 this.metrics = null;
|
nicholas@2224
|
10 this.interfaces = null;
|
nicholas@2224
|
11 this.loudness = null;
|
nicholas@2224
|
12 this.errors = [];
|
nicholas@2224
|
13 this.schema = null;
|
nicholas@2224
|
14 this.exitText = "Thank you.";
|
nicholas@2224
|
15
|
nicholas@2224
|
16 this.processAttribute = function(attribute,schema,schemaRoot)
|
nicholas@2224
|
17 {
|
nicholas@2224
|
18 // attribute is the string returned from getAttribute on the XML
|
nicholas@2224
|
19 // schema is the <xs:attribute> node
|
nicholas@2224
|
20 if (schema.getAttribute('name') == undefined && schema.getAttribute('ref') != undefined)
|
nicholas@2224
|
21 {
|
nicholas@2224
|
22 schema = schemaRoot.getAllElementsByName(schema.getAttribute('ref'))[0];
|
nicholas@2224
|
23 }
|
nicholas@2224
|
24 var defaultOpt = schema.getAttribute('default');
|
nicholas@2224
|
25 if (attribute == null) {
|
nicholas@2224
|
26 attribute = defaultOpt;
|
nicholas@2224
|
27 }
|
nicholas@2224
|
28 var dataType = schema.getAttribute('type');
|
nicholas@2224
|
29 if (typeof dataType == "string") { dataType = dataType.substr(3);}
|
nicholas@2224
|
30 else {
|
nicholas@2224
|
31 var rest = schema.getAllElementsByTagName("xs:restriction").concat(schema.getAllElementsByTagName("xs:enumeration"));
|
nicholas@2224
|
32 if (rest.length > 0) {
|
nicholas@2224
|
33 dataType = rest[0].getAttribute("base");
|
nicholas@2224
|
34 if (typeof dataType == "string") {
|
nicholas@2224
|
35 dataType = dataType.substr(3);
|
nicholas@2224
|
36 } else {
|
nicholas@2224
|
37 dataType = "string";
|
nicholas@2224
|
38 }
|
nicholas@2224
|
39 } else {
|
nicholas@2224
|
40 dataType = "string";
|
nicholas@2224
|
41 }
|
nicholas@2224
|
42 }
|
nicholas@2224
|
43 if (attribute == null)
|
nicholas@2224
|
44 {
|
nicholas@2224
|
45 return attribute;
|
nicholas@2224
|
46 }
|
nicholas@2224
|
47 switch(dataType)
|
nicholas@2224
|
48 {
|
nicholas@2224
|
49 case "boolean":
|
nicholas@2224
|
50 if (attribute == 'true'){attribute = true;}else{attribute=false;}
|
nicholas@2224
|
51 break;
|
nicholas@2224
|
52 case "negativeInteger":
|
nicholas@2224
|
53 case "positiveInteger":
|
nicholas@2224
|
54 case "nonNegativeInteger":
|
nicholas@2224
|
55 case "nonPositiveInteger":
|
nicholas@2224
|
56 case "integer":
|
nicholas@2224
|
57 case "decimal":
|
nicholas@2224
|
58 case "short":
|
nicholas@2224
|
59 attribute = Number(attribute);
|
nicholas@2224
|
60 break;
|
nicholas@2224
|
61 case "string":
|
nicholas@2224
|
62 default:
|
nicholas@2224
|
63 attribute = String(attribute);
|
nicholas@2224
|
64 break;
|
nicholas@2224
|
65 }
|
nicholas@2224
|
66 return attribute;
|
nicholas@2224
|
67 };
|
nicholas@2224
|
68
|
nicholas@2224
|
69 this.decode = function(projectXML) {
|
nicholas@2224
|
70 this.errors = [];
|
nicholas@2224
|
71 // projectXML - DOM Parsed document
|
nicholas@2224
|
72 this.projectXML = projectXML.childNodes[0];
|
nicholas@2224
|
73 var setupNode = projectXML.getElementsByTagName('setup')[0];
|
nicholas@2224
|
74 var schemaSetup = this.schema.getAllElementsByName('setup')[0];
|
nicholas@2224
|
75 // First decode the attributes
|
nicholas@2224
|
76 var attributes = schemaSetup.getAllElementsByTagName('xs:attribute');
|
nicholas@2224
|
77 for (var i in attributes)
|
nicholas@2224
|
78 {
|
nicholas@2224
|
79 if (isNaN(Number(i)) == true){break;}
|
nicholas@2224
|
80 var attributeName = attributes[i].getAttribute('name') || attributes[i].getAttribute('ref');
|
nicholas@2224
|
81 var projectAttr = setupNode.getAttribute(attributeName);
|
nicholas@2224
|
82 projectAttr = this.processAttribute(projectAttr,attributes[i],this.schema);
|
nicholas@2224
|
83 switch(typeof projectAttr)
|
nicholas@2224
|
84 {
|
nicholas@2224
|
85 case "number":
|
nicholas@2224
|
86 case "boolean":
|
nicholas@2224
|
87 eval('this.'+attributeName+' = '+projectAttr);
|
nicholas@2224
|
88 break;
|
nicholas@2224
|
89 case "string":
|
nicholas@2224
|
90 eval('this.'+attributeName+' = "'+projectAttr+'"');
|
nicholas@2224
|
91 break;
|
nicholas@2224
|
92 }
|
nicholas@2224
|
93
|
nicholas@2224
|
94 }
|
nicholas@2224
|
95
|
nicholas@2224
|
96 var exitTextNode = setupNode.getElementsByTagName('exitText');
|
nicholas@2224
|
97 if (exitTextNode.length == 1) {
|
nicholas@2224
|
98 this.exitText = exitTextNode[0].textContent;
|
nicholas@2224
|
99 }
|
nicholas@2224
|
100
|
nicholas@2224
|
101 this.metrics = new this.metricNode();
|
nicholas@2224
|
102
|
nicholas@2224
|
103 this.metrics.decode(this,setupNode.getElementsByTagName('metric')[0]);
|
nicholas@2224
|
104
|
nicholas@2224
|
105 // Now process the survey node options
|
nicholas@2224
|
106 var survey = setupNode.getElementsByTagName('survey');
|
nicholas@2224
|
107 for (var i in survey) {
|
nicholas@2224
|
108 if (isNaN(Number(i)) == true){break;}
|
nicholas@2224
|
109 var location = survey[i].getAttribute('location');
|
nicholas@2257
|
110 switch(location)
|
nicholas@2257
|
111 {
|
nicholas@2257
|
112 case 'pre':
|
nicholas@2257
|
113 case 'before':
|
nicholas@2257
|
114 if (this.preTest != null){console.log("Already a pre/before test survey defined! Ignoring second!!");}
|
nicholas@2257
|
115 else {
|
nicholas@2257
|
116 this.preTest = new this.surveyNode(this);
|
nicholas@2257
|
117 this.preTest.decode(this,survey[i]);
|
nicholas@2257
|
118 }
|
nicholas@2257
|
119 break;
|
nicholas@2257
|
120 case 'post':
|
nicholas@2257
|
121 case 'after':
|
nicholas@2257
|
122 if (this.postTest != null){console.log("Already a post/after test survey defined! Ignoring second!!");}
|
nicholas@2257
|
123 else {
|
nicholas@2257
|
124 this.postTest = new this.surveyNode(this);
|
nicholas@2257
|
125 this.postTest.decode(this,survey[i]);
|
nicholas@2257
|
126 }
|
nicholas@2257
|
127 break;
|
nicholas@2257
|
128 }
|
nicholas@2224
|
129 }
|
nicholas@2224
|
130
|
nicholas@2224
|
131 var interfaceNode = setupNode.getElementsByTagName('interface');
|
nicholas@2224
|
132 if (interfaceNode.length > 1)
|
nicholas@2224
|
133 {
|
nicholas@2224
|
134 this.errors.push("Only one <interface> node in the <setup> node allowed! Others except first ingnored!");
|
nicholas@2224
|
135 }
|
nicholas@2224
|
136 this.interfaces = new this.interfaceNode(this);
|
nicholas@2224
|
137 if (interfaceNode.length != 0)
|
nicholas@2224
|
138 {
|
nicholas@2224
|
139 interfaceNode = interfaceNode[0];
|
nicholas@2224
|
140 this.interfaces.decode(this,interfaceNode,this.schema.getAllElementsByName('interface')[1]);
|
nicholas@2224
|
141 }
|
nicholas@2224
|
142
|
nicholas@2224
|
143 // Page tags
|
nicholas@2224
|
144 var pageTags = projectXML.getElementsByTagName('page');
|
nicholas@2224
|
145 var pageSchema = this.schema.getAllElementsByName('page')[0];
|
nicholas@2224
|
146 for (var i=0; i<pageTags.length; i++)
|
nicholas@2224
|
147 {
|
nicholas@2224
|
148 var node = new this.page(this);
|
nicholas@2224
|
149 node.decode(this,pageTags[i],pageSchema);
|
nicholas@2224
|
150 this.pages.push(node);
|
nicholas@2224
|
151 }
|
nicholas@2224
|
152 };
|
nicholas@2224
|
153
|
nicholas@2224
|
154 this.encode = function()
|
nicholas@2224
|
155 {
|
nicholas@2224
|
156 var RootDocument = document.implementation.createDocument(null,"waet");
|
nicholas@2224
|
157 var root = RootDocument.children[0];
|
nicholas@2224
|
158 root.setAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
|
nicholas@2224
|
159 root.setAttribute("xsi:noNamespaceSchemaLocation","test-schema.xsd");
|
nicholas@2224
|
160 // Build setup node
|
nicholas@2224
|
161 var setup = RootDocument.createElement("setup");
|
nicholas@2224
|
162 var schemaSetup = this.schema.getAllElementsByName('setup')[0];
|
nicholas@2224
|
163 // First decode the attributes
|
nicholas@2224
|
164 var attributes = schemaSetup.getAllElementsByTagName('xs:attribute');
|
nicholas@2224
|
165 for (var i=0; i<attributes.length; i++)
|
nicholas@2224
|
166 {
|
nicholas@2224
|
167 var name = attributes[i].getAttribute("name");
|
nicholas@2224
|
168 if (name == undefined) {
|
nicholas@2224
|
169 name = attributes[i].getAttribute("ref");
|
nicholas@2224
|
170 }
|
nicholas@2224
|
171 if(eval("this."+name+" != undefined") || attributes[i].getAttribute("use") == "required")
|
nicholas@2224
|
172 {
|
nicholas@2224
|
173 eval("setup.setAttribute('"+name+"',this."+name+")");
|
nicholas@2224
|
174 }
|
nicholas@2224
|
175 }
|
nicholas@2224
|
176 root.appendChild(setup);
|
nicholas@2224
|
177 // Survey node
|
nicholas@2224
|
178 if (this.exitText != null) {
|
nicholas@2224
|
179 var exitTextNode = RootDocument.createElement('exitText');
|
nicholas@2224
|
180 exitTextNode.textContent = this.exitText;
|
nicholas@2224
|
181 setup.appendChild(exitTextNode);
|
nicholas@2224
|
182 }
|
nicholas@2224
|
183 setup.appendChild(this.preTest.encode(RootDocument));
|
nicholas@2224
|
184 setup.appendChild(this.postTest.encode(RootDocument));
|
nicholas@2224
|
185 setup.appendChild(this.metrics.encode(RootDocument));
|
nicholas@2224
|
186 setup.appendChild(this.interfaces.encode(RootDocument));
|
nicholas@2224
|
187 for (var page of this.pages)
|
nicholas@2224
|
188 {
|
nicholas@2224
|
189 root.appendChild(page.encode(RootDocument));
|
nicholas@2224
|
190 }
|
nicholas@2224
|
191 return RootDocument;
|
nicholas@2224
|
192 };
|
nicholas@2224
|
193
|
nicholas@2224
|
194 this.surveyNode = function(specification) {
|
nicholas@2224
|
195 this.location = null;
|
nicholas@2224
|
196 this.options = [];
|
nicholas@2224
|
197 this.parent = null;
|
nicholas@2224
|
198 this.schema = specification.schema.getAllElementsByName('survey')[0];
|
nicholas@2224
|
199 this.specification = specification;
|
nicholas@2224
|
200
|
nicholas@2224
|
201 this.OptionNode = function(specification) {
|
nicholas@2224
|
202 this.type = undefined;
|
nicholas@2224
|
203 this.schema = specification.schema.getAllElementsByName('surveyentry')[0];
|
nicholas@2224
|
204 this.id = undefined;
|
nicholas@2224
|
205 this.name = undefined;
|
nicholas@2224
|
206 this.mandatory = undefined;
|
nicholas@2224
|
207 this.statement = undefined;
|
nicholas@2224
|
208 this.boxsize = undefined;
|
nicholas@2224
|
209 this.options = [];
|
nicholas@2224
|
210 this.min = undefined;
|
nicholas@2224
|
211 this.max = undefined;
|
nicholas@2224
|
212 this.step = undefined;
|
nicholas@2224
|
213
|
nicholas@2224
|
214 this.decode = function(parent,child)
|
nicholas@2224
|
215 {
|
nicholas@2224
|
216 var attributeMap = this.schema.getAllElementsByTagName('xs:attribute');
|
nicholas@2224
|
217 for (var i in attributeMap){
|
nicholas@2224
|
218 if(isNaN(Number(i)) == true){break;}
|
nicholas@2224
|
219 var attributeName = attributeMap[i].getAttribute('name') || attributeMap[i].getAttribute('ref');
|
nicholas@2224
|
220 var projectAttr = child.getAttribute(attributeName);
|
nicholas@2224
|
221 projectAttr = parent.processAttribute(projectAttr,attributeMap[i],parent.schema);
|
nicholas@2224
|
222 switch(typeof projectAttr)
|
nicholas@2224
|
223 {
|
nicholas@2224
|
224 case "number":
|
nicholas@2224
|
225 case "boolean":
|
nicholas@2224
|
226 eval('this.'+attributeName+' = '+projectAttr);
|
nicholas@2224
|
227 break;
|
nicholas@2224
|
228 case "string":
|
nicholas@2224
|
229 eval('this.'+attributeName+' = "'+projectAttr+'"');
|
nicholas@2224
|
230 break;
|
nicholas@2224
|
231 }
|
nicholas@2224
|
232 }
|
nicholas@2224
|
233 this.statement = child.getElementsByTagName('statement')[0].textContent;
|
nicholas@2224
|
234 if (this.type == "checkbox" || this.type == "radio") {
|
nicholas@2224
|
235 var children = child.getElementsByTagName('option');
|
nicholas@2224
|
236 if (children.length == null) {
|
nicholas@2224
|
237 console.log('Malformed' +child.nodeName+ 'entry');
|
nicholas@2224
|
238 this.statement = 'Malformed' +child.nodeName+ 'entry';
|
nicholas@2224
|
239 this.type = 'statement';
|
nicholas@2224
|
240 } else {
|
nicholas@2224
|
241 this.options = [];
|
nicholas@2224
|
242 for (var i in children)
|
nicholas@2224
|
243 {
|
nicholas@2224
|
244 if (isNaN(Number(i))==true){break;}
|
nicholas@2224
|
245 this.options.push({
|
nicholas@2224
|
246 name: children[i].getAttribute('name'),
|
nicholas@2224
|
247 text: children[i].textContent
|
nicholas@2224
|
248 });
|
nicholas@2224
|
249 }
|
nicholas@2224
|
250 }
|
nicholas@2224
|
251 }
|
nicholas@2224
|
252 };
|
nicholas@2224
|
253
|
nicholas@2224
|
254 this.exportXML = function(doc)
|
nicholas@2224
|
255 {
|
nicholas@2224
|
256 var node = doc.createElement('surveyentry');
|
nicholas@2224
|
257 node.setAttribute('type',this.type);
|
nicholas@2224
|
258 var statement = doc.createElement('statement');
|
nicholas@2224
|
259 statement.textContent = this.statement;
|
nicholas@2224
|
260 node.appendChild(statement);
|
nicholas@2224
|
261 node.id = this.id;
|
nicholas@2224
|
262 if (this.name != undefined) { node.setAttribute("name",this.name);}
|
nicholas@2224
|
263 if (this.mandatory != undefined) { node.setAttribute("mandatory",this.mandatory);}
|
nicholas@2224
|
264 node.id = this.id;
|
nicholas@2224
|
265 if (this.name != undefined) {node.setAttribute("name",this.name);}
|
nicholas@2224
|
266 switch(this.type)
|
nicholas@2224
|
267 {
|
nicholas@2224
|
268 case "checkbox":
|
nicholas@2224
|
269 case "radio":
|
nicholas@2224
|
270 for (var i=0; i<this.options.length; i++)
|
nicholas@2224
|
271 {
|
nicholas@2224
|
272 var option = this.options[i];
|
nicholas@2224
|
273 var optionNode = doc.createElement("option");
|
nicholas@2224
|
274 optionNode.setAttribute("name",option.name);
|
nicholas@2224
|
275 optionNode.textContent = option.text;
|
nicholas@2224
|
276 node.appendChild(optionNode);
|
nicholas@2224
|
277 }
|
nicholas@2224
|
278 case "number":
|
nicholas@2224
|
279 if (this.min != undefined) {node.setAttribute("min", this.min);}
|
nicholas@2224
|
280 if (this.max != undefined) {node.setAttribute("max", this.max);}
|
nicholas@2224
|
281 case "question":
|
nicholas@2224
|
282 if (this.boxsize != undefined) {node.setAttribute("boxsize",this.boxsize);}
|
nicholas@2224
|
283 if (this.mandatory != undefined) {node.setAttribute("mandatory",this.mandatory);}
|
nicholas@2224
|
284 default:
|
nicholas@2224
|
285 break;
|
nicholas@2224
|
286 }
|
nicholas@2224
|
287 return node;
|
nicholas@2224
|
288 };
|
nicholas@2224
|
289 };
|
nicholas@2224
|
290 this.decode = function(parent,xml) {
|
nicholas@2224
|
291 this.parent = parent;
|
nicholas@2224
|
292 this.location = xml.getAttribute('location');
|
nicholas@2224
|
293 if (this.location == 'before'){this.location = 'pre';}
|
nicholas@2224
|
294 else if (this.location == 'after'){this.location = 'post';}
|
nicholas@2260
|
295 var children = xml.getAllElementsByTagName('surveyentry');
|
nicholas@2260
|
296 for (var i=0; i<children.length; i++)
|
nicholas@2224
|
297 {
|
nicholas@2224
|
298 var node = new this.OptionNode(this.specification);
|
nicholas@2260
|
299 node.decode(parent,children[i]);
|
nicholas@2224
|
300 this.options.push(node);
|
nicholas@2224
|
301 }
|
nicholas@2257
|
302 if (this.options.length == 0) {
|
nicholas@2257
|
303 console.log("Empty survey node");
|
nicholas@2257
|
304 console.log(this);
|
nicholas@2257
|
305 }
|
nicholas@2224
|
306 };
|
nicholas@2224
|
307 this.encode = function(doc) {
|
nicholas@2224
|
308 var node = doc.createElement('survey');
|
nicholas@2224
|
309 node.setAttribute('location',this.location);
|
nicholas@2224
|
310 for (var i=0; i<this.options.length; i++)
|
nicholas@2224
|
311 {
|
nicholas@2224
|
312 node.appendChild(this.options[i].exportXML(doc));
|
nicholas@2224
|
313 }
|
nicholas@2224
|
314 return node;
|
nicholas@2224
|
315 };
|
nicholas@2224
|
316 };
|
nicholas@2224
|
317
|
nicholas@2224
|
318 this.interfaceNode = function(specification)
|
nicholas@2224
|
319 {
|
nicholas@2224
|
320 this.title = null;
|
nicholas@2224
|
321 this.name = null;
|
nicholas@2224
|
322 this.options = [];
|
nicholas@2224
|
323 this.scales = [];
|
nicholas@2224
|
324 this.schema = specification.schema.getAllElementsByName('interface')[1];
|
nicholas@2224
|
325
|
nicholas@2224
|
326 this.decode = function(parent,xml) {
|
nicholas@2224
|
327 this.name = xml.getAttribute('name');
|
nicholas@2224
|
328 var titleNode = xml.getElementsByTagName('title');
|
nicholas@2224
|
329 if (titleNode.length == 1)
|
nicholas@2224
|
330 {
|
nicholas@2224
|
331 this.title = titleNode[0].textContent;
|
nicholas@2224
|
332 }
|
nicholas@2224
|
333 var interfaceOptionNodes = xml.getElementsByTagName('interfaceoption');
|
nicholas@2224
|
334 // Extract interfaceoption node schema
|
nicholas@2224
|
335 var interfaceOptionNodeSchema = this.schema.getAllElementsByName('interfaceoption')[0];
|
nicholas@2224
|
336 var attributeMap = interfaceOptionNodeSchema.getAllElementsByTagName('xs:attribute');
|
nicholas@2224
|
337 for (var i=0; i<interfaceOptionNodes.length; i++)
|
nicholas@2224
|
338 {
|
nicholas@2224
|
339 var ioNode = interfaceOptionNodes[i];
|
nicholas@2224
|
340 var option = {};
|
nicholas@2224
|
341 for (var j=0; j<attributeMap.length; j++)
|
nicholas@2224
|
342 {
|
nicholas@2224
|
343 var attributeName = attributeMap[j].getAttribute('name') || attributeMap[j].getAttribute('ref');
|
nicholas@2224
|
344 var projectAttr = ioNode.getAttribute(attributeName);
|
nicholas@2224
|
345 if(parent.processAttribute) {
|
nicholas@2224
|
346 parent.processAttribute(projectAttr, attributeMap[j], parent.schema)
|
nicholas@2224
|
347 } else {
|
nicholas@2224
|
348 parent.parent.processAttribute(projectAttr, attributeMap[j], parent.parent.schema)
|
nicholas@2224
|
349 }
|
nicholas@2224
|
350 switch(typeof projectAttr)
|
nicholas@2224
|
351 {
|
nicholas@2224
|
352 case "number":
|
nicholas@2224
|
353 case "boolean":
|
nicholas@2224
|
354 eval('option.'+attributeName+' = '+projectAttr);
|
nicholas@2224
|
355 break;
|
nicholas@2224
|
356 case "string":
|
nicholas@2224
|
357 eval('option.'+attributeName+' = "'+projectAttr+'"');
|
nicholas@2224
|
358 break;
|
nicholas@2224
|
359 }
|
nicholas@2224
|
360 }
|
nicholas@2224
|
361 this.options.push(option);
|
nicholas@2224
|
362 }
|
nicholas@2224
|
363
|
nicholas@2224
|
364 // Now the scales nodes
|
nicholas@2224
|
365 var scaleParent = xml.getElementsByTagName('scales');
|
nicholas@2224
|
366 if (scaleParent.length == 1) {
|
nicholas@2224
|
367 scaleParent = scaleParent[0];
|
nicholas@2252
|
368 var scalelabels = scaleParent.getAllElementsByTagName('scalelabel');
|
nicholas@2252
|
369 for (var i=0; i<scalelabels.length; i++) {
|
nicholas@2224
|
370 this.scales.push({
|
nicholas@2252
|
371 text: scalelabels[i].textContent,
|
nicholas@2252
|
372 position: Number(scalelabels[i].getAttribute('position'))
|
nicholas@2224
|
373 });
|
nicholas@2224
|
374 }
|
nicholas@2224
|
375 }
|
nicholas@2224
|
376 };
|
nicholas@2224
|
377
|
nicholas@2224
|
378 this.encode = function(doc) {
|
nicholas@2224
|
379 var node = doc.createElement("interface");
|
nicholas@2224
|
380 if (typeof name == "string")
|
nicholas@2224
|
381 node.setAttribute("name",this.name);
|
nicholas@2243
|
382 if (typeof this.title == "string") {
|
nicholas@2243
|
383 var titleNode = doc.createElement("title");
|
nicholas@2243
|
384 titleNode.textContent = this.title;
|
nicholas@2243
|
385 node.appendChild(titleNode);
|
nicholas@2243
|
386 }
|
nicholas@2224
|
387 for (var option of this.options)
|
nicholas@2224
|
388 {
|
nicholas@2224
|
389 var child = doc.createElement("interfaceoption");
|
nicholas@2224
|
390 child.setAttribute("type",option.type);
|
nicholas@2224
|
391 child.setAttribute("name",option.name);
|
nicholas@2224
|
392 node.appendChild(child);
|
nicholas@2224
|
393 }
|
nicholas@2224
|
394 if (this.scales.length != 0) {
|
nicholas@2224
|
395 var scales = doc.createElement("scales");
|
nicholas@2224
|
396 for (var scale of this.scales)
|
nicholas@2224
|
397 {
|
nicholas@2224
|
398 var child = doc.createElement("scalelabel");
|
nicholas@2224
|
399 child.setAttribute("position",scale.position);
|
nicholas@2224
|
400 child.textContent = scale.text;
|
nicholas@2224
|
401 scales.appendChild(child);
|
nicholas@2224
|
402 }
|
nicholas@2224
|
403 node.appendChild(scales);
|
nicholas@2224
|
404 }
|
nicholas@2224
|
405 return node;
|
nicholas@2224
|
406 };
|
nicholas@2224
|
407 };
|
nicholas@2224
|
408
|
nicholas@2224
|
409 this.metricNode = function() {
|
nicholas@2224
|
410 this.enabled = [];
|
nicholas@2224
|
411 this.decode = function(parent, xml) {
|
nicholas@2224
|
412 var children = xml.getElementsByTagName('metricenable');
|
nicholas@2224
|
413 for (var i in children) {
|
nicholas@2224
|
414 if (isNaN(Number(i)) == true){break;}
|
nicholas@2224
|
415 this.enabled.push(children[i].textContent);
|
nicholas@2224
|
416 }
|
nicholas@2224
|
417 }
|
nicholas@2224
|
418 this.encode = function(doc) {
|
nicholas@2224
|
419 var node = doc.createElement('metric');
|
nicholas@2224
|
420 for (var i in this.enabled)
|
nicholas@2224
|
421 {
|
nicholas@2224
|
422 if (isNaN(Number(i)) == true){break;}
|
nicholas@2224
|
423 var child = doc.createElement('metricenable');
|
nicholas@2224
|
424 child.textContent = this.enabled[i];
|
nicholas@2224
|
425 node.appendChild(child);
|
nicholas@2224
|
426 }
|
nicholas@2224
|
427 return node;
|
nicholas@2224
|
428 }
|
nicholas@2224
|
429 }
|
nicholas@2224
|
430
|
nicholas@2224
|
431 this.page = function(specification) {
|
nicholas@2224
|
432 this.presentedId = undefined;
|
nicholas@2224
|
433 this.id = undefined;
|
nicholas@2224
|
434 this.hostURL = undefined;
|
nicholas@2224
|
435 this.randomiseOrder = undefined;
|
nicholas@2224
|
436 this.loop = undefined;
|
nicholas@2224
|
437 this.showElementComments = undefined;
|
nicholas@2224
|
438 this.outsideReference = null;
|
nicholas@2224
|
439 this.loudness = null;
|
nicholas@2224
|
440 this.label = null;
|
nicholas@2224
|
441 this.preTest = null;
|
nicholas@2224
|
442 this.postTest = null;
|
nicholas@2224
|
443 this.interfaces = [];
|
nicholas@2224
|
444 this.commentBoxPrefix = "Comment on track";
|
nicholas@2224
|
445 this.audioElements = [];
|
nicholas@2224
|
446 this.commentQuestions = [];
|
nicholas@2224
|
447 this.schema = specification.schema.getAllElementsByName("page")[0];
|
nicholas@2224
|
448 this.specification = specification;
|
nicholas@2224
|
449 this.parent = null;
|
nicholas@2224
|
450
|
nicholas@2224
|
451 this.decode = function(parent,xml)
|
nicholas@2224
|
452 {
|
nicholas@2224
|
453 this.parent = parent;
|
nicholas@2224
|
454 var attributeMap = this.schema.getAllElementsByTagName('xs:attribute');
|
nicholas@2224
|
455 for (var i=0; i<attributeMap.length; i++)
|
nicholas@2224
|
456 {
|
nicholas@2224
|
457 var attributeName = attributeMap[i].getAttribute('name') || attributeMap[i].getAttribute('ref');
|
nicholas@2224
|
458 var projectAttr = xml.getAttribute(attributeName);
|
nicholas@2224
|
459 projectAttr = parent.processAttribute(projectAttr,attributeMap[i],parent.schema);
|
nicholas@2224
|
460 switch(typeof projectAttr)
|
nicholas@2224
|
461 {
|
nicholas@2224
|
462 case "number":
|
nicholas@2224
|
463 case "boolean":
|
nicholas@2224
|
464 eval('this.'+attributeName+' = '+projectAttr);
|
nicholas@2224
|
465 break;
|
nicholas@2224
|
466 case "string":
|
nicholas@2224
|
467 eval('this.'+attributeName+' = "'+projectAttr+'"');
|
nicholas@2224
|
468 break;
|
nicholas@2224
|
469 }
|
nicholas@2224
|
470 }
|
nicholas@2224
|
471
|
nicholas@2224
|
472 // Get the Comment Box Prefix
|
nicholas@2224
|
473 var CBP = xml.getElementsByTagName('commentboxprefix');
|
nicholas@2224
|
474 if (CBP.length != 0) {
|
nicholas@2224
|
475 this.commentBoxPrefix = CBP[0].textContent;
|
nicholas@2224
|
476 }
|
nicholas@2224
|
477
|
nicholas@2224
|
478 // Now decode the interfaces
|
nicholas@2224
|
479 var interfaceNode = xml.getElementsByTagName('interface');
|
nicholas@2224
|
480 for (var i=0; i<interfaceNode.length; i++)
|
nicholas@2224
|
481 {
|
nicholas@2224
|
482 var node = new parent.interfaceNode(this.specification);
|
nicholas@2224
|
483 node.decode(this,interfaceNode[i],parent.schema.getAllElementsByName('interface')[1]);
|
nicholas@2224
|
484 this.interfaces.push(node);
|
nicholas@2224
|
485 }
|
nicholas@2224
|
486
|
nicholas@2224
|
487 // Now process the survey node options
|
nicholas@2224
|
488 var survey = xml.getElementsByTagName('survey');
|
nicholas@2224
|
489 var surveySchema = parent.schema.getAllElementsByName('survey')[0];
|
nicholas@2224
|
490 for (var i in survey) {
|
nicholas@2224
|
491 if (isNaN(Number(i)) == true){break;}
|
nicholas@2224
|
492 var location = survey[i].getAttribute('location');
|
nicholas@2224
|
493 if (location == 'pre' || location == 'before')
|
nicholas@2224
|
494 {
|
nicholas@2224
|
495 if (this.preTest != null){this.errors.push("Already a pre/before test survey defined! Ignoring second!!");}
|
nicholas@2224
|
496 else {
|
nicholas@2224
|
497 this.preTest = new parent.surveyNode(this.specification);
|
nicholas@2224
|
498 this.preTest.decode(parent,survey[i],surveySchema);
|
nicholas@2224
|
499 }
|
nicholas@2224
|
500 } else if (location == 'post' || location == 'after') {
|
nicholas@2224
|
501 if (this.postTest != null){this.errors.push("Already a post/after test survey defined! Ignoring second!!");}
|
nicholas@2224
|
502 else {
|
nicholas@2224
|
503 this.postTest = new parent.surveyNode(this.specification);
|
nicholas@2224
|
504 this.postTest.decode(parent,survey[i],surveySchema);
|
nicholas@2224
|
505 }
|
nicholas@2224
|
506 }
|
nicholas@2224
|
507 }
|
nicholas@2224
|
508
|
nicholas@2224
|
509 // Now process the audioelement tags
|
nicholas@2224
|
510 var audioElements = xml.getElementsByTagName('audioelement');
|
nicholas@2224
|
511 for (var i=0; i<audioElements.length; i++)
|
nicholas@2224
|
512 {
|
nicholas@2224
|
513 var node = new this.audioElementNode(this.specification);
|
nicholas@2224
|
514 node.decode(this,audioElements[i]);
|
nicholas@2224
|
515 this.audioElements.push(node);
|
nicholas@2224
|
516 }
|
nicholas@2224
|
517
|
nicholas@2224
|
518 // Now decode the commentquestions
|
nicholas@2224
|
519 var commentQuestions = xml.getElementsByTagName('commentquestion');
|
nicholas@2224
|
520 for (var i=0; i<commentQuestions.length; i++)
|
nicholas@2224
|
521 {
|
nicholas@2224
|
522 var node = new this.commentQuestionNode(this.specification);
|
nicholas@2224
|
523 node.decode(parent,commentQuestions[i]);
|
nicholas@2224
|
524 this.commentQuestions.push(node);
|
nicholas@2224
|
525 }
|
nicholas@2224
|
526 };
|
nicholas@2224
|
527
|
nicholas@2224
|
528 this.encode = function(root)
|
nicholas@2224
|
529 {
|
nicholas@2224
|
530 var AHNode = root.createElement("page");
|
nicholas@2224
|
531 // First decode the attributes
|
nicholas@2224
|
532 var attributes = this.schema.getAllElementsByTagName('xs:attribute');
|
nicholas@2224
|
533 for (var i=0; i<attributes.length; i++)
|
nicholas@2224
|
534 {
|
nicholas@2224
|
535 var name = attributes[i].getAttribute("name");
|
nicholas@2224
|
536 if (name == undefined) {
|
nicholas@2224
|
537 name = attributes[i].getAttribute("ref");
|
nicholas@2224
|
538 }
|
nicholas@2224
|
539 if(eval("this."+name+" != undefined") || attributes[i].getAttribute("use") == "required")
|
nicholas@2224
|
540 {
|
nicholas@2224
|
541 eval("AHNode.setAttribute('"+name+"',this."+name+")");
|
nicholas@2224
|
542 }
|
nicholas@2224
|
543 }
|
nicholas@2224
|
544 if(this.loudness != null) {AHNode.setAttribute("loudness",this.loudness);}
|
nicholas@2224
|
545 // <commentboxprefix>
|
nicholas@2224
|
546 var commentboxprefix = root.createElement("commentboxprefix");
|
nicholas@2224
|
547 commentboxprefix.textContent = this.commentBoxPrefix;
|
nicholas@2224
|
548 AHNode.appendChild(commentboxprefix);
|
nicholas@2224
|
549
|
nicholas@2224
|
550 for (var i=0; i<this.interfaces.length; i++)
|
nicholas@2224
|
551 {
|
nicholas@2224
|
552 AHNode.appendChild(this.interfaces[i].encode(root));
|
nicholas@2224
|
553 }
|
nicholas@2224
|
554
|
nicholas@2224
|
555 for (var i=0; i<this.audioElements.length; i++) {
|
nicholas@2224
|
556 AHNode.appendChild(this.audioElements[i].encode(root));
|
nicholas@2224
|
557 }
|
nicholas@2224
|
558 // Create <CommentQuestion>
|
nicholas@2224
|
559 for (var i=0; i<this.commentQuestions.length; i++)
|
nicholas@2224
|
560 {
|
nicholas@2224
|
561 AHNode.appendChild(this.commentQuestions[i].encode(root));
|
nicholas@2224
|
562 }
|
nicholas@2224
|
563
|
nicholas@2224
|
564 AHNode.appendChild(this.preTest.encode(root));
|
nicholas@2224
|
565 AHNode.appendChild(this.postTest.encode(root));
|
nicholas@2224
|
566 return AHNode;
|
nicholas@2224
|
567 };
|
nicholas@2224
|
568
|
nicholas@2224
|
569 this.commentQuestionNode = function(specification) {
|
nicholas@2224
|
570 this.id = null;
|
nicholas@2224
|
571 this.name = undefined;
|
nicholas@2224
|
572 this.type = undefined;
|
nicholas@2224
|
573 this.options = [];
|
nicholas@2224
|
574 this.statement = undefined;
|
nicholas@2224
|
575 this.schema = specification.schema.getAllElementsByName('commentquestion')[0];
|
nicholas@2224
|
576 this.decode = function(parent,xml)
|
nicholas@2224
|
577 {
|
nicholas@2224
|
578 this.id = xml.id;
|
nicholas@2224
|
579 this.name = xml.getAttribute('name');
|
nicholas@2224
|
580 this.type = xml.getAttribute('type');
|
nicholas@2224
|
581 this.statement = xml.getElementsByTagName('statement')[0].textContent;
|
nicholas@2224
|
582 var optNodes = xml.getElementsByTagName('option');
|
nicholas@2224
|
583 for (var i=0; i<optNodes.length; i++)
|
nicholas@2224
|
584 {
|
nicholas@2224
|
585 var optNode = optNodes[i];
|
nicholas@2224
|
586 this.options.push({
|
nicholas@2224
|
587 name: optNode.getAttribute('name'),
|
nicholas@2224
|
588 text: optNode.textContent
|
nicholas@2224
|
589 });
|
nicholas@2224
|
590 }
|
nicholas@2224
|
591 };
|
nicholas@2224
|
592
|
nicholas@2224
|
593 this.encode = function(root)
|
nicholas@2224
|
594 {
|
nicholas@2224
|
595 var node = root.createElement("commentquestion");
|
nicholas@2224
|
596 node.id = this.id;
|
nicholas@2224
|
597 node.setAttribute("type",this.type);
|
nicholas@2224
|
598 if (this.name != undefined){node.setAttribute("name",this.name);}
|
nicholas@2224
|
599 var statement = root.createElement("statement");
|
nicholas@2224
|
600 statement.textContent = this.statement;
|
nicholas@2224
|
601 node.appendChild(statement);
|
nicholas@2224
|
602 for (var option of this.options)
|
nicholas@2224
|
603 {
|
nicholas@2224
|
604 var child = root.createElement("option");
|
nicholas@2224
|
605 child.setAttribute("name",option.name);
|
nicholas@2224
|
606 child.textContent = option.text;
|
nicholas@2224
|
607 node.appendChild(child);
|
nicholas@2224
|
608 }
|
nicholas@2224
|
609 return node;
|
nicholas@2224
|
610 };
|
nicholas@2224
|
611 };
|
nicholas@2224
|
612
|
nicholas@2224
|
613 this.audioElementNode = function(specification) {
|
nicholas@2224
|
614 this.url = null;
|
nicholas@2224
|
615 this.id = null;
|
nicholas@2224
|
616 this.name = null;
|
nicholas@2224
|
617 this.parent = null;
|
nicholas@2224
|
618 this.type = null;
|
nicholas@2224
|
619 this.marker = null;
|
nicholas@2224
|
620 this.enforce = false;
|
nicholas@2224
|
621 this.gain = 0.0;
|
nicholas@2224
|
622 this.schema = specification.schema.getAllElementsByName('audioelement')[0];;
|
nicholas@2224
|
623 this.parent = null;
|
nicholas@2224
|
624 this.decode = function(parent,xml)
|
nicholas@2224
|
625 {
|
nicholas@2224
|
626 this.parent = parent;
|
nicholas@2224
|
627 var attributeMap = this.schema.getAllElementsByTagName('xs:attribute');
|
nicholas@2224
|
628 for (var i=0; i<attributeMap.length; i++)
|
nicholas@2224
|
629 {
|
nicholas@2224
|
630 var attributeName = attributeMap[i].getAttribute('name') || attributeMap[i].getAttribute('ref');
|
nicholas@2224
|
631 var projectAttr = xml.getAttribute(attributeName);
|
nicholas@2224
|
632 projectAttr = parent.parent.processAttribute(projectAttr,attributeMap[i],parent.parent.schema);
|
nicholas@2224
|
633 switch(typeof projectAttr)
|
nicholas@2224
|
634 {
|
nicholas@2224
|
635 case "number":
|
nicholas@2224
|
636 case "boolean":
|
nicholas@2224
|
637 eval('this.'+attributeName+' = '+projectAttr);
|
nicholas@2224
|
638 break;
|
nicholas@2224
|
639 case "string":
|
nicholas@2224
|
640 eval('this.'+attributeName+' = "'+projectAttr+'"');
|
nicholas@2224
|
641 break;
|
nicholas@2224
|
642 }
|
nicholas@2224
|
643 }
|
nicholas@2224
|
644
|
nicholas@2224
|
645 };
|
nicholas@2224
|
646 this.encode = function(root)
|
nicholas@2224
|
647 {
|
nicholas@2224
|
648 var AENode = root.createElement("audioelement");
|
nicholas@2224
|
649 var attributes = this.schema.getAllElementsByTagName('xs:attribute');
|
nicholas@2224
|
650 for (var i=0; i<attributes.length; i++)
|
nicholas@2224
|
651 {
|
nicholas@2224
|
652 var name = attributes[i].getAttribute("name");
|
nicholas@2224
|
653 if (name == undefined) {
|
nicholas@2224
|
654 name = attributes[i].getAttribute("ref");
|
nicholas@2224
|
655 }
|
nicholas@2224
|
656 if(eval("this."+name+" != undefined") || attributes[i].getAttribute("use") == "required")
|
nicholas@2224
|
657 {
|
nicholas@2224
|
658 eval("AENode.setAttribute('"+name+"',this."+name+")");
|
nicholas@2224
|
659 }
|
nicholas@2224
|
660 }
|
nicholas@2224
|
661 return AENode;
|
nicholas@2224
|
662 };
|
nicholas@2224
|
663 };
|
nicholas@2224
|
664 };
|
nicholas@2224
|
665 } |