UANodeSet.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. import type { AddressSpace } from './AddressSpace';
  13. import type { IAddressSpace } from './IAddressSpace';
  14. import { UAAlias } from './UAAlias';
  15. export class UANodeSet implements IToXML{
  16. constructor(public fileName: string,
  17. public models: Model[],
  18. public aliases: UAAlias[],
  19. public nodes: UABaseNode[],
  20. public nameSpaceTable: NamespaceTable,
  21. public xmlns_xsi: string,
  22. public xmlns_xsd: string,
  23. public lastModified: string,
  24. public xmlns: string) {
  25. }
  26. reIndex(nst: NamespaceTable) {
  27. //add all missing namespaces to addressspace ns table
  28. for(const value of this.nameSpaceTable.nsMap.getValues()) {
  29. nst.addUri(value);
  30. }
  31. for(const node of this.nodes) {
  32. node.reIndex(nst, this.nameSpaceTable);
  33. }
  34. }
  35. resolveReferences(nm: Map<string, UABaseNode>) {
  36. for(const node of this.nodes) {
  37. node.resolveReferences(nm);
  38. }
  39. }
  40. toXML(lnst: NamespaceTable, gnst: NamespaceTable): XMLElem {
  41. const xmlUANodeSet =new XMLElem('UANodeSet');
  42. xmlUANodeSet.attr('xmlns:xsi', this.xmlns_xsi);
  43. xmlUANodeSet.attr('xmlns:xsd', this.xmlns_xsd);
  44. xmlUANodeSet.attr('xmlns', this.xmlns);
  45. xmlUANodeSet.attr('LastModified', this.lastModified);
  46. const xmlNameSpaceUris=xmlUANodeSet.add(new XMLElem('NamespaceUris'));
  47. for(let i=1;i<this.nameSpaceTable.nsMap.size(); i++) { //skip UA(0) ns
  48. const uri=this.nameSpaceTable.nsMap.get(i);
  49. xmlNameSpaceUris.elem("Uri", uri);
  50. }
  51. const xmlModels=xmlUANodeSet.add(new XMLElem('Models'));
  52. for(const model of this.models) {
  53. xmlModels.add(model.toXML())
  54. }
  55. const xmlAliases=xmlUANodeSet.add(new XMLElem('Aliases'));
  56. for(const alias of this.aliases) {
  57. xmlAliases.add(alias.toXML())
  58. }
  59. for(const node of this.nodes) {
  60. xmlUANodeSet.add(node.toXML(lnst, gnst))
  61. }
  62. return xmlUANodeSet;
  63. }
  64. static async load(url: string, addressSpace: AddressSpace): Promise<UANodeSet> {
  65. const xml= await (await fetch(url)).text();
  66. const fileName= url.split('/').pop()||url
  67. return this.parse(xml, fileName, addressSpace);
  68. }
  69. static parse(xml: string, fileName: string, addressSpace: IAddressSpace): UANodeSet {
  70. const parseOptions:Partial<X2jOptions>={
  71. ignoreAttributes: false,
  72. alwaysCreateTextNode: true, //force consistent result
  73. parseTagValue:false, //disable number detection. Otherwise string values might end up as numbers.
  74. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  75. isArray: (name, jpath, isLeafNode, isAttribute):boolean => {
  76. switch(jpath) {
  77. case 'UANodeSet.NamespaceUris.Uri':
  78. case 'UANodeSet.Models.Model':
  79. case 'UANodeSet.UAObject':
  80. case 'UANodeSet.UAObject.References.Reference':
  81. case 'UANodeSet.UAObject.DisplayName':
  82. case 'UANodeSet.UAObject.Description':
  83. case 'UANodeSet.UAObject.Category':
  84. case 'UANodeSet.UAObject.RolePermissions.RolePermission':
  85. case 'UANodeSet.UAObjectType':
  86. case 'UANodeSet.UAObjectType.References.Reference':
  87. case 'UANodeSet.UAObjectType.DisplayName':
  88. case 'UANodeSet.UAObjectType.Description':
  89. case 'UANodeSet.UAObjectType.Category':
  90. case 'UANodeSet.UAObjectType.RolePermissions.RolePermission':
  91. case 'UANodeSet.UAMethod':
  92. case 'UANodeSet.UAMethod.References.Reference':
  93. case 'UANodeSet.UAMethod.DisplayName':
  94. case 'UANodeSet.UAMethod.Description':
  95. case 'UANodeSet.UAMethod.Category':
  96. case 'UANodeSet.UAMethod.RolePermissions.RolePermission':
  97. case 'UANodeSet.UAVariable':
  98. case 'UANodeSet.UAVariable.References.Reference':
  99. case 'UANodeSet.UAVariable.DisplayName':
  100. case 'UANodeSet.UAVariable.Description':
  101. case 'UANodeSet.UAVariable.Category':
  102. case 'UANodeSet.UAVariable.RolePermissions.RolePermission':
  103. case 'UANodeSet.UAVariableType':
  104. case 'UANodeSet.UAVariableType.References.Reference':
  105. case 'UANodeSet.UAVariableType.DisplayName':
  106. case 'UANodeSet.UAVariableType.Description':
  107. case 'UANodeSet.UAVariableType.Category':
  108. case 'UANodeSet.UAVariableType.RolePermissions.RolePermission':
  109. case 'UANodeSet.UAReferenceType':
  110. case 'UANodeSet.UAReferenceType.References.Reference':
  111. case 'UANodeSet.UAReferenceType.DisplayName':
  112. case 'UANodeSet.UAReferenceType.Description':
  113. case 'UANodeSet.UAReferenceType.Category':
  114. case 'UANodeSet.UAReferenceType.RolePermissions.RolePermission':
  115. return true;
  116. default:
  117. return false;
  118. }
  119. }
  120. }
  121. const parser = new XMLParser(parseOptions);
  122. const xmlObj = parser.parse(xml);
  123. const xmlAliases=xmlObj['UANodeSet']['Aliases']||[];
  124. const aliases: UAAlias[]=[];
  125. for(const xmlAlias of xmlAliases.Alias) {
  126. aliases.push(UAAlias.fromXML(xmlAlias));
  127. }
  128. const models: Model[]=[];
  129. const xmlModels=xmlObj['UANodeSet']['Models']||[];
  130. for(const xmlModel of xmlModels.Model) {
  131. models.push(Model.fromXML(xmlModel));
  132. }
  133. const nodes:UABaseNode[]=[];
  134. const xmlObjects=xmlObj['UANodeSet']['UAObject']||[];
  135. for(const xmlObject of xmlObjects) {
  136. nodes.push(UAObject.fromXML(xmlObject, addressSpace));
  137. }
  138. const xmlVariables=xmlObj['UANodeSet']['UAVariable']||[];
  139. for(const xmlVariable of xmlVariables) {
  140. nodes.push(UAVariable.fromXML(xmlVariable, addressSpace));
  141. }
  142. const xmlMethods=xmlObj['UANodeSet']['UAMethod']||[];
  143. for(const xmlMethod of xmlMethods) {
  144. nodes.push(UAMethod.fromXML(xmlMethod, addressSpace));
  145. }
  146. const xmlReferenceTypes=xmlObj['UANodeSet']['UAReferenceType']||[];
  147. for(const xmlReferenceType of xmlReferenceTypes) {
  148. nodes.push(UAReferenceType.fromXML(xmlReferenceType, addressSpace));
  149. }
  150. const xmlObjectTypes=xmlObj['UANodeSet']['UAObjectType']||[];
  151. for(const xmlObjectType of xmlObjectTypes) {
  152. nodes.push(UAObjectType.fromXML(xmlObjectType, addressSpace));
  153. }
  154. const xmlVariableTypes=xmlObj['UANodeSet']['UAVariableType']||[];
  155. for(const xmlVariableType of xmlVariableTypes) {
  156. nodes.push(UAVariableType.fromXML(xmlVariableType, addressSpace));
  157. }
  158. const uaNamespaceUris=xmlObj['UANodeSet']['NamespaceUris']||[];
  159. const nst=new NamespaceTable();
  160. for(const nsUri of uaNamespaceUris['Uri']||[]) {
  161. nst.addUri(nsUri['#text'])
  162. }
  163. return new UANodeSet(fileName, models, aliases, nodes, nst, xmlObj['UANodeSet']['@_xmlns'], xmlObj['UANodeSet']['@_xmlns:xsd'], xmlObj['UANodeSet']['@_xmlns:xsi'], xmlObj['UANodeSet']['@_LastModified']);
  164. }
  165. }