annotate js/specification.js @ 2788:a4bcbcda23f7

#133 Added to specification.js (encoder)
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Sat, 22 Apr 2017 17:48:48 +0100
parents 28bc4e90e026
children 394b5e20d78f 8372c220db7a
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@2774 226 this.minWait = undefined;
nicholas@2538 227 this.step = undefined;
nicholas@2464 228 this.conditions = [];
nicholas@2538 229
nicholas@2538 230 this.decode = function (parent, child) {
nicholas@2687 231 this.schema = schemaRoot.getAllElementsByName(child.nodeName)[0];
nicholas@2538 232 var attributeMap = this.schema.getAllElementsByTagName('xs:attribute');
nicholas@2684 233 var i;
nicholas@2684 234 for (i in attributeMap) {
nicholas@2684 235 if (isNaN(Number(i)) === true) {
nicholas@2538 236 break;
nicholas@2538 237 }
nicholas@2538 238 var attributeName = attributeMap[i].getAttribute('name') || attributeMap[i].getAttribute('ref');
nicholas@2538 239 var projectAttr = child.getAttribute(attributeName);
nicholas@2692 240 projectAttr = processAttribute(projectAttr, attributeMap[i]);
nicholas@2687 241 if (projectAttr !== null) {
nicholas@2687 242 this[attributeName] = projectAttr;
nicholas@2538 243 }
nicholas@2538 244 }
n@2582 245 if (child.nodeName == 'surveyentry') {
n@2582 246 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 247 console.log("Please use the newer, type specifc nodes");
n@2582 248 if (!this.type) {
n@2582 249 throw ("Type not specified");
n@2582 250 }
n@2582 251 } else {
n@2582 252 this.type = child.nodeName.split('survey')[1];
n@2582 253 }
nicholas@2538 254 this.statement = child.getElementsByTagName('statement')[0].textContent;
nicholas@2538 255 if (this.type == "checkbox" || this.type == "radio") {
nicholas@2538 256 var children = child.getElementsByTagName('option');
nicholas@2684 257 if (children.length === null) {
nicholas@2538 258 console.log('Malformed' + child.nodeName + 'entry');
nicholas@2538 259 this.statement = 'Malformed' + child.nodeName + 'entry';
nicholas@2538 260 this.type = 'statement';
nicholas@2538 261 } else {
nicholas@2538 262 this.options = [];
nicholas@2684 263 for (i = 0; i < children.length; i++) {
nicholas@2538 264 this.options.push({
nicholas@2538 265 name: children[i].getAttribute('name'),
nicholas@2538 266 text: children[i].textContent
nicholas@2538 267 });
nicholas@2538 268 }
nicholas@2538 269 }
n@2582 270 } else if (this.type == "slider") {
n@2582 271 this.leftText = "";
n@2582 272 this.rightText = "";
n@2582 273 var minText = child.getElementsByTagName("minText");
n@2582 274 var maxText = child.getElementsByTagName("maxText");
n@2582 275 if (minText.length > 0) {
n@2582 276 this.leftText = minText[0].textContent;
n@2582 277 }
n@2582 278 if (maxText.length > 0) {
n@2582 279 this.rightText = maxText[0].textContent;
n@2582 280 }
nicholas@2538 281 }
nicholas@2464 282 var conditionElements = child.getElementsByTagName("conditional");
nicholas@2684 283 for (i = 0; i < conditionElements.length; i++) {
nicholas@2464 284 var condition = conditionElements[i];
nicholas@2464 285 var obj = {
nicholas@2464 286 check: condition.getAttribute("check"),
nicholas@2464 287 value: condition.getAttribute("value"),
nicholas@2464 288 jumpToOnPass: condition.getAttribute("jumpToOnPass"),
nicholas@2464 289 jumpToOnFail: condition.getAttribute("jumpToOnFail")
nicholas@2684 290 };
nicholas@2464 291 this.conditions.push(obj);
nicholas@2464 292 }
nicholas@2538 293 };
nicholas@2538 294
nicholas@2538 295 this.exportXML = function (doc) {
n@2583 296 var node = doc.createElement('survey' + this.type);
nicholas@2538 297 var statement = doc.createElement('statement');
nicholas@2538 298 statement.textContent = this.statement;
nicholas@2538 299 node.appendChild(statement);
nicholas@2224 300 node.id = this.id;
nicholas@2684 301 if (this.name !== undefined) {
nicholas@2538 302 node.setAttribute("name", this.name);
nicholas@2538 303 }
nicholas@2684 304 if (this.mandatory !== undefined) {
nicholas@2538 305 node.setAttribute("mandatory", this.mandatory);
nicholas@2538 306 }
nicholas@2224 307 node.id = this.id;
nicholas@2684 308 if (this.name !== undefined) {
nicholas@2538 309 node.setAttribute("name", this.name);
nicholas@2538 310 }
nicholas@2538 311 switch (this.type) {
nicholas@2224 312 case "checkbox":
nicholas@2684 313 if (this.min !== undefined) {
nicholas@2566 314 node.setAttribute("min", this.min);
nicholas@2566 315 } else {
nicholas@2566 316 node.setAttribute("min", "0");
nicholas@2566 317 }
nicholas@2684 318 if (this.max !== undefined) {
nicholas@2566 319 node.setAttribute("max", this.max);
nicholas@2566 320 } else {
nicholas@2573 321 node.setAttribute("max", "undefined");
nicholas@2686 322 } /* falls through */
nicholas@2224 323 case "radio":
nicholas@2538 324 for (var i = 0; i < this.options.length; i++) {
nicholas@2224 325 var option = this.options[i];
nicholas@2224 326 var optionNode = doc.createElement("option");
nicholas@2538 327 optionNode.setAttribute("name", option.name);
nicholas@2224 328 optionNode.textContent = option.text;
nicholas@2224 329 node.appendChild(optionNode);
nicholas@2224 330 }
nicholas@2562 331 break;
nicholas@2224 332 case "number":
nicholas@2684 333 if (this.min !== undefined) {
nicholas@2538 334 node.setAttribute("min", this.min);
nicholas@2538 335 }
nicholas@2684 336 if (this.max !== undefined) {
nicholas@2538 337 node.setAttribute("max", this.max);
nicholas@2538 338 }
nicholas@2562 339 break;
nicholas@2224 340 case "question":
nicholas@2684 341 if (this.boxsize !== undefined) {
nicholas@2538 342 node.setAttribute("boxsize", this.boxsize);
nicholas@2538 343 }
nicholas@2684 344 if (this.mandatory !== undefined) {
nicholas@2538 345 node.setAttribute("mandatory", this.mandatory);
nicholas@2538 346 }
nicholas@2562 347 break;
nicholas@2562 348 case "video":
nicholas@2684 349 if (this.mandatory !== undefined) {
nicholas@2573 350 node.setAttribute("mandatory", this.mandatory);
nicholas@2686 351 } /* falls through */
nicholas@2562 352 case "youtube":
nicholas@2684 353 if (this.url !== undefined) {
nicholas@2573 354 node.setAttribute("url", this.url);
nicholas@2573 355 }
nicholas@2562 356 break;
n@2583 357 case "slider":
n@2583 358 node.setAttribute("min", this.min);
n@2583 359 node.setAttribute("max", this.max);
n@2583 360 if (this.leftText) {
n@2583 361 var minText = doc.createElement("minText");
n@2583 362 minText.textContent = this.leftText;
n@2583 363 node.appendChild(minText);
n@2583 364 }
n@2583 365 if (this.rightText) {
n@2583 366 var maxText = doc.createElement("maxText");
n@2583 367 maxText.textContent = this.rightText;
n@2583 368 node.appendChild(maxText);
n@2583 369 }
nicholas@2686 370 break;
nicholas@2224 371 default:
nicholas@2224 372 break;
nicholas@2224 373 }
nicholas@2684 374 this.conditions.forEach(function (condition) {
nicholas@2465 375 var conditionDOM = doc.createElement("conditional");
nicholas@2538 376 conditionDOM.setAttribute("check", condition.check);
nicholas@2538 377 conditionDOM.setAttribute("value", condition.value);
nicholas@2538 378 conditionDOM.setAttribute("jumpToOnPass", condition.jumpToOnPass);
nicholas@2538 379 conditionDOM.setAttribute("jumpToOnFail", condition.jumpToOnFail);
nicholas@2465 380 node.appendChild(conditionDOM);
nicholas@2684 381 });
nicholas@2538 382 return node;
nicholas@2538 383 };
nicholas@2538 384 };
nicholas@2538 385 this.decode = function (parent, xml) {
nicholas@2224 386 this.parent = parent;
nicholas@2538 387 this.location = xml.getAttribute('location');
nicholas@2538 388 if (this.location == 'before') {
nicholas@2538 389 this.location = 'pre';
nicholas@2538 390 } else if (this.location == 'after') {
nicholas@2538 391 this.location = 'post';
nicholas@2538 392 }
nicholas@2684 393 var child = xml.firstElementChild;
n@2582 394 while (child) {
nicholas@2538 395 var node = new this.OptionNode(this.specification);
n@2582 396 node.decode(parent, child);
nicholas@2538 397 this.options.push(node);
n@2582 398 child = child.nextElementSibling;
nicholas@2538 399 }
nicholas@2684 400 if (this.options.length === 0) {
nicholas@2257 401 console.log("Empty survey node");
nicholas@2257 402 console.log(this);
nicholas@2257 403 }
nicholas@2538 404 };
nicholas@2538 405 this.encode = function (doc) {
nicholas@2538 406 var node = doc.createElement('survey');
nicholas@2538 407 node.setAttribute('location', this.location);
nicholas@2538 408 for (var i = 0; i < this.options.length; i++) {
nicholas@2538 409 node.appendChild(this.options[i].exportXML(doc));
nicholas@2538 410 }
nicholas@2538 411 return node;
nicholas@2538 412 };
nicholas@2538 413 };
nicholas@2538 414
nicholas@2538 415 this.interfaceNode = function (specification) {
nicholas@2688 416 this.title = undefined;
nicholas@2688 417 this.name = undefined;
nicholas@2777 418 this.image = undefined;
nicholas@2538 419 this.options = [];
nicholas@2538 420 this.scales = [];
nicholas@2687 421 this.schema = schemaRoot.getAllElementsByName('interface')[1];
nicholas@2538 422
nicholas@2538 423 this.decode = function (parent, xml) {
nicholas@2538 424 this.name = xml.getAttribute('name');
nicholas@2538 425 var titleNode = xml.getElementsByTagName('title');
nicholas@2538 426 if (titleNode.length == 1) {
nicholas@2538 427 this.title = titleNode[0].textContent;
nicholas@2538 428 }
nicholas@2538 429 var interfaceOptionNodes = xml.getElementsByTagName('interfaceoption');
nicholas@2538 430 // Extract interfaceoption node schema
nicholas@2538 431 var interfaceOptionNodeSchema = this.schema.getAllElementsByName('interfaceoption')[0];
nicholas@2538 432 var attributeMap = interfaceOptionNodeSchema.getAllElementsByTagName('xs:attribute');
nicholas@2684 433 var i, j;
nicholas@2684 434 for (i = 0; i < interfaceOptionNodes.length; i++) {
nicholas@2538 435 var ioNode = interfaceOptionNodes[i];
nicholas@2538 436 var option = {};
nicholas@2684 437 for (j = 0; j < attributeMap.length; j++) {
nicholas@2538 438 var attributeName = attributeMap[j].getAttribute('name') || attributeMap[j].getAttribute('ref');
nicholas@2538 439 var projectAttr = ioNode.getAttribute(attributeName);
nicholas@2692 440 projectAttr = processAttribute(projectAttr, attributeMap[j]);
nicholas@2687 441 if (projectAttr !== null) {
nicholas@2688 442 option[attributeName] = projectAttr;
nicholas@2538 443 }
nicholas@2538 444 }
n@2787 445 if (option.type == "check" && ioNode.firstElementChild) {
n@2787 446 option.errorMessage = ioNode.firstElementChild.textContent;
n@2787 447 }
nicholas@2538 448 this.options.push(option);
nicholas@2538 449 }
nicholas@2777 450 // Get the image node
nicholas@2777 451 var imageNode = xml.getElementsByTagName("image");
nicholas@2777 452 if (imageNode.length == 1) {
nicholas@2777 453 this.image = imageNode[0].getAttribute("src");
nicholas@2777 454 }
nicholas@2538 455 // Now the scales nodes
nicholas@2538 456 var scaleParent = xml.getElementsByTagName('scales');
nicholas@2538 457 if (scaleParent.length == 1) {
nicholas@2538 458 scaleParent = scaleParent[0];
nicholas@2252 459 var scalelabels = scaleParent.getAllElementsByTagName('scalelabel');
nicholas@2684 460 for (i = 0; i < scalelabels.length; i++) {
nicholas@2538 461 this.scales.push({
nicholas@2538 462 text: scalelabels[i].textContent,
nicholas@2538 463 position: Number(scalelabels[i].getAttribute('position'))
nicholas@2538 464 });
nicholas@2538 465 }
nicholas@2538 466 }
nicholas@2538 467 };
nicholas@2538 468
nicholas@2538 469 this.encode = function (doc) {
nicholas@2538 470 var node = doc.createElement("interface");
nicholas@2684 471 if (typeof this.name == "string" && this.name.length > 0)
nicholas@2538 472 node.setAttribute("name", this.name);
nicholas@2243 473 if (typeof this.title == "string") {
nicholas@2243 474 var titleNode = doc.createElement("title");
nicholas@2243 475 titleNode.textContent = this.title;
nicholas@2243 476 node.appendChild(titleNode);
nicholas@2243 477 }
nicholas@2684 478 this.options.forEach(function (option) {
nicholas@2224 479 var child = doc.createElement("interfaceoption");
nicholas@2538 480 child.setAttribute("type", option.type);
nicholas@2538 481 child.setAttribute("name", option.name);
n@2788 482 if (option.type == "check" && option.errorMessage !== undefined) {
n@2788 483 var errorMessage = doc.createElement("errormessage");
n@2788 484 errorMessage.textContent = option.errorMessage;
n@2788 485 child.appendChild(errorMessage);
n@2788 486 }
nicholas@2224 487 node.appendChild(child);
nicholas@2684 488 });
nicholas@2780 489 if (typeof this.image == "string" && this.image.length !== 0) {
nicholas@2780 490 var imgNode = doc.createElement("image");
nicholas@2780 491 imgNode.setAttribute("src", this.image);
nicholas@2780 492 node.appendChild(imgNode);
nicholas@2780 493 }
nicholas@2684 494 if (this.scales.length !== 0) {
nicholas@2224 495 var scales = doc.createElement("scales");
nicholas@2684 496 this.scales.forEach(function (scale) {
nicholas@2224 497 var child = doc.createElement("scalelabel");
nicholas@2538 498 child.setAttribute("position", scale.position);
nicholas@2224 499 child.textContent = scale.text;
nicholas@2224 500 scales.appendChild(child);
nicholas@2684 501 });
nicholas@2224 502 node.appendChild(scales);
nicholas@2224 503 }
nicholas@2224 504 return node;
nicholas@2538 505 };
nicholas@2538 506 };
nicholas@2538 507
nicholas@2538 508 this.metricNode = function () {
nicholas@2224 509 this.enabled = [];
nicholas@2538 510 this.decode = function (parent, xml) {
nicholas@2224 511 var children = xml.getElementsByTagName('metricenable');
nicholas@2538 512 for (var i in children) {
nicholas@2684 513 if (isNaN(Number(i)) === true) {
nicholas@2538 514 break;
nicholas@2538 515 }
nicholas@2224 516 this.enabled.push(children[i].textContent);
nicholas@2224 517 }
nicholas@2684 518 };
nicholas@2538 519 this.encode = function (doc) {
nicholas@2224 520 var node = doc.createElement('metric');
nicholas@2538 521 for (var i in this.enabled) {
nicholas@2684 522 if (isNaN(Number(i)) === true) {
nicholas@2538 523 break;
nicholas@2538 524 }
nicholas@2224 525 var child = doc.createElement('metricenable');
nicholas@2224 526 child.textContent = this.enabled[i];
nicholas@2224 527 node.appendChild(child);
nicholas@2224 528 }
nicholas@2224 529 return node;
nicholas@2684 530 };
nicholas@2684 531 };
nicholas@2538 532
nicholas@2538 533 this.page = function (specification) {
nicholas@2538 534 this.presentedId = undefined;
nicholas@2538 535 this.id = undefined;
nicholas@2470 536 this.title = undefined;
nicholas@2538 537 this.hostURL = undefined;
nicholas@2538 538 this.randomiseOrder = undefined;
nicholas@2538 539 this.loop = undefined;
nicholas@2688 540 this.outsideReference = undefined;
nicholas@2688 541 this.loudness = undefined;
nicholas@2688 542 this.label = undefined;
nicholas@2688 543 this.labelStart = undefined;
nicholas@2688 544 this.preTest = undefined;
nicholas@2688 545 this.postTest = undefined;
nicholas@2538 546 this.interfaces = [];
nicholas@2688 547 this.playOne = undefined;
nicholas@2688 548 this.restrictMovement = undefined;
n@2713 549 this.position = undefined;
nicholas@2538 550 this.commentBoxPrefix = "Comment on track";
nicholas@2538 551 this.audioElements = [];
nicholas@2538 552 this.commentQuestions = [];
nicholas@2687 553 this.schema = schemaRoot.getAllElementsByName("page")[0];
nicholas@2224 554 this.specification = specification;
nicholas@2688 555 this.parent = undefined;
nicholas@2538 556
nicholas@2538 557 this.decode = function (parent, xml) {
nicholas@2224 558 this.parent = parent;
nicholas@2538 559 var attributeMap = this.schema.getAllElementsByTagName('xs:attribute');
nicholas@2684 560 var i, node;
nicholas@2684 561 for (i = 0; i < attributeMap.length; i++) {
nicholas@2538 562 var attributeName = attributeMap[i].getAttribute('name') || attributeMap[i].getAttribute('ref');
nicholas@2538 563 var projectAttr = xml.getAttribute(attributeName);
nicholas@2692 564 projectAttr = processAttribute(projectAttr, attributeMap[i]);
nicholas@2687 565 if (projectAttr !== null) {
nicholas@2687 566 this[attributeName] = projectAttr;
nicholas@2538 567 }
nicholas@2538 568 }
nicholas@2538 569
nicholas@2470 570 // Get the title
nicholas@2470 571 var title = xml.getElementsByTagName('title');
nicholas@2684 572 if (title.length !== 0 && title[0].parentElement == xml) {
nicholas@2470 573 this.title = title[0].textContent;
nicholas@2470 574 }
nicholas@2538 575
nicholas@2538 576 // Get the Comment Box Prefix
nicholas@2538 577 var CBP = xml.getElementsByTagName('commentboxprefix');
nicholas@2684 578 if (CBP.length !== 0 && CBP[0].parentElement == xml) {
nicholas@2538 579 this.commentBoxPrefix = CBP[0].textContent;
nicholas@2538 580 }
nicholas@2538 581
nicholas@2538 582 // Now decode the interfaces
nicholas@2538 583 var interfaceNode = xml.getElementsByTagName('interface');
nicholas@2684 584 for (i = 0; i < interfaceNode.length; i++) {
nicholas@2684 585 node = new parent.interfaceNode(this.specification);
nicholas@2538 586 node.decode(this, interfaceNode[i], parent.schema.getAllElementsByName('interface')[1]);
nicholas@2538 587 this.interfaces.push(node);
nicholas@2538 588 }
nicholas@2538 589
nicholas@2538 590 // Now process the survey node options
nicholas@2538 591 var survey = xml.getElementsByTagName('survey');
nicholas@2538 592 var surveySchema = parent.schema.getAllElementsByName('survey')[0];
nicholas@2684 593 for (i = 0; i < survey.length; i++) {
nicholas@2538 594 var location = survey[i].getAttribute('location');
nicholas@2538 595 if (location == 'pre' || location == 'before') {
nicholas@2688 596 if (this.preTest !== undefined) {
nicholas@2538 597 this.errors.push("Already a pre/before test survey defined! Ignoring second!!");
nicholas@2538 598 } else {
nicholas@2538 599 this.preTest = new parent.surveyNode(this.specification);
nicholas@2538 600 this.preTest.decode(parent, survey[i], surveySchema);
nicholas@2538 601 }
nicholas@2538 602 } else if (location == 'post' || location == 'after') {
nicholas@2688 603 if (this.postTest !== undefined) {
nicholas@2538 604 this.errors.push("Already a post/after test survey defined! Ignoring second!!");
nicholas@2538 605 } else {
nicholas@2538 606 this.postTest = new parent.surveyNode(this.specification);
nicholas@2538 607 this.postTest.decode(parent, survey[i], surveySchema);
nicholas@2538 608 }
nicholas@2538 609 }
nicholas@2538 610 }
nicholas@2538 611
nicholas@2538 612 // Now process the audioelement tags
nicholas@2538 613 var audioElements = xml.getElementsByTagName('audioelement');
nicholas@2684 614 for (i = 0; i < audioElements.length; i++) {
nicholas@2684 615 var audioNode = new this.audioElementNode(this.specification);
nicholas@2684 616 audioNode.decode(this, audioElements[i]);
nicholas@2684 617 this.audioElements.push(audioNode);
nicholas@2538 618 }
nicholas@2538 619
nicholas@2538 620 // Now decode the commentquestions
n@2579 621 var cqNode = xml.getElementsByTagName('commentquestions');
nicholas@2684 622 if (cqNode.length !== 0) {
n@2579 623 cqNode = cqNode[0];
nicholas@2644 624 var commentQuestion = cqNode.firstElementChild;
nicholas@2644 625 while (commentQuestion) {
nicholas@2684 626 node = new this.commentQuestionNode(this.specification);
nicholas@2644 627 node.decode(parent, commentQuestion);
n@2579 628 this.commentQuestions.push(node);
nicholas@2644 629 commentQuestion = commentQuestion.nextElementSibling;
n@2579 630 }
nicholas@2538 631 }
nicholas@2538 632 };
nicholas@2538 633
nicholas@2538 634 this.encode = function (root) {
nicholas@2538 635 var AHNode = root.createElement("page");
nicholas@2224 636 // First decode the attributes
nicholas@2224 637 var attributes = this.schema.getAllElementsByTagName('xs:attribute');
nicholas@2684 638 var i;
nicholas@2684 639 for (i = 0; i < attributes.length; i++) {
nicholas@2224 640 var name = attributes[i].getAttribute("name");
nicholas@2688 641 if (name === null) {
nicholas@2224 642 name = attributes[i].getAttribute("ref");
nicholas@2224 643 }
nicholas@2685 644 if (this[name] !== undefined || attributes[i].getAttribute("use") == "required") {
nicholas@2685 645 AHNode.setAttribute(name, this[name]);
nicholas@2224 646 }
nicholas@2224 647 }
nicholas@2224 648 // <commentboxprefix>
nicholas@2224 649 var commentboxprefix = root.createElement("commentboxprefix");
nicholas@2224 650 commentboxprefix.textContent = this.commentBoxPrefix;
nicholas@2224 651 AHNode.appendChild(commentboxprefix);
nicholas@2538 652
nicholas@2684 653 for (i = 0; i < this.interfaces.length; i++) {
nicholas@2538 654 AHNode.appendChild(this.interfaces[i].encode(root));
nicholas@2538 655 }
nicholas@2538 656
nicholas@2684 657 for (i = 0; i < this.audioElements.length; i++) {
nicholas@2538 658 AHNode.appendChild(this.audioElements[i].encode(root));
nicholas@2538 659 }
nicholas@2538 660 // Create <CommentQuestion>
nicholas@2684 661 for (i = 0; i < this.commentQuestions.length; i++) {
nicholas@2538 662 AHNode.appendChild(this.commentQuestions[i].encode(root));
nicholas@2538 663 }
nicholas@2538 664
nicholas@2538 665 AHNode.appendChild(this.preTest.encode(root));
nicholas@2224 666 AHNode.appendChild(this.postTest.encode(root));
nicholas@2538 667 return AHNode;
nicholas@2538 668 };
nicholas@2538 669
nicholas@2538 670 this.commentQuestionNode = function (specification) {
nicholas@2688 671 this.id = undefined;
nicholas@2224 672 this.name = undefined;
nicholas@2538 673 this.type = undefined;
nicholas@2538 674 this.statement = undefined;
nicholas@2687 675 this.schema = schemaRoot.getAllElementsByName('commentquestion')[0];
nicholas@2538 676 this.decode = function (parent, xml) {
nicholas@2538 677 this.id = xml.id;
nicholas@2224 678 this.name = xml.getAttribute('name');
nicholas@2688 679 if (this.name === null) {
nicholas@2688 680 this.name = undefined;
nicholas@2688 681 }
n@2579 682 switch (xml.nodeName) {
n@2579 683 case "commentradio":
n@2579 684 this.type = "radio";
n@2579 685 this.options = [];
n@2579 686 break;
n@2579 687 case "commentcheckbox":
n@2579 688 this.type = "checkbox";
n@2579 689 this.options = [];
n@2579 690 break;
n@2579 691 case "commentslider":
n@2579 692 this.type = "slider";
n@2579 693 this.min = undefined;
n@2579 694 this.max = undefined;
n@2579 695 this.step = undefined;
n@2579 696 break;
n@2579 697 case "commentquestion":
n@2579 698 this.type = "question";
n@2579 699 break;
nicholas@2686 700 default:
nicholas@2686 701 throw ("Unknown comment type " + xml.nodeName);
n@2579 702 }
nicholas@2538 703 this.statement = xml.getElementsByTagName('statement')[0].textContent;
n@2579 704 if (this.type == "radio" || this.type == "checkbox") {
n@2579 705 var optNodes = xml.getElementsByTagName('option');
n@2579 706 for (var i = 0; i < optNodes.length; i++) {
n@2579 707 var optNode = optNodes[i];
n@2579 708 this.options.push({
n@2579 709 name: optNode.getAttribute('name'),
n@2579 710 text: optNode.textContent
n@2579 711 });
n@2579 712 }
n@2579 713 }
n@2579 714 if (this.type == "slider") {
n@2579 715 this.min = Number(xml.getAttribute("min"));
n@2579 716 this.max = Number(xml.getAttribute("max"));
n@2579 717 this.step = Number(xml.getAttribute("step"));
nicholas@2684 718 if (this.step === undefined) {
n@2579 719 this.step = 1;
n@2579 720 }
n@2579 721 this.value = Number(xml.getAttribute("value"));
nicholas@2684 722 if (this.value === undefined) {
nicholas@2684 723 this.value = this.min;
n@2579 724 }
n@2580 725 this.leftText = xml.getElementsByTagName("minText");
n@2580 726 if (this.leftText && this.leftText.length > 0) {
n@2580 727 this.leftText = this.leftText[0].textContent;
n@2580 728 } else {
n@2580 729 this.leftText = "";
n@2580 730 }
n@2580 731 this.rightText = xml.getElementsByTagName("maxText");
n@2580 732 if (this.rightText && this.rightText.length > 0) {
n@2580 733 this.rightText = this.rightText[0].textContent;
n@2580 734 } else {
n@2580 735 this.rightText = "";
n@2580 736 }
nicholas@2538 737 }
nicholas@2538 738 };
nicholas@2538 739
nicholas@2538 740 this.encode = function (root) {
n@2579 741 var node;
n@2579 742 switch (this.type) {
n@2579 743 case "radio":
n@2579 744 node = root.createElement("commentradio");
n@2579 745 break;
n@2579 746 case "checkbox":
n@2579 747 node = root.createElement("commentcheckbox");
n@2579 748 break;
n@2579 749 case "slider":
n@2579 750 node = root.createElement("commentslider");
n@2579 751 break;
n@2579 752 case "question":
n@2579 753 node = root.createElement("commentquestion");
n@2579 754 break;
nicholas@2686 755 default:
nicholas@2686 756 throw ("Unknown type " + this.type);
n@2579 757 }
nicholas@2224 758 node.id = this.id;
nicholas@2538 759 node.setAttribute("type", this.type);
nicholas@2684 760 if (this.name !== undefined) {
nicholas@2538 761 node.setAttribute("name", this.name);
nicholas@2538 762 }
nicholas@2224 763 var statement = root.createElement("statement");
nicholas@2224 764 statement.textContent = this.statement;
nicholas@2224 765 node.appendChild(statement);
n@2579 766 if (this.type == "radio" || this.type == "checkbox") {
nicholas@2684 767 this.options.forEach(function (option) {
n@2579 768 var child = root.createElement("option");
n@2579 769 child.setAttribute("name", option.name);
n@2579 770 child.textContent = option.text;
n@2579 771 node.appendChild(child);
nicholas@2684 772 });
n@2579 773 }
n@2579 774 if (this.type == "slider") {
n@2579 775 node.setAttribute("min", this.min);
n@2579 776 node.setAttribute("max", this.max);
n@2579 777 if (this.step !== 1) {
n@2579 778 node.setAttribute("step", this.step);
n@2579 779 }
n@2579 780 if (this.value !== this.min) {
n@2579 781 node.setAttribute("value", this.value);
n@2579 782 }
n@2580 783 if (this.leftText.length > 0) {
n@2580 784 var leftText = root.createElement("minText");
n@2580 785 leftText.textContent = this.leftText;
n@2580 786 node.appendChild(leftText);
n@2580 787 }
n@2580 788 if (this.rightText.length > 0) {
n@2580 789 var rightText = root.createElement("maxText");
n@2580 790 rightText.textContent = this.rightText;
n@2580 791 node.appendChild(rightText);
n@2580 792 }
nicholas@2224 793 }
nicholas@2224 794 return node;
nicholas@2538 795 };
nicholas@2538 796 };
nicholas@2538 797
nicholas@2538 798 this.audioElementNode = function (specification) {
nicholas@2688 799 this.url = undefined;
nicholas@2688 800 this.id = undefined;
nicholas@2688 801 this.name = undefined;
nicholas@2688 802 this.parent = undefined;
nicholas@2688 803 this.type = undefined;
nicholas@2688 804 this.marker = undefined;
nicholas@2538 805 this.enforce = false;
nicholas@2538 806 this.gain = 0.0;
nicholas@2688 807 this.label = undefined;
nicholas@2460 808 this.startTime = undefined;
nicholas@2460 809 this.stopTime = undefined;
nicholas@2614 810 this.sampleRate = undefined;
nicholas@2777 811 this.image = undefined;
nicholas@2614 812 this.alternatives = [];
nicholas@2687 813 this.schema = schemaRoot.getAllElementsByName('audioelement')[0];
nicholas@2688 814 this.parent = undefined;
nicholas@2538 815 this.decode = function (parent, xml) {
nicholas@2538 816 this.parent = parent;
nicholas@2538 817 var attributeMap = this.schema.getAllElementsByTagName('xs:attribute');
nicholas@2538 818 for (var i = 0; i < attributeMap.length; i++) {
nicholas@2538 819 var attributeName = attributeMap[i].getAttribute('name') || attributeMap[i].getAttribute('ref');
nicholas@2538 820 var projectAttr = xml.getAttribute(attributeName);
nicholas@2692 821 projectAttr = processAttribute(projectAttr, attributeMap[i]);
nicholas@2687 822 if (projectAttr !== null) {
nicholas@2687 823 this[attributeName] = projectAttr;
nicholas@2538 824 }
nicholas@2538 825 }
nicholas@2614 826 // Get the alternative nodes
nicholas@2614 827 var child = xml.firstElementChild;
nicholas@2614 828 while (child) {
nicholas@2614 829 if (child.nodeName == "alternative") {
nicholas@2614 830 this.alternatives.push({
nicholas@2614 831 'url': child.getAttribute("url"),
nicholas@2614 832 'sampleRate': child.getAttribute("sampleRate")
nicholas@2614 833 });
nicholas@2614 834 }
nicholas@2614 835 child = child.nextElementSibling;
nicholas@2614 836 }
nicholas@2538 837
nicholas@2538 838 };
nicholas@2538 839 this.encode = function (root) {
nicholas@2538 840 var AENode = root.createElement("audioelement");
nicholas@2538 841 var attributes = this.schema.getAllElementsByTagName('xs:attribute');
nicholas@2538 842 for (var i = 0; i < attributes.length; i++) {
nicholas@2224 843 var name = attributes[i].getAttribute("name");
nicholas@2688 844 if (name === null) {
nicholas@2224 845 name = attributes[i].getAttribute("ref");
nicholas@2224 846 }
nicholas@2685 847 if (this[name] !== undefined || attributes[i].getAttribute("use") == "required") {
nicholas@2685 848 AENode.setAttribute(name, this[name]);
nicholas@2224 849 }
nicholas@2224 850 }
nicholas@2614 851 this.alternatives.forEach(function (alt) {
nicholas@2614 852 var node = root.createElement("alternative");
nicholas@2614 853 node.setAttribute("url", alt.url);
nicholas@2614 854 node.setAttribute("sampleRate", alt.sampleRate);
nicholas@2614 855 AENode.appendChild(node);
nicholas@2614 856 });
nicholas@2538 857 return AENode;
nicholas@2538 858 };
nicholas@2538 859 };
nicholas@2538 860 };
b@2437 861 }