From 5f1af9fda24c98c7e60a282876c989385e6a9b80 Mon Sep 17 00:00:00 2001 From: ScottDowne Date: Wed, 5 Jan 2011 10:51:33 -0500 Subject: [PATCH] [#221] parser plugin structure with XML parser, needs test and data-timeline-sources --- parsers/parserXML/data.XML | 9 ++ parsers/parserXML/popcorn.parseXML.html | 44 ++++++++++ parsers/parserXML/popcorn.parseXML.js | 106 ++++++++++++++++++++++++ popcorn.js | 19 +---- test/data/parserData.json | 9 +- test/popcorn.unit.js | 2 +- 6 files changed, 170 insertions(+), 19 deletions(-) create mode 100644 parsers/parserXML/data.XML create mode 100644 parsers/parserXML/popcorn.parseXML.html create mode 100644 parsers/parserXML/popcorn.parseXML.js diff --git a/parsers/parserXML/data.XML b/parsers/parserXML/data.XML new file mode 100644 index 0000000..a1657d5 --- /dev/null +++ b/parsers/parserXML/data.XML @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/parsers/parserXML/popcorn.parseXML.html b/parsers/parserXML/popcorn.parseXML.html new file mode 100644 index 0000000..1a57a4d --- /dev/null +++ b/parsers/parserXML/popcorn.parseXML.html @@ -0,0 +1,44 @@ + + + + Popcorn 0.1 parser Plug-in Demo + + + + + + + + +

Popcorn 0.1 parser Plug-in Demo

+

+
+ +
+ +
+ + diff --git a/parsers/parserXML/popcorn.parseXML.js b/parsers/parserXML/popcorn.parseXML.js new file mode 100644 index 0000000..e11bff0 --- /dev/null +++ b/parsers/parserXML/popcorn.parseXML.js @@ -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 ); diff --git a/popcorn.js b/popcorn.js index 5825f22..56e94e8 100644 --- a/popcorn.js +++ b/popcorn.js @@ -764,23 +764,12 @@ 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] ); } } } diff --git a/test/data/parserData.json b/test/data/parserData.json index 4974e8d..7bdd4b6 100644 --- a/test/data/parserData.json +++ b/test/data/parserData.json @@ -1,7 +1,10 @@ { - "parserTest": + "data": [ { - "start": 5, - "end": 6 + "parserTest": { + "start": 5, + "end": 6 + } } + ] } diff --git a/test/popcorn.unit.js b/test/popcorn.unit.js index aab8c79..8ffd6fc 100644 --- a/test/popcorn.unit.js +++ b/test/popcorn.unit.js @@ -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"); -- 2.43.0