12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- import {XMLParser, type X2jOptions} from 'fast-xml-parser';
- import axios from 'axios';
- import { UAObject } from './UAObject';
- import type { UABaseNode } from './UABaseNode';
- import { UAVariable } from './UAVariable';
- import { NamespaceTable } from './NameSpaceTable';
- export class UANodeSet {
- constructor(public nodes: UABaseNode[],
- public nameSpaceTable: NamespaceTable) {
- }
- reIndex(nst: NamespaceTable) {
- 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);
- }
- }
- static async load(url: string) {
- const xml= (await axios.get(url)).data;
- const parseOptions:Partial<X2jOptions>={
- ignoreAttributes: false,
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
- isArray: (name, jpath, isLeafNode, isAttribute):boolean => {
- if(jpath=='UANodeSet.NamespaceUris.Uri') return true;
- if(jpath=='UANodeSet.UAObject.References.Reference') return true;
- if(jpath=='UANodeSet.UAVariable.References.Reference') return true;
- return false;
- }
- }
- const parser = new XMLParser(parseOptions);
- const jObj = parser.parse(xml);
- const nodes:UABaseNode[]=[];
- const uaObjects=jObj['UANodeSet']['UAObject'];
- for(const uaObject of uaObjects) {
- nodes.push(UAObject.parse(uaObject));
- }
- const uaVariables=jObj['UANodeSet']['UAVariable'];
- for(const uaVariable of uaVariables) {
- nodes.push(UAVariable.parse(uaVariable));
- }
- const uaNamespaceUris=jObj['UANodeSet']['NamespaceUris'];
- const nst=new NamespaceTable();
- if(uaNamespaceUris) {
- for(const nsUri of uaNamespaceUris['Uri']) {
- nst.addUri(nsUri)
- }
- }
- return new UANodeSet(nodes, nst);
- }
- }
|