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