popcorn-js/parsers/parserXML/popcorn.parserXML.js

122 строки
3.4 KiB
JavaScript

// PARSER: 0.1 XML
(function (Popcorn) {
/**
*
*
*/
Popcorn.parser( "parseXML", "XML", function( data ) {
// declare needed variables
var returnData = {
title: "",
remote: "",
data: []
},
manifestData = {};
// Simple function to convert 0:05 to 0.5 in seconds
// acceptable formats are HH:MM:SS:MM, MM:SS:MM, SS:MM, SS
var toSeconds = function(time) {
var t = time.split(":");
if (t.length === 1) {
return parseFloat(t[0], 10);
} else if (t.length === 2) {
return parseFloat(t[0], 10) + parseFloat(t[1] / 12, 10);
} else if (t.length === 3) {
return parseInt(t[0] * 60, 10) + parseFloat(t[1], 10) + parseFloat(t[2] / 12, 10);
} else if (t.length === 4) {
return parseInt(t[0] * 3600, 10) + parseInt(t[1] * 60, 10) + parseFloat(t[2], 10) + parseFloat(t[3] / 12, 10);
}
};
// 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 = toSeconds( data );
// converts out into end
} else if ( key === "out" ){
returnObject.end = toSeconds( 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.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 );