annotate js/specification.js @ 2692:7ff393f1fefa

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