annotate js/specification.js @ 2562:9c167c0d6c26

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