UANodeSet.ts 8.6 KB

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