UANodeSet.ts 8.1 KB

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