Skip to content

Commit

Permalink
First version
Browse files Browse the repository at this point in the history
  • Loading branch information
p-muessig committed Apr 1, 2014
0 parents commit 8e65be8
Show file tree
Hide file tree
Showing 10 changed files with 502 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea/
javaLibs/
node_modules/
24 changes: 24 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
The MIT License (MIT)
[OSI Approved License]

The MIT License (MIT)

Copyright (c) 2014 E2E Technologies Ltd

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
87 changes: 87 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# xslt2node

A XSLT package wrapping the XSLT interface of the Java API for XML Processing (JAXP).

## Quick Example

var config = {
xsltPath: 'discount.xsl',
sourcePath: 'order.xml',
result: 'result.xml',
params: {
discount: '2014/08/01'
},
props: {
indent: 'yes'
}
};

xslt4node.transform(config, function (err) {
if (err) {
console.log(err);
}
));

## Installation

The easiest way to install it is via NPM:

npm install xslt4node

## Documentation

### transform(config, callback)

Executes a transformation defined by the contents of `config`.

__Arguments__

* `config` - _Object defining the transformation_
* `xsltPath` {String} optional - _Path to the XSLT document used to create the transformer._
* `xslt` {String | Buffer} optional - _{String} or {Buffer} holding the XSLT document used to create the transformer._
* `sourcePath`{String} optional - _Path to the XML document to be transformed._
* `source` {String | Buffer} optional - _{String} or {Buffer} holding the XML document to be transformed._
* `result`{String | Function} - _If `result` is a string, this string is interpreted as the path to the file to hold the result of the transformation._
_If `result` is {Function} `String` or {Function} `Buffer`, the result of the transformation is stored in the second parameter of `callback`._
* `params` {Object} optional - _`params`'s properties provide name/value pairs for the parameters for the transformation._
* `props` {Object} optional - _`props`'s properties provide name/value pairs for the output properties for the transformation._
* `callback(err, result)` {Function} - _A callback that is called, when the transformation has finished or an error occurred. The error is stored in `err`. If `config.result` is `String` or `Buffer`, the argument `result` holds the transformed XML document._

Either `xsltPath` or `xslt` but not both may be specified. If neither `xsltPath` nor `xslt` are specified, the transformer performs
the _identity transform_. Either `sourcePath` or `source` but not both have to be specified.

Some examples are found in the directory _test_.

### addLibrary(path)

Adds a Java archive file to the classpath. May be used to specify a XSLT processor (e.g. by adding _saxon9he.jar_)

__Arguments__

* path {String} - _The path to an Java archive file._

## License

(The MIT License)

Copyright (c) 2014 [E2E Technologies Ltd] (http://www.e2ebridge.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

by [E2E Technologies Ltd] (http://www.e2ebridge.com)
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Copyright: E2E Technologies Ltd
*/
'use strict';

module.exports = require('./lib/xslt4node');
213 changes: 213 additions & 0 deletions lib/xslt4node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
/**
* Copyright: E2E Technologies Ltd
*/

'use strict';

var java = require('java');
var async = require('async');

/*
* Wrap Java functions
*/

function newFileInputStream(path, callback) {
java.newInstance('java.io.FileInputStream', path, callback);
}

function newFileOutputStream(path, callback) {
java.newInstance('java.io.FileOutputStream', path, callback);
}

function newArrayInputStream(buffer, callback) {
java.newInstance('java.io.ByteArrayInputStream', buffer.toByteArray(), callback);
}

function newArrayOutputStream(callback) {
java.newInstance('java.io.ByteArrayOutputStream', 1024, callback);
}

function newReader(string, callback) {
java.newInstance('java.io.StringReader', string, callback);
}

function newWriter(callback) {
java.newInstance('java.io.StringWriter', 1024, callback);
}

function newStreamSource(stream, callback) {
java.newInstance('javax.xml.transform.stream.StreamSource', stream, callback);
}

function newStreamResult(stream, callback) {
java.newInstance('javax.xml.transform.stream.StreamResult', stream, callback);
}

function newTransformerFactory(callback) {
java.callStaticMethod('javax.xml.transform.TransformerFactory', 'newInstance', callback);
}

Buffer.prototype.toByteArray = function () {
return java.newArray('byte', Array.prototype.map.call(this, function (e) { return java.newByte(e); }));
};


/*
* helper functions
*/

function addTask(taskName, tasks, props, setter) {
tasks[taskName] = ['transformer', function (callback, results) {
async.forEach(Object.keys(props), function (p, callback) {
results.transformer[setter](p, props[p], callback);
}, callback);
}];
tasks.transform.unshift(taskName);
}

function isType(type, object) {
return typeof object === 'function' && object.name === type;
}

/*
* module interface
*/

/**
*
* @param path
*/
module.exports.addLibrary = function (path) {
if (typeof path === 'string' && java.classpath.indexOf(path) === -1) {
java.classpath.push(path);
}
};

/**
*
* @param config
* @param callback
* @returns {*}
*/
module.exports.transform = function (config, callback) {
var tasks = {
streamSource: ['source', function (callback, results) {
newStreamSource(results.source, callback);
}],
streamResult: ['result', function (callback, results) {
newStreamResult(results.result, callback);
}],
factory: function (callback) {
newTransformerFactory(callback);
},
transform: ['transformer', 'streamSource', 'streamResult', function (callback, results) {
results.transformer.transform(results.streamSource, results.streamResult, callback);
}]
};

if (typeof callback !== 'function') {
callback = function () {}; // if no callback passed, create use an empty function
}

if (config.xsltPath !== undefined && config.xslt !== undefined) {
return callback(new Error('Properties \'xsltPath\' and \'xslt\' must not be used together.'));
}
if (config.sourcePath !== undefined && config.source !== undefined) {
return callback(new Error('Properties \'sourcePath\' and \'source\' must not be used together.'));
}

if (config.xsltPath !== undefined || config.xslt !== undefined) {
if (config.xsltPath) {
tasks.xsltSource = function (callback) {
newFileInputStream(config.xsltPath, callback);
};
} else if (typeof config.xslt === 'string') {
tasks.xsltSource = function (callback) {
newReader(config.xslt, callback);
};
} else if (Buffer.isBuffer(config.xslt)) {
tasks.xsltSource = function (callback) {
newArrayInputStream(config.xslt, callback);
};
} else {
return callback(new Error('Unsupported format for property \'xslt\'. Has to be \'string\' or \'Buffer\'.'));
}
tasks.xsltStreamSource = ['xsltSource', function (callback, results) {
newStreamSource(results.xsltSource, callback);
}];
tasks.transformer = ['factory', 'xsltStreamSource', function (callback, results) {
results.factory.newTransformer(results.xsltStreamSource, callback);
}];
} else {
tasks.transformer = ['factory', function (callback, results) {
results.factory.newTransformer(callback);
}];
}

if (config.sourcePath) {
tasks.source = function (callback) {
newFileInputStream(config.sourcePath, callback);
};
} else if (typeof config.source === 'string') {
tasks.source = function (callback) {
newReader(config.source, callback);
};
} else if (Buffer.isBuffer(config.source)) {
tasks.source = function (callback) {
newArrayInputStream(config.source, callback);
};
} else {
return callback(new Error('Property \'sourcePath\' or \'source\' has to be given.'));
}

if (typeof config.result === 'string') {
tasks.result = function (callback) {
newFileOutputStream(config.result, callback);
};
} else if (isType('String', config.result)) {
tasks.result = function (callback) {
newWriter(callback);
};
} else if (isType('Buffer', config.result)) {
tasks.result = function (callback) {
newArrayOutputStream(callback);
};
} else {
return callback(new Error('Property \'result\' has to be given.'));
}

if (config.params) {
addTask('params', tasks, config.params, 'setParameter');
}

if (config.props) {
addTask('props', tasks, config.props, 'setOutputProperty');
}

async.auto(
tasks,
function (err, results) {
if (results.source) {
results.source.closeSync();
}
if (results.result) {
results.result.closeSync();
}
if (err) {
callback(err.cause ? new Error(err.cause.getLocalizedMessageSync()) : err);
} else if (isType('String', config.result)) {
results.result.toString(callback);
} else if (isType('Buffer', config.result)) {
results.result.toByteArray(function (err, result) {
if (err) {
callback(err);
} else {
callback(null, new Buffer(result));
}
});
} else {
callback(null);
}
}
);
};
25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "xslt4node",
"version": "0.1.0",
"description": "A XSLT package wrapping the XSLT interface of the Java API for XML Processing (JAXP)",
"dependencies": {
"java": ">=0.3.1",
"async": ">=0.4.0"
},
"devDependencies": {
"nodeunit": ">0.0.0"
},
"author": {
"name": "E2E Technologies Ltd",
"email": "[email protected]",
"url": "http://e2ebridge.com"
},
"license": "MIT",
"keywords": [
"xslt"
],
"readmeFilename": "README.md",
"scripts": {
"test": "nodeunit test"
}
}
1 change: 1 addition & 0 deletions test/discount.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:param name="discount"/><xsl:template match="/"><order><xsl:variable name="sub-total" select="sum(//price)"/><total><xsl:value-of select="$sub-total"/></total>15% discount if paid by: <xsl:value-of select="$discount"/></order></xsl:template></xsl:stylesheet>
3 changes: 3 additions & 0 deletions test/order.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!--Represents a customer order--><order><book ISBN="10-861003-324"><title>The Handmaid's Tale</title><price>19.95</price></book><cd ISBN="2-3631-4"><title>Americana</title><price>16.95</price>
</cd>
</order>
1 change: 1 addition & 0 deletions test/result.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version="1.0" encoding="UTF-8"?><order><total>36.9</total>15% discount if paid by: 1972/01/01</order>
Loading

0 comments on commit 8e65be8

Please sign in to comment.