2018-09-20 00:41:57 +01:00
|
|
|
// tslint:disable:no-any
|
2018-10-31 21:57:10 +00:00
|
|
|
import { Element as XmlElement, xml2js } from "xml-js";
|
2018-09-20 00:41:57 +01:00
|
|
|
import { IXmlableObject, XmlComponent } from ".";
|
2018-10-16 11:28:25 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts the given xml element (in json format) into XmlComponent.
|
|
|
|
* @param element the xml element in json presentation
|
|
|
|
*/
|
2018-10-17 09:15:32 +03:00
|
|
|
|
|
|
|
export function convertToXmlComponent(element: XmlElement): ImportedXmlComponent | string | undefined {
|
|
|
|
switch (element.type) {
|
2018-10-31 21:57:10 +00:00
|
|
|
case undefined:
|
2018-10-17 09:15:32 +03:00
|
|
|
case "element":
|
|
|
|
const xmlComponent = new ImportedXmlComponent(element.name as string, element.attributes);
|
|
|
|
const childElments = element.elements || [];
|
|
|
|
for (const childElm of childElments) {
|
|
|
|
const child = convertToXmlComponent(childElm);
|
|
|
|
if (child !== undefined) {
|
|
|
|
xmlComponent.push(child);
|
|
|
|
}
|
2018-10-16 11:28:25 +03:00
|
|
|
}
|
2018-10-17 09:15:32 +03:00
|
|
|
return xmlComponent;
|
|
|
|
case "text":
|
|
|
|
return element.text as string;
|
|
|
|
default:
|
|
|
|
return undefined;
|
2018-10-16 11:28:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-28 14:54:07 +02:00
|
|
|
/**
|
|
|
|
* Represents imported xml component from xml file.
|
|
|
|
*/
|
|
|
|
export class ImportedXmlComponent extends XmlComponent {
|
2018-10-31 21:57:10 +00:00
|
|
|
/**
|
|
|
|
* Converts the xml string to a XmlComponent tree.
|
|
|
|
*
|
|
|
|
* @param importedContent xml content of the imported component
|
|
|
|
*/
|
|
|
|
public static fromXmlString(importedContent: string): ImportedXmlComponent {
|
|
|
|
const xmlObj = xml2js(importedContent, { compact: false }) as XmlElement;
|
|
|
|
return convertToXmlComponent(xmlObj) as ImportedXmlComponent;
|
|
|
|
}
|
2018-09-20 00:41:57 +01:00
|
|
|
/**
|
|
|
|
* Converts the xml string to a XmlComponent tree.
|
|
|
|
*
|
|
|
|
* @param importedContent xml content of the imported component
|
|
|
|
*/
|
|
|
|
|
|
|
|
// tslint:disable-next-line:variable-name
|
|
|
|
private readonly _attr: any;
|
|
|
|
|
|
|
|
// tslint:disable-next-line:variable-name
|
2018-03-28 14:54:07 +02:00
|
|
|
constructor(rootKey: string, _attr?: any) {
|
|
|
|
super(rootKey);
|
|
|
|
if (_attr) {
|
|
|
|
this._attr = _attr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transforms the object so it can be converted to xml. Example:
|
|
|
|
* <w:someKey someAttr="1" otherAttr="11">
|
|
|
|
* <w:child childAttr="2">
|
|
|
|
* </w:child>
|
|
|
|
* </w:someKey>
|
|
|
|
* {
|
|
|
|
* 'w:someKey': [
|
|
|
|
* {
|
|
|
|
* _attr: {
|
|
|
|
* someAttr: "1",
|
|
|
|
* otherAttr: "11"
|
|
|
|
* }
|
|
|
|
* },
|
|
|
|
* {
|
|
|
|
* 'w:child': [
|
|
|
|
* {
|
|
|
|
* _attr: {
|
|
|
|
* childAttr: "2"
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ]
|
|
|
|
* }
|
|
|
|
* ]
|
|
|
|
* }
|
|
|
|
*/
|
2018-09-20 00:41:57 +01:00
|
|
|
public prepForXml(): IXmlableObject | undefined {
|
2018-03-28 14:54:07 +02:00
|
|
|
const result = super.prepForXml();
|
2018-09-20 00:41:57 +01:00
|
|
|
if (!result) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2018-03-28 14:54:07 +02:00
|
|
|
if (!!this._attr) {
|
|
|
|
if (!Array.isArray(result[this.rootKey])) {
|
|
|
|
result[this.rootKey] = [result[this.rootKey]];
|
|
|
|
}
|
|
|
|
result[this.rootKey].unshift({ _attr: this._attr });
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-10-16 11:28:25 +03:00
|
|
|
public push(xmlComponent: XmlComponent | string): void {
|
2018-03-28 14:54:07 +02:00
|
|
|
this.root.push(xmlComponent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used for the attributes of root element that is being imported.
|
|
|
|
*/
|
|
|
|
export class ImportedRootElementAttributes extends XmlComponent {
|
2018-09-20 00:41:57 +01:00
|
|
|
// tslint:disable-next-line:variable-name
|
|
|
|
constructor(private readonly _attr: any) {
|
2018-03-28 14:54:07 +02:00
|
|
|
super("");
|
|
|
|
}
|
|
|
|
|
|
|
|
public prepForXml(): IXmlableObject {
|
|
|
|
return {
|
|
|
|
_attr: this._attr,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|