basic XML import works + small fixes

This commit is contained in:
Jan Prochazka
2022-01-30 11:26:10 +01:00
parent 91a77765b6
commit 8bad6da348
7 changed files with 75 additions and 18 deletions

View File

@@ -18,7 +18,7 @@ export class JslGridDisplay extends GridDisplay {
this.filterable = true; this.filterable = true;
if (structure.columns) { if (structure?.columns) {
this.columns = _.uniqBy( this.columns = _.uniqBy(
structure.columns structure.columns
.map(col => ({ .map(col => ({
@@ -39,7 +39,7 @@ export class JslGridDisplay extends GridDisplay {
); );
} }
if (structure.__isDynamicStructure) { if (structure?.__isDynamicStructure) {
this.columns = analyseCollectionDisplayColumns(rows, this); this.columns = analyseCollectionDisplayColumns(rows, this);
} }

View File

@@ -9,7 +9,7 @@
rows: rowFunc ? model.rows.map(rowFunc) : model.rows, rows: rowFunc ? model.rows.map(rowFunc) : model.rows,
structure: { structure: {
...model.structure, ...model.structure,
columns: func(model.structure.columns), columns: func(model.structure?.columns),
}, },
}, },
}); });
@@ -40,7 +40,7 @@
</script> </script>
<ManagerInnerContainer width={managerSize}> <ManagerInnerContainer width={managerSize}>
{#each structure.columns || [] as column, index} {#each structure?.columns || [] as column, index}
{#if index == editingColumn} {#if index == editingColumn}
<ColumnNameEditor <ColumnNameEditor
defaultValue={column.columnName} defaultValue={column.columnName}
@@ -54,7 +54,7 @@
onBlur={() => (editingColumn = null)} onBlur={() => (editingColumn = null)}
focusOnCreate focusOnCreate
blurOnEnter blurOnEnter
existingNames={structure.columns.map(x => x.columnName)} existingNames={structure?.columns.map(x => x.columnName)}
/> />
{:else} {:else}
<ColumnManagerRow <ColumnManagerRow
@@ -77,6 +77,6 @@
dispatchChangeColumns($$props, cols => [...cols, { columnName }]); dispatchChangeColumns($$props, cols => [...cols, { columnName }]);
}} }}
placeholder="New column" placeholder="New column"
existingNames={(structure.columns || []).map(x => x.columnName)} existingNames={(structure?.columns || []).map(x => x.columnName)}
/> />
</ManagerInnerContainer> </ManagerInnerContainer>

View File

@@ -38,6 +38,7 @@
'icon file': 'mdi mdi-file', 'icon file': 'mdi mdi-file',
'icon loading': 'mdi mdi-loading mdi-spin', 'icon loading': 'mdi mdi-loading mdi-spin',
'icon close': 'mdi mdi-close', 'icon close': 'mdi mdi-close',
'icon stop': 'mdi mdi-close-octagon',
'icon filter': 'mdi mdi-filter', 'icon filter': 'mdi mdi-filter',
'icon filter-off': 'mdi mdi-filter-off', 'icon filter-off': 'mdi mdi-filter-off',
'icon reload': 'mdi mdi-reload', 'icon reload': 'mdi mdi-reload',

View File

@@ -174,7 +174,7 @@
<svelte:fragment slot="footer"> <svelte:fragment slot="footer">
<div class="flex m-2"> <div class="flex m-2">
{#if busy} {#if busy}
<LargeButton icon="icon close" on:click={handleCancel}>Cancel</LargeButton> <LargeButton icon="icon stop" on:click={handleCancel}>Stop</LargeButton>
{:else} {:else}
<LargeFormButton on:click={handleExecute} icon="icon run">Run</LargeFormButton> <LargeFormButton on:click={handleExecute} icon="icon run">Run</LargeFormButton>
{/if} {/if}

View File

@@ -3,7 +3,7 @@ const stream = require('stream');
const NodeXmlStream = require('node-xml-stream'); const NodeXmlStream = require('node-xml-stream');
class ParseStream extends stream.Transform { class ParseStream extends stream.Transform {
constructor({ elementName }) { constructor({ itemElementName }) {
super({ objectMode: true }); super({ objectMode: true });
this.rowsWritten = 0; this.rowsWritten = 0;
this.parser = new NodeXmlStream(); this.parser = new NodeXmlStream();
@@ -17,7 +17,7 @@ class ParseStream extends stream.Transform {
} }
}); });
this.parser.on('closetag', (name, attrs) => { this.parser.on('closetag', (name, attrs) => {
if (name == elementName) { if (name == itemElementName) {
this.rowsWritten += 1; this.rowsWritten += 1;
this.push({ ...this.stack[this.stack.length - 1].attrs, ...this.stack[this.stack.length - 1].nodes }); this.push({ ...this.stack[this.stack.length - 1].attrs, ...this.stack[this.stack.length - 1].nodes });
} }
@@ -30,11 +30,11 @@ class ParseStream extends stream.Transform {
} }
} }
async function reader({ fileName, encoding = 'utf-8', elementName }) { async function reader({ fileName, encoding = 'utf-8', itemElementName }) {
console.log(`Reading file ${fileName}`); console.log(`Reading file ${fileName}`);
const fileStream = fs.createReadStream(fileName, encoding); const fileStream = fs.createReadStream(fileName, encoding);
const parser = new ParseStream({ elementName }); const parser = new ParseStream({ itemElementName });
fileStream.pipe(parser); fileStream.pipe(parser);
return parser; return parser;
} }

View File

@@ -1,19 +1,68 @@
const fs = require('fs'); const fs = require('fs');
const stream = require('stream'); const stream = require('stream');
function escapeXml(value) {
return value.replace(/[<>&'"]/g, function (c) {
switch (c) {
case '<':
return '&lt;';
case '>':
return '&gt;';
case '&':
return '&amp;';
case "'":
return '&apos;';
case '"':
return '&quot;';
}
});
}
class StringifyStream extends stream.Transform { class StringifyStream extends stream.Transform {
constructor() { constructor({ itemElementName, rootElementName }) {
super({ objectMode: true }); 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) { _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(); 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}`); console.log(`Writing file ${fileName}`);
const stringify = new StringifyStream(); const stringify = new StringifyStream({ itemElementName, rootElementName });
const fileStream = fs.createWriteStream(fileName, encoding); const fileStream = fs.createWriteStream(fileName, encoding);
stringify.pipe(fileStream); stringify.pipe(fileStream);
stringify['finisher'] = fileStream; stringify['finisher'] = fileStream;

View File

@@ -14,9 +14,16 @@ const fileFormat = {
args: [ args: [
{ {
type: 'text', type: 'text',
name: 'elementName', name: 'rootElementName',
label: 'Element name', label: 'Root element name',
apiName: 'elementName', apiName: 'rootElementName',
direction: 'target',
},
{
type: 'text',
name: 'itemElementName',
label: 'Item element name',
apiName: 'itemElementName',
}, },
], ],
}; };