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