mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-18 07:56:01 +00:00
basic XML import works + small fixes
This commit is contained in:
@@ -1,19 +1,68 @@
|
||||
const fs = require('fs');
|
||||
const stream = require('stream');
|
||||
|
||||
function escapeXml(value) {
|
||||
return value.replace(/[<>&'"]/g, function (c) {
|
||||
switch (c) {
|
||||
case '<':
|
||||
return '<';
|
||||
case '>':
|
||||
return '>';
|
||||
case '&':
|
||||
return '&';
|
||||
case "'":
|
||||
return ''';
|
||||
case '"':
|
||||
return '"';
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
class StringifyStream extends stream.Transform {
|
||||
constructor() {
|
||||
constructor({ itemElementName, rootElementName }) {
|
||||
super({ objectMode: true });
|
||||
this.itemElementName = itemElementName;
|
||||
this.rootElementName = rootElementName;
|
||||
|
||||
this.startElement(this.rootElementName);
|
||||
}
|
||||
|
||||
startElement(element) {
|
||||
this.push('<');
|
||||
this.push(element);
|
||||
this.push('>\n');
|
||||
}
|
||||
|
||||
endElement(element) {
|
||||
this.push('</');
|
||||
this.push(element);
|
||||
this.push('>\n');
|
||||
}
|
||||
|
||||
elementValue(element, value) {
|
||||
this.startElement(element);
|
||||
this.push(escapeXml(`${value}`));
|
||||
this.endElement(element);
|
||||
}
|
||||
|
||||
_transform(chunk, encoding, done) {
|
||||
this.push(JSON.stringify(chunk) + '\n');
|
||||
this.startElement(this.itemElementName);
|
||||
for (const key of Object.keys(chunk)) {
|
||||
this.elementValue(key, chunk[key]);
|
||||
}
|
||||
this.endElement(this.itemElementName);
|
||||
done();
|
||||
}
|
||||
|
||||
_final(callback) {
|
||||
this.endElement(this.rootElementName);
|
||||
callback();
|
||||
}
|
||||
}
|
||||
|
||||
async function writer({ fileName, encoding = 'utf-8' }) {
|
||||
async function writer({ fileName, encoding = 'utf-8', itemElementName, rootElementName }) {
|
||||
console.log(`Writing file ${fileName}`);
|
||||
const stringify = new StringifyStream();
|
||||
const stringify = new StringifyStream({ itemElementName, rootElementName });
|
||||
const fileStream = fs.createWriteStream(fileName, encoding);
|
||||
stringify.pipe(fileStream);
|
||||
stringify['finisher'] = fileStream;
|
||||
|
||||
Reference in New Issue
Block a user