-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
148 lines (138 loc) · 4.47 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"use strict";
const xml2js = require("xml2js");
const fs = require("fs");
const Execution = global.ExecutionClass;
class parseXmlJsonExecutor extends Execution {
constructor(process) {
super(process);
}
exec(params) {
let _this = this;
let endOptions = { end: "end" };
function parseToJson(xml) {
return new Promise(function (resolve, reject) {
let parser = new xml2js.Parser(params.json_options);
parser.parseString(xml, function (err, result) {
if (err) {
reject(err);
} else {
resolve(JSON.stringify(result));
}
});
});
}
function parseToXml(json) {
return new Promise(function (resolve, reject) {
let builder = new xml2js.Builder(params.xml_options);
let result = builder.buildObject(JSON.parse(json));
resolve(result);
});
}
function evaluateResults(results) {
return new Promise(function (resolve, reject) {
if (params.output_file) {
fs.writeFile(params.output_file, results, "utf8", function (err) {
if (err) {
reject(err);
} else {
resolve();
}
});
} else {
endOptions.data_output = results;
resolve();
}
_this.end(endOptions);
});
}
if (params.to === "json") {
new Promise(function (resolve, reject) {
let xml = "";
if (params.xml) {
xml = params.xml;
resolve(xml);
} else if (params.xml_file) {
fs.readFile(params.xml_file, function (err, result) {
if (err) {
reject(err);
} else {
xml = result;
resolve(xml);
}
});
} else {
reject(new Error(`Parsing to ${params.to}, params xml: ${params.xml} or xml_file: ${params.xml_file} not set correctly`));
}
}).then(xml => {
parseToJson(xml)
.then(results => {
evaluateResults(results)
.catch(err => {
endOptions.end = "error";
endOptions.messageLog = `writeToOutputFileJson: ${err}`;
endOptions.err_output = `writeToOutputFileJson: ${err}`;
_this.end(endOptions);
});
})
.catch(err => {
endOptions.end = "error";
endOptions.messageLog = `parseToJson: ${err}`;
endOptions.err_output = `parseToJson: ${err}`;
_this.end(endOptions);
});
}).catch(err => {
endOptions.end = "error";
endOptions.messageLog = `readFileToJson: ${err}`;
endOptions.err_output = `readFileToJson: ${err}`;
_this.end(endOptions);
});
} else if (params.to === "xml") {
new Promise(function (resolve, reject) {
let json = "";
if (params.json) {
json = params.json;
resolve(json);
} else if (params.json_file) {
fs.readFile(params.json_file, function (err, result) {
if (err) {
reject(err);
} else {
json = result;
resolve(json);
}
});
} else {
reject(new Error(`Parsing to ${params.to}, params json: ${params.json} or json_file: ${params.json_file} not set correctly`));
}
}).then(json => {
parseToXml(json)
.then(results => {
evaluateResults(results)
.catch(err => {
endOptions.end = "error";
endOptions.messageLog = `writeToOutputFileXml: ${err}`;
endOptions.err_output = `writeToOutputFileXml: ${err}`;
_this.end(endOptions);
});
})
.catch(err => {
endOptions.end = "error";
endOptions.messageLog = `parseToXml: ${err}`;
endOptions.err_output = `parseToXml: ${err}`;
_this.end(endOptions);
});
}).catch(err => {
endOptions.end = "error";
endOptions.messageLog = `readFileToXml: ${err}`;
endOptions.err_output = `readFileToXml: ${err}`;
_this.end(endOptions);
});
} else {
endOptions.end = "error";
endOptions.messageLog = `param 'to': ${params.to} not set correctly use to json/xml`;
endOptions.err_output = `param 'to': ${params.to} not set correctly use to json/xml`;
_this.end(endOptions);
}
}
}
module.exports = parseXmlJsonExecutor;