Skip to content

Commit

Permalink
Handle <Aliases> tag parsing better, disable CDATA interpolation,…
Browse files Browse the repository at this point in the history
… add unformatted download option (#28)

* change cdata option parsing and add single entity parsing for single alias entry

* bump v0.2.0

* change cdata option parsing and add single entity parsing for single alias entry

* bump v0.2.0

* add unformatted option
  • Loading branch information
mwood77 authored Apr 13, 2024
1 parent 8848b3e commit b993bfe
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 14 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pf2open",
"version": "0.1.9",
"version": "0.2.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
Expand Down
27 changes: 18 additions & 9 deletions src/app/services/converter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class ConverterService {
this.displayConversionCard$.next(false);
}

async convert(file: File) {
async convert(file: File, pretty?: boolean) {
this.displayConversionCard$.next(false);
this.displayConversionCard$.next(true);

Expand All @@ -49,8 +49,7 @@ export class ConverterService {

// Correctly handle CDATA tags
const parserOoptions = {
cdataPropName: "CDATA",
leadingZeros: true,
numberParseOptions: {hex: false, leadingZeros: true}
}

const parser = new XMLParser(parserOoptions);
Expand All @@ -63,7 +62,7 @@ export class ConverterService {
subscriber.error(opnJson); // bubble error message up to the calling method
}

const opnXml = that.jsonToXML(opnJson as opnRoot);
const opnXml = that.jsonToXML(opnJson as opnRoot, pretty);

that.conversionAvailable$.next(true);
subscriber.next(opnXml); // opnXml object will be returned to the calling method
Expand Down Expand Up @@ -139,11 +138,14 @@ export class ConverterService {
let mappedAlias = Array<opnAlias>();

for (const [key, value] of Object.entries(pfAliases)) {
if (key === 'alias') {
if (key === 'alias' && value instanceof Array) {
value.forEach((alias: pfAlias) => {
mappedAlias.push(this.mapAliasEntity(alias));
});
}
if (key === 'alias' && !(value instanceof Array)) {
mappedAlias.push(this.mapAliasEntity(value));
}
};

aliases = mappedAlias
Expand Down Expand Up @@ -198,10 +200,14 @@ export class ConverterService {
return arrayBuilder.build(array);
}

jsonToXML(opnJson: opnRoot) {
jsonToXML(opnJson: opnRoot, pretty?: boolean) {

const shouldPretty =
pretty != undefined ?
pretty : true;

const builder = new XMLBuilder({
format: true,
format: shouldPretty,
// Correctly encode handled CDATA tags
cdataPropName: 'CDATA',
});
Expand All @@ -215,10 +221,13 @@ export class ConverterService {
const result = builder.build(opnJson);

const split = result.toString().split("</system>");
const splitWithAliases = [split[0], `<aliases>${additionalArrays}</aliases>`, split[1]];
const splitWithAliases = [split[0]+'</system>', `<aliases>${additionalArrays}</aliases>`, split[1]];
const concatenatedResult = splitWithAliases.join('');

return xmlFormat(concatenatedResult);
if (shouldPretty) {
return xmlFormat(concatenatedResult, {});
}
return concatenatedResult.replaceAll('\n', '');
}

const result = builder.build(opnJson);
Expand Down
15 changes: 13 additions & 2 deletions src/app/upload/upload.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,20 @@
<!-- Everything worked out! -->
<div *ngIf="this.conversionAvailable && !this.conversionError">
<div class="center">

<div class="spacer"></div>
<button mat-stroked-button color="accent" class="convert-button" (click)="download()">
<div class="convert-button-text">Download Generated Config</div>
<div class="convert-button-text">Download generated config</div>
</button>
</div>

<div class="spacer"></div>

<div class="center">
<p class="text">Having trouble with whitespace during import?</p>
</div>
<div class="center">
<button mat-stroked-button class="convert-button" (click)="downloadNoPretty()">
<div class="convert-button-text">Download generated config without formatting</div>
</button>
</div>

Expand Down
28 changes: 28 additions & 0 deletions src/app/upload/upload.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class UploadComponent implements OnInit, OnDestroy {
fileName = '';
unsupportedFile = false;
renderedXML: any = null;
renderedNotPrettyXML: any = null;
conversionError = false;

constructor(private converterService: ConverterService) {
Expand Down Expand Up @@ -58,6 +59,19 @@ export class UploadComponent implements OnInit, OnDestroy {
document.body.removeChild(element);
}

downloadNoPretty() {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(this.renderedNotPrettyXML));
element.setAttribute('download', 'unformatted-pf2opn-generated-opnsense-config.xml');

element.style.display = 'none';
document.body.appendChild(element);

element.click();

document.body.removeChild(element);
}

async onFileSelected(event: any) {
const file: File = event.target.files[0];
this.fileName = file.name;
Expand All @@ -80,6 +94,20 @@ export class UploadComponent implements OnInit, OnDestroy {
this.progressColor = 'warn';
},
);
(await this.converterService.convert(file, false)).subscribe(
res => {
this.progressMode = 'determinate';
this.incrementProgress();
this.renderedNotPrettyXML = res;
this.conversionError = false
},
err => {
this.renderedNotPrettyXML = err;
console.error('something went boom: ' + err);
this.conversionError = true
this.progressColor = 'warn';
},
);
} else {
this.unsupportedFile = true;
this.converterService.cancel();
Expand Down

0 comments on commit b993bfe

Please sign in to comment.