import {XMLParser, type X2jOptions} from 'fast-xml-parser'; import { UAObject } from './UAObject'; import type { UABaseNode } from './UABaseNode'; import { UAVariable } from './UAVariable'; import { NamespaceTable } from './NameSpaceTable'; import { Model } from './Model'; import { XMLElem, type IToXML } from '@/util/XmlElem'; export class UANodeSet implements IToXML{ constructor(public fileName: string, public models: Model[], public nodes: UABaseNode[], public nameSpaceTable: NamespaceTable) { } reIndex(nst: NamespaceTable) { //add all missing namespaces to addressspace ns table for(const value of this.nameSpaceTable.nsMap.getValues()) { nst.addUri(value); } for(const node of this.nodes) { node.reIndex(nst, this.nameSpaceTable); } } resolveChildren(nm: Map) { for(const node of this.nodes) { node.resolveChildren(nm); } } toXML(): XMLElem { const elem =new XMLElem('UANodeSet'); const xmlModels=elem.add(new XMLElem('Models')); for(const model of this.models) { xmlModels.add(model.toXML()) } return elem; } static async load(url: string) { const parseOptions:Partial={ ignoreAttributes: false, // eslint-disable-next-line @typescript-eslint/no-unused-vars isArray: (name, jpath, isLeafNode, isAttribute):boolean => { switch(jpath) { case 'UANodeSet.NamespaceUris.Uri': case 'UANodeSet.UAObject.References.Reference': case 'UANodeSet.UAObject': case 'UANodeSet.UAVariable': case 'UANodeSet.UAVariable.References.Reference': case 'UANodeSet.Models.Model': return true; default: return false; } } } const parser = new XMLParser(parseOptions); const xml= await (await fetch(url)).text() const xmlObj = parser.parse(xml); const models: Model[]=[]; const xmlModels=xmlObj['UANodeSet']['Models']; for(const xmlModel of xmlModels.Model) { models.push(Model.fromXML(xmlModel)); } const nodes:UABaseNode[]=[]; const xmlObjects=xmlObj['UANodeSet']['UAObject']; for(const xmlObject of xmlObjects) { nodes.push(UAObject.fromXML(xmlObject)); } const xmlVariables=xmlObj['UANodeSet']['UAVariable']; for(const xmlVariable of xmlVariables) { nodes.push(UAVariable.parse(xmlVariable)); } const uaNamespaceUris=xmlObj['UANodeSet']['NamespaceUris']; const nst=new NamespaceTable(); if(uaNamespaceUris) { for(const nsUri of uaNamespaceUris['Uri']) { nst.addUri(nsUri) } } const fileName= url.split('/').pop()||url return new UANodeSet(fileName, models, nodes, nst); } }