annotate js/specification.js @ 3103:d79b9939b19e

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