UNPKG

isoxml-angular

Version:

JavaScript library to parse and generate ISOXML (ISO11783-10) files

90 lines (89 loc) 4.96 kB
"use strict"; 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ExtendedISO11783TaskDataFile = void 0; const classRegistry_1 = require("../classRegistry"); const ISO11783TaskDataFile_1 = require("../baseEntities/ISO11783TaskDataFile"); const AttachedFile_1 = require("./AttachedFile"); const xmlManager_1 = require("../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: "1" /* 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 class ExtendedISO11783TaskDataFile extends ISO11783TaskDataFile_1.ISO11783TaskDataFile { constructor(attributes, isoxmlManager) { super(attributes, isoxmlManager); this.tag = "ISO11783_TaskData" /* 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_1.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 = (0, xmlManager_1.xml2js)(data); const fileContent = yield (0, classRegistry_1.getEntityClassByTag)('main', "XFC" /* TAGS.ExternalFileContents */) .fromXML(xml["XFC" /* 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, AttachedFile_1.ExtendedAttachedFile.linkListFromISOXMLManager(this.isoxmlManager) ]; } } exports.ExtendedISO11783TaskDataFile = ExtendedISO11783TaskDataFile; (0, classRegistry_1.registerEntityClass)('main', "ISO11783_TaskData" /* TAGS.ISO11783TaskDataFile */, ExtendedISO11783TaskDataFile);