forked from nbqx/saxon-stream2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (45 loc) · 1.22 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
var fs = require('fs'),
exec = require('child_process').exec;
var through2 = require('through2'),
tmp = require('temporary');
function saxonStream2(jarPath,xslPath,opt){
var buf = [];
var timeout = 5000;
var saxonOpts = ['-warnings:silent'];
if(opt!==undefined){
if(opt.timeout!==undefined) timeout = opt.timeout;
if(opt.saxonOpts!==undefined) saxonOpts = opt.saxonOpts;
}
return through2(function(c,e,n){
buf.push(c+'');
n();
},function(n){
var self = this;
var data = buf.join('');
var xml = new tmp.File();
var result = new tmp.File();
xml.writeFileSync(data);
var opts = [
'-jar',jarPath,
'-s:'+xml.path,
'-xsl:'+xslPath,
'-o:'+result.path
];
// Array.prototype.push.apply(opts,saxonOpts);
opts = opts.concat(saxonOpts);
var cmd = exec('java '+opts.join(' '),{timeout:timeout},function(err,stdout,stderr){
if(err) return n(err);
if(stderr) return n(stderr);
if(stdout!==''){
console.log(stdout);
}
});
cmd.on('exit',function(code,sig){
var cont = fs.readFileSync(result.path);
self.push(cont);
result.unlinkSync();
n();
});
});
};
module.exports = saxonStream2;