-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8e65be8
Showing
10 changed files
with
502 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea/ | ||
javaLibs/ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.