Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Usage
Using streams:

```javascript
var cplParser = require('node-cpl');
var cplParser = require('cpl');
var fs = require('fs');

var cplStream = cplParser.createStream();
Expand All @@ -26,7 +26,7 @@ cplStream.on('cpl', function(cpl) {
Using callbacks:

```javascript
cplParser.parseCPL(fs.readFileSync('cpl.xml', 'UTF-8'), function(err, cpl) {
cplParser.parse(fs.readFileSync('cpl.xml', 'UTF-8'), function(err, cpl) {
if (err) return console.error(err);
console.log(cpl);
});
Expand All @@ -35,7 +35,8 @@ cplParser.parseCPL(fs.readFileSync('cpl.xml', 'UTF-8'), function(err, cpl) {
The CPL object takes the form:

```json
{
{
"isCPL": true,
"id": "7b1e5649-ff30-489b-b74a-c2e1060bb590",
"text": "AIDA-ACT1_FTR_F_IT-DE_DE_51_2K_20090217_AAM",
"issueDate": "2009-02-04T10:34:09.000Z",
Expand All @@ -60,6 +61,10 @@ The CPL object takes the form:
}
```

Non CPL .xml files
------------------
Any files parsed that are not a valid CPL will return an object with a field "isCPL" set to false. This can be useful to determine whether an .xml file is a CPL or not.

License (MIT)
-------------

Expand Down
22 changes: 20 additions & 2 deletions lib/parse_cpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,28 @@ var createStream = function(options) {
options = {};

var saxStream = sax.createStream(false, {trim: true, xmlns: true});
var cpl = {duration: 0, frames: 0, reels: []};
var cpl = {isCPL: true, duration: 0, frames: 0, reels: []};
var tag = null;
var level = 'root';
var firstTag = true;

saxStream.on('opentag', function (node) {
tag = node.local;

//Test whether we have an CompositionPlaylist tag as the first tag.
//If not its not a CPL.
if (firstTag){
if (tag !== 'COMPOSITIONPLAYLIST'){
//TODO: Investigate cleaniest way to close the stream at this point,
//so we dont go through the rest of the file needlessly.
cpl.isCPL = false;
}
firstTag = false;
}

//If we know we not looking at a cpl, ignore all events till end.
if (!cpl.isCPL) return;

switch (tag) {
case 'REEL':
level = 'reel';
Expand All @@ -52,11 +68,12 @@ var createStream = function(options) {
case 'MAINSTEREOSCOPICPICTURE':
cpl.threeD = true;
break;
}

}
});

saxStream.on('closetag', function (tag) {
if (!cpl.isCPL) return;
// Are we leaving a reel?
switch (tag) {
case 'REEL':
Expand All @@ -75,6 +92,7 @@ var createStream = function(options) {
});

saxStream.on('text', function (t) {
if (!cpl.isCPL) return;
var currentReel = cpl.reels.last();
if (level === 'root')
switch (tag) {
Expand Down
19 changes: 19 additions & 0 deletions test/cpl_parser_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var getCPLFiles = function() {
*/
var cpl1Asserts = function(cpl) {
assert(cpl);
assert(cpl.isCPL);
assert.equal(cpl.id, '7b1e5649-ff30-489b-b74a-c2e1060bb590');
assert.equal(cpl.issuer, 'Arts Alliance Media');
assert.equal(cpl.issueDate.getTime(), new Date('2009-02-04T10:34:09-00:00').getTime());
Expand All @@ -54,6 +55,7 @@ var cpl1Asserts = function(cpl) {
*/
var cpl1TextAsserts = function(cpl) {
assert(cpl);
assert(cpl.isCPL);
assert.equal(cpl.name, 'AIDA-ACT1');
assert.equal(cpl.type, 'feature');
assert.equal(cpl.aspectRatio, 'flat');
Expand All @@ -72,6 +74,7 @@ var cpl1Asserts = function(cpl) {
*/
var cpl2Asserts = function(cpl) {
assert(cpl);
assert(cpl.isCPL);
assert.equal(cpl.id, '3a4a95d1-6947-4752-8c73-57fc80111973');
assert.equal(cpl.issuer, 'Qube');
assert.equal(cpl.issueDate.getTime(), new Date('2010-03-26T10:35:49+00:00').getTime());
Expand All @@ -81,6 +84,11 @@ var cpl2Asserts = function(cpl) {
assert.equal(cpl.type, 'feature');
};

var notCplAssert = function(cpl){
assert(cpl);
assert.equal(cpl.isCPL, false);
}

/**
* CPL parser tests
*/
Expand Down Expand Up @@ -153,5 +161,16 @@ describe('cplParser', function() {
});
});

describe('parseNonCplXML', function() {
it('should parse a Non CPL xml and return an object with a isCPL field as false', function(done){
var cplStream = cplParser.createStream();
fs.createReadStream(path.resolve(__dirname, path.join('not_cpls', 'not_cpl.xml'))).pipe(cplStream);
cplStream.on('cpl', function(cpl){
notCplAssert(cpl);
done();
});
});
});


});
Loading