UANodeSet.ts 5.2 KB

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