-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
73 lines (61 loc) · 2.5 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
var js2xmlparser = require('js2xmlparser');
var java = require('java');
var xml2js = require('xml2js');
var path = require('path');
var javaClassDependencies = [
path.join(__dirname, 'java_dependencies', 'node-hl7-complete-4.0.0-SNAPSHOT.jar'),
path.join(__dirname, 'java_dependencies', 'hapi-base-2.3.jar'),
path.join(__dirname, 'java_dependencies', 'slf4j-api-1.7.16.jar'),
path.join(__dirname, 'java_dependencies', 'hapi-osgi-base-2.3.jar')
];
java.classpath = java.classpath.concat(javaClassDependencies);
var NodeHL7Complete = function(options) {
var javaBridgeParser = java.newInstanceSync('node_hl7_complete.hl7.Parser');
// This will set HL7 validation off/on. It is true by default in the Java impl.
var setStrictMode = function(trueOrFalse) {
javaBridgeParser.setStrictMode(trueOrFalse);
}
var hl7ToJs = function(hl7String, callback) {
javaBridgeParser.hl7ToXml(hl7String, function(javaBridgeParserError, responseString) {
if (javaBridgeParserError) {
callback(javaBridgeParserError, null);
// Unfortunately, the Java NPM module doesn't handle exceptions well... HL7 implementation
// code prefixes "ERROR:" for the String response if there was an exception.
} else if (responseString.indexOf('ERROR:') > -1) {
callback(responseString, null);
} else {
xml2js.parseString(responseString, function(xml2jsError, jsObject) {
if (xml2jsError) {
callback(xml2jsError, null);
} else {
callback(null, jsObject);
}
});
}
});
};
var jsToHl7 = function(dataType, jsData, callback) {
var xmlMessage = js2xmlparser.parse(dataType, jsData[dataType]);
var nameSpacedXmlMessage = xmlMessage
.replace('<' + dataType + '>', '<' + dataType + ' xmlns=\"urn:hl7-org:v2xml\">')
.replace(/[^\x20-\x7E]/gmi, '');
javaBridgeParser.xmlToHl7(nameSpacedXmlMessage, function(xml2Hl7Error, responseString) {
if (xml2Hl7Error) {
callback(xml2Hl7Error, null);
// Unfortunately, the Java NPM module doesn't handle exceptions well... HL7 implementation
// code prefixes "ERROR:" for the String response if there was an exception.
} else if (responseString.indexOf('ERROR:') > -1) {
callback(responseString, null);
} else {
callback(null, responseString);
}
});
};
// public API
return {
hl7ToJs: hl7ToJs,
jsToHl7: jsToHl7,
setStrictMode: setStrictMode
};
};
module.exports = NodeHL7Complete;