nicholas@2224
|
1 function Specification() {
|
nicholas@2538
|
2 // Handles the decoding of the project specification XML into a simple JavaScript Object.
|
nicholas@2538
|
3
|
nicholas@2378
|
4 // <setup> attributes
|
nicholas@2538
|
5 this.interface = null;
|
nicholas@2538
|
6 this.projectReturn = null;
|
nicholas@2303
|
7 this.returnURL = null;
|
nicholas@2538
|
8 this.randomiseOrder = null;
|
nicholas@2538
|
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@2538
|
17
|
nicholas@2378
|
18 // nodes
|
nicholas@2378
|
19 this.metrics = null;
|
nicholas@2378
|
20 this.preTest = undefined;
|
nicholas@2378
|
21 this.postTest = undefined;
|
nicholas@2538
|
22 this.pages = [];
|
nicholas@2538
|
23 this.interfaces = null;
|
nicholas@2538
|
24 this.errors = [];
|
nicholas@2538
|
25 this.schema = null;
|
nicholas@2224
|
26 this.exitText = "Thank you.";
|
nicholas@2538
|
27
|
nicholas@2538
|
28 this.processAttribute = function (attribute, schema, schemaRoot) {
|
nicholas@2538
|
29 // attribute is the string returned from getAttribute on the XML
|
nicholas@2538
|
30 // schema is the <xs:attribute> node
|
nicholas@2538
|
31 if (schema.getAttribute('name') == undefined && schema.getAttribute('ref') != undefined) {
|
nicholas@2538
|
32 schema = schemaRoot.getAllElementsByName(schema.getAttribute('ref'))[0];
|
nicholas@2538
|
33 }
|
nicholas@2538
|
34 var defaultOpt = schema.getAttribute('default');
|
nicholas@2538
|
35 if (attribute == null) {
|
nicholas@2538
|
36 attribute = defaultOpt;
|
nicholas@2538
|
37 }
|
nicholas@2538
|
38 var dataType = schema.getAttribute('type');
|
nicholas@2538
|
39 if (typeof dataType == "string") {
|
nicholas@2538
|
40 dataType = dataType.substr(3);
|
nicholas@2538
|
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@2538
|
54 if (attribute == null) {
|
nicholas@2538
|
55 return attribute;
|
nicholas@2538
|
56 }
|
nicholas@2538
|
57 switch (dataType) {
|
nicholas@2538
|
58 case "boolean":
|
nicholas@2538
|
59 if (attribute == 'true') {
|
nicholas@2538
|
60 attribute = true;
|
nicholas@2538
|
61 } else {
|
nicholas@2538
|
62 attribute = false;
|
nicholas@2538
|
63 }
|
nicholas@2538
|
64 break;
|
nicholas@2538
|
65 case "negativeInteger":
|
nicholas@2538
|
66 case "positiveInteger":
|
nicholas@2538
|
67 case "nonNegativeInteger":
|
nicholas@2538
|
68 case "nonPositiveInteger":
|
nicholas@2538
|
69 case "integer":
|
nicholas@2538
|
70 case "decimal":
|
nicholas@2538
|
71 case "short":
|
nicholas@2538
|
72 attribute = Number(attribute);
|
nicholas@2538
|
73 break;
|
nicholas@2538
|
74 case "string":
|
nicholas@2538
|
75 default:
|
nicholas@2538
|
76 attribute = String(attribute);
|
nicholas@2538
|
77 break;
|
nicholas@2538
|
78 }
|
nicholas@2538
|
79 return attribute;
|
nicholas@2538
|
80 };
|
nicholas@2538
|
81
|
nicholas@2538
|
82 this.decode = function (projectXML) {
|
nicholas@2538
|
83 this.errors = [];
|
nicholas@2538
|
84 // projectXML - DOM Parsed document
|
nicholas@2538
|
85 this.projectXML = projectXML.childNodes[0];
|
nicholas@2538
|
86 var setupNode = projectXML.getElementsByTagName('setup')[0];
|
nicholas@2538
|
87 var schemaSetup = this.schema.getAllElementsByName('setup')[0];
|
nicholas@2538
|
88 // First decode the attributes
|
nicholas@2538
|
89 var attributes = schemaSetup.getAllElementsByTagName('xs:attribute');
|
nicholas@2538
|
90 for (var i = 0; i < attributes.length; i++) {
|
nicholas@2538
|
91 var attributeName = attributes[i].getAttribute('name') || attributes[i].getAttribute('ref');
|
nicholas@2538
|
92 var projectAttr = setupNode.getAttribute(attributeName);
|
nicholas@2538
|
93 projectAttr = this.processAttribute(projectAttr, attributes[i], this.schema);
|
nicholas@2538
|
94 switch (typeof projectAttr) {
|
nicholas@2538
|
95 case "number":
|
nicholas@2538
|
96 case "boolean":
|
nicholas@2538
|
97 eval('this.' + attributeName + ' = ' + projectAttr);
|
nicholas@2538
|
98 break;
|
nicholas@2538
|
99 case "string":
|
nicholas@2538
|
100 eval('this.' + attributeName + ' = "' + projectAttr + '"');
|
nicholas@2538
|
101 break;
|
nicholas@2538
|
102 }
|
nicholas@2538
|
103
|
nicholas@2538
|
104 }
|
nicholas@2538
|
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@2538
|
110
|
nicholas@2538
|
111 this.metrics = new this.metricNode();
|
nicholas@2538
|
112
|
nicholas@2538
|
113 this.metrics.decode(this, setupNode.getElementsByTagName('metric')[0]);
|
nicholas@2538
|
114
|
nicholas@2538
|
115 // Now process the survey node options
|
nicholas@2538
|
116 var survey = setupNode.getElementsByTagName('survey');
|
nicholas@2538
|
117 for (var i = 0; i < survey.length; i++) {
|
nicholas@2538
|
118 var location = survey[i].getAttribute('location');
|
nicholas@2538
|
119 switch (location) {
|
nicholas@2257
|
120 case 'pre':
|
nicholas@2257
|
121 case 'before':
|
nicholas@2538
|
122 this.preTest = new this.surveyNode(this);
|
nicholas@2538
|
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@2538
|
128 this.postTest.decode(this, survey[i]);
|
nicholas@2257
|
129 break;
|
nicholas@2257
|
130 }
|
nicholas@2538
|
131 }
|
nicholas@2538
|
132
|
nicholas@2538
|
133 var interfaceNode = setupNode.getElementsByTagName('interface');
|
nicholas@2538
|
134 if (interfaceNode.length > 1) {
|
nicholas@2538
|
135 this.errors.push("Only one <interface> node in the <setup> node allowed! Others except first ingnored!");
|
nicholas@2538
|
136 }
|
nicholas@2538
|
137 this.interfaces = new this.interfaceNode(this);
|
nicholas@2538
|
138 if (interfaceNode.length != 0) {
|
nicholas@2538
|
139 interfaceNode = interfaceNode[0];
|
nicholas@2538
|
140 this.interfaces.decode(this, interfaceNode, this.schema.getAllElementsByName('interface')[1]);
|
nicholas@2538
|
141 }
|
nicholas@2538
|
142
|
nicholas@2538
|
143 // Page tags
|
nicholas@2538
|
144 var pageTags = projectXML.getElementsByTagName('page');
|
nicholas@2538
|
145 var pageSchema = this.schema.getAllElementsByName('page')[0];
|
nicholas@2538
|
146 for (var i = 0; i < pageTags.length; i++) {
|
nicholas@2538
|
147 var node = new this.page(this);
|
nicholas@2538
|
148 node.decode(this, pageTags[i], pageSchema);
|
nicholas@2538
|
149 this.pages.push(node);
|
nicholas@2538
|
150 }
|
nicholas@2538
|
151 };
|
nicholas@2538
|
152
|
nicholas@2538
|
153 this.encode = function () {
|
nicholas@2538
|
154 var RootDocument = document.implementation.createDocument(null, "waet");
|
nicholas@2538
|
155 var root = RootDocument.firstChild;
|
nicholas@2538
|
156 root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
|
nicholas@2538
|
157 root.setAttribute("xsi:noNamespaceSchemaLocation", "test-schema.xsd");
|
nicholas@2538
|
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@2538
|
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@2538
|
168 if (eval("this." + name + " != undefined") || attributes[i].getAttribute("use") == "required") {
|
nicholas@2538
|
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@2538
|
183 for (var page of this.pages) {
|
nicholas@2224
|
184 root.appendChild(page.encode(RootDocument));
|
nicholas@2224
|
185 }
|
nicholas@2538
|
186 return RootDocument;
|
nicholas@2538
|
187 };
|
nicholas@2538
|
188
|
nicholas@2538
|
189 this.surveyNode = function (specification) {
|
nicholas@2538
|
190 this.location = null;
|
nicholas@2538
|
191 this.options = [];
|
nicholas@2224
|
192 this.parent = null;
|
nicholas@2538
|
193 this.schema = specification.schema.getAllElementsByName('survey')[0];
|
nicholas@2224
|
194 this.specification = specification;
|
nicholas@2538
|
195
|
nicholas@2538
|
196 this.OptionNode = function (specification) {
|
nicholas@2538
|
197 this.type = undefined;
|
nicholas@2538
|
198 this.schema = specification.schema.getAllElementsByName('surveyentry')[0];
|
nicholas@2538
|
199 this.id = undefined;
|
nicholas@2224
|
200 this.name = undefined;
|
nicholas@2538
|
201 this.mandatory = undefined;
|
nicholas@2538
|
202 this.statement = undefined;
|
nicholas@2538
|
203 this.boxsize = undefined;
|
nicholas@2538
|
204 this.options = [];
|
nicholas@2538
|
205 this.min = undefined;
|
nicholas@2538
|
206 this.max = undefined;
|
nicholas@2538
|
207 this.step = undefined;
|
nicholas@2464
|
208 this.conditions = [];
|
nicholas@2538
|
209
|
nicholas@2538
|
210 this.decode = function (parent, child) {
|
nicholas@2538
|
211 var attributeMap = this.schema.getAllElementsByTagName('xs:attribute');
|
nicholas@2538
|
212 for (var i in attributeMap) {
|
nicholas@2538
|
213 if (isNaN(Number(i)) == true) {
|
nicholas@2538
|
214 break;
|
nicholas@2538
|
215 }
|
nicholas@2538
|
216 var attributeName = attributeMap[i].getAttribute('name') || attributeMap[i].getAttribute('ref');
|
nicholas@2538
|
217 var projectAttr = child.getAttribute(attributeName);
|
nicholas@2538
|
218 projectAttr = parent.processAttribute(projectAttr, attributeMap[i], parent.schema);
|
nicholas@2538
|
219 switch (typeof projectAttr) {
|
nicholas@2538
|
220 case "number":
|
nicholas@2538
|
221 case "boolean":
|
nicholas@2538
|
222 eval('this.' + attributeName + ' = ' + projectAttr);
|
nicholas@2538
|
223 break;
|
nicholas@2538
|
224 case "string":
|
nicholas@2538
|
225 eval('this.' + attributeName + ' = "' + projectAttr + '"');
|
nicholas@2538
|
226 break;
|
nicholas@2538
|
227 }
|
nicholas@2538
|
228 }
|
nicholas@2538
|
229 this.statement = child.getElementsByTagName('statement')[0].textContent;
|
nicholas@2538
|
230 if (this.type == "checkbox" || this.type == "radio") {
|
nicholas@2538
|
231 var children = child.getElementsByTagName('option');
|
nicholas@2538
|
232 if (children.length == null) {
|
nicholas@2538
|
233 console.log('Malformed' + child.nodeName + 'entry');
|
nicholas@2538
|
234 this.statement = 'Malformed' + child.nodeName + 'entry';
|
nicholas@2538
|
235 this.type = 'statement';
|
nicholas@2538
|
236 } else {
|
nicholas@2538
|
237 this.options = [];
|
nicholas@2538
|
238 for (var i = 0; i < children.length; i++) {
|
nicholas@2538
|
239 this.options.push({
|
nicholas@2538
|
240 name: children[i].getAttribute('name'),
|
nicholas@2538
|
241 text: children[i].textContent
|
nicholas@2538
|
242 });
|
nicholas@2538
|
243 }
|
nicholas@2538
|
244 }
|
nicholas@2538
|
245 }
|
nicholas@2464
|
246 var conditionElements = child.getElementsByTagName("conditional");
|
nicholas@2538
|
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@2538
|
257 };
|
nicholas@2538
|
258
|
nicholas@2538
|
259 this.exportXML = function (doc) {
|
nicholas@2538
|
260 var node = doc.createElement('surveyentry');
|
nicholas@2538
|
261 node.setAttribute('type', this.type);
|
nicholas@2538
|
262 var statement = doc.createElement('statement');
|
nicholas@2538
|
263 statement.textContent = this.statement;
|
nicholas@2538
|
264 node.appendChild(statement);
|
nicholas@2224
|
265 node.id = this.id;
|
nicholas@2538
|
266 if (this.name != undefined) {
|
nicholas@2538
|
267 node.setAttribute("name", this.name);
|
nicholas@2538
|
268 }
|
nicholas@2538
|
269 if (this.mandatory != undefined) {
|
nicholas@2538
|
270 node.setAttribute("mandatory", this.mandatory);
|
nicholas@2538
|
271 }
|
nicholas@2224
|
272 node.id = this.id;
|
nicholas@2538
|
273 if (this.name != undefined) {
|
nicholas@2538
|
274 node.setAttribute("name", this.name);
|
nicholas@2538
|
275 }
|
nicholas@2538
|
276 switch (this.type) {
|
nicholas@2224
|
277 case "checkbox":
|
nicholas@2224
|
278 case "radio":
|
nicholas@2538
|
279 for (var i = 0; i < this.options.length; i++) {
|
nicholas@2224
|
280 var option = this.options[i];
|
nicholas@2224
|
281 var optionNode = doc.createElement("option");
|
nicholas@2538
|
282 optionNode.setAttribute("name", option.name);
|
nicholas@2224
|
283 optionNode.textContent = option.text;
|
nicholas@2224
|
284 node.appendChild(optionNode);
|
nicholas@2224
|
285 }
|
nicholas@2224
|
286 case "number":
|
nicholas@2538
|
287 if (this.min != undefined) {
|
nicholas@2538
|
288 node.setAttribute("min", this.min);
|
nicholas@2538
|
289 }
|
nicholas@2538
|
290 if (this.max != undefined) {
|
nicholas@2538
|
291 node.setAttribute("max", this.max);
|
nicholas@2538
|
292 }
|
nicholas@2224
|
293 case "question":
|
nicholas@2538
|
294 if (this.boxsize != undefined) {
|
nicholas@2538
|
295 node.setAttribute("boxsize", this.boxsize);
|
nicholas@2538
|
296 }
|
nicholas@2538
|
297 if (this.mandatory != undefined) {
|
nicholas@2538
|
298 node.setAttribute("mandatory", this.mandatory);
|
nicholas@2538
|
299 }
|
nicholas@2224
|
300 default:
|
nicholas@2224
|
301 break;
|
nicholas@2224
|
302 }
|
nicholas@2465
|
303 for (var condition of this.conditions) {
|
nicholas@2465
|
304 var conditionDOM = doc.createElement("conditional");
|
nicholas@2538
|
305 conditionDOM.setAttribute("check", condition.check);
|
nicholas@2538
|
306 conditionDOM.setAttribute("value", condition.value);
|
nicholas@2538
|
307 conditionDOM.setAttribute("jumpToOnPass", condition.jumpToOnPass);
|
nicholas@2538
|
308 conditionDOM.setAttribute("jumpToOnFail", condition.jumpToOnFail);
|
nicholas@2465
|
309 node.appendChild(conditionDOM);
|
nicholas@2465
|
310 }
|
nicholas@2538
|
311 return node;
|
nicholas@2538
|
312 };
|
nicholas@2538
|
313 };
|
nicholas@2538
|
314 this.decode = function (parent, xml) {
|
nicholas@2224
|
315 this.parent = parent;
|
nicholas@2538
|
316 this.location = xml.getAttribute('location');
|
nicholas@2538
|
317 if (this.location == 'before') {
|
nicholas@2538
|
318 this.location = 'pre';
|
nicholas@2538
|
319 } else if (this.location == 'after') {
|
nicholas@2538
|
320 this.location = 'post';
|
nicholas@2538
|
321 }
|
nicholas@2260
|
322 var children = xml.getAllElementsByTagName('surveyentry');
|
nicholas@2538
|
323 for (var i = 0; i < children.length; i++) {
|
nicholas@2538
|
324 var node = new this.OptionNode(this.specification);
|
nicholas@2538
|
325 node.decode(parent, children[i]);
|
nicholas@2538
|
326 this.options.push(node);
|
nicholas@2538
|
327 }
|
nicholas@2257
|
328 if (this.options.length == 0) {
|
nicholas@2257
|
329 console.log("Empty survey node");
|
nicholas@2257
|
330 console.log(this);
|
nicholas@2257
|
331 }
|
nicholas@2538
|
332 };
|
nicholas@2538
|
333 this.encode = function (doc) {
|
nicholas@2538
|
334 var node = doc.createElement('survey');
|
nicholas@2538
|
335 node.setAttribute('location', this.location);
|
nicholas@2538
|
336 for (var i = 0; i < this.options.length; i++) {
|
nicholas@2538
|
337 node.appendChild(this.options[i].exportXML(doc));
|
nicholas@2538
|
338 }
|
nicholas@2538
|
339 return node;
|
nicholas@2538
|
340 };
|
nicholas@2538
|
341 };
|
nicholas@2538
|
342
|
nicholas@2538
|
343 this.interfaceNode = function (specification) {
|
nicholas@2538
|
344 this.title = null;
|
nicholas@2538
|
345 this.name = null;
|
nicholas@2538
|
346 this.options = [];
|
nicholas@2538
|
347 this.scales = [];
|
nicholas@2538
|
348 this.schema = specification.schema.getAllElementsByName('interface')[1];
|
nicholas@2538
|
349
|
nicholas@2538
|
350 this.decode = function (parent, xml) {
|
nicholas@2538
|
351 this.name = xml.getAttribute('name');
|
nicholas@2538
|
352 var titleNode = xml.getElementsByTagName('title');
|
nicholas@2538
|
353 if (titleNode.length == 1) {
|
nicholas@2538
|
354 this.title = titleNode[0].textContent;
|
nicholas@2538
|
355 }
|
nicholas@2538
|
356 var interfaceOptionNodes = xml.getElementsByTagName('interfaceoption');
|
nicholas@2538
|
357 // Extract interfaceoption node schema
|
nicholas@2538
|
358 var interfaceOptionNodeSchema = this.schema.getAllElementsByName('interfaceoption')[0];
|
nicholas@2538
|
359 var attributeMap = interfaceOptionNodeSchema.getAllElementsByTagName('xs:attribute');
|
nicholas@2538
|
360 for (var i = 0; i < interfaceOptionNodes.length; i++) {
|
nicholas@2538
|
361 var ioNode = interfaceOptionNodes[i];
|
nicholas@2538
|
362 var option = {};
|
nicholas@2538
|
363 for (var j = 0; j < attributeMap.length; j++) {
|
nicholas@2538
|
364 var attributeName = attributeMap[j].getAttribute('name') || attributeMap[j].getAttribute('ref');
|
nicholas@2538
|
365 var projectAttr = ioNode.getAttribute(attributeName);
|
nicholas@2538
|
366 if (parent.processAttribute) {
|
nicholas@2224
|
367 parent.processAttribute(projectAttr, attributeMap[j], parent.schema)
|
nicholas@2224
|
368 } else {
|
nicholas@2224
|
369 parent.parent.processAttribute(projectAttr, attributeMap[j], parent.parent.schema)
|
nicholas@2224
|
370 }
|
nicholas@2538
|
371 switch (typeof projectAttr) {
|
nicholas@2538
|
372 case "number":
|
nicholas@2538
|
373 case "boolean":
|
nicholas@2538
|
374 eval('option.' + attributeName + ' = ' + projectAttr);
|
nicholas@2538
|
375 break;
|
nicholas@2538
|
376 case "string":
|
nicholas@2538
|
377 eval('option.' + attributeName + ' = "' + projectAttr + '"');
|
nicholas@2538
|
378 break;
|
nicholas@2538
|
379 }
|
nicholas@2538
|
380 }
|
nicholas@2538
|
381 this.options.push(option);
|
nicholas@2538
|
382 }
|
nicholas@2538
|
383
|
nicholas@2538
|
384 // Now the scales nodes
|
nicholas@2538
|
385 var scaleParent = xml.getElementsByTagName('scales');
|
nicholas@2538
|
386 if (scaleParent.length == 1) {
|
nicholas@2538
|
387 scaleParent = scaleParent[0];
|
nicholas@2252
|
388 var scalelabels = scaleParent.getAllElementsByTagName('scalelabel');
|
nicholas@2538
|
389 for (var i = 0; i < scalelabels.length; i++) {
|
nicholas@2538
|
390 this.scales.push({
|
nicholas@2538
|
391 text: scalelabels[i].textContent,
|
nicholas@2538
|
392 position: Number(scalelabels[i].getAttribute('position'))
|
nicholas@2538
|
393 });
|
nicholas@2538
|
394 }
|
nicholas@2538
|
395 }
|
nicholas@2538
|
396 };
|
nicholas@2538
|
397
|
nicholas@2538
|
398 this.encode = function (doc) {
|
nicholas@2538
|
399 var node = doc.createElement("interface");
|
nicholas@2550
|
400 if (typeof name == "string" && name.length > 0)
|
nicholas@2538
|
401 node.setAttribute("name", this.name);
|
nicholas@2243
|
402 if (typeof this.title == "string") {
|
nicholas@2243
|
403 var titleNode = doc.createElement("title");
|
nicholas@2243
|
404 titleNode.textContent = this.title;
|
nicholas@2243
|
405 node.appendChild(titleNode);
|
nicholas@2243
|
406 }
|
nicholas@2538
|
407 for (var option of this.options) {
|
nicholas@2224
|
408 var child = doc.createElement("interfaceoption");
|
nicholas@2538
|
409 child.setAttribute("type", option.type);
|
nicholas@2538
|
410 child.setAttribute("name", option.name);
|
nicholas@2224
|
411 node.appendChild(child);
|
nicholas@2224
|
412 }
|
nicholas@2224
|
413 if (this.scales.length != 0) {
|
nicholas@2224
|
414 var scales = doc.createElement("scales");
|
nicholas@2538
|
415 for (var scale of this.scales) {
|
nicholas@2224
|
416 var child = doc.createElement("scalelabel");
|
nicholas@2538
|
417 child.setAttribute("position", scale.position);
|
nicholas@2224
|
418 child.textContent = scale.text;
|
nicholas@2224
|
419 scales.appendChild(child);
|
nicholas@2224
|
420 }
|
nicholas@2224
|
421 node.appendChild(scales);
|
nicholas@2224
|
422 }
|
nicholas@2224
|
423 return node;
|
nicholas@2538
|
424 };
|
nicholas@2538
|
425 };
|
nicholas@2538
|
426
|
nicholas@2538
|
427 this.metricNode = function () {
|
nicholas@2224
|
428 this.enabled = [];
|
nicholas@2538
|
429 this.decode = function (parent, xml) {
|
nicholas@2224
|
430 var children = xml.getElementsByTagName('metricenable');
|
nicholas@2538
|
431 for (var i in children) {
|
nicholas@2538
|
432 if (isNaN(Number(i)) == true) {
|
nicholas@2538
|
433 break;
|
nicholas@2538
|
434 }
|
nicholas@2224
|
435 this.enabled.push(children[i].textContent);
|
nicholas@2224
|
436 }
|
nicholas@2224
|
437 }
|
nicholas@2538
|
438 this.encode = function (doc) {
|
nicholas@2224
|
439 var node = doc.createElement('metric');
|
nicholas@2538
|
440 for (var i in this.enabled) {
|
nicholas@2538
|
441 if (isNaN(Number(i)) == true) {
|
nicholas@2538
|
442 break;
|
nicholas@2538
|
443 }
|
nicholas@2224
|
444 var child = doc.createElement('metricenable');
|
nicholas@2224
|
445 child.textContent = this.enabled[i];
|
nicholas@2224
|
446 node.appendChild(child);
|
nicholas@2224
|
447 }
|
nicholas@2224
|
448 return node;
|
nicholas@2224
|
449 }
|
nicholas@2224
|
450 }
|
nicholas@2538
|
451
|
nicholas@2538
|
452 this.page = function (specification) {
|
nicholas@2538
|
453 this.presentedId = undefined;
|
nicholas@2538
|
454 this.id = undefined;
|
nicholas@2470
|
455 this.title = undefined;
|
nicholas@2538
|
456 this.hostURL = undefined;
|
nicholas@2538
|
457 this.randomiseOrder = undefined;
|
nicholas@2538
|
458 this.loop = undefined;
|
nicholas@2538
|
459 this.outsideReference = null;
|
nicholas@2538
|
460 this.loudness = null;
|
nicholas@2224
|
461 this.label = null;
|
nicholas@2538
|
462 this.preTest = null;
|
nicholas@2538
|
463 this.postTest = null;
|
nicholas@2538
|
464 this.interfaces = [];
|
n@2426
|
465 this.playOne = null;
|
nicholas@2538
|
466 this.commentBoxPrefix = "Comment on track";
|
nicholas@2538
|
467 this.audioElements = [];
|
nicholas@2538
|
468 this.commentQuestions = [];
|
nicholas@2538
|
469 this.schema = specification.schema.getAllElementsByName("page")[0];
|
nicholas@2224
|
470 this.specification = specification;
|
nicholas@2224
|
471 this.parent = null;
|
nicholas@2538
|
472
|
nicholas@2538
|
473 this.decode = function (parent, xml) {
|
nicholas@2224
|
474 this.parent = parent;
|
nicholas@2538
|
475 var attributeMap = this.schema.getAllElementsByTagName('xs:attribute');
|
nicholas@2538
|
476 for (var i = 0; i < attributeMap.length; i++) {
|
nicholas@2538
|
477 var attributeName = attributeMap[i].getAttribute('name') || attributeMap[i].getAttribute('ref');
|
nicholas@2538
|
478 var projectAttr = xml.getAttribute(attributeName);
|
nicholas@2538
|
479 projectAttr = parent.processAttribute(projectAttr, attributeMap[i], parent.schema);
|
nicholas@2538
|
480 switch (typeof projectAttr) {
|
nicholas@2538
|
481 case "number":
|
nicholas@2538
|
482 case "boolean":
|
nicholas@2538
|
483 eval('this.' + attributeName + ' = ' + projectAttr);
|
nicholas@2538
|
484 break;
|
nicholas@2538
|
485 case "string":
|
nicholas@2538
|
486 eval('this.' + attributeName + ' = "' + projectAttr + '"');
|
nicholas@2538
|
487 break;
|
nicholas@2538
|
488 }
|
nicholas@2538
|
489 }
|
nicholas@2538
|
490
|
nicholas@2470
|
491 // Get the title
|
nicholas@2470
|
492 var title = xml.getElementsByTagName('title');
|
nicholas@2501
|
493 if (title.length != 0 && title[0].parentElement == xml) {
|
nicholas@2470
|
494 this.title = title[0].textContent;
|
nicholas@2470
|
495 }
|
nicholas@2538
|
496
|
nicholas@2538
|
497 // Get the Comment Box Prefix
|
nicholas@2538
|
498 var CBP = xml.getElementsByTagName('commentboxprefix');
|
nicholas@2538
|
499 if (CBP.length != 0 && CBP[0].parentElement == xml) {
|
nicholas@2538
|
500 this.commentBoxPrefix = CBP[0].textContent;
|
nicholas@2538
|
501 }
|
nicholas@2538
|
502
|
nicholas@2538
|
503 // Now decode the interfaces
|
nicholas@2538
|
504 var interfaceNode = xml.getElementsByTagName('interface');
|
nicholas@2538
|
505 for (var i = 0; i < interfaceNode.length; i++) {
|
nicholas@2538
|
506 var node = new parent.interfaceNode(this.specification);
|
nicholas@2538
|
507 node.decode(this, interfaceNode[i], parent.schema.getAllElementsByName('interface')[1]);
|
nicholas@2538
|
508 this.interfaces.push(node);
|
nicholas@2538
|
509 }
|
nicholas@2538
|
510
|
nicholas@2538
|
511 // Now process the survey node options
|
nicholas@2538
|
512 var survey = xml.getElementsByTagName('survey');
|
nicholas@2538
|
513 var surveySchema = parent.schema.getAllElementsByName('survey')[0];
|
nicholas@2538
|
514 for (var i = 0; i < survey.length; i++) {
|
nicholas@2538
|
515 var location = survey[i].getAttribute('location');
|
nicholas@2538
|
516 if (location == 'pre' || location == 'before') {
|
nicholas@2538
|
517 if (this.preTest != null) {
|
nicholas@2538
|
518 this.errors.push("Already a pre/before test survey defined! Ignoring second!!");
|
nicholas@2538
|
519 } else {
|
nicholas@2538
|
520 this.preTest = new parent.surveyNode(this.specification);
|
nicholas@2538
|
521 this.preTest.decode(parent, survey[i], surveySchema);
|
nicholas@2538
|
522 }
|
nicholas@2538
|
523 } else if (location == 'post' || location == 'after') {
|
nicholas@2538
|
524 if (this.postTest != null) {
|
nicholas@2538
|
525 this.errors.push("Already a post/after test survey defined! Ignoring second!!");
|
nicholas@2538
|
526 } else {
|
nicholas@2538
|
527 this.postTest = new parent.surveyNode(this.specification);
|
nicholas@2538
|
528 this.postTest.decode(parent, survey[i], surveySchema);
|
nicholas@2538
|
529 }
|
nicholas@2538
|
530 }
|
nicholas@2538
|
531 }
|
nicholas@2538
|
532
|
nicholas@2538
|
533 // Now process the audioelement tags
|
nicholas@2538
|
534 var audioElements = xml.getElementsByTagName('audioelement');
|
nicholas@2538
|
535 for (var i = 0; i < audioElements.length; i++) {
|
nicholas@2538
|
536 var node = new this.audioElementNode(this.specification);
|
nicholas@2538
|
537 node.decode(this, audioElements[i]);
|
nicholas@2538
|
538 this.audioElements.push(node);
|
nicholas@2538
|
539 }
|
nicholas@2538
|
540
|
nicholas@2538
|
541 // Now decode the commentquestions
|
nicholas@2538
|
542 var commentQuestions = xml.getElementsByTagName('commentquestion');
|
nicholas@2538
|
543 for (var i = 0; i < commentQuestions.length; i++) {
|
nicholas@2538
|
544 var node = new this.commentQuestionNode(this.specification);
|
nicholas@2538
|
545 node.decode(parent, commentQuestions[i]);
|
nicholas@2538
|
546 this.commentQuestions.push(node);
|
nicholas@2538
|
547 }
|
nicholas@2538
|
548 };
|
nicholas@2538
|
549
|
nicholas@2538
|
550 this.encode = function (root) {
|
nicholas@2538
|
551 var AHNode = root.createElement("page");
|
nicholas@2224
|
552 // First decode the attributes
|
nicholas@2224
|
553 var attributes = this.schema.getAllElementsByTagName('xs:attribute');
|
nicholas@2538
|
554 for (var i = 0; i < attributes.length; i++) {
|
nicholas@2224
|
555 var name = attributes[i].getAttribute("name");
|
nicholas@2224
|
556 if (name == undefined) {
|
nicholas@2224
|
557 name = attributes[i].getAttribute("ref");
|
nicholas@2224
|
558 }
|
nicholas@2538
|
559 if (eval("this." + name + " != undefined") || attributes[i].getAttribute("use") == "required") {
|
nicholas@2538
|
560 eval("AHNode.setAttribute('" + name + "',this." + name + ")");
|
nicholas@2224
|
561 }
|
nicholas@2224
|
562 }
|
nicholas@2538
|
563 if (this.loudness != null) {
|
nicholas@2538
|
564 AHNode.setAttribute("loudness", this.loudness);
|
nicholas@2538
|
565 }
|
nicholas@2224
|
566 // <commentboxprefix>
|
nicholas@2224
|
567 var commentboxprefix = root.createElement("commentboxprefix");
|
nicholas@2224
|
568 commentboxprefix.textContent = this.commentBoxPrefix;
|
nicholas@2224
|
569 AHNode.appendChild(commentboxprefix);
|
nicholas@2538
|
570
|
nicholas@2538
|
571 for (var i = 0; i < this.interfaces.length; i++) {
|
nicholas@2538
|
572 AHNode.appendChild(this.interfaces[i].encode(root));
|
nicholas@2538
|
573 }
|
nicholas@2538
|
574
|
nicholas@2538
|
575 for (var i = 0; i < this.audioElements.length; i++) {
|
nicholas@2538
|
576 AHNode.appendChild(this.audioElements[i].encode(root));
|
nicholas@2538
|
577 }
|
nicholas@2538
|
578 // Create <CommentQuestion>
|
nicholas@2538
|
579 for (var i = 0; i < this.commentQuestions.length; i++) {
|
nicholas@2538
|
580 AHNode.appendChild(this.commentQuestions[i].encode(root));
|
nicholas@2538
|
581 }
|
nicholas@2538
|
582
|
nicholas@2538
|
583 AHNode.appendChild(this.preTest.encode(root));
|
nicholas@2224
|
584 AHNode.appendChild(this.postTest.encode(root));
|
nicholas@2538
|
585 return AHNode;
|
nicholas@2538
|
586 };
|
nicholas@2538
|
587
|
nicholas@2538
|
588 this.commentQuestionNode = function (specification) {
|
nicholas@2538
|
589 this.id = null;
|
nicholas@2224
|
590 this.name = undefined;
|
nicholas@2538
|
591 this.type = undefined;
|
nicholas@2538
|
592 this.options = [];
|
nicholas@2538
|
593 this.statement = undefined;
|
nicholas@2538
|
594 this.schema = specification.schema.getAllElementsByName('commentquestion')[0];
|
nicholas@2538
|
595 this.decode = function (parent, xml) {
|
nicholas@2538
|
596 this.id = xml.id;
|
nicholas@2224
|
597 this.name = xml.getAttribute('name');
|
nicholas@2538
|
598 this.type = xml.getAttribute('type');
|
nicholas@2538
|
599 this.statement = xml.getElementsByTagName('statement')[0].textContent;
|
nicholas@2538
|
600 var optNodes = xml.getElementsByTagName('option');
|
nicholas@2538
|
601 for (var i = 0; i < optNodes.length; i++) {
|
nicholas@2538
|
602 var optNode = optNodes[i];
|
nicholas@2538
|
603 this.options.push({
|
nicholas@2538
|
604 name: optNode.getAttribute('name'),
|
nicholas@2538
|
605 text: optNode.textContent
|
nicholas@2538
|
606 });
|
nicholas@2538
|
607 }
|
nicholas@2538
|
608 };
|
nicholas@2538
|
609
|
nicholas@2538
|
610 this.encode = function (root) {
|
nicholas@2538
|
611 var node = root.createElement("commentquestion");
|
nicholas@2224
|
612 node.id = this.id;
|
nicholas@2538
|
613 node.setAttribute("type", this.type);
|
nicholas@2538
|
614 if (this.name != undefined) {
|
nicholas@2538
|
615 node.setAttribute("name", this.name);
|
nicholas@2538
|
616 }
|
nicholas@2224
|
617 var statement = root.createElement("statement");
|
nicholas@2224
|
618 statement.textContent = this.statement;
|
nicholas@2224
|
619 node.appendChild(statement);
|
nicholas@2538
|
620 for (var option of this.options) {
|
nicholas@2224
|
621 var child = root.createElement("option");
|
nicholas@2538
|
622 child.setAttribute("name", option.name);
|
nicholas@2224
|
623 child.textContent = option.text;
|
nicholas@2224
|
624 node.appendChild(child);
|
nicholas@2224
|
625 }
|
nicholas@2224
|
626 return node;
|
nicholas@2538
|
627 };
|
nicholas@2538
|
628 };
|
nicholas@2538
|
629
|
nicholas@2538
|
630 this.audioElementNode = function (specification) {
|
nicholas@2538
|
631 this.url = null;
|
nicholas@2538
|
632 this.id = null;
|
nicholas@2224
|
633 this.name = null;
|
nicholas@2538
|
634 this.parent = null;
|
nicholas@2538
|
635 this.type = null;
|
nicholas@2538
|
636 this.marker = null;
|
nicholas@2538
|
637 this.enforce = false;
|
nicholas@2538
|
638 this.gain = 0.0;
|
nicholas@2409
|
639 this.label = null;
|
nicholas@2460
|
640 this.startTime = undefined;
|
nicholas@2460
|
641 this.stopTime = undefined;
|
nicholas@2538
|
642 this.schema = specification.schema.getAllElementsByName('audioelement')[0];;
|
nicholas@2538
|
643 this.parent = null;
|
nicholas@2538
|
644 this.decode = function (parent, xml) {
|
nicholas@2538
|
645 this.parent = parent;
|
nicholas@2538
|
646 var attributeMap = this.schema.getAllElementsByTagName('xs:attribute');
|
nicholas@2538
|
647 for (var i = 0; i < attributeMap.length; i++) {
|
nicholas@2538
|
648 var attributeName = attributeMap[i].getAttribute('name') || attributeMap[i].getAttribute('ref');
|
nicholas@2538
|
649 var projectAttr = xml.getAttribute(attributeName);
|
nicholas@2538
|
650 projectAttr = parent.parent.processAttribute(projectAttr, attributeMap[i], parent.parent.schema);
|
nicholas@2538
|
651 switch (typeof projectAttr) {
|
nicholas@2538
|
652 case "number":
|
nicholas@2538
|
653 case "boolean":
|
nicholas@2538
|
654 eval('this.' + attributeName + ' = ' + projectAttr);
|
nicholas@2538
|
655 break;
|
nicholas@2538
|
656 case "string":
|
nicholas@2538
|
657 eval('this.' + attributeName + ' = "' + projectAttr + '"');
|
nicholas@2538
|
658 break;
|
nicholas@2538
|
659 }
|
nicholas@2538
|
660 }
|
nicholas@2538
|
661
|
nicholas@2538
|
662 };
|
nicholas@2538
|
663 this.encode = function (root) {
|
nicholas@2538
|
664 var AENode = root.createElement("audioelement");
|
nicholas@2538
|
665 var attributes = this.schema.getAllElementsByTagName('xs:attribute');
|
nicholas@2538
|
666 for (var i = 0; i < attributes.length; i++) {
|
nicholas@2224
|
667 var name = attributes[i].getAttribute("name");
|
nicholas@2224
|
668 if (name == undefined) {
|
nicholas@2224
|
669 name = attributes[i].getAttribute("ref");
|
nicholas@2224
|
670 }
|
nicholas@2538
|
671 if (eval("this." + name + " != undefined") || attributes[i].getAttribute("use") == "required") {
|
nicholas@2538
|
672 eval("AENode.setAttribute('" + name + "',this." + name + ")");
|
nicholas@2224
|
673 }
|
nicholas@2224
|
674 }
|
nicholas@2538
|
675 return AENode;
|
nicholas@2538
|
676 };
|
nicholas@2538
|
677 };
|
nicholas@2538
|
678 };
|
b@2437
|
679 }
|