UANodeSet.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import {XMLParser, type X2jOptions} from 'fast-xml-parser';
  2. import { UAObject } from './UAObject';
  3. import type { UABaseNode } from './UABaseNode';
  4. import { UAVariable } from './UAVariable';
  5. import { NamespaceTable } from './NameSpaceTable';
  6. import { Model } from './Model';
  7. import { XMLElem, type IToXML } from '@/util/XmlElem';
  8. export class UANodeSet implements IToXML{
  9. constructor(public fileName: string,
  10. public models: Model[],
  11. public nodes: UABaseNode[],
  12. public nameSpaceTable: NamespaceTable) {
  13. }
  14. reIndex(nst: NamespaceTable) {
  15. //add all missing namespaces to addressspace ns table
  16. for(const value of this.nameSpaceTable.nsMap.getValues()) {
  17. nst.addUri(value);
  18. }
  19. for(const node of this.nodes) {
  20. node.reIndex(nst, this.nameSpaceTable);
  21. }
  22. }
  23. resolveChildren(nm: Map<string, UABaseNode>) {
  24. for(const node of this.nodes) {
  25. node.resolveChildren(nm);
  26. }
  27. }
  28. toXML(): XMLElem {
  29. const elem =new XMLElem('UANodeSet');
  30. const xmlModels=elem.add(new XMLElem('Models'));
  31. for(const model of this.models) {
  32. xmlModels.add(model.toXML())
  33. }
  34. return elem;
  35. }
  36. static async load(url: string) {
  37. const parseOptions:Partial<X2jOptions>={
  38. ignoreAttributes: false,
  39. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  40. isArray: (name, jpath, isLeafNode, isAttribute):boolean => {
  41. switch(jpath) {
  42. case 'UANodeSet.NamespaceUris.Uri':
  43. case 'UANodeSet.UAObject.References.Reference':
  44. case 'UANodeSet.UAObject':
  45. case 'UANodeSet.UAVariable':
  46. case 'UANodeSet.UAVariable.References.Reference':
  47. case 'UANodeSet.Models.Model':
  48. return true;
  49. default:
  50. return false;
  51. }
  52. }
  53. }
  54. const parser = new XMLParser(parseOptions);
  55. const xml= await (await fetch(url)).text()
  56. const xmlObj = parser.parse(xml);
  57. const models: Model[]=[];
  58. const xmlModels=xmlObj['UANodeSet']['Models'];
  59. for(const xmlModel of xmlModels.Model) {
  60. models.push(Model.fromXML(xmlModel));
  61. }
  62. const nodes:UABaseNode[]=[];
  63. const xmlObjects=xmlObj['UANodeSet']['UAObject'];
  64. for(const xmlObject of xmlObjects) {
  65. nodes.push(UAObject.fromXML(xmlObject));
  66. }
  67. const xmlVariables=xmlObj['UANodeSet']['UAVariable'];
  68. for(const xmlVariable of xmlVariables) {
  69. nodes.push(UAVariable.parse(xmlVariable));
  70. }
  71. const uaNamespaceUris=xmlObj['UANodeSet']['NamespaceUris'];
  72. const nst=new NamespaceTable();
  73. if(uaNamespaceUris) {
  74. for(const nsUri of uaNamespaceUris['Uri']) {
  75. nst.addUri(nsUri)
  76. }
  77. }
  78. const fileName= url.split('/').pop()||url
  79. return new UANodeSet(fileName, models, nodes, nst);
  80. }
  81. }