]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
[#221] parser plugin structure with XML parser, needs test and data-timeline-sources
authorScottDowne <sdowne@learn.senecac.on.ca>
Wed, 5 Jan 2011 15:51:33 +0000 (10:51 -0500)
committerScottDowne <sdowne@learn.senecac.on.ca>
Wed, 5 Jan 2011 15:51:33 +0000 (10:51 -0500)
parsers/parserXML/data.XML [new file with mode: 0644]
parsers/parserXML/popcorn.parseXML.html [new file with mode: 0644]
parsers/parserXML/popcorn.parseXML.js [new file with mode: 0644]
popcorn.js
test/data/parserData.json
test/popcorn.unit.js

diff --git a/parsers/parserXML/data.XML b/parsers/parserXML/data.XML
new file mode 100644 (file)
index 0000000..a1657d5
--- /dev/null
@@ -0,0 +1,9 @@
+<popcorn>
+  <manifest>
+    <somedata id="somedata" userid="13072070@N00" />
+  </manifest>
+  <timeline>
+    <flickr in="1" out="5" target="flickrdiv" numberofimages="4" padding="4px" resourceid="somedata"/>
+    <flickr in="10" out="11" target="flickrdiv" numberofimages="8" padding="4px" resourceid="somedata"/>
+  </timeline>
+</popcorn>
diff --git a/parsers/parserXML/popcorn.parseXML.html b/parsers/parserXML/popcorn.parseXML.html
new file mode 100644 (file)
index 0000000..1a57a4d
--- /dev/null
@@ -0,0 +1,44 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <title>Popcorn 0.1 parser Plug-in Demo</title>
+  
+  <script src="../../popcorn.js"></script>
+  <script src="../../test/jquery.js"></script>
+  <script src="../../plugins/flickr/popcorn.flickr.js"></script>
+  <script src="popcorn.parseXML.js"></script>
+  <script>
+    document.addEventListener('DOMContentLoaded', function () {
+      var p = Popcorn('#video')
+      .parseXML("data.XML")
+      .play();
+
+    }, false);
+
+  </script>
+</head>
+<body>
+  <h1 id="qunit-header">Popcorn 0.1 parser Plug-in Demo</h1>
+  <p></p>
+  <div>
+    <video id='video' 
+      controls preload='none' 
+      width= '250px'
+      poster="../../test/poster.png"> 
+
+      <source id='mp4'
+        src="../../test/trailer.mp4"
+        type='video/mp4; codecs="avc1, mp4a"'> 
+
+      <source id='ogv'
+        src="../../test/trailer.ogv"
+        type='video/ogg; codecs="theora, vorbis"'> 
+
+      <p>Your user agent does not support the HTML5 Video element.</p> 
+
+    </video> 
+  </div>
+  
+  <div id="flickrdiv"></div>
+</body>
+</html>
diff --git a/parsers/parserXML/popcorn.parseXML.js b/parsers/parserXML/popcorn.parseXML.js
new file mode 100644 (file)
index 0000000..e11bff0
--- /dev/null
@@ -0,0 +1,106 @@
+// PARSER: 0.1 XML
+
+(function (Popcorn) {
+
+  /**
+   *
+   *
+   */
+  Popcorn.parser( "parseXML", "XML", function( data ) {
+
+    // declare needed variables
+    var returnData = {
+          title: "",
+          remote: "",
+          data: []
+        },
+        manifestData = {};
+
+    // turns a node tree element into a straight up javascript object
+    // also converts in and out to start and end
+    // also links manifest data with ids
+    var objectifyAttributes = function ( nodeAttributes ) {
+
+      var returnObject = {};
+
+      for ( var i = 0, nal = nodeAttributes.length; i < nal; i++ ) {
+
+        var key  = nodeAttributes.item(i).nodeName,
+            data = nodeAttributes.item(i).nodeValue;
+
+        // converts in into start
+        if (key === "in") {
+          returnObject.start = data;
+        // converts out into end
+        } else if ( key === "out" ){
+          returnObject.end = data;
+        // this is where ids in the manifest are linked
+        } else if ( key === "resourceid" ) {
+          Popcorn.extend( returnObject, manifestData[data] );
+        // everything else
+        } else {
+          returnObject[key] = data;
+        }
+
+      }
+
+      return returnObject;
+    };
+
+    // creates an object of all atrributes keyd by name
+    var createTrack = function( name, attributes ) {
+      var track = {};
+      track[name] = attributes;
+      return track;
+    };
+
+    // recursive function to process a node, or process the next child node
+    var parseNode = function ( node, allAttributes, manifest ) {
+      var attributes = {};
+      Popcorn.extend( attributes, allAttributes, objectifyAttributes( node.attributes ), { text: node.textContent } );
+
+      var childNodes = node.childNodes;
+
+      // processes the node
+      if ( childNodes.length < 1 || ( childNodes.length === 1 && childNodes[0].nodeType === 3 ) ) {
+
+        if ( !manifest ) {
+          returnData.data.push( createTrack( node.nodeName, attributes ) );
+        } else {
+          manifestData[attributes.id] = attributes;
+        }
+
+      // process the next child node
+      } else {
+
+        for ( var i = 0; i < childNodes.length; i++ ) {
+
+          if ( childNodes[i].nodeType === 1 ) {
+            parseNode( childNodes[i], attributes, manifest );
+          }
+
+        }
+      }
+    };
+
+    // this is where things actually start
+    var x = data.xml.documentElement.childNodes;
+
+    for ( var i = 0, xl = x.length; i < xl; i++ ) {
+
+      if ( x[i].nodeType === 1 ) {
+
+        // start the process of each main node type, manifest or timeline
+        if ( x[i].nodeName === "manifest" ) {
+          parseNode( x[i], {}, true );
+        } else { // timeline
+          parseNode( x[i], {}, false );
+        }
+
+      }
+    }
+
+    return returnData;
+  });
+
+})( Popcorn );
index 5825f22bbbea61f8ae383fbc2ac3f7ae392b3420..56e94e81c0146d1045b87593e09080e21ec30f89 100644 (file)
           var tracksObject = definition( data );
 
           // creating tracks out of parsed object
-          for ( var key in tracksObject ) {
-            if ( tracksObject.hasOwnProperty(key) ) {
+          for ( var i = 0, todl = tracksObject.data.length; i < todl; i++ ) {
 
-              // an array of tracks of all one type
-              if ( tracksObject[key].constructor === Array ) {
+            for ( var key in tracksObject.data[i] ) {
 
-                for (var i = 0, tol = tracksObject[key].length; i < tol; i++) {
-                  if ( typeof that[key] === "function") {
-                    that[key]( tracksObject[key][i] );
-                  }
-                }
-
-              // one single track
-              } else if ( typeof tracksObject[key] === "object" ) {
-                if ( typeof that[key] === "function") {
-                  that[key]( tracksObject[key] );
-                }
+              if ( tracksObject.data[i].hasOwnProperty(key) ) {
+                that[key]( tracksObject.data[i][key] );
               }
             }
           }
index 4974e8d9374adbeab9bf60a973d807828b615fe1..7bdd4b60abbf7da078fe94863027e354cb9d8f04 100644 (file)
@@ -1,7 +1,10 @@
 {
-  "parserTest":
+  "data": [
     {
-      "start": 5,
-      "end": 6
+      "parserTest": {
+        "start": 5,
+        "end": 6
+      }
     }
+  ]
 }
index aab8c797070582f6e7ae89428df6eaa3e9ebf5ce..8ffd6fccff8b0299f38df7cdba8b19ed17d0ffc7 100644 (file)
@@ -1127,7 +1127,7 @@ test("Parsing Functions", function () {
     return data.json;
   });
 
-  ok(typeof popperly.parseJSON === "function", "Popcorn.parser created a parserJSON function");
+  ok(typeof popperly.parseJSON === "function", "Popcorn.parser created a parseJSON function");
   plus();
 
   ok(typeof popperly.parseJSON().parseJSON("data/test.js").parseJSON === "function" , "parseJSON function is chainable");