comparison specification.js @ 2192:ed5a54029157

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