UANodeSet.ts 4.5 KB

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