changeset 1172:e3b16bb23a50

randomiseOrder a global function. Schema update (hostURL attribute on <pages> not mandatory). Specification node can create XML.
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Wed, 10 Feb 2016 14:46:57 +0000
parents e49c4eeb4fb2
children d4348ae0bc02
files core.js test-schema.xsd test_create/test_create.html
diffstat 3 files changed, 226 insertions(+), 161 deletions(-) [+]
line wrap: on
line diff
--- a/core.js	Wed Feb 10 12:14:25 2016 +0000
+++ b/core.js	Wed Feb 10 14:46:57 2016 +0000
@@ -1724,34 +1724,6 @@
 	this.errors = [];
 	this.schema = null;
 	
-	this.randomiseOrder = function(input)
-	{
-		// This takes an array of information and randomises the order
-		var N = input.length;
-		
-		var inputSequence = []; // For safety purposes: keep track of randomisation
-		for (var counter = 0; counter < N; ++counter) 
-			inputSequence.push(counter) // Fill array
-		var inputSequenceClone = inputSequence.slice(0);
-		
-		var holdArr = [];
-		var outputSequence = [];
-		for (var n=0; n<N; n++)
-		{
-			// First pick a random number
-			var r = Math.random();
-			// Multiply and floor by the number of elements left
-			r = Math.floor(r*input.length);
-			// Pick out that element and delete from the array
-			holdArr.push(input.splice(r,1)[0]);
-			// Do the same with sequence
-			outputSequence.push(inputSequence.splice(r,1)[0]);
-		}
-		console.log(inputSequenceClone.toString()); // print original array to console
-		console.log(outputSequence.toString()); 	// print randomised array to console
-		return holdArr;
-	};
-	
 	this.processAttribute = function(attribute,schema)
 	{
 		// attribute is the string returned from getAttribute on the XML
@@ -1870,11 +1842,37 @@
 	
 	this.encode = function()
 	{
-		var root = document.implementation.createDocument(null,"waet");
-		
+		var RootDocument = document.implementation.createDocument(null,"waet");
+		var root = RootDocument.children[0];
+        root.setAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
+        root.setAttribute("xsi:noNamespaceSchemaLocation","test-schema.xsd");
 		// Build setup node
-		
-		return root;
+        var setup = RootDocument.createElement("setup");
+        var schemaSetup = this.schema.getAllElementsByName('setup')[0];
+        // First decode the attributes
+        var attributes = schemaSetup.getAllElementsByTagName('xs:attribute');
+        for (var i=0; i<attributes.length; i++)
+        {
+            var name = attributes[i].getAttribute("name");
+            if (name == undefined) {
+                name = attributes[i].getAttribute("ref");
+            }
+            if(eval("this."+name+" != undefined") || attributes[i].getAttribute("use") == "required")
+            {
+                eval("setup.setAttribute('"+name+"',this."+name+")");
+            }
+        }
+        root.appendChild(setup);
+        // Survey node
+        setup.appendChild(this.preTest.encode(RootDocument));
+        setup.appendChild(this.postTest.encode(RootDocument));
+        setup.appendChild(this.metrics.encode(RootDocument));
+        setup.appendChild(this.interfaces.encode(RootDocument));
+        for (var page of this.pages)
+        {
+            root.appendChild(page.encode(RootDocument));
+        }
+		return RootDocument;
 	};
 	
 	this.surveyNode = function() {
@@ -1934,11 +1932,11 @@
 				}
 			};
 			
-			this.exportXML = function(root)
+			this.exportXML = function(doc)
 			{
-				var node = root.createElement('surveyelement');
+				var node = doc.createElement('surveyelement');
 				node.setAttribute('type',this.type);
-				var statement = root.createElement('statement');
+				var statement = doc.createElement('statement');
 				statement.textContent = this.statement;
 				node.appendChild(statement);
 				switch(this.type)
@@ -1963,7 +1961,7 @@
 					for (var i=0; i<this.options.length; i++)
 					{
 						var option = this.options[i];
-						var optionNode = root.createElement("option");
+						var optionNode = doc.createElement("option");
 						optionNode.setAttribute("name",option.name);
 						optionNode.textContent = option.text;
 						node.appendChild(optionNode);
@@ -1985,12 +1983,12 @@
 				this.options.push(node);
 			}
 		};
-		this.encode = function(root) {
-			var node = root.createElement('survey');
+		this.encode = function(doc) {
+			var node = doc.createElement('survey');
 			node.setAttribute('location',this.location);
 			for (var i=0; i<this.options.length; i++)
 			{
-				node.appendChild(this.options[i].exportXML());
+				node.appendChild(this.options[i].exportXML(doc));
 			}
 			return node;
 		};
@@ -2052,8 +2050,29 @@
 			}
 		};
 		
-		this.encode = function(root) {
-			
+		this.encode = function(doc) {
+			var node = doc.createElement("interface");
+            if (typeof name == "string")
+                node.setAttribute("name",this.name);
+            for (var option of this.options)
+            {
+                var child = doc.createElement("interfaceoption");
+                child.setAttribute("type",option.type);
+                child.setAttribute("name",option.name);
+                node.appendChild(child);
+            }
+            if (this.scales.length != 0) {
+                var scales = doc.createElement("scales");
+                for (var scale of this.scales)
+                {
+                    var child = doc.createElement("scalelabel");
+                    child.setAttribute("position",scale.position);
+                    child.textContent = scale.text;
+                    scales.appendChild(child);
+                }
+                node.appendChild(scales);
+            }
+            return node;
 		};
 	};
 	
@@ -2066,12 +2085,12 @@
                 this.enabled.push(children[i].textContent);
             }
         }
-        this.encode = function(root) {
-            var node = root.createElement('metric');
+        this.encode = function(doc) {
+            var node = doc.createElement('metric');
             for (var i in this.enabled)
             {
                 if (isNaN(Number(i)) == true){break;}
-                var child = root.createElement('metricenable');
+                var child = doc.createElement('metricenable');
                 child.textContent = this.enabled[i];
                 node.appendChild(child);
             }
@@ -2174,18 +2193,26 @@
 		
 		this.encode = function(root)
 		{
-			var AHNode = root.createElement("audioHolder");
-			AHNode.id = this.id;
-			AHNode.setAttribute("hostURL",this.hostURL);
-			AHNode.setAttribute("sampleRate",this.sampleRate);
-			AHNode.setAttribute("randomiseOrder",this.randomiseOrder);
-			AHNode.setAttribute("repeatCount",this.repeatCount);
-			AHNode.setAttribute("loop",this.loop);
-			AHNode.setAttribute("elementComments",this.elementComments);
+			var AHNode = root.createElement("page");
+            // First decode the attributes
+            var attributes = this.schema.getAllElementsByTagName('xs:attribute');
+            for (var i=0; i<attributes.length; i++)
+            {
+                var name = attributes[i].getAttribute("name");
+                if (name == undefined) {
+                    name = attributes[i].getAttribute("ref");
+                }
+                if(eval("this."+name+" != undefined") || attributes[i].getAttribute("use") == "required")
+                {
+                    eval("AHNode.setAttribute('"+name+"',this."+name+")");
+                }
+            }
 			if(this.loudness != null) {AHNode.setAttribute("loudness",this.loudness);}
-			if(this.initialPosition != null) {
-				AHNode.setAttribute("loudness",this.initialPosition*100);
-				}
+            // <commentboxprefix>
+            var commentboxprefix = root.createElement("commentboxprefix");
+            commentboxprefix.textContent = this.commentBoxPrefix;
+            AHNode.appendChild(commentboxprefix);
+            
 			for (var i=0; i<this.interfaces.length; i++)
 			{
 				AHNode.appendChild(this.interfaces[i].encode(root));
@@ -2197,23 +2224,11 @@
 			// Create <CommentQuestion>
 			for (var i=0; i<this.commentQuestions.length; i++)
 			{
-				AHNode.appendChild(this.commentQuestions[i].exportXML(root));
+				AHNode.appendChild(this.commentQuestions[i].encode(root));
 			}
 			
-			// Create <PreTest>
-			var AHPreTest = root.createElement("PreTest");
-			for (var i=0; i<this.preTest.options.length; i++)
-			{
-				AHPreTest.appendChild(this.preTest.options[i].exportXML(root));
-			}
-			
-			var AHPostTest = root.createElement("PostTest");
-			for (var i=0; i<this.postTest.options.length; i++)
-			{
-				AHPostTest.appendChild(this.postTest.options[i].exportXML(root));
-			}
-			AHNode.appendChild(AHPreTest);
-			AHNode.appendChild(AHPostTest);
+			AHNode.appendChild(this.preTest.encode(root));
+            AHNode.appendChild(this.postTest.encode(root));
 			return AHNode;
 		};
 		
@@ -2241,7 +2256,20 @@
 			
 			this.encode = function(root)
 			{
-				
+				var node = root.createElement("commentquestion");
+                node.id = this.id;
+                node.setAttribute("type",this.type);
+                var statement = root.createElement("statement");
+                statement.textContent = this.statement;
+                node.appendChild(statement);
+                for (var option of this.options)
+                {
+                    var child = root.createElement("option");
+                    child.setAttribute("name",option.name);
+                    child.textContent = option.text;
+                    node.appendChild(child);
+                }
+                return node;
 			};
 		};
 		
@@ -2279,15 +2307,19 @@
 			};
 			this.encode = function(root)
 			{
-				var AENode = root.createElement("audioElements");
-				AENode.id = this.id;
-				AENode.setAttribute("url",this.url);
-				AENode.setAttribute("type",this.type);
-				AENode.setAttribute("gain",linearToDecibel(this.gain));
-				if (this.marker != false)
-				{
-					AENode.setAttribute("marker",this.marker*100);
-				}
+				var AENode = root.createElement("audioelement");
+				var attributes = this.schema.getAllElementsByTagName('xs:attribute');
+                for (var i=0; i<attributes.length; i++)
+                {
+                    var name = attributes[i].getAttribute("name");
+                    if (name == undefined) {
+                        name = attributes[i].getAttribute("ref");
+                    }
+                    if(eval("this."+name+" != undefined") || attributes[i].getAttribute("use") == "required")
+                    {
+                        eval("AENode.setAttribute('"+name+"',this."+name+")");
+                    }
+                }
 				return AENode;
 			};
 		};
--- a/test-schema.xsd	Wed Feb 10 12:14:25 2016 +0000
+++ b/test-schema.xsd	Wed Feb 10 14:46:57 2016 +0000
@@ -28,7 +28,7 @@
         <xs:element ref="interface" maxOccurs="1"/>
       </xs:sequence>
       <xs:attribute name="interface" type="xs:string" use="required"/>
-      <xs:attribute name="projectReturn" use="required" type="xs:string"/>
+      <xs:attribute name="projectReturn" type="xs:string" use="required"/>
       <xs:attribute name="randomiseOrder" type="xs:boolean" default="false"/>
       <xs:attribute name="testPages" type="xs:nonNegativeInteger" default="0"/>
       <xs:attribute name="loudness" type="xs:nonPositiveInteger" use="optional"/>
@@ -47,7 +47,7 @@
         <xs:element ref="survey" minOccurs="0" maxOccurs="2"/>
       </xs:sequence>
       <xs:attribute ref="id" use="required"/>
-      <xs:attribute name="hostURL" type="xs:anyURI" use="required"/>
+      <xs:attribute name="hostURL" type="xs:anyURI"/>
       <xs:attribute name="randomiseOrder" type="xs:boolean" default="false"/>
       <xs:attribute name="repeatCount" type="xs:nonNegativeInteger" default="0"/>
       <xs:attribute name="loop" type="xs:boolean" default="false"/>
@@ -116,7 +116,7 @@
       <xs:attribute ref="id" use="required"/>
       <xs:attribute name="url" type="xs:anyURI" use="required"/>
       <xs:attribute name="gain" type="xs:decimal" default="0"/>
-      <xs:attribute name="type">
+      <xs:attribute name="type" default="normal">
         <xs:simpleType>
           <xs:restriction base="xs:string">
             <xs:enumeration value="normal"/>
--- a/test_create/test_create.html	Wed Feb 10 12:14:25 2016 +0000
+++ b/test_create/test_create.html	Wed Feb 10 14:46:57 2016 +0000
@@ -18,34 +18,6 @@
             this.errors = [];
             this.schema = null;
 
-            this.randomiseOrder = function(input)
-            {
-                // This takes an array of information and randomises the order
-                var N = input.length;
-
-                var inputSequence = []; // For safety purposes: keep track of randomisation
-                for (var counter = 0; counter < N; ++counter) 
-                    inputSequence.push(counter) // Fill array
-                var inputSequenceClone = inputSequence.slice(0);
-
-                var holdArr = [];
-                var outputSequence = [];
-                for (var n=0; n<N; n++)
-                {
-                    // First pick a random number
-                    var r = Math.random();
-                    // Multiply and floor by the number of elements left
-                    r = Math.floor(r*input.length);
-                    // Pick out that element and delete from the array
-                    holdArr.push(input.splice(r,1)[0]);
-                    // Do the same with sequence
-                    outputSequence.push(inputSequence.splice(r,1)[0]);
-                }
-                console.log(inputSequenceClone.toString()); // print original array to console
-                console.log(outputSequence.toString()); 	// print randomised array to console
-                return holdArr;
-            };
-
             this.processAttribute = function(attribute,schema)
             {
                 // attribute is the string returned from getAttribute on the XML
@@ -164,11 +136,37 @@
 
             this.encode = function()
             {
-                var root = document.implementation.createDocument(null,"waet");
-
+                var RootDocument = document.implementation.createDocument(null,"waet");
+                var root = RootDocument.children[0];
+                root.setAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
+                root.setAttribute("xsi:noNamespaceSchemaLocation","test-schema.xsd");
                 // Build setup node
-
-                return root;
+                var setup = RootDocument.createElement("setup");
+                var schemaSetup = this.schema.getAllElementsByName('setup')[0];
+                // First decode the attributes
+                var attributes = schemaSetup.getAllElementsByTagName('xs:attribute');
+                for (var i=0; i<attributes.length; i++)
+                {
+                    var name = attributes[i].getAttribute("name");
+                    if (name == undefined) {
+                        name = attributes[i].getAttribute("ref");
+                    }
+                    if(eval("this."+name+" != undefined") || attributes[i].getAttribute("use") == "required")
+                    {
+                        eval("setup.setAttribute('"+name+"',this."+name+")");
+                    }
+                }
+                root.appendChild(setup);
+                // Survey node
+                setup.appendChild(this.preTest.encode(RootDocument));
+                setup.appendChild(this.postTest.encode(RootDocument));
+                setup.appendChild(this.metrics.encode(RootDocument));
+                setup.appendChild(this.interfaces.encode(RootDocument));
+                for (var page of this.pages)
+                {
+                    root.appendChild(page.encode(RootDocument));
+                }
+                return RootDocument;
             };
 
             this.surveyNode = function() {
@@ -228,11 +226,11 @@
                         }
                     };
 
-                    this.exportXML = function(root)
+                    this.exportXML = function(doc)
                     {
-                        var node = root.createElement('surveyelement');
+                        var node = doc.createElement('surveyelement');
                         node.setAttribute('type',this.type);
-                        var statement = root.createElement('statement');
+                        var statement = doc.createElement('statement');
                         statement.textContent = this.statement;
                         node.appendChild(statement);
                         switch(this.type)
@@ -257,7 +255,7 @@
                             for (var i=0; i<this.options.length; i++)
                             {
                                 var option = this.options[i];
-                                var optionNode = root.createElement("option");
+                                var optionNode = doc.createElement("option");
                                 optionNode.setAttribute("name",option.name);
                                 optionNode.textContent = option.text;
                                 node.appendChild(optionNode);
@@ -279,12 +277,12 @@
                         this.options.push(node);
                     }
                 };
-                this.encode = function(root) {
-                    var node = root.createElement('survey');
+                this.encode = function(doc) {
+                    var node = doc.createElement('survey');
                     node.setAttribute('location',this.location);
                     for (var i=0; i<this.options.length; i++)
                     {
-                        node.appendChild(this.options[i].exportXML());
+                        node.appendChild(this.options[i].exportXML(doc));
                     }
                     return node;
                 };
@@ -346,8 +344,29 @@
                     }
                 };
 
-                this.encode = function(root) {
-
+                this.encode = function(doc) {
+                    var node = doc.createElement("interface");
+                    if (typeof name == "string")
+                        node.setAttribute("name",this.name);
+                    for (var option of this.options)
+                    {
+                        var child = doc.createElement("interfaceoption");
+                        child.setAttribute("type",option.type);
+                        child.setAttribute("name",option.name);
+                        node.appendChild(child);
+                    }
+                    if (this.scales.length != 0) {
+                        var scales = doc.createElement("scales");
+                        for (var scale of this.scales)
+                        {
+                            var child = doc.createElement("scalelabel");
+                            child.setAttribute("position",scale.position);
+                            child.textContent = scale.text;
+                            scales.appendChild(child);
+                        }
+                        node.appendChild(scales);
+                    }
+                    return node;
                 };
             };
 
@@ -360,12 +379,12 @@
                         this.enabled.push(children[i].textContent);
                     }
                 }
-                this.encode = function(root) {
-                    var node = root.createElement('metric');
+                this.encode = function(doc) {
+                    var node = doc.createElement('metric');
                     for (var i in this.enabled)
                     {
                         if (isNaN(Number(i)) == true){break;}
-                        var child = root.createElement('metricenable');
+                        var child = doc.createElement('metricenable');
                         child.textContent = this.enabled[i];
                         node.appendChild(child);
                     }
@@ -468,18 +487,26 @@
 
                 this.encode = function(root)
                 {
-                    var AHNode = root.createElement("audioHolder");
-                    AHNode.id = this.id;
-                    AHNode.setAttribute("hostURL",this.hostURL);
-                    AHNode.setAttribute("sampleRate",this.sampleRate);
-                    AHNode.setAttribute("randomiseOrder",this.randomiseOrder);
-                    AHNode.setAttribute("repeatCount",this.repeatCount);
-                    AHNode.setAttribute("loop",this.loop);
-                    AHNode.setAttribute("elementComments",this.elementComments);
+                    var AHNode = root.createElement("page");
+                    // First decode the attributes
+                    var attributes = this.schema.getAllElementsByTagName('xs:attribute');
+                    for (var i=0; i<attributes.length; i++)
+                    {
+                        var name = attributes[i].getAttribute("name");
+                        if (name == undefined) {
+                            name = attributes[i].getAttribute("ref");
+                        }
+                        if(eval("this."+name+" != undefined") || attributes[i].getAttribute("use") == "required")
+                        {
+                            eval("AHNode.setAttribute('"+name+"',this."+name+")");
+                        }
+                    }
                     if(this.loudness != null) {AHNode.setAttribute("loudness",this.loudness);}
-                    if(this.initialPosition != null) {
-                        AHNode.setAttribute("loudness",this.initialPosition*100);
-                        }
+                    // <commentboxprefix>
+                    var commentboxprefix = root.createElement("commentboxprefix");
+                    commentboxprefix.textContent = this.commentBoxPrefix;
+                    AHNode.appendChild(commentboxprefix);
+
                     for (var i=0; i<this.interfaces.length; i++)
                     {
                         AHNode.appendChild(this.interfaces[i].encode(root));
@@ -491,23 +518,11 @@
                     // Create <CommentQuestion>
                     for (var i=0; i<this.commentQuestions.length; i++)
                     {
-                        AHNode.appendChild(this.commentQuestions[i].exportXML(root));
+                        AHNode.appendChild(this.commentQuestions[i].encode(root));
                     }
 
-                    // Create <PreTest>
-                    var AHPreTest = root.createElement("PreTest");
-                    for (var i=0; i<this.preTest.options.length; i++)
-                    {
-                        AHPreTest.appendChild(this.preTest.options[i].exportXML(root));
-                    }
-
-                    var AHPostTest = root.createElement("PostTest");
-                    for (var i=0; i<this.postTest.options.length; i++)
-                    {
-                        AHPostTest.appendChild(this.postTest.options[i].exportXML(root));
-                    }
-                    AHNode.appendChild(AHPreTest);
-                    AHNode.appendChild(AHPostTest);
+                    AHNode.appendChild(this.preTest.encode(root));
+                    AHNode.appendChild(this.postTest.encode(root));
                     return AHNode;
                 };
 
@@ -535,7 +550,20 @@
 
                     this.encode = function(root)
                     {
-
+                        var node = root.createElement("commentquestion");
+                        node.id = this.id;
+                        node.setAttribute("type",this.type);
+                        var statement = root.createElement("statement");
+                        statement.textContent = this.statement;
+                        node.appendChild(statement);
+                        for (var option of this.options)
+                        {
+                            var child = root.createElement("option");
+                            child.setAttribute("name",option.name);
+                            child.textContent = option.text;
+                            node.appendChild(child);
+                        }
+                        return node;
                     };
                 };
 
@@ -573,20 +601,25 @@
                     };
                     this.encode = function(root)
                     {
-                        var AENode = root.createElement("audioElements");
-                        AENode.id = this.id;
-                        AENode.setAttribute("url",this.url);
-                        AENode.setAttribute("type",this.type);
-                        AENode.setAttribute("gain",linearToDecibel(this.gain));
-                        if (this.marker != false)
+                        var AENode = root.createElement("audioelement");
+                        var attributes = this.schema.getAllElementsByTagName('xs:attribute');
+                        for (var i=0; i<attributes.length; i++)
                         {
-                            AENode.setAttribute("marker",this.marker*100);
+                            var name = attributes[i].getAttribute("name");
+                            if (name == undefined) {
+                                name = attributes[i].getAttribute("ref");
+                            }
+                            if(eval("this."+name+" != undefined") || attributes[i].getAttribute("use") == "required")
+                            {
+                                eval("AENode.setAttribute('"+name+"',this."+name+")");
+                            }
                         }
                         return AENode;
                     };
                 };
             };
         }
+
     </script>
     <script type="text/javascript" src="test_core.js"/>
     <script type="text/javascript">