123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- 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<string, UABaseNode>) {
- 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<X2jOptions>={
- 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);
- }
- }
|