-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
207 lines (176 loc) · 5.69 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// node modules
const express = require('express');
const bodyParser = require('body-parser');
const fileUpload = require('express-fileupload');
const readSync = require('read-file-relative').readSync;
const xml2js = require('xml2js');
const libxslt = require('libxslt');
const viz = require('viz.js');
const async = require('async');
const cheerio = require('cheerio');
const axios = require('axios');
const plantumlEncoder = require('plantuml-encoder')
// xml/js transformers
const parseXmlString = xml2js.parseString;
const xmlBuilder = new xml2js.Builder();
// express server setup
const app = express();
app.use(bodyParser.text({ type: 'application/xml' }));
app.use(bodyParser.json());
app.use(fileUpload());
app.use(express.static('public'));
const PORT = 3001;
app.listen(PORT);
// preload xml stylesheets
const htmlxslt = readSync('/IngestLddView.xsl');
const dotxslt = readSync('/IngestLddDot.xsl');
const umlxslt = readSync('/IngestLddPlantUml.xsl');
console.log(`\nname: ldd-transform\nport: ${PORT}\n`);
//////////////////ENDPOINTS////////////////////
// reports an error if present. returns whether or not an error was sent
function reportError(err, res, callback) {
if (err) {
let errorMessage = err;
if( err instanceof Error) {
errorMessage = err.message;
}
if(res && errorMessage && errorMessage.length > 0) {
res.status(500).send(errorMessage);
return true;
}
if (callback) {
callback(err);
}
}
return false;
}
// this function assumes there is only one file in the request, and returns its contents
function extractFile(req) {
if (!req.files) {
return res.status(400).send('No files were uploaded.');
}
for(const key in req.files) {
return req.files[key].data.toString();
}
}
/*----------------XML to JSON----------------*/
app.post('/xml/to/json', function (req, res) {
let xml = req.body
xmlToJson(xml, res);
});
app.post('/file/to/json', function(req, res) {
const file = extractFile(req);
xmlToJson(file, res);
})
function xmlToJson(xml, res) {
parseXmlString(xml, function(err, result) {
reportError(err, res) || res.json(result);
})
}
/*----------------JSON to XML----------------*/
app.post('/json/to/xml', function (req, res) {
let object = req.body
let xml = xmlBuilder.buildObject(object);
res.send(xml);
});
/*----------------XML to HTML----------------*/
app.post('/xml/to/html', function(req, res) {
let xml = req.body;
xmlToHtml(xml, res);
});
app.post('/file/to/html', function(req, res) {
const file = extractFile(req);
xmlToHtml(file, res);
})
function xmlToHtml(xml, res, callback) {
libxslt.parse(htmlxslt, function(err, stylesheet) {
if (!reportError(err, res, callback)) {
stylesheet.apply(xml, function(err, result) {
if (!reportError(err, res, callback)) {
if (res) res.send(result);
if (callback) callback(null, result);
}
})
}
});
}
/*----------------XML to Graph----------------*/
app.post('/xml/to/graph', function(req, res) {
let xml = req.body;
xmlToGraph(xml, res);
});
app.post('/file/to/graph', function(req, res) {
const file = extractFile(req);
xmlToGraph(file, res);
})
function xmlToGraph(xml, res, callback) {
libxslt.parse(dotxslt, function(err, stylesheet) {
if (!reportError(err, res, callback)) {
stylesheet.apply(xml, function(err, result) {
if (!reportError(err, res, callback)) {
try {
let svg = viz(result);
if( res ) res.send(svg);
if( callback ) callback(null, svg);
} catch (vizErr) {
reportError("Error visualizing graph", res);
}
}
})
}
});
}
/*----------------XML to UML----------------*/
app.post('/xml/to/uml', function(req, res) {
let xml = req.body;
xmlToUml(xml, res);
});
app.post('/file/to/uml', function(req, res) {
const file = extractFile(req);
xmlToUml(file, res);
})
function xmlToUml(xml, res) {
libxslt.parse(umlxslt, function(err, stylesheet) {
if (!reportError(err, res)) {
stylesheet.apply(xml, function(err, result) {
if (!reportError(err, res)) {
let compressed = plantumlEncoder.encode(result);
console.log(compressed)
axios.get(`http://plantuml:8080/svg/${compressed}`).then((response) => {
res.send(response.data);
}, (err) => {
reportError(err, res);
})
}
})
}
});
}
/*----------------XML to Doc----------------*/
app.post('/xml/to/doc', function(req, res) {
let xml = req.body;
buildDoc(xml, res);
});
app.post('/file/to/doc', function(req, res) {
const file = extractFile(req);
buildDoc(file, res);
})
function buildDoc(xml, res) {
async.parallel(
[
function(callback) { xmlToHtml(xml, null, callback) },
function(callback) { xmlToGraph(xml, null, callback) }
],
function(err, results) {
reportError(err, res) || compile(results[0], results[1]);
}
)
function compile(html, graph) {
let $h = cheerio.load(html);
let $g = cheerio.load(graph, { xmlMode: true });
$g('svg').css('max-width', '100%').css('height', 'auto');
$h('h1').first().after($g.xml('svg'));
$h('title').text($h('h1').first().text());
res.send($h.html());
}
}