Skip to content

Instantly share code, notes, and snippets.

@kerphi
Created April 8, 2013 21:57
Show Gist options
  • Save kerphi/5340913 to your computer and use it in GitHub Desktop.
Save kerphi/5340913 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
//
// example :
// find xml-orig/ -type f | ./xml2xml.js -c 10 --xslt mafeuille.xslt --dpath ./xml-dest --epath ./xml-error
//
// Requirements:
// npm install shelljs optimist lazy async
//
// tentative non concluante :
// sudo apt-get install libxml2-dev
// sudo apt-get install libxslt-dev
// npm install node_xslt
// var xslt = require('node_xslt')
// var stylesheet = xslt.readXsltFile('mafeuille.xsl');
// var document = '';
// document = xslt.readXmlFile(task.orig);
// var transformedString = xslt.transform(stylesheet, document);
// fs.fileWriteSync(task.dest, transformedString);
"use strict";
var fs = require('fs');
var shell = require('shelljs');
var Lazy = require('lazy');
var async = require('async');
var start_time = new Date();
// get the command line argument
var optimist = require('optimist')
.usage('Usage: $0 -c [number] --xslt [filepath] --dpath [destinationpath] --epath [errorpath]')
.demand('concurrency').alias('concurrency', 'c').default('concurrency', 4)
.describe('concurrency', 'how many XML are converted in parallele')
.demand('xslt').describe('xslt', 'the stylsheet tu use for the xml transformations')
.demand('dpath').describe('dpath', 'where is stored transformed xml')
.demand('epath').describe('epath', 'where is stored error xml')
;
var argv = optimist.argv;
if (argv.help) {
optimist.showHelp();
process.exit(0);
}
var xsltErrors = {
0: 'No error (normal operation)',
1: 'No argument',
2: 'Too many parameters',
3: 'Unknown option',
4: 'Failed to parse the stylesheet',
5: 'Error in the stylesheet',
6: 'Error in one of the documents',
7: 'Unsupported xsl:output method',
8: 'String parameter contains both quote and double-quotes',
9: 'Internal processing error',
10: 'Processing was stopped by a terminating message',
11: 'Could not write the result to the output file',
};
// count number of transformed xml files
var nb = 0;
// create a queue to handle with concurrency several tasks
// one task is one xml transformation using xsltproc
var q = async.queue(function (task, callback) {
shell.exec('xsltproc --nonet -o ' + task.dest + ' ' + argv.xslt + ' ' + task.orig, {silent: true, async: true}, function (code, output) {
nb++;
// if an error is returned by xsltproc, write it !
if (code != 0) {
fs.writeFile(task.error, output + '\n\n' + xsltErrors[code], callback);
console.log(code);
} else {
// just a signal on the terminal to tell that code is running
process.stdout.write('.');
callback();
}
});
}, argv.concurrency);
// called when queue is empty
q.drain = function() {
var end_time = new Date();
console.log('');
console.log(nb + ' items have been processed');
console.log('start time: ' + start_time);
console.log('end time: ' + end_time);
console.log('elapsed time: ' + (end_time - start_time)/1000 + ' seconds');
}
// split input stream line by line
// one line is one xml path
Lazy(process.stdin)
.lines
.map(String)
.map(function (line) {
q.push({
orig: line,
dest: line.replace(line.split('/')[0], argv.dpath),
error: line.replace(line.split('/')[0], argv.epath)
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment