annotate js/specification.js @ 2685:e4e7211842ad

#180. Removed evil evals!
author Nicholas Jillings <nicholas.jillings@mail.bcu.ac.uk>
date Sat, 11 Mar 2017 08:09:57 +0000
parents f9cba2241c35
children a005429f575b
rev   line source
nicholas@2684 1 /* globals document, console */
nicholas@2224 2 function Specification() {
nicholas@2538 3 // Handles the decoding of the project specification XML into a simple JavaScript Object.
nicholas@2538 4
nicholas@2378 5 // <setup> attributes
nicholas@2538 6 this.interface = null;
nicholas@2538 7 this.projectReturn = null;
nicholas@2303 8 this.returnURL = null;
nicholas@2538 9 this.randomiseOrder = null;
nicholas@2538 10 this.poolSize = null;
nicholas@2378 11 this.loudness = null;
nicholas@2378 12 this.sampleRate = null;
nicholas@2378 13 this.calibration = null;
nicholas@2378 14 this.crossFade = null;
nicholas@2378 15 this.preSilence = null;
nicholas@2378 16 this.postSilence = null;
n@2426 17 this.playOne = null;
nicholas@2538 18
nicholas@2378 19 // nodes
nicholas@2378 20 this.metrics = null;
nicholas@2378 21 this.preTest = undefined;
nicholas@2378 22 this.postTest = undefined;
nicholas@2538 23 this.pages = [];
nicholas@2538 24 this.interfaces = null;
nicholas@2538 25 this.errors = [];
nicholas@2538 26 this.schema = null;
nicholas@2224 27 this.exitText = "Thank you.";
nicholas@2538 28
nicholas@2538 29 this.processAttribute = function (attribute, schema, schemaRoot) {
nicholas@2538 30 // attribute is the string returned from getAttribute on the XML
nicholas@2538 31 // schema is the <xs:attribute> node
nicholas@2684 32 if (schema.getAttribute('name') === undefined && schema.getAttribute('ref') !== undefined) {
nicholas@2538 33 schema = schemaRoot.getAllElementsByName(schema.getAttribute('ref'))[0];
nicholas@2538 34 }
nicholas@2538 35 var defaultOpt = schema.getAttribute('default');
nicholas@2684 36 if (attribute === null) {
nicholas@2538 37 attribute = defaultOpt;
nicholas@2538 38 }
nicholas@2538 39 var dataType = schema.getAttribute('type');
nicholas@2538 40 if (typeof dataType == "string") {
nicholas@2538 41 dataType = dataType.substr(3);
nicholas@2538 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@2684 55 if (attribute === null) {
nicholas@2538 56 return attribute;
nicholas@2538 57 }
nicholas@2538 58 switch (dataType) {
nicholas@2538 59 case "boolean":
nicholas@2538 60 if (attribute == 'true') {
nicholas@2538 61 attribute = true;
nicholas@2538 62 } else {
nicholas@2538 63 attribute = false;
nicholas@2538 64 }
nicholas@2538 65 break;
nicholas@2538 66 case "negativeInteger":
nicholas@2538 67 case "positiveInteger":
nicholas@2538 68 case "nonNegativeInteger":
nicholas@2538 69 case "nonPositiveInteger":
nicholas@2538 70 case "integer":
nicholas@2538 71 case "decimal":
nicholas@2538 72 case "short":
nicholas@2538 73 attribute = Number(attribute);
nicholas@2538 74 break;
nicholas@2538 75 case "string":
nicholas@2538 76 default:
nicholas@2538 77 attribute = String(attribute);
nicholas@2538 78 break;
nicholas@2538 79 }
nicholas@2538 80 return attribute;
nicholas@2538 81 };
nicholas@2538 82
nicholas@2538 83 this.decode = function (projectXML) {
nicholas@2538 84 this.errors = [];
nicholas@2538 85 // projectXML - DOM Parsed document
nicholas@2538 86 this.projectXML = projectXML.childNodes[0];
nicholas@2538 87 var setupNode = projectXML.getElementsByTagName('setup')[0];
nicholas@2538 88 var schemaSetup = this.schema.getAllElementsByName('setup')[0];
nicholas@2538 89 // First decode the attributes
nicholas@2538 90 var attributes = schemaSetup.getAllElementsByTagName('xs:attribute');
nicholas@2684 91 var i;
nicholas@2684 92 for (i = 0; i < attributes.length; i++) {
nicholas@2538 93 var attributeName = attributes[i].getAttribute('name') || attributes[i].getAttribute('ref');
nicholas@2538 94 var projectAttr = setupNode.getAttribute(attributeName);
nicholas@2538 95 projectAttr = this.processAttribute(projectAttr, attributes[i], this.schema);
nicholas@2538 96 switch (typeof projectAttr) {
nicholas@2538 97 case "number":
nicholas@2685 98 this[attributeName] = Number(projectAttr);
nicholas@2685 99 break;
nicholas@2538 100 case "boolean":
nicholas@2685 101 this[attributeName] = Boolean(projectAttr);
nicholas@2538 102 break;
nicholas@2538 103 case "string":
nicholas@2685 104 this[attributeName] = String(projectAttr);
nicholas@2538 105 break;
nicholas@2685 106 default:
nicholas@2685 107 throw ("Unkown attribute type " + projectAttr);
nicholas@2538 108 }
nicholas@2538 109
nicholas@2538 110 }
nicholas@2538 111
nicholas@2224 112 var exitTextNode = setupNode.getElementsByTagName('exitText');
nicholas@2224 113 if (exitTextNode.length == 1) {
nicholas@2224 114 this.exitText = exitTextNode[0].textContent;
nicholas@2224 115 }
nicholas@2538 116
nicholas@2538 117 this.metrics = new this.metricNode();
nicholas@2538 118
nicholas@2538 119 this.metrics.decode(this, setupNode.getElementsByTagName('metric')[0]);
nicholas@2538 120
nicholas@2538 121 // Now process the survey node options
nicholas@2538 122 var survey = setupNode.getElementsByTagName('survey');
nicholas@2684 123 for (i = 0; i < survey.length; i++) {
nicholas@2538 124 var location = survey[i].getAttribute('location');
nicholas@2538 125 switch (location) {
nicholas@2257 126 case 'pre':
nicholas@2257 127 case 'before':
nicholas@2538 128 this.preTest = new this.surveyNode(this);
nicholas@2538 129 this.preTest.decode(this, survey[i]);
nicholas@2257 130 break;
nicholas@2257 131 case 'post':
nicholas@2257 132 case 'after':
b@2437 133 this.postTest = new this.surveyNode(this);
nicholas@2538 134 this.postTest.decode(this, survey[i]);
nicholas@2257 135 break;
nicholas@2257 136 }
nicholas@2538 137 }
nicholas@2538 138
nicholas@2538 139 var interfaceNode = setupNode.getElementsByTagName('interface');
nicholas@2538 140 if (interfaceNode.length > 1) {
nicholas@2538 141 this.errors.push("Only one <interface> node in the <setup> node allowed! Others except first ingnored!");
nicholas@2538 142 }
nicholas@2538 143 this.interfaces = new this.interfaceNode(this);
nicholas@2684 144 if (interfaceNode.length !== 0) {
nicholas@2538 145 interfaceNode = interfaceNode[0];
nicholas@2538 146 this.interfaces.decode(this, interfaceNode, this.schema.getAllElementsByName('interface')[1]);
nicholas@2538 147 }
nicholas@2538 148
nicholas@2538 149 // Page tags
nicholas@2538 150 var pageTags = projectXML.getElementsByTagName('page');
nicholas@2538 151 var pageSchema = this.schema.getAllElementsByName('page')[0];
nicholas@2684 152 for (i = 0; i < pageTags.length; i++) {
nicholas@2538 153 var node = new this.page(this);
nicholas@2538 154 node.decode(this, pageTags[i], pageSchema);
nicholas@2538 155 this.pages.push(node);
nicholas@2538 156 }
nicholas@2538 157 };
nicholas@2538 158
nicholas@2538 159 this.encode = function () {
nicholas@2538 160 var RootDocument = document.implementation.createDocument(null, "waet");
nicholas@2538 161 var root = RootDocument.firstChild;
nicholas@2538 162 root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
nicholas@2538 163 root.setAttribute("xsi:noNamespaceSchemaLocation", "test-schema.xsd");
nicholas@2538 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@2538 169 for (var i = 0; i < attributes.length; i++) {
nicholas@2224 170 var name = attributes[i].getAttribute("name");
nicholas@2684 171 if (name === undefined) {
nicholas@2224 172 name = attributes[i].getAttribute("ref");
nicholas@2224 173 }
nicholas@2685 174 if (this[name] !== undefined || attributes[i].getAttribute("use") == "required") {
nicholas@2685 175 setup.setAttribute(name, this[name]);
nicholas@2224 176 }
nicholas@2224 177 }
nicholas@2224 178 root.appendChild(setup);
nicholas@2224 179 // Survey node
nicholas@2684 180 if (this.exitText !== null) {
nicholas@2224 181 var exitTextNode = RootDocument.createElement('exitText');
nicholas@2224 182 exitTextNode.textContent = this.exitText;
nicholas@2224 183 setup.appendChild(exitTextNode);
nicholas@2224 184 }
nicholas@2224 185 setup.appendChild(this.preTest.encode(RootDocument));
nicholas@2224 186 setup.appendChild(this.postTest.encode(RootDocument));
nicholas@2224 187 setup.appendChild(this.metrics.encode(RootDocument));
nicholas@2224 188 setup.appendChild(this.interfaces.encode(RootDocument));
nicholas@2684 189 this.pages.forEach(function (page) {
nicholas@2224 190 root.appendChild(page.encode(RootDocument));
nicholas@2684 191 });
nicholas@2538 192 return RootDocument;
nicholas@2538 193 };
nicholas@2538 194
nicholas@2538 195 this.surveyNode = function (specification) {
nicholas@2538 196 this.location = null;
nicholas@2538 197 this.options = [];
nicholas@2224 198 this.parent = null;
nicholas@2538 199 this.schema = specification.schema.getAllElementsByName('survey')[0];
nicholas@2224 200 this.specification = specification;
nicholas@2538 201
nicholas@2538 202 this.OptionNode = function (specification) {
nicholas@2538 203 this.type = undefined;
n@2582 204 this.schema = undefined;
nicholas@2538 205 this.id = undefined;
nicholas@2224 206 this.name = undefined;
nicholas@2538 207 this.mandatory = undefined;
nicholas@2538 208 this.statement = undefined;
nicholas@2538 209 this.boxsize = undefined;
nicholas@2538 210 this.options = [];
nicholas@2538 211 this.min = undefined;
nicholas@2538 212 this.max = undefined;
nicholas@2538 213 this.step = undefined;
nicholas@2464 214 this.conditions = [];
nicholas@2538 215
nicholas@2538 216 this.decode = function (parent, child) {
n@2582 217 this.schema = specification.schema.getAllElementsByName(child.nodeName)[0];
nicholas@2538 218 var attributeMap = this.schema.getAllElementsByTagName('xs:attribute');
nicholas@2684 219 var i;
nicholas@2684 220 for (i in attributeMap) {
nicholas@2684 221 if (isNaN(Number(i)) === true) {
nicholas@2538 222 break;
nicholas@2538 223 }
nicholas@2538 224 var attributeName = attributeMap[i].getAttribute('name') || attributeMap[i].getAttribute('ref');
nicholas@2538 225 var projectAttr = child.getAttribute(attributeName);
nicholas@2538 226 projectAttr = parent.processAttribute(projectAttr, attributeMap[i], parent.schema);
nicholas@2538 227 switch (typeof projectAttr) {
nicholas@2538 228 case "number":
nicholas@2685 229 this[attributeName] = Number(projectAttr);
nicholas@2685 230 break;
nicholas@2538 231 case "boolean":
nicholas@2685 232 this[attributeName] = Boolean(projectAttr);
nicholas@2538 233 break;
nicholas@2538 234 case "string":
nicholas@2685 235 this[attributeName] = String(projectAttr);
nicholas@2538 236 break;
nicholas@2685 237 default:
nicholas@2685 238 throw ("Unkown attribute type " + projectAttr);
nicholas@2538 239 }
nicholas@2538 240 }
n@2582 241 if (child.nodeName == 'surveyentry') {
n@2582 242 console.log("NOTE - Use of <surveyelement> is now deprecated. Whilst these will still work, newer nodes and tighter error checking will not be enforced");
n@2582 243 console.log("Please use the newer, type specifc nodes");
n@2582 244 if (!this.type) {
n@2582 245 throw ("Type not specified");
n@2582 246 }
n@2582 247 } else {
n@2582 248 this.type = child.nodeName.split('survey')[1];
n@2582 249 }
nicholas@2538 250 this.statement = child.getElementsByTagName('statement')[0].textContent;
nicholas@2538 251 if (this.type == "checkbox" || this.type == "radio") {
nicholas@2538 252 var children = child.getElementsByTagName('option');
nicholas@2684 253 if (children.length === null) {
nicholas@2538 254 console.log('Malformed' + child.nodeName + 'entry');
nicholas@2538 255 this.statement = 'Malformed' + child.nodeName + 'entry';
nicholas@2538 256 this.type = 'statement';
nicholas@2538 257 } else {
nicholas@2538 258 this.options = [];
nicholas@2684 259 for (i = 0; i < children.length; i++) {
nicholas@2538 260 this.options.push({
nicholas@2538 261 name: children[i].getAttribute('name'),
nicholas@2538 262 text: children[i].textContent
nicholas@2538 263 });
nicholas@2538 264 }
nicholas@2538 265 }
n@2582 266 } else if (this.type == "slider") {
n@2582 267 this.leftText = "";
n@2582 268 this.rightText = "";
n@2582 269 var minText = child.getElementsByTagName("minText");
n@2582 270 var maxText = child.getElementsByTagName("maxText");
n@2582 271 if (minText.length > 0) {
n@2582 272 this.leftText = minText[0].textContent;
n@2582 273 }
n@2582 274 if (maxText.length > 0) {
n@2582 275 this.rightText = maxText[0].textContent;
n@2582 276 }
nicholas@2538 277 }
nicholas@2464 278 var conditionElements = child.getElementsByTagName("conditional");
nicholas@2684 279 for (i = 0; i < conditionElements.length; i++) {
nicholas@2464 280 var condition = conditionElements[i];
nicholas@2464 281 var obj = {
nicholas@2464 282 check: condition.getAttribute("check"),
nicholas@2464 283 value: condition.getAttribute("value"),
nicholas@2464 284 jumpToOnPass: condition.getAttribute("jumpToOnPass"),
nicholas@2464 285 jumpToOnFail: condition.getAttribute("jumpToOnFail")
nicholas@2684 286 };
nicholas@2464 287 this.conditions.push(obj);
nicholas@2464 288 }
nicholas@2538 289 };
nicholas@2538 290
nicholas@2538 291 this.exportXML = function (doc) {
n@2583 292 var node = doc.createElement('survey' + this.type);
nicholas@2538 293 var statement = doc.createElement('statement');
nicholas@2538 294 statement.textContent = this.statement;
nicholas@2538 295 node.appendChild(statement);
nicholas@2224 296 node.id = this.id;
nicholas@2684 297 if (this.name !== undefined) {
nicholas@2538 298 node.setAttribute("name", this.name);
nicholas@2538 299 }
nicholas@2684 300 if (this.mandatory !== undefined) {
nicholas@2538 301 node.setAttribute("mandatory", this.mandatory);
nicholas@2538 302 }
nicholas@2224 303 node.id = this.id;
nicholas@2684 304 if (this.name !== undefined) {
nicholas@2538 305 node.setAttribute("name", this.name);
nicholas@2538 306 }
nicholas@2538 307 switch (this.type) {
nicholas@2224 308 case "checkbox":
nicholas@2684 309 if (this.min !== undefined) {
nicholas@2566 310 node.setAttribute("min", this.min);
nicholas@2566 311 } else {
nicholas@2566 312 node.setAttribute("min", "0");
nicholas@2566 313 }
nicholas@2684 314 if (this.max !== undefined) {
nicholas@2566 315 node.setAttribute("max", this.max);
nicholas@2566 316 } else {
nicholas@2573 317 node.setAttribute("max", "undefined");
nicholas@2566 318 }
nicholas@2224 319 case "radio":
nicholas@2538 320 for (var i = 0; i < this.options.length; i++) {
nicholas@2224 321 var option = this.options[i];
nicholas@2224 322 var optionNode = doc.createElement("option");
nicholas@2538 323 optionNode.setAttribute("name", option.name);
nicholas@2224 324 optionNode.textContent = option.text;
nicholas@2224 325 node.appendChild(optionNode);
nicholas@2224 326 }
nicholas@2562 327 break;
nicholas@2224 328 case "number":
nicholas@2684 329 if (this.min !== undefined) {
nicholas@2538 330 node.setAttribute("min", this.min);
nicholas@2538 331 }
nicholas@2684 332 if (this.max !== undefined) {
nicholas@2538 333 node.setAttribute("max", this.max);
nicholas@2538 334 }
nicholas@2562 335 break;
nicholas@2224 336 case "question":
nicholas@2684 337 if (this.boxsize !== undefined) {
nicholas@2538 338 node.setAttribute("boxsize", this.boxsize);
nicholas@2538 339 }
nicholas@2684 340 if (this.mandatory !== undefined) {
nicholas@2538 341 node.setAttribute("mandatory", this.mandatory);
nicholas@2538 342 }
nicholas@2562 343 break;
nicholas@2562 344 case "video":
nicholas@2684 345 if (this.mandatory !== undefined) {
nicholas@2573 346 node.setAttribute("mandatory", this.mandatory);
nicholas@2573 347 }
nicholas@2562 348 case "youtube":
nicholas@2684 349 if (this.url !== undefined) {
nicholas@2573 350 node.setAttribute("url", this.url);
nicholas@2573 351 }
nicholas@2562 352 break;
n@2583 353 case "slider":
n@2583 354 node.setAttribute("min", this.min);
n@2583 355 node.setAttribute("max", this.max);
n@2583 356 if (this.leftText) {
n@2583 357 var minText = doc.createElement("minText");
n@2583 358 minText.textContent = this.leftText;
n@2583 359 node.appendChild(minText);
n@2583 360 }
n@2583 361 if (this.rightText) {
n@2583 362 var maxText = doc.createElement("maxText");
n@2583 363 maxText.textContent = this.rightText;
n@2583 364 node.appendChild(maxText);
n@2583 365 }
nicholas@2224 366 default:
nicholas@2224 367 break;
nicholas@2224 368 }
nicholas@2684 369 this.conditions.forEach(function (condition) {
nicholas@2465 370 var conditionDOM = doc.createElement("conditional");
nicholas@2538 371 conditionDOM.setAttribute("check", condition.check);
nicholas@2538 372 conditionDOM.setAttribute("value", condition.value);
nicholas@2538 373 conditionDOM.setAttribute("jumpToOnPass", condition.jumpToOnPass);
nicholas@2538 374 conditionDOM.setAttribute("jumpToOnFail", condition.jumpToOnFail);
nicholas@2465 375 node.appendChild(conditionDOM);
nicholas@2684 376 });
nicholas@2538 377 return node;
nicholas@2538 378 };
nicholas@2538 379 };
nicholas@2538 380 this.decode = function (parent, xml) {
nicholas@2224 381 this.parent = parent;
nicholas@2538 382 this.location = xml.getAttribute('location');
nicholas@2538 383 if (this.location == 'before') {
nicholas@2538 384 this.location = 'pre';
nicholas@2538 385 } else if (this.location == 'after') {
nicholas@2538 386 this.location = 'post';
nicholas@2538 387 }
nicholas@2684 388 var child = xml.firstElementChild;
n@2582 389 while (child) {
nicholas@2538 390 var node = new this.OptionNode(this.specification);
n@2582 391 node.decode(parent, child);
nicholas@2538 392 this.options.push(node);
n@2582 393 child = child.nextElementSibling;
nicholas@2538 394 }
nicholas@2684 395 if (this.options.length === 0) {
nicholas@2257 396 console.log("Empty survey node");
nicholas@2257 397 console.log(this);
nicholas@2257 398 }
nicholas@2538 399 };
nicholas@2538 400 this.encode = function (doc) {
nicholas@2538 401 var node = doc.createElement('survey');
nicholas@2538 402 node.setAttribute('location', this.location);
nicholas@2538 403 for (var i = 0; i < this.options.length; i++) {
nicholas@2538 404 node.appendChild(this.options[i].exportXML(doc));
nicholas@2538 405 }
nicholas@2538 406 return node;
nicholas@2538 407 };
nicholas@2538 408 };
nicholas@2538 409
nicholas@2538 410 this.interfaceNode = function (specification) {
nicholas@2538 411 this.title = null;
nicholas@2538 412 this.name = null;
nicholas@2538 413 this.options = [];
nicholas@2538 414 this.scales = [];
nicholas@2538 415 this.schema = specification.schema.getAllElementsByName('interface')[1];
nicholas@2538 416
nicholas@2538 417 this.decode = function (parent, xml) {
nicholas@2538 418 this.name = xml.getAttribute('name');
nicholas@2538 419 var titleNode = xml.getElementsByTagName('title');
nicholas@2538 420 if (titleNode.length == 1) {
nicholas@2538 421 this.title = titleNode[0].textContent;
nicholas@2538 422 }
nicholas@2538 423 var interfaceOptionNodes = xml.getElementsByTagName('interfaceoption');
nicholas@2538 424 // Extract interfaceoption node schema
nicholas@2538 425 var interfaceOptionNodeSchema = this.schema.getAllElementsByName('interfaceoption')[0];
nicholas@2538 426 var attributeMap = interfaceOptionNodeSchema.getAllElementsByTagName('xs:attribute');
nicholas@2684 427 var i, j;
nicholas@2684 428 for (i = 0; i < interfaceOptionNodes.length; i++) {
nicholas@2538 429 var ioNode = interfaceOptionNodes[i];
nicholas@2538 430 var option = {};
nicholas@2684 431 for (j = 0; j < attributeMap.length; j++) {
nicholas@2538 432 var attributeName = attributeMap[j].getAttribute('name') || attributeMap[j].getAttribute('ref');
nicholas@2538 433 var projectAttr = ioNode.getAttribute(attributeName);
nicholas@2538 434 if (parent.processAttribute) {
nicholas@2684 435 parent.processAttribute(projectAttr, attributeMap[j], parent.schema);
nicholas@2224 436 } else {
nicholas@2684 437 parent.parent.processAttribute(projectAttr, attributeMap[j], parent.parent.schema);
nicholas@2224 438 }
nicholas@2538 439 switch (typeof projectAttr) {
nicholas@2538 440 case "number":
nicholas@2685 441 option[attributeName] = Number(projectAttr);
nicholas@2685 442 break;
nicholas@2538 443 case "boolean":
nicholas@2685 444 option[attributeName] = Boolean(projectAttr);
nicholas@2538 445 break;
nicholas@2538 446 case "string":
nicholas@2685 447 option[attributeName] = String(projectAttr);
nicholas@2538 448 break;
nicholas@2538 449 }
nicholas@2538 450 }
nicholas@2538 451 this.options.push(option);
nicholas@2538 452 }
nicholas@2538 453
nicholas@2538 454 // Now the scales nodes
nicholas@2538 455 var scaleParent = xml.getElementsByTagName('scales');
nicholas@2538 456 if (scaleParent.length == 1) {
nicholas@2538 457 scaleParent = scaleParent[0];
nicholas@2252 458 var scalelabels = scaleParent.getAllElementsByTagName('scalelabel');
nicholas@2684 459 for (i = 0; i < scalelabels.length; i++) {
nicholas@2538 460 this.scales.push({
nicholas@2538 461 text: scalelabels[i].textContent,
nicholas@2538 462 position: Number(scalelabels[i].getAttribute('position'))
nicholas@2538 463 });
nicholas@2538 464 }
nicholas@2538 465 }
nicholas@2538 466 };
nicholas@2538 467
nicholas@2538 468 this.encode = function (doc) {
nicholas@2538 469 var node = doc.createElement("interface");
nicholas@2684 470 if (typeof this.name == "string" && this.name.length > 0)
nicholas@2538 471 node.setAttribute("name", this.name);
nicholas@2243 472 if (typeof this.title == "string") {
nicholas@2243 473 var titleNode = doc.createElement("title");
nicholas@2243 474 titleNode.textContent = this.title;
nicholas@2243 475 node.appendChild(titleNode);
nicholas@2243 476 }
nicholas@2684 477 this.options.forEach(function (option) {
nicholas@2224 478 var child = doc.createElement("interfaceoption");
nicholas@2538 479 child.setAttribute("type", option.type);
nicholas@2538 480 child.setAttribute("name", option.name);
nicholas@2224 481 node.appendChild(child);
nicholas@2684 482 });
nicholas@2684 483 if (this.scales.length !== 0) {
nicholas@2224 484 var scales = doc.createElement("scales");
nicholas@2684 485 this.scales.forEach(function (scale) {
nicholas@2224 486 var child = doc.createElement("scalelabel");
nicholas@2538 487 child.setAttribute("position", scale.position);
nicholas@2224 488 child.textContent = scale.text;
nicholas@2224 489 scales.appendChild(child);
nicholas@2684 490 });
nicholas@2224 491 node.appendChild(scales);
nicholas@2224 492 }
nicholas@2224 493 return node;
nicholas@2538 494 };
nicholas@2538 495 };
nicholas@2538 496
nicholas@2538 497 this.metricNode = function () {
nicholas@2224 498 this.enabled = [];
nicholas@2538 499 this.decode = function (parent, xml) {
nicholas@2224 500 var children = xml.getElementsByTagName('metricenable');
nicholas@2538 501 for (var i in children) {
nicholas@2684 502 if (isNaN(Number(i)) === true) {
nicholas@2538 503 break;
nicholas@2538 504 }
nicholas@2224 505 this.enabled.push(children[i].textContent);
nicholas@2224 506 }
nicholas@2684 507 };
nicholas@2538 508 this.encode = function (doc) {
nicholas@2224 509 var node = doc.createElement('metric');
nicholas@2538 510 for (var i in this.enabled) {
nicholas@2684 511 if (isNaN(Number(i)) === true) {
nicholas@2538 512 break;
nicholas@2538 513 }
nicholas@2224 514 var child = doc.createElement('metricenable');
nicholas@2224 515 child.textContent = this.enabled[i];
nicholas@2224 516 node.appendChild(child);
nicholas@2224 517 }
nicholas@2224 518 return node;
nicholas@2684 519 };
nicholas@2684 520 };
nicholas@2538 521
nicholas@2538 522 this.page = function (specification) {
nicholas@2538 523 this.presentedId = undefined;
nicholas@2538 524 this.id = undefined;
nicholas@2470 525 this.title = undefined;
nicholas@2538 526 this.hostURL = undefined;
nicholas@2538 527 this.randomiseOrder = undefined;
nicholas@2538 528 this.loop = undefined;
nicholas@2538 529 this.outsideReference = null;
nicholas@2538 530 this.loudness = null;
nicholas@2224 531 this.label = null;
nicholas@2573 532 this.labelStart = "";
nicholas@2538 533 this.preTest = null;
nicholas@2538 534 this.postTest = null;
nicholas@2538 535 this.interfaces = [];
n@2426 536 this.playOne = null;
nicholas@2622 537 this.restrictMovement = null;
nicholas@2538 538 this.commentBoxPrefix = "Comment on track";
nicholas@2538 539 this.audioElements = [];
nicholas@2538 540 this.commentQuestions = [];
nicholas@2538 541 this.schema = specification.schema.getAllElementsByName("page")[0];
nicholas@2224 542 this.specification = specification;
nicholas@2224 543 this.parent = null;
nicholas@2538 544
nicholas@2538 545 this.decode = function (parent, xml) {
nicholas@2224 546 this.parent = parent;
nicholas@2538 547 var attributeMap = this.schema.getAllElementsByTagName('xs:attribute');
nicholas@2684 548 var i, node;
nicholas@2684 549 for (i = 0; i < attributeMap.length; i++) {
nicholas@2538 550 var attributeName = attributeMap[i].getAttribute('name') || attributeMap[i].getAttribute('ref');
nicholas@2538 551 var projectAttr = xml.getAttribute(attributeName);
nicholas@2538 552 projectAttr = parent.processAttribute(projectAttr, attributeMap[i], parent.schema);
nicholas@2538 553 switch (typeof projectAttr) {
nicholas@2538 554 case "number":
nicholas@2685 555 this[attributeName] = Number(projectAttr);
nicholas@2685 556 break;
nicholas@2538 557 case "boolean":
nicholas@2685 558 this[attributeName] = Boolean(projectAttr);
nicholas@2538 559 break;
nicholas@2538 560 case "string":
nicholas@2685 561 this[attributeName] = String(projectAttr);
nicholas@2538 562 break;
nicholas@2538 563 }
nicholas@2538 564 }
nicholas@2538 565
nicholas@2470 566 // Get the title
nicholas@2470 567 var title = xml.getElementsByTagName('title');
nicholas@2684 568 if (title.length !== 0 && title[0].parentElement == xml) {
nicholas@2470 569 this.title = title[0].textContent;
nicholas@2470 570 }
nicholas@2538 571
nicholas@2538 572 // Get the Comment Box Prefix
nicholas@2538 573 var CBP = xml.getElementsByTagName('commentboxprefix');
nicholas@2684 574 if (CBP.length !== 0 && CBP[0].parentElement == xml) {
nicholas@2538 575 this.commentBoxPrefix = CBP[0].textContent;
nicholas@2538 576 }
nicholas@2538 577
nicholas@2538 578 // Now decode the interfaces
nicholas@2538 579 var interfaceNode = xml.getElementsByTagName('interface');
nicholas@2684 580 for (i = 0; i < interfaceNode.length; i++) {
nicholas@2684 581 node = new parent.interfaceNode(this.specification);
nicholas@2538 582 node.decode(this, interfaceNode[i], parent.schema.getAllElementsByName('interface')[1]);
nicholas@2538 583 this.interfaces.push(node);
nicholas@2538 584 }
nicholas@2538 585
nicholas@2538 586 // Now process the survey node options
nicholas@2538 587 var survey = xml.getElementsByTagName('survey');
nicholas@2538 588 var surveySchema = parent.schema.getAllElementsByName('survey')[0];
nicholas@2684 589 for (i = 0; i < survey.length; i++) {
nicholas@2538 590 var location = survey[i].getAttribute('location');
nicholas@2538 591 if (location == 'pre' || location == 'before') {
nicholas@2684 592 if (this.preTest !== null) {
nicholas@2538 593 this.errors.push("Already a pre/before test survey defined! Ignoring second!!");
nicholas@2538 594 } else {
nicholas@2538 595 this.preTest = new parent.surveyNode(this.specification);
nicholas@2538 596 this.preTest.decode(parent, survey[i], surveySchema);
nicholas@2538 597 }
nicholas@2538 598 } else if (location == 'post' || location == 'after') {
nicholas@2684 599 if (this.postTest !== null) {
nicholas@2538 600 this.errors.push("Already a post/after test survey defined! Ignoring second!!");
nicholas@2538 601 } else {
nicholas@2538 602 this.postTest = new parent.surveyNode(this.specification);
nicholas@2538 603 this.postTest.decode(parent, survey[i], surveySchema);
nicholas@2538 604 }
nicholas@2538 605 }
nicholas@2538 606 }
nicholas@2538 607
nicholas@2538 608 // Now process the audioelement tags
nicholas@2538 609 var audioElements = xml.getElementsByTagName('audioelement');
nicholas@2684 610 for (i = 0; i < audioElements.length; i++) {
nicholas@2684 611 var audioNode = new this.audioElementNode(this.specification);
nicholas@2684 612 audioNode.decode(this, audioElements[i]);
nicholas@2684 613 this.audioElements.push(audioNode);
nicholas@2538 614 }
nicholas@2538 615
nicholas@2538 616 // Now decode the commentquestions
n@2579 617 var cqNode = xml.getElementsByTagName('commentquestions');
nicholas@2684 618 if (cqNode.length !== 0) {
n@2579 619 cqNode = cqNode[0];
nicholas@2644 620 var commentQuestion = cqNode.firstElementChild;
nicholas@2644 621 while (commentQuestion) {
nicholas@2684 622 node = new this.commentQuestionNode(this.specification);
nicholas@2644 623 node.decode(parent, commentQuestion);
n@2579 624 this.commentQuestions.push(node);
nicholas@2644 625 commentQuestion = commentQuestion.nextElementSibling;
n@2579 626 }
nicholas@2538 627 }
nicholas@2538 628 };
nicholas@2538 629
nicholas@2538 630 this.encode = function (root) {
nicholas@2538 631 var AHNode = root.createElement("page");
nicholas@2224 632 // First decode the attributes
nicholas@2224 633 var attributes = this.schema.getAllElementsByTagName('xs:attribute');
nicholas@2684 634 var i;
nicholas@2684 635 for (i = 0; i < attributes.length; i++) {
nicholas@2224 636 var name = attributes[i].getAttribute("name");
nicholas@2684 637 if (name === undefined) {
nicholas@2224 638 name = attributes[i].getAttribute("ref");
nicholas@2224 639 }
nicholas@2685 640 if (this[name] !== undefined || attributes[i].getAttribute("use") == "required") {
nicholas@2685 641 AHNode.setAttribute(name, this[name]);
nicholas@2224 642 }
nicholas@2224 643 }
nicholas@2684 644 if (this.loudness !== null) {
nicholas@2538 645 AHNode.setAttribute("loudness", this.loudness);
nicholas@2538 646 }
nicholas@2224 647 // <commentboxprefix>
nicholas@2224 648 var commentboxprefix = root.createElement("commentboxprefix");
nicholas@2224 649 commentboxprefix.textContent = this.commentBoxPrefix;
nicholas@2224 650 AHNode.appendChild(commentboxprefix);
nicholas@2538 651
nicholas@2684 652 for (i = 0; i < this.interfaces.length; i++) {
nicholas@2538 653 AHNode.appendChild(this.interfaces[i].encode(root));
nicholas@2538 654 }
nicholas@2538 655
nicholas@2684 656 for (i = 0; i < this.audioElements.length; i++) {
nicholas@2538 657 AHNode.appendChild(this.audioElements[i].encode(root));
nicholas@2538 658 }
nicholas@2538 659 // Create <CommentQuestion>
nicholas@2684 660 for (i = 0; i < this.commentQuestions.length; i++) {
nicholas@2538 661 AHNode.appendChild(this.commentQuestions[i].encode(root));
nicholas@2538 662 }
nicholas@2538 663
nicholas@2538 664 AHNode.appendChild(this.preTest.encode(root));
nicholas@2224 665 AHNode.appendChild(this.postTest.encode(root));
nicholas@2538 666 return AHNode;
nicholas@2538 667 };
nicholas@2538 668
nicholas@2538 669 this.commentQuestionNode = function (specification) {
nicholas@2538 670 this.id = null;
nicholas@2224 671 this.name = undefined;
nicholas@2538 672 this.type = undefined;
nicholas@2538 673 this.statement = undefined;
nicholas@2538 674 this.schema = specification.schema.getAllElementsByName('commentquestion')[0];
nicholas@2538 675 this.decode = function (parent, xml) {
nicholas@2538 676 this.id = xml.id;
nicholas@2224 677 this.name = xml.getAttribute('name');
n@2579 678 switch (xml.nodeName) {
n@2579 679 case "commentradio":
n@2579 680 this.type = "radio";
n@2579 681 this.options = [];
n@2579 682 break;
n@2579 683 case "commentcheckbox":
n@2579 684 this.type = "checkbox";
n@2579 685 this.options = [];
n@2579 686 break;
n@2579 687 case "commentslider":
n@2579 688 this.type = "slider";
n@2579 689 this.min = undefined;
n@2579 690 this.max = undefined;
n@2579 691 this.step = undefined;
n@2579 692 break;
n@2579 693 case "commentquestion":
n@2579 694 default:
n@2579 695 this.type = "question";
n@2579 696 break;
n@2579 697 }
nicholas@2538 698 this.statement = xml.getElementsByTagName('statement')[0].textContent;
n@2579 699 if (this.type == "radio" || this.type == "checkbox") {
n@2579 700 var optNodes = xml.getElementsByTagName('option');
n@2579 701 for (var i = 0; i < optNodes.length; i++) {
n@2579 702 var optNode = optNodes[i];
n@2579 703 this.options.push({
n@2579 704 name: optNode.getAttribute('name'),
n@2579 705 text: optNode.textContent
n@2579 706 });
n@2579 707 }
n@2579 708 }
n@2579 709 if (this.type == "slider") {
n@2579 710 this.min = Number(xml.getAttribute("min"));
n@2579 711 this.max = Number(xml.getAttribute("max"));
n@2579 712 this.step = Number(xml.getAttribute("step"));
nicholas@2684 713 if (this.step === undefined) {
n@2579 714 this.step = 1;
n@2579 715 }
n@2579 716 this.value = Number(xml.getAttribute("value"));
nicholas@2684 717 if (this.value === undefined) {
nicholas@2684 718 this.value = this.min;
n@2579 719 }
n@2580 720 this.leftText = xml.getElementsByTagName("minText");
n@2580 721 if (this.leftText && this.leftText.length > 0) {
n@2580 722 this.leftText = this.leftText[0].textContent;
n@2580 723 } else {
n@2580 724 this.leftText = "";
n@2580 725 }
n@2580 726 this.rightText = xml.getElementsByTagName("maxText");
n@2580 727 if (this.rightText && this.rightText.length > 0) {
n@2580 728 this.rightText = this.rightText[0].textContent;
n@2580 729 } else {
n@2580 730 this.rightText = "";
n@2580 731 }
nicholas@2538 732 }
nicholas@2538 733 };
nicholas@2538 734
nicholas@2538 735 this.encode = function (root) {
n@2579 736 var node;
n@2579 737 switch (this.type) {
n@2579 738 case "radio":
n@2579 739 node = root.createElement("commentradio");
n@2579 740 break;
n@2579 741 case "checkbox":
n@2579 742 node = root.createElement("commentcheckbox");
n@2579 743 break;
n@2579 744 case "slider":
n@2579 745 node = root.createElement("commentslider");
n@2579 746 break;
n@2579 747 case "question":
n@2579 748 default:
n@2579 749 node = root.createElement("commentquestion");
n@2579 750 break;
n@2579 751 }
nicholas@2224 752 node.id = this.id;
nicholas@2538 753 node.setAttribute("type", this.type);
nicholas@2684 754 if (this.name !== undefined) {
nicholas@2538 755 node.setAttribute("name", this.name);
nicholas@2538 756 }
nicholas@2224 757 var statement = root.createElement("statement");
nicholas@2224 758 statement.textContent = this.statement;
nicholas@2224 759 node.appendChild(statement);
n@2579 760 if (this.type == "radio" || this.type == "checkbox") {
nicholas@2684 761 this.options.forEach(function (option) {
n@2579 762 var child = root.createElement("option");
n@2579 763 child.setAttribute("name", option.name);
n@2579 764 child.textContent = option.text;
n@2579 765 node.appendChild(child);
nicholas@2684 766 });
n@2579 767 }
n@2579 768 if (this.type == "slider") {
n@2579 769 node.setAttribute("min", this.min);
n@2579 770 node.setAttribute("max", this.max);
n@2579 771 if (this.step !== 1) {
n@2579 772 node.setAttribute("step", this.step);
n@2579 773 }
n@2579 774 if (this.value !== this.min) {
n@2579 775 node.setAttribute("value", this.value);
n@2579 776 }
n@2580 777 if (this.leftText.length > 0) {
n@2580 778 var leftText = root.createElement("minText");
n@2580 779 leftText.textContent = this.leftText;
n@2580 780 node.appendChild(leftText);
n@2580 781 }
n@2580 782 if (this.rightText.length > 0) {
n@2580 783 var rightText = root.createElement("maxText");
n@2580 784 rightText.textContent = this.rightText;
n@2580 785 node.appendChild(rightText);
n@2580 786 }
nicholas@2224 787 }
nicholas@2224 788 return node;
nicholas@2538 789 };
nicholas@2538 790 };
nicholas@2538 791
nicholas@2538 792 this.audioElementNode = function (specification) {
nicholas@2538 793 this.url = null;
nicholas@2538 794 this.id = null;
nicholas@2224 795 this.name = null;
nicholas@2538 796 this.parent = null;
nicholas@2538 797 this.type = null;
nicholas@2538 798 this.marker = null;
nicholas@2538 799 this.enforce = false;
nicholas@2538 800 this.gain = 0.0;
nicholas@2409 801 this.label = null;
nicholas@2460 802 this.startTime = undefined;
nicholas@2460 803 this.stopTime = undefined;
nicholas@2614 804 this.sampleRate = undefined;
nicholas@2614 805 this.alternatives = [];
nicholas@2684 806 this.schema = specification.schema.getAllElementsByName('audioelement')[0];
nicholas@2538 807 this.parent = null;
nicholas@2538 808 this.decode = function (parent, xml) {
nicholas@2538 809 this.parent = parent;
nicholas@2538 810 var attributeMap = this.schema.getAllElementsByTagName('xs:attribute');
nicholas@2538 811 for (var i = 0; i < attributeMap.length; i++) {
nicholas@2538 812 var attributeName = attributeMap[i].getAttribute('name') || attributeMap[i].getAttribute('ref');
nicholas@2538 813 var projectAttr = xml.getAttribute(attributeName);
nicholas@2538 814 projectAttr = parent.parent.processAttribute(projectAttr, attributeMap[i], parent.parent.schema);
nicholas@2538 815 switch (typeof projectAttr) {
nicholas@2538 816 case "number":
nicholas@2685 817 this[attributeName] = Number(projectAttr);
nicholas@2685 818 break;
nicholas@2538 819 case "boolean":
nicholas@2685 820 this[attributeName] = Boolean(projectAttr);
nicholas@2538 821 break;
nicholas@2538 822 case "string":
nicholas@2685 823 this[attributeName] = String(projectAttr);
nicholas@2538 824 break;
nicholas@2538 825 }
nicholas@2538 826 }
nicholas@2614 827 // Get the alternative nodes
nicholas@2614 828 var child = xml.firstElementChild;
nicholas@2614 829 while (child) {
nicholas@2614 830 if (child.nodeName == "alternative") {
nicholas@2614 831 this.alternatives.push({
nicholas@2614 832 'url': child.getAttribute("url"),
nicholas@2614 833 'sampleRate': child.getAttribute("sampleRate")
nicholas@2614 834 });
nicholas@2614 835 }
nicholas@2614 836 child = child.nextElementSibling;
nicholas@2614 837 }
nicholas@2538 838
nicholas@2538 839 };
nicholas@2538 840 this.encode = function (root) {
nicholas@2538 841 var AENode = root.createElement("audioelement");
nicholas@2538 842 var attributes = this.schema.getAllElementsByTagName('xs:attribute');
nicholas@2538 843 for (var i = 0; i < attributes.length; i++) {
nicholas@2224 844 var name = attributes[i].getAttribute("name");
nicholas@2684 845 if (name === undefined) {
nicholas@2224 846 name = attributes[i].getAttribute("ref");
nicholas@2224 847 }
nicholas@2685 848 if (this[name] !== undefined || attributes[i].getAttribute("use") == "required") {
nicholas@2685 849 AENode.setAttribute(name, this[name]);
nicholas@2224 850 }
nicholas@2224 851 }
nicholas@2614 852 this.alternatives.forEach(function (alt) {
nicholas@2614 853 var node = root.createElement("alternative");
nicholas@2614 854 node.setAttribute("url", alt.url);
nicholas@2614 855 node.setAttribute("sampleRate", alt.sampleRate);
nicholas@2614 856 AENode.appendChild(node);
nicholas@2614 857 });
nicholas@2538 858 return AENode;
nicholas@2538 859 };
nicholas@2538 860 };
nicholas@2538 861 };
b@2437 862 }