isoxml
Version:
JavaScript library to parse and generate ISOXML (ISO11783-10) files
68 lines (67 loc) • 3.33 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 { AttachedFile, AttachedFilePreserveEnum } from "../baseEntities";
import { TAGS } from "../baseEntities/constants";
import { registerEntityClass } from "../classRegistry";
import { js2xml, xml2js } from '../xmlManager';
import { ExtendedISO11783LinkListFile } from "./ISO11783LinkListFile";
export class ExtendedAttachedFile extends AttachedFile {
constructor(attributes, isoxmlManager) {
super(attributes, isoxmlManager);
this.tag = TAGS.AttachedFile;
}
static fromXML(xml, isoxmlManager, internalId) {
return __awaiter(this, void 0, void 0, function* () {
const entity = yield 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 = xml2js(linkListString);
}
catch (e) {
throw new Error('Failed to parse LinkList file');
}
yield ExtendedISO11783LinkListFile.fromXML(linkListXml[TAGS.ISO11783LinkListFile][0], isoxmlManager, 'LINKLIST');
}
else {
entity.fileData = yield isoxmlManager.getParsedFile(filename, true);
}
return entity;
});
}
toXML() {
if (this.attributes.FileType === 1) {
const linkListFile = ExtendedISO11783LinkListFile.fromISOXMLManager(this.isoxmlManager);
const json = {
[TAGS.ISO11783LinkListFile]: linkListFile.toXML()
};
const xmlString = 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: AttachedFilePreserveEnum.PreserveOnTaskControllerAndSendBackToFMIS,
FileType: 1,
ManufacturerGLN: ''
}, isoxmlManager);
}
}
registerEntityClass('main', TAGS.AttachedFile, ExtendedAttachedFile);