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