isoxml
Version:
JavaScript library to parse and generate ISOXML (ISO11783-10) files
87 lines (86 loc) • 4.68 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { getEntityClassByTag, registerEntityClass } from '../classRegistry';
import { ISO11783TaskDataFile, ISO11783TaskDataFileDataTransferOriginEnum } from '../baseEntities/ISO11783TaskDataFile';
import { TAGS } from '../baseEntities/constants';
import { ExtendedAttachedFile } from './AttachedFile';
import { xml2js } from '../xmlManager';
function isoxmlManagerOptionsToAttributes(isoxmlManager) {
const opts = isoxmlManager.options;
return {
VersionMajor: (opts.version === 4 ? '4' : '3'),
VersionMinor: (opts.version === 4 ? '2' : '3'),
ManagementSoftwareManufacturer: opts.fmisTitle,
ManagementSoftwareVersion: opts.fmisVersion,
DataTransferOrigin: ISO11783TaskDataFileDataTransferOriginEnum.FMIS
};
}
// This implementation doesn't support generation of External Files. Particularly:
// * During parsing from XML, all the external files will be parsed and merged into the instance of this class.
// All the XFR tags will be removed
// * During saving to XML, no external files will be saved even if XFR tags were manually added by user
export class ExtendedISO11783TaskDataFile extends ISO11783TaskDataFile {
constructor(attributes, isoxmlManager) {
super(attributes, isoxmlManager);
this.tag = TAGS.ISO11783TaskDataFile;
}
static fromISOXMLManagerOptions(isoxmlManager) {
return new ExtendedISO11783TaskDataFile(isoxmlManagerOptionsToAttributes(isoxmlManager), isoxmlManager);
}
static fromXML(xml, isoxmlManager, internalId) {
return __awaiter(this, void 0, void 0, function* () {
const entity = yield ISO11783TaskDataFile.fromXML(xml, isoxmlManager, internalId, ExtendedISO11783TaskDataFile);
// parse all external files and add them to the main task data file
const externalFiles = entity.attributes.ExternalFileReference || [];
for (const externalFile of externalFiles) {
const filename = externalFile.attributes.Filename;
const data = yield isoxmlManager.getParsedFile(`${filename}.XML`, false);
const xml = xml2js(data);
const fileContent = yield getEntityClassByTag('main', TAGS.ExternalFileContents)
.fromXML(xml[TAGS.ExternalFileContents][0], isoxmlManager, filename);
entity.appendFromExternalFile(fileContent);
}
entity.attributes.ExternalFileReference = [];
isoxmlManager.options.version = entity.attributes.VersionMajor === '4' ? 4 : 3;
isoxmlManager.options.fmisTitle = entity.attributes.ManagementSoftwareManufacturer;
isoxmlManager.options.fmisVersion = entity.attributes.ManagementSoftwareVersion;
return entity;
});
}
toXML() {
if (this.isoxmlManager.options.version === 4) {
this.attributes.VersionMajor = '4';
this.attributes.VersionMinor = '2';
}
else {
this.attributes.VersionMajor = '3';
this.attributes.VersionMinor = '3';
}
this.attributes.ManagementSoftwareManufacturer = this.isoxmlManager.options.fmisTitle;
this.attributes.ManagementSoftwareVersion = this.isoxmlManager.options.fmisVersion;
return super.toXML();
}
appendFromExternalFile(fileContents) {
Object.keys(fileContents.attributes).forEach(attrName => {
this.attributes[attrName] = [
...(this.attributes[attrName] || []),
...fileContents.attributes[attrName]
];
});
}
addLinkListFile() {
const withoutLinkList = (this.attributes.AttachedFile || []).filter(file => file.attributes.FileType !== 1);
this.attributes.AttachedFile = [
...withoutLinkList,
ExtendedAttachedFile.linkListFromISOXMLManager(this.isoxmlManager)
];
}
}
registerEntityClass('main', TAGS.ISO11783TaskDataFile, ExtendedISO11783TaskDataFile);