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