UNPKG

isoxml-angular

Version:

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

71 lines (70 loc) 3.63 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.ExtendedAttachedFile = void 0; const baseEntities_1 = require("../baseEntities"); const classRegistry_1 = require("../classRegistry"); const xmlManager_1 = require("../xmlManager"); const ISO11783LinkListFile_1 = require("./ISO11783LinkListFile"); class ExtendedAttachedFile extends baseEntities_1.AttachedFile { constructor(attributes, isoxmlManager) { super(attributes, isoxmlManager); this.tag = "AFE" /* TAGS.AttachedFile */; } static fromXML(xml, isoxmlManager, internalId) { return __awaiter(this, void 0, void 0, function* () { const entity = yield baseEntities_1.AttachedFile.fromXML(xml, isoxmlManager, internalId, ExtendedAttachedFile); const filename = entity.attributes.FilenameWithExtension; if (filename.toUpperCase() !== filename) { isoxmlManager.addWarning(`[${internalId}] Name of attached file must be uppercase (real name: ${filename})`); } if (entity.attributes.FileType === 1) { const linkListString = yield isoxmlManager.getParsedFile(filename, false); let linkListXml; try { linkListXml = (0, xmlManager_1.xml2js)(linkListString); } catch (e) { throw new Error('Failed to parse LinkList file'); } yield ISO11783LinkListFile_1.ExtendedISO11783LinkListFile.fromXML(linkListXml["ISO11783LinkList" /* TAGS.ISO11783LinkListFile */][0], isoxmlManager, 'LINKLIST'); } else { entity.fileData = yield isoxmlManager.getParsedFile(filename, true); } return entity; }); } toXML() { if (this.attributes.FileType === 1) { const linkListFile = ISO11783LinkListFile_1.ExtendedISO11783LinkListFile.fromISOXMLManager(this.isoxmlManager); const json = { ["ISO11783LinkList" /* TAGS.ISO11783LinkListFile */]: linkListFile.toXML() }; const xmlString = (0, xmlManager_1.js2xml)(json); this.isoxmlManager.addFileToSave(xmlString, this.attributes.FilenameWithExtension); } else { this.isoxmlManager.addFileToSave(this.fileData, this.attributes.FilenameWithExtension); } return super.toXML(); } static linkListFromISOXMLManager(isoxmlManager) { return new ExtendedAttachedFile({ FilenameWithExtension: 'LINKLIST.XML', Preserve: "2" /* AttachedFilePreserveEnum.PreserveOnTaskControllerAndSendBackToFMIS */, FileType: 1, ManufacturerGLN: '' }, isoxmlManager); } } exports.ExtendedAttachedFile = ExtendedAttachedFile; (0, classRegistry_1.registerEntityClass)('main', "AFE" /* TAGS.AttachedFile */, ExtendedAttachedFile);