UANodeSet.ts 3.8 KB

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