UANodeSet.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. 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. case 'UANodeSet.UADataType':
  118. case 'UANodeSet.UADataType.References.Reference':
  119. case 'UANodeSet.UADataType.DisplayName':
  120. case 'UANodeSet.UADataType.Description':
  121. return true;
  122. default:
  123. return false;
  124. }
  125. }
  126. }
  127. const parser = new XMLParser(parseOptions);
  128. const xmlObj = parser.parse(xml);
  129. const xmlAliases=xmlObj['UANodeSet']['Aliases']||[];
  130. const aliases: UAAlias[]=[];
  131. for(const xmlAlias of xmlAliases.Alias) {
  132. aliases.push(UAAlias.fromXML(xmlAlias));
  133. }
  134. const models: UAModel[]=[];
  135. const xmlModels=xmlObj['UANodeSet']['Models']||[];
  136. for(const xmlModel of xmlModels.Model) {
  137. models.push(UAModel.fromXML(xmlModel));
  138. }
  139. const nodes:UABaseNode[]=[];
  140. const xmlObjects=xmlObj['UANodeSet']['UAObject']||[];
  141. for(const xmlObject of xmlObjects) {
  142. nodes.push(UAObject.fromXML(xmlObject, addressSpace));
  143. }
  144. const xmlVariables=xmlObj['UANodeSet']['UAVariable']||[];
  145. for(const xmlVariable of xmlVariables) {
  146. nodes.push(UAVariable.fromXML(xmlVariable, addressSpace));
  147. }
  148. const xmlMethods=xmlObj['UANodeSet']['UAMethod']||[];
  149. for(const xmlMethod of xmlMethods) {
  150. nodes.push(UAMethod.fromXML(xmlMethod, addressSpace));
  151. }
  152. const xmlReferenceTypes=xmlObj['UANodeSet']['UAReferenceType']||[];
  153. for(const xmlReferenceType of xmlReferenceTypes) {
  154. nodes.push(UAReferenceType.fromXML(xmlReferenceType, addressSpace));
  155. }
  156. const xmlObjectTypes=xmlObj['UANodeSet']['UAObjectType']||[];
  157. for(const xmlObjectType of xmlObjectTypes) {
  158. nodes.push(UAObjectType.fromXML(xmlObjectType, addressSpace));
  159. }
  160. const xmlVariableTypes=xmlObj['UANodeSet']['UAVariableType']||[];
  161. for(const xmlVariableType of xmlVariableTypes) {
  162. nodes.push(UAVariableType.fromXML(xmlVariableType, addressSpace));
  163. }
  164. const xmlDataTypes=xmlObj['UANodeSet']['UADataType']||[];
  165. for(const xmlDataType of xmlDataTypes) {
  166. nodes.push(UADataType.fromXML(xmlDataType, addressSpace));
  167. }
  168. const uaNamespaceUris=xmlObj['UANodeSet']['NamespaceUris']||[];
  169. const nst=new NamespaceTable();
  170. for(const nsUri of uaNamespaceUris['Uri']||[]) {
  171. nst.addUri(nsUri['#text'])
  172. }
  173. return new UANodeSet(fileName, models, aliases, nodes, nst, xmlObj['UANodeSet']['@_xmlns'], xmlObj['UANodeSet']['@_xmlns:xsi'], xmlObj['UANodeSet']['@_xmlns:xsd'], xmlObj['UANodeSet']['@_LastModified']);
  174. }
  175. }