@itwin/ecschema-metadata 3.5.0-dev.19 → 3.5.0-dev.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. package/lib/cjs/Constants.d.ts +23 -23
  2. package/lib/cjs/Constants.js +33 -33
  3. package/lib/cjs/Constants.js.map +1 -1
  4. package/lib/cjs/Context.d.ts +139 -139
  5. package/lib/cjs/Context.js +204 -204
  6. package/lib/cjs/DelayedPromise.d.ts +88 -88
  7. package/lib/cjs/DelayedPromise.js +91 -91
  8. package/lib/cjs/DelayedPromise.js.map +1 -1
  9. package/lib/cjs/Deserialization/AbstractParser.d.ts +45 -45
  10. package/lib/cjs/Deserialization/AbstractParser.js +11 -11
  11. package/lib/cjs/Deserialization/AbstractParser.js.map +1 -1
  12. package/lib/cjs/Deserialization/Helper.d.ts +251 -251
  13. package/lib/cjs/Deserialization/Helper.js +843 -843
  14. package/lib/cjs/Deserialization/Helper.js.map +1 -1
  15. package/lib/cjs/Deserialization/JsonParser.d.ts +165 -165
  16. package/lib/cjs/Deserialization/JsonParser.js +680 -680
  17. package/lib/cjs/Deserialization/JsonParser.js.map +1 -1
  18. package/lib/cjs/Deserialization/JsonProps.d.ts +282 -282
  19. package/lib/cjs/Deserialization/JsonProps.js +9 -9
  20. package/lib/cjs/Deserialization/JsonProps.js.map +1 -1
  21. package/lib/cjs/Deserialization/SchemaGraphUtil.d.ts +34 -34
  22. package/lib/cjs/Deserialization/SchemaGraphUtil.js +77 -77
  23. package/lib/cjs/Deserialization/SchemaGraphUtil.js.map +1 -1
  24. package/lib/cjs/Deserialization/XmlParser.d.ts +85 -85
  25. package/lib/cjs/Deserialization/XmlParser.js +973 -973
  26. package/lib/cjs/Deserialization/XmlParser.js.map +1 -1
  27. package/lib/cjs/Deserialization/XmlSerializationUtils.d.ts +54 -54
  28. package/lib/cjs/Deserialization/XmlSerializationUtils.js +174 -174
  29. package/lib/cjs/Deserialization/XmlSerializationUtils.js.map +1 -1
  30. package/lib/cjs/ECName.d.ts +30 -30
  31. package/lib/cjs/ECName.js +85 -85
  32. package/lib/cjs/ECName.js.map +1 -1
  33. package/lib/cjs/ECObjects.d.ts +162 -162
  34. package/lib/cjs/ECObjects.js +453 -453
  35. package/lib/cjs/ECObjects.js.map +1 -1
  36. package/lib/cjs/Exception.d.ts +43 -43
  37. package/lib/cjs/Exception.js +86 -86
  38. package/lib/cjs/Exception.js.map +1 -1
  39. package/lib/cjs/Interfaces.d.ts +66 -66
  40. package/lib/cjs/Interfaces.js +9 -9
  41. package/lib/cjs/Interfaces.js.map +1 -1
  42. package/lib/cjs/Metadata/Class.d.ts +243 -243
  43. package/lib/cjs/Metadata/Class.js +558 -558
  44. package/lib/cjs/Metadata/Class.js.map +1 -1
  45. package/lib/cjs/Metadata/Constant.d.ts +64 -64
  46. package/lib/cjs/Metadata/Constant.js +123 -123
  47. package/lib/cjs/Metadata/Constant.js.map +1 -1
  48. package/lib/cjs/Metadata/CustomAttribute.d.ts +23 -23
  49. package/lib/cjs/Metadata/CustomAttribute.js +27 -27
  50. package/lib/cjs/Metadata/CustomAttribute.js.map +1 -1
  51. package/lib/cjs/Metadata/CustomAttributeClass.d.ts +39 -39
  52. package/lib/cjs/Metadata/CustomAttributeClass.js +68 -68
  53. package/lib/cjs/Metadata/CustomAttributeClass.js.map +1 -1
  54. package/lib/cjs/Metadata/EntityClass.d.ts +77 -77
  55. package/lib/cjs/Metadata/EntityClass.js +230 -230
  56. package/lib/cjs/Metadata/EntityClass.js.map +1 -1
  57. package/lib/cjs/Metadata/Enumeration.d.ts +86 -86
  58. package/lib/cjs/Metadata/Enumeration.js +159 -159
  59. package/lib/cjs/Metadata/Enumeration.js.map +1 -1
  60. package/lib/cjs/Metadata/Format.d.ts +98 -98
  61. package/lib/cjs/Metadata/Format.js +242 -242
  62. package/lib/cjs/Metadata/Format.js.map +1 -1
  63. package/lib/cjs/Metadata/InvertedUnit.d.ts +49 -49
  64. package/lib/cjs/Metadata/InvertedUnit.js +97 -97
  65. package/lib/cjs/Metadata/InvertedUnit.js.map +1 -1
  66. package/lib/cjs/Metadata/KindOfQuantity.d.ts +65 -65
  67. package/lib/cjs/Metadata/KindOfQuantity.js +225 -225
  68. package/lib/cjs/Metadata/KindOfQuantity.js.map +1 -1
  69. package/lib/cjs/Metadata/Mixin.d.ts +54 -54
  70. package/lib/cjs/Metadata/Mixin.js +113 -113
  71. package/lib/cjs/Metadata/Mixin.js.map +1 -1
  72. package/lib/cjs/Metadata/OverrideFormat.d.ts +55 -55
  73. package/lib/cjs/Metadata/OverrideFormat.js +88 -88
  74. package/lib/cjs/Metadata/OverrideFormat.js.map +1 -1
  75. package/lib/cjs/Metadata/Phenomenon.d.ts +33 -33
  76. package/lib/cjs/Metadata/Phenomenon.js +59 -59
  77. package/lib/cjs/Metadata/Phenomenon.js.map +1 -1
  78. package/lib/cjs/Metadata/Property.d.ts +197 -197
  79. package/lib/cjs/Metadata/Property.js +491 -491
  80. package/lib/cjs/Metadata/Property.js.map +1 -1
  81. package/lib/cjs/Metadata/PropertyCategory.d.ts +39 -39
  82. package/lib/cjs/Metadata/PropertyCategory.js +61 -61
  83. package/lib/cjs/Metadata/PropertyCategory.js.map +1 -1
  84. package/lib/cjs/Metadata/RelationshipClass.d.ts +152 -152
  85. package/lib/cjs/Metadata/RelationshipClass.js +349 -349
  86. package/lib/cjs/Metadata/RelationshipClass.js.map +1 -1
  87. package/lib/cjs/Metadata/Schema.d.ts +291 -291
  88. package/lib/cjs/Metadata/Schema.js +520 -520
  89. package/lib/cjs/Metadata/Schema.js.map +1 -1
  90. package/lib/cjs/Metadata/SchemaItem.d.ts +61 -61
  91. package/lib/cjs/Metadata/SchemaItem.js +136 -136
  92. package/lib/cjs/Metadata/SchemaItem.js.map +1 -1
  93. package/lib/cjs/Metadata/Unit.d.ts +76 -76
  94. package/lib/cjs/Metadata/Unit.js +162 -162
  95. package/lib/cjs/Metadata/UnitSystem.d.ts +20 -20
  96. package/lib/cjs/Metadata/UnitSystem.js +29 -29
  97. package/lib/cjs/Metadata/UnitSystem.js.map +1 -1
  98. package/lib/cjs/PropertyTypes.d.ts +49 -49
  99. package/lib/cjs/PropertyTypes.js +91 -91
  100. package/lib/cjs/PropertyTypes.js.map +1 -1
  101. package/lib/cjs/SchemaJsonLocater.d.ts +34 -34
  102. package/lib/cjs/SchemaJsonLocater.js +42 -42
  103. package/lib/cjs/SchemaJsonLocater.js.map +1 -1
  104. package/lib/cjs/SchemaKey.d.ts +110 -110
  105. package/lib/cjs/SchemaKey.js +219 -219
  106. package/lib/cjs/SchemaKey.js.map +1 -1
  107. package/lib/cjs/SchemaLoader.d.ts +23 -23
  108. package/lib/cjs/SchemaLoader.js +47 -47
  109. package/lib/cjs/SchemaLoader.js.map +1 -1
  110. package/lib/cjs/SchemaPartVisitorDelegate.d.ts +274 -274
  111. package/lib/cjs/SchemaPartVisitorDelegate.js +212 -212
  112. package/lib/cjs/SchemaPartVisitorDelegate.js.map +1 -1
  113. package/lib/cjs/UnitConversion/Graph.d.ts +34 -34
  114. package/lib/cjs/UnitConversion/Graph.js +83 -83
  115. package/lib/cjs/UnitConversion/Graph.js.map +1 -1
  116. package/lib/cjs/UnitConversion/Parser.d.ts +8 -8
  117. package/lib/cjs/UnitConversion/Parser.js +42 -42
  118. package/lib/cjs/UnitConversion/Parser.js.map +1 -1
  119. package/lib/cjs/UnitConversion/UnitConversion.d.ts +45 -45
  120. package/lib/cjs/UnitConversion/UnitConversion.js +77 -77
  121. package/lib/cjs/UnitConversion/UnitConversion.js.map +1 -1
  122. package/lib/cjs/UnitConversion/UnitConverter.d.ts +39 -39
  123. package/lib/cjs/UnitConversion/UnitConverter.js +116 -116
  124. package/lib/cjs/UnitConversion/UnitConverter.js.map +1 -1
  125. package/lib/cjs/UnitConversion/UnitTree.d.ts +43 -43
  126. package/lib/cjs/UnitConversion/UnitTree.js +169 -169
  127. package/lib/cjs/UnitConversion/UnitTree.js.map +1 -1
  128. package/lib/cjs/UnitProvider/SchemaUnitProvider.d.ts +77 -77
  129. package/lib/cjs/UnitProvider/SchemaUnitProvider.js +236 -236
  130. package/lib/cjs/UnitProvider/SchemaUnitProvider.js.map +1 -1
  131. package/lib/cjs/Validation/SchemaWalker.d.ts +23 -23
  132. package/lib/cjs/Validation/SchemaWalker.js +53 -53
  133. package/lib/cjs/Validation/SchemaWalker.js.map +1 -1
  134. package/lib/cjs/ecschema-metadata.d.ts +51 -51
  135. package/lib/cjs/ecschema-metadata.js +96 -96
  136. package/lib/cjs/ecschema-metadata.js.map +1 -1
  137. package/lib/cjs/utils/SchemaGraph.d.ts +25 -25
  138. package/lib/cjs/utils/SchemaGraph.js +65 -65
  139. package/lib/cjs/utils/SchemaGraph.js.map +1 -1
  140. package/package.json +8 -8
@@ -1,521 +1,521 @@
1
- "use strict";
2
- /*---------------------------------------------------------------------------------------------
3
- * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
4
- * See LICENSE.md in the project root for license terms and full copyright notice.
5
- *--------------------------------------------------------------------------------------------*/
6
- /** @packageDocumentation
7
- * @module Metadata
8
- */
9
- Object.defineProperty(exports, "__esModule", { value: true });
10
- exports.MutableSchema = exports.Schema = void 0;
11
- const Helper_1 = require("../Deserialization/Helper");
12
- const JsonParser_1 = require("../Deserialization/JsonParser");
13
- const XmlSerializationUtils_1 = require("../Deserialization/XmlSerializationUtils");
14
- const Exception_1 = require("../Exception");
15
- const SchemaKey_1 = require("../SchemaKey");
16
- const ECName_1 = require("../ECName");
17
- const Class_1 = require("./Class");
18
- const Constant_1 = require("./Constant");
19
- const CustomAttribute_1 = require("./CustomAttribute");
20
- const CustomAttributeClass_1 = require("./CustomAttributeClass");
21
- const EntityClass_1 = require("./EntityClass");
22
- const Enumeration_1 = require("./Enumeration");
23
- const Format_1 = require("./Format");
24
- const InvertedUnit_1 = require("./InvertedUnit");
25
- const KindOfQuantity_1 = require("./KindOfQuantity");
26
- const Mixin_1 = require("./Mixin");
27
- const Phenomenon_1 = require("./Phenomenon");
28
- const PropertyCategory_1 = require("./PropertyCategory");
29
- const RelationshipClass_1 = require("./RelationshipClass");
30
- const SchemaItem_1 = require("./SchemaItem");
31
- const Unit_1 = require("./Unit");
32
- const UnitSystem_1 = require("./UnitSystem");
33
- const SCHEMAURL3_2_JSON = "https://dev.bentley.com/json_schemas/ec/32/ecschema";
34
- const SCHEMAURL3_2_XML = "http://www.bentley.com/schemas/Bentley.ECXML.3.2";
35
- /**
36
- * @beta
37
- */
38
- class Schema {
39
- /** @internal */
40
- constructor(context, nameOrKey, alias, readVer, writeVer, minorVer) {
41
- this._schemaKey = (typeof (nameOrKey) === "string") ? new SchemaKey_1.SchemaKey(nameOrKey, new SchemaKey_1.ECVersion(readVer, writeVer, minorVer)) : nameOrKey;
42
- this._context = context;
43
- this.references = [];
44
- this._items = new Map();
45
- if (alias !== undefined && ECName_1.ECName.validate(alias)) {
46
- this._alias = alias;
47
- }
48
- else if (nameOrKey !== undefined) {
49
- throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.InvalidECJson, `The Schema ${this.name} does not have the required 'alias' attribute.`);
50
- }
51
- }
52
- get schemaKey() {
53
- if (undefined === this._schemaKey)
54
- throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.InvalidECJson, `The schema '${this.name}' has an invalid SchemaKey.`);
55
- return this._schemaKey;
56
- }
57
- get name() { return this.schemaKey.name; }
58
- get readVersion() { return this.schemaKey.readVersion; }
59
- get writeVersion() { return this.schemaKey.writeVersion; }
60
- get minorVersion() { return this.schemaKey.minorVersion; }
61
- get alias() {
62
- if (this._alias === undefined || this._alias === null) {
63
- throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.InvalidECJson, `The Schema ${this.name} does not have the required 'alias' attribute.`);
64
- }
65
- else {
66
- return this._alias;
67
- }
68
- }
69
- get label() { return this._label; }
70
- get description() { return this._description; }
71
- get customAttributes() { return this._customAttributes; }
72
- /** Returns the schema name. */
73
- get fullName() { return this.schemaKey.name; }
74
- /** Returns the schema. */
75
- get schema() { return this; }
76
- /** Returns the schema context this schema is within. */
77
- get context() { return this._context; }
78
- /**
79
- * Returns a SchemaItemKey given the item name and the schema it belongs to
80
- * @param fullName fully qualified name {Schema name}.{Item Name}
81
- */
82
- getSchemaItemKey(fullName) {
83
- const [schemaName, itemName] = SchemaItem_1.SchemaItem.parseFullName(fullName);
84
- let schemaKey = this.schemaKey;
85
- if (this.name !== schemaName) {
86
- const newSchemaRef = this.getReferenceSync(schemaName);
87
- if (undefined === newSchemaRef)
88
- throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.InvalidECJson, `Unable to find the referenced SchemaItem ${itemName}.`);
89
- schemaKey = newSchemaRef.schemaKey;
90
- }
91
- return new SchemaKey_1.SchemaItemKey(itemName, schemaKey);
92
- }
93
- addItem(item) {
94
- if (undefined !== this.getItemSync(item.name))
95
- throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.DuplicateItem, `The SchemaItem ${item.name} cannot be added to the schema ${this.name} because it already exists`);
96
- this._items.set(item.name.toUpperCase(), item);
97
- }
98
- /**
99
- * @alpha
100
- */
101
- createClass(type, name, modifier) {
102
- const item = new type(this, name, modifier);
103
- this.addItem(item);
104
- return item;
105
- }
106
- /**
107
- * Deletes a class from within this schema.
108
- * @param name the local (unqualified) class name, lookup is case-insensitive
109
- * @alpha
110
- */
111
- async deleteClass(name) {
112
- const schemaItem = await this.getItem(name);
113
- if (Class_1.ECClass.isECClass(schemaItem)) {
114
- this._items.delete(name.toUpperCase());
115
- }
116
- }
117
- /**
118
- * Deletes a class from within this schema.
119
- * @param name the local (unqualified) class name, lookup is case-insensitive
120
- * @alpha
121
- */
122
- deleteClassSync(name) {
123
- const schemaItem = this.getItemSync(name);
124
- if (Class_1.ECClass.isECClass(schemaItem))
125
- this._items.delete(name.toUpperCase());
126
- }
127
- /**
128
- * @alpha
129
- */
130
- createItem(type, name) {
131
- const item = new type(this, name);
132
- this.addItem(item);
133
- return item;
134
- }
135
- addCustomAttribute(customAttribute) {
136
- if (!this._customAttributes)
137
- this._customAttributes = new Map();
138
- this._customAttributes.set(customAttribute.className, customAttribute);
139
- }
140
- /**
141
- * Creates a EntityClass with the provided name in this schema.
142
- * @param name
143
- * @param modifier
144
- */
145
- async createEntityClass(name, modifier) {
146
- return this.createClass(EntityClass_1.EntityClass, name, modifier);
147
- }
148
- createEntityClassSync(name, modifier) {
149
- return this.createClass(EntityClass_1.EntityClass, name, modifier);
150
- }
151
- /**
152
- * Creates a Mixin with the provided name in this schema.
153
- * @param name
154
- */
155
- async createMixinClass(name) { return this.createClass(Mixin_1.Mixin, name); }
156
- createMixinClassSync(name) { return this.createClass(Mixin_1.Mixin, name); }
157
- /**
158
- * Creates a StructClass with the provided name in this schema.
159
- * @param name
160
- * @param modifier
161
- */
162
- async createStructClass(name, modifier) {
163
- return this.createClass(Class_1.StructClass, name, modifier);
164
- }
165
- createStructClassSync(name, modifier) {
166
- return this.createClass(Class_1.StructClass, name, modifier);
167
- }
168
- /**
169
- * Creates a CustomAttributeClass with the provided name in this schema.
170
- * @param name
171
- * @param modifier
172
- */
173
- async createCustomAttributeClass(name, modifier) {
174
- return this.createClass(CustomAttributeClass_1.CustomAttributeClass, name, modifier);
175
- }
176
- createCustomAttributeClassSync(name, modifier) {
177
- return this.createClass(CustomAttributeClass_1.CustomAttributeClass, name, modifier);
178
- }
179
- /**
180
- * Creates a RelationshipClass with the provided name in this schema.
181
- * @param name
182
- * @param modifier
183
- */
184
- async createRelationshipClass(name, modifier) {
185
- return this.createRelationshipClassSync(name, modifier);
186
- }
187
- createRelationshipClassSync(name, modifier) {
188
- return this.createClass(RelationshipClass_1.RelationshipClass, name, modifier);
189
- }
190
- /**
191
- * Creates an Enumeration with the provided name in this schema.
192
- * @param name
193
- */
194
- async createEnumeration(name, primitiveType) {
195
- return this.createEnumerationSync(name, primitiveType);
196
- }
197
- createEnumerationSync(name, primitiveType) {
198
- const item = new Enumeration_1.Enumeration(this, name, primitiveType);
199
- this.addItem(item);
200
- return item;
201
- }
202
- /**
203
- * Creates an KindOfQuantity with the provided name in this schema.
204
- * @param name
205
- */
206
- async createKindOfQuantity(name) {
207
- return this.createKindOfQuantitySync(name);
208
- }
209
- createKindOfQuantitySync(name) {
210
- return this.createItem(KindOfQuantity_1.KindOfQuantity, name);
211
- }
212
- /**
213
- * Creates a Constant with the provided name in this schema.
214
- * @param name
215
- */
216
- async createConstant(name) {
217
- return this.createItem(Constant_1.Constant, name);
218
- }
219
- createConstantSync(name) {
220
- return this.createItem(Constant_1.Constant, name);
221
- }
222
- /**
223
- * Creates a Inverted Unit with the provided name in this schema.
224
- * @param name
225
- */
226
- async createInvertedUnit(name) {
227
- return this.createItem(InvertedUnit_1.InvertedUnit, name);
228
- }
229
- createInvertedUnitSync(name) {
230
- return this.createItem(InvertedUnit_1.InvertedUnit, name);
231
- }
232
- /**
233
- * Creates an Format with the provided name in this schema.
234
- * @param name
235
- */
236
- async createFormat(name) {
237
- return this.createItem(Format_1.Format, name);
238
- }
239
- createFormatSync(name) {
240
- return this.createItem(Format_1.Format, name);
241
- }
242
- /**
243
- * Creates a UnitSystem with the provided name in this schema.
244
- * @param name
245
- */
246
- async createUnitSystem(name) {
247
- return this.createItem(UnitSystem_1.UnitSystem, name);
248
- }
249
- createUnitSystemSync(name) {
250
- return this.createItem(UnitSystem_1.UnitSystem, name);
251
- }
252
- /**
253
- * Creates a Phenomenon with the provided name in this schema.
254
- * @param name
255
- */
256
- async createPhenomenon(name) {
257
- return this.createItem(Phenomenon_1.Phenomenon, name);
258
- }
259
- createPhenomenonSync(name) {
260
- return this.createItem(Phenomenon_1.Phenomenon, name);
261
- }
262
- /**
263
- * Creates a Unit with the provided name in this schema.
264
- * @param name
265
- */
266
- async createUnit(name) {
267
- return this.createItem(Unit_1.Unit, name);
268
- }
269
- createUnitSync(name) {
270
- return this.createItem(Unit_1.Unit, name);
271
- }
272
- /**
273
- * Creates an PropertyCategory with the provided name in this schema.
274
- * @param name
275
- */
276
- async createPropertyCategory(name) {
277
- return this.createItem(PropertyCategory_1.PropertyCategory, name);
278
- }
279
- createPropertyCategorySync(name) {
280
- return this.createItem(PropertyCategory_1.PropertyCategory, name);
281
- }
282
- /**
283
- *
284
- * @param refSchema
285
- */
286
- async addReference(refSchema) {
287
- // TODO validation of reference schema. For now just adding
288
- this.addReferenceSync(refSchema);
289
- }
290
- addReferenceSync(refSchema) {
291
- this.references.push(refSchema);
292
- }
293
- /**
294
- * @alpha Used for schema editing.
295
- */
296
- setContext(context) {
297
- this._context = context;
298
- }
299
- /**
300
- * Sets the version of the SchemaKey identifying the schema.
301
- * @param readVersion The read version of the schema. If undefined, the value from the existing SchemaKey will be used.
302
- * @param writeVersion The write version of the schema. If undefined, the value from the existing SchemaKey will be used.
303
- * @param minorVersion The minor version of the schema. If undefined, the value from the existing SchemaKey will be used.
304
- */
305
- setVersion(readVersion, writeVersion, minorVersion) {
306
- if (!this._schemaKey)
307
- throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.InvalidSchemaKey, `The schema '${this.name}' has an invalid SchemaKey.`);
308
- const newVersion = new SchemaKey_1.ECVersion(readVersion !== null && readVersion !== void 0 ? readVersion : this._schemaKey.readVersion, writeVersion !== null && writeVersion !== void 0 ? writeVersion : this._schemaKey.writeVersion, minorVersion !== null && minorVersion !== void 0 ? minorVersion : this._schemaKey.minorVersion);
309
- this._schemaKey = new SchemaKey_1.SchemaKey(this._schemaKey.name, newVersion);
310
- }
311
- /**
312
- * Gets an item from within this schema. To get by full name use lookupItem instead.
313
- * @param key the local (unqualified) name, lookup is case-insensitive
314
- */
315
- async getItem(name) {
316
- // this method exists so we can rewire it later when we load partial schemas, for now it is identical to the sync version
317
- return this.getItemSync(name);
318
- }
319
- /**
320
- * Gets an item from within this schema. To get by full name use lookupItem instead.
321
- * @param key the local (unqualified) name, lookup is case-insensitive
322
- */
323
- getItemSync(name) {
324
- // Case-insensitive search
325
- return this._items.get(name.toUpperCase());
326
- }
327
- /**
328
- * Attempts to find a schema item within this schema or a (directly) referenced schema
329
- * @param key The full name or a SchemaItemKey identifying the desired item.
330
- */
331
- async lookupItem(key) {
332
- let schemaName, itemName;
333
- if (typeof (key) === "string") {
334
- [schemaName, itemName] = SchemaItem_1.SchemaItem.parseFullName(key);
335
- }
336
- else {
337
- itemName = key.name;
338
- schemaName = key.schemaName;
339
- }
340
- if (!schemaName || schemaName.toUpperCase() === this.name.toUpperCase()) {
341
- return this.getItem(itemName);
342
- }
343
- const refSchema = await this.getReference(schemaName);
344
- if (!refSchema)
345
- return undefined;
346
- return refSchema.getItem(itemName);
347
- }
348
- /**
349
- * Attempts to find a schema item within this schema or a (directly) referenced schema
350
- * @param key The full name or a SchemaItemKey identifying the desired item.
351
- */
352
- lookupItemSync(key) {
353
- let schemaName, itemName;
354
- if (typeof (key) === "string") {
355
- [schemaName, itemName] = SchemaItem_1.SchemaItem.parseFullName(key);
356
- }
357
- else {
358
- itemName = key.name;
359
- schemaName = key.schemaName;
360
- }
361
- if (!schemaName || schemaName.toUpperCase() === this.name.toUpperCase()) {
362
- return this.getItemSync(itemName);
363
- }
364
- const refSchema = this.getReferenceSync(schemaName);
365
- if (!refSchema)
366
- return undefined;
367
- return refSchema.getItemSync(itemName);
368
- }
369
- getItems() {
370
- if (!this._items)
371
- return new Map().values();
372
- return this._items.values();
373
- }
374
- *getClasses() {
375
- for (const [, value] of this._items) {
376
- if (Class_1.ECClass.isECClass(value))
377
- yield value;
378
- }
379
- }
380
- async getReference(refSchemaName) {
381
- if (this.references.length === 0)
382
- return undefined;
383
- return this.references.find((ref) => ref.name.toLowerCase() === refSchemaName.toLowerCase());
384
- }
385
- getReferenceNameByAlias(alias) {
386
- if (this.references.length === 0)
387
- return undefined;
388
- const schema = this.references.find((ref) => ref.alias ? ref.alias.toLowerCase() === alias.toLowerCase() : false);
389
- return schema ? schema.name : undefined;
390
- }
391
- getReferenceSync(refSchemaName) {
392
- if (this.references.length === 0)
393
- return undefined;
394
- return this.references.find((ref) => ref.name.toLowerCase() === refSchemaName.toLowerCase());
395
- }
396
- /**
397
- * Save this Schema's properties to an object for serializing to JSON.
398
- */
399
- toJSON() {
400
- const schemaJson = {};
401
- schemaJson.$schema = SCHEMAURL3_2_JSON; // $schema is required
402
- schemaJson.name = this.name; // name is required
403
- schemaJson.version = this.schemaKey.version.toString(true);
404
- schemaJson.alias = this.alias; // alias is required
405
- if (undefined !== this.label) // label is optional
406
- schemaJson.label = this.label;
407
- if (undefined !== this.description) // description is optional
408
- schemaJson.description = this.description;
409
- if (undefined !== this.references && this.references.length > 0) // references is optional
410
- schemaJson.references = this.references.map(({ name, schemaKey }) => ({ name, version: schemaKey.version.toString() }));
411
- const customAttributes = (0, CustomAttribute_1.serializeCustomAttributes)(this.customAttributes);
412
- if (undefined !== customAttributes)
413
- schemaJson.customAttributes = customAttributes;
414
- if (this._items.size > 0) {
415
- schemaJson.items = {};
416
- this._items.forEach((schemaItem) => {
417
- schemaJson.items[schemaItem.name] = schemaItem.toJSON(false, true);
418
- });
419
- }
420
- return schemaJson;
421
- }
422
- /**
423
- * Converts the schema to a DOM XML Document.
424
- * @param schemaXml An empty DOM document to which the schema will be written
425
- */
426
- async toXml(schemaXml) {
427
- const schemaMetadata = schemaXml.createElement("ECSchema");
428
- schemaMetadata.setAttribute("xmlns", SCHEMAURL3_2_XML);
429
- schemaMetadata.setAttribute("version", this.schemaKey.version.toString());
430
- schemaMetadata.setAttribute("schemaName", this.name);
431
- schemaMetadata.setAttribute("alias", this.alias ? this.alias : "");
432
- if (undefined !== this.label)
433
- schemaMetadata.setAttribute("displayLabel", this.label);
434
- if (undefined !== this.description)
435
- schemaMetadata.setAttribute("description", this.description);
436
- // Map used for CA serialization
437
- const refSchemaMap = new Map();
438
- this.references.forEach(({ name, schemaKey, alias }) => {
439
- const schemaRef = schemaXml.createElement("ECSchemaReference");
440
- schemaRef.setAttribute("name", name);
441
- schemaRef.setAttribute("version", schemaKey.version.toString());
442
- schemaRef.setAttribute("alias", alias ? alias : "");
443
- schemaMetadata.appendChild(schemaRef);
444
- refSchemaMap.set(name, schemaKey.version.toString());
445
- });
446
- if (this._customAttributes) {
447
- const parentElem = schemaXml.createElement("ECCustomAttributes");
448
- for (const [name, attribute] of this._customAttributes) {
449
- const caElem = await XmlSerializationUtils_1.XmlSerializationUtils.writeCustomAttribute(name, attribute, schemaXml, this);
450
- parentElem.appendChild(caElem);
451
- }
452
- schemaMetadata.appendChild(parentElem);
453
- }
454
- for (const [, item] of this._items) {
455
- const itemXml = await item.toXml(schemaXml);
456
- schemaMetadata.appendChild(itemXml);
457
- }
458
- schemaXml.appendChild(schemaMetadata);
459
- return schemaXml;
460
- }
461
- fromJSONSync(schemaProps) {
462
- if (undefined === this._schemaKey) {
463
- const schemaName = schemaProps.name;
464
- const version = SchemaKey_1.ECVersion.fromString(schemaProps.version);
465
- this._schemaKey = new SchemaKey_1.SchemaKey(schemaName, version);
466
- }
467
- else {
468
- if (schemaProps.name.toLowerCase() !== this.name.toLowerCase())
469
- throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.InvalidECJson, `The Schema ${this.name} does not match the provided name, '${schemaProps.name}'.`);
470
- if (this.schemaKey.version.compare(SchemaKey_1.ECVersion.fromString(schemaProps.version)))
471
- throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.InvalidECJson, `The Schema ${this.name} has the version '${this.schemaKey.version}' that does not match the provided version '${schemaProps.version}'.`);
472
- }
473
- if (SCHEMAURL3_2_JSON !== schemaProps.$schema && SCHEMAURL3_2_XML !== schemaProps.$schema) // TODO: Allow for 3.x URI versions to allow the API to read newer specs. (Start at 3.2 though)
474
- throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.MissingSchemaUrl, `The Schema ${this.name} has an unsupported namespace '${schemaProps.$schema}'.`);
475
- if (ECName_1.ECName.validate(schemaProps.alias)) {
476
- this._alias = schemaProps.alias;
477
- }
478
- else {
479
- throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.InvalidECJson, `The Schema ${schemaProps.name} does not have the required 'alias' attribute.`);
480
- }
481
- if (undefined !== schemaProps.label)
482
- this._label = schemaProps.label;
483
- if (undefined !== schemaProps.description)
484
- this._description = schemaProps.description;
485
- }
486
- async fromJSON(schemaProps) {
487
- this.fromJSONSync(schemaProps);
488
- }
489
- static async fromJson(jsonObj, context) {
490
- let schema = new Schema(context);
491
- const reader = new Helper_1.SchemaReadHelper(JsonParser_1.JsonParser, context);
492
- const rawSchema = typeof jsonObj === "string" ? JSON.parse(jsonObj) : jsonObj;
493
- schema = await reader.readSchema(schema, rawSchema);
494
- return schema;
495
- }
496
- static fromJsonSync(jsonObj, context) {
497
- let schema = new Schema(context);
498
- const reader = new Helper_1.SchemaReadHelper(JsonParser_1.JsonParser, context);
499
- const rawSchema = typeof jsonObj === "string" ? JSON.parse(jsonObj) : jsonObj;
500
- schema = reader.readSchemaSync(schema, rawSchema);
501
- return schema;
502
- }
503
- /**
504
- * @internal
505
- */
506
- static isSchema(object) {
507
- const schema = object;
508
- return schema !== undefined && schema.schemaKey !== undefined && schema.context !== undefined;
509
- }
510
- }
511
- exports.Schema = Schema;
512
- /**
513
- * Hackish approach that works like a "friend class" so we can access protected members without making them public.
514
- * We cannot put this into Helper.ts and make it non-export, because we are importing Helper.ts from this file, and the circular import
515
- * would prevent this class from extending Schema.
516
- * @internal
517
- */
518
- class MutableSchema extends Schema {
519
- }
520
- exports.MutableSchema = MutableSchema;
1
+ "use strict";
2
+ /*---------------------------------------------------------------------------------------------
3
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
4
+ * See LICENSE.md in the project root for license terms and full copyright notice.
5
+ *--------------------------------------------------------------------------------------------*/
6
+ /** @packageDocumentation
7
+ * @module Metadata
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.MutableSchema = exports.Schema = void 0;
11
+ const Helper_1 = require("../Deserialization/Helper");
12
+ const JsonParser_1 = require("../Deserialization/JsonParser");
13
+ const XmlSerializationUtils_1 = require("../Deserialization/XmlSerializationUtils");
14
+ const Exception_1 = require("../Exception");
15
+ const SchemaKey_1 = require("../SchemaKey");
16
+ const ECName_1 = require("../ECName");
17
+ const Class_1 = require("./Class");
18
+ const Constant_1 = require("./Constant");
19
+ const CustomAttribute_1 = require("./CustomAttribute");
20
+ const CustomAttributeClass_1 = require("./CustomAttributeClass");
21
+ const EntityClass_1 = require("./EntityClass");
22
+ const Enumeration_1 = require("./Enumeration");
23
+ const Format_1 = require("./Format");
24
+ const InvertedUnit_1 = require("./InvertedUnit");
25
+ const KindOfQuantity_1 = require("./KindOfQuantity");
26
+ const Mixin_1 = require("./Mixin");
27
+ const Phenomenon_1 = require("./Phenomenon");
28
+ const PropertyCategory_1 = require("./PropertyCategory");
29
+ const RelationshipClass_1 = require("./RelationshipClass");
30
+ const SchemaItem_1 = require("./SchemaItem");
31
+ const Unit_1 = require("./Unit");
32
+ const UnitSystem_1 = require("./UnitSystem");
33
+ const SCHEMAURL3_2_JSON = "https://dev.bentley.com/json_schemas/ec/32/ecschema";
34
+ const SCHEMAURL3_2_XML = "http://www.bentley.com/schemas/Bentley.ECXML.3.2";
35
+ /**
36
+ * @beta
37
+ */
38
+ class Schema {
39
+ /** @internal */
40
+ constructor(context, nameOrKey, alias, readVer, writeVer, minorVer) {
41
+ this._schemaKey = (typeof (nameOrKey) === "string") ? new SchemaKey_1.SchemaKey(nameOrKey, new SchemaKey_1.ECVersion(readVer, writeVer, minorVer)) : nameOrKey;
42
+ this._context = context;
43
+ this.references = [];
44
+ this._items = new Map();
45
+ if (alias !== undefined && ECName_1.ECName.validate(alias)) {
46
+ this._alias = alias;
47
+ }
48
+ else if (nameOrKey !== undefined) {
49
+ throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.InvalidECJson, `The Schema ${this.name} does not have the required 'alias' attribute.`);
50
+ }
51
+ }
52
+ get schemaKey() {
53
+ if (undefined === this._schemaKey)
54
+ throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.InvalidECJson, `The schema '${this.name}' has an invalid SchemaKey.`);
55
+ return this._schemaKey;
56
+ }
57
+ get name() { return this.schemaKey.name; }
58
+ get readVersion() { return this.schemaKey.readVersion; }
59
+ get writeVersion() { return this.schemaKey.writeVersion; }
60
+ get minorVersion() { return this.schemaKey.minorVersion; }
61
+ get alias() {
62
+ if (this._alias === undefined || this._alias === null) {
63
+ throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.InvalidECJson, `The Schema ${this.name} does not have the required 'alias' attribute.`);
64
+ }
65
+ else {
66
+ return this._alias;
67
+ }
68
+ }
69
+ get label() { return this._label; }
70
+ get description() { return this._description; }
71
+ get customAttributes() { return this._customAttributes; }
72
+ /** Returns the schema name. */
73
+ get fullName() { return this.schemaKey.name; }
74
+ /** Returns the schema. */
75
+ get schema() { return this; }
76
+ /** Returns the schema context this schema is within. */
77
+ get context() { return this._context; }
78
+ /**
79
+ * Returns a SchemaItemKey given the item name and the schema it belongs to
80
+ * @param fullName fully qualified name {Schema name}.{Item Name}
81
+ */
82
+ getSchemaItemKey(fullName) {
83
+ const [schemaName, itemName] = SchemaItem_1.SchemaItem.parseFullName(fullName);
84
+ let schemaKey = this.schemaKey;
85
+ if (this.name !== schemaName) {
86
+ const newSchemaRef = this.getReferenceSync(schemaName);
87
+ if (undefined === newSchemaRef)
88
+ throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.InvalidECJson, `Unable to find the referenced SchemaItem ${itemName}.`);
89
+ schemaKey = newSchemaRef.schemaKey;
90
+ }
91
+ return new SchemaKey_1.SchemaItemKey(itemName, schemaKey);
92
+ }
93
+ addItem(item) {
94
+ if (undefined !== this.getItemSync(item.name))
95
+ throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.DuplicateItem, `The SchemaItem ${item.name} cannot be added to the schema ${this.name} because it already exists`);
96
+ this._items.set(item.name.toUpperCase(), item);
97
+ }
98
+ /**
99
+ * @alpha
100
+ */
101
+ createClass(type, name, modifier) {
102
+ const item = new type(this, name, modifier);
103
+ this.addItem(item);
104
+ return item;
105
+ }
106
+ /**
107
+ * Deletes a class from within this schema.
108
+ * @param name the local (unqualified) class name, lookup is case-insensitive
109
+ * @alpha
110
+ */
111
+ async deleteClass(name) {
112
+ const schemaItem = await this.getItem(name);
113
+ if (Class_1.ECClass.isECClass(schemaItem)) {
114
+ this._items.delete(name.toUpperCase());
115
+ }
116
+ }
117
+ /**
118
+ * Deletes a class from within this schema.
119
+ * @param name the local (unqualified) class name, lookup is case-insensitive
120
+ * @alpha
121
+ */
122
+ deleteClassSync(name) {
123
+ const schemaItem = this.getItemSync(name);
124
+ if (Class_1.ECClass.isECClass(schemaItem))
125
+ this._items.delete(name.toUpperCase());
126
+ }
127
+ /**
128
+ * @alpha
129
+ */
130
+ createItem(type, name) {
131
+ const item = new type(this, name);
132
+ this.addItem(item);
133
+ return item;
134
+ }
135
+ addCustomAttribute(customAttribute) {
136
+ if (!this._customAttributes)
137
+ this._customAttributes = new Map();
138
+ this._customAttributes.set(customAttribute.className, customAttribute);
139
+ }
140
+ /**
141
+ * Creates a EntityClass with the provided name in this schema.
142
+ * @param name
143
+ * @param modifier
144
+ */
145
+ async createEntityClass(name, modifier) {
146
+ return this.createClass(EntityClass_1.EntityClass, name, modifier);
147
+ }
148
+ createEntityClassSync(name, modifier) {
149
+ return this.createClass(EntityClass_1.EntityClass, name, modifier);
150
+ }
151
+ /**
152
+ * Creates a Mixin with the provided name in this schema.
153
+ * @param name
154
+ */
155
+ async createMixinClass(name) { return this.createClass(Mixin_1.Mixin, name); }
156
+ createMixinClassSync(name) { return this.createClass(Mixin_1.Mixin, name); }
157
+ /**
158
+ * Creates a StructClass with the provided name in this schema.
159
+ * @param name
160
+ * @param modifier
161
+ */
162
+ async createStructClass(name, modifier) {
163
+ return this.createClass(Class_1.StructClass, name, modifier);
164
+ }
165
+ createStructClassSync(name, modifier) {
166
+ return this.createClass(Class_1.StructClass, name, modifier);
167
+ }
168
+ /**
169
+ * Creates a CustomAttributeClass with the provided name in this schema.
170
+ * @param name
171
+ * @param modifier
172
+ */
173
+ async createCustomAttributeClass(name, modifier) {
174
+ return this.createClass(CustomAttributeClass_1.CustomAttributeClass, name, modifier);
175
+ }
176
+ createCustomAttributeClassSync(name, modifier) {
177
+ return this.createClass(CustomAttributeClass_1.CustomAttributeClass, name, modifier);
178
+ }
179
+ /**
180
+ * Creates a RelationshipClass with the provided name in this schema.
181
+ * @param name
182
+ * @param modifier
183
+ */
184
+ async createRelationshipClass(name, modifier) {
185
+ return this.createRelationshipClassSync(name, modifier);
186
+ }
187
+ createRelationshipClassSync(name, modifier) {
188
+ return this.createClass(RelationshipClass_1.RelationshipClass, name, modifier);
189
+ }
190
+ /**
191
+ * Creates an Enumeration with the provided name in this schema.
192
+ * @param name
193
+ */
194
+ async createEnumeration(name, primitiveType) {
195
+ return this.createEnumerationSync(name, primitiveType);
196
+ }
197
+ createEnumerationSync(name, primitiveType) {
198
+ const item = new Enumeration_1.Enumeration(this, name, primitiveType);
199
+ this.addItem(item);
200
+ return item;
201
+ }
202
+ /**
203
+ * Creates an KindOfQuantity with the provided name in this schema.
204
+ * @param name
205
+ */
206
+ async createKindOfQuantity(name) {
207
+ return this.createKindOfQuantitySync(name);
208
+ }
209
+ createKindOfQuantitySync(name) {
210
+ return this.createItem(KindOfQuantity_1.KindOfQuantity, name);
211
+ }
212
+ /**
213
+ * Creates a Constant with the provided name in this schema.
214
+ * @param name
215
+ */
216
+ async createConstant(name) {
217
+ return this.createItem(Constant_1.Constant, name);
218
+ }
219
+ createConstantSync(name) {
220
+ return this.createItem(Constant_1.Constant, name);
221
+ }
222
+ /**
223
+ * Creates a Inverted Unit with the provided name in this schema.
224
+ * @param name
225
+ */
226
+ async createInvertedUnit(name) {
227
+ return this.createItem(InvertedUnit_1.InvertedUnit, name);
228
+ }
229
+ createInvertedUnitSync(name) {
230
+ return this.createItem(InvertedUnit_1.InvertedUnit, name);
231
+ }
232
+ /**
233
+ * Creates an Format with the provided name in this schema.
234
+ * @param name
235
+ */
236
+ async createFormat(name) {
237
+ return this.createItem(Format_1.Format, name);
238
+ }
239
+ createFormatSync(name) {
240
+ return this.createItem(Format_1.Format, name);
241
+ }
242
+ /**
243
+ * Creates a UnitSystem with the provided name in this schema.
244
+ * @param name
245
+ */
246
+ async createUnitSystem(name) {
247
+ return this.createItem(UnitSystem_1.UnitSystem, name);
248
+ }
249
+ createUnitSystemSync(name) {
250
+ return this.createItem(UnitSystem_1.UnitSystem, name);
251
+ }
252
+ /**
253
+ * Creates a Phenomenon with the provided name in this schema.
254
+ * @param name
255
+ */
256
+ async createPhenomenon(name) {
257
+ return this.createItem(Phenomenon_1.Phenomenon, name);
258
+ }
259
+ createPhenomenonSync(name) {
260
+ return this.createItem(Phenomenon_1.Phenomenon, name);
261
+ }
262
+ /**
263
+ * Creates a Unit with the provided name in this schema.
264
+ * @param name
265
+ */
266
+ async createUnit(name) {
267
+ return this.createItem(Unit_1.Unit, name);
268
+ }
269
+ createUnitSync(name) {
270
+ return this.createItem(Unit_1.Unit, name);
271
+ }
272
+ /**
273
+ * Creates an PropertyCategory with the provided name in this schema.
274
+ * @param name
275
+ */
276
+ async createPropertyCategory(name) {
277
+ return this.createItem(PropertyCategory_1.PropertyCategory, name);
278
+ }
279
+ createPropertyCategorySync(name) {
280
+ return this.createItem(PropertyCategory_1.PropertyCategory, name);
281
+ }
282
+ /**
283
+ *
284
+ * @param refSchema
285
+ */
286
+ async addReference(refSchema) {
287
+ // TODO validation of reference schema. For now just adding
288
+ this.addReferenceSync(refSchema);
289
+ }
290
+ addReferenceSync(refSchema) {
291
+ this.references.push(refSchema);
292
+ }
293
+ /**
294
+ * @alpha Used for schema editing.
295
+ */
296
+ setContext(context) {
297
+ this._context = context;
298
+ }
299
+ /**
300
+ * Sets the version of the SchemaKey identifying the schema.
301
+ * @param readVersion The read version of the schema. If undefined, the value from the existing SchemaKey will be used.
302
+ * @param writeVersion The write version of the schema. If undefined, the value from the existing SchemaKey will be used.
303
+ * @param minorVersion The minor version of the schema. If undefined, the value from the existing SchemaKey will be used.
304
+ */
305
+ setVersion(readVersion, writeVersion, minorVersion) {
306
+ if (!this._schemaKey)
307
+ throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.InvalidSchemaKey, `The schema '${this.name}' has an invalid SchemaKey.`);
308
+ const newVersion = new SchemaKey_1.ECVersion(readVersion !== null && readVersion !== void 0 ? readVersion : this._schemaKey.readVersion, writeVersion !== null && writeVersion !== void 0 ? writeVersion : this._schemaKey.writeVersion, minorVersion !== null && minorVersion !== void 0 ? minorVersion : this._schemaKey.minorVersion);
309
+ this._schemaKey = new SchemaKey_1.SchemaKey(this._schemaKey.name, newVersion);
310
+ }
311
+ /**
312
+ * Gets an item from within this schema. To get by full name use lookupItem instead.
313
+ * @param key the local (unqualified) name, lookup is case-insensitive
314
+ */
315
+ async getItem(name) {
316
+ // this method exists so we can rewire it later when we load partial schemas, for now it is identical to the sync version
317
+ return this.getItemSync(name);
318
+ }
319
+ /**
320
+ * Gets an item from within this schema. To get by full name use lookupItem instead.
321
+ * @param key the local (unqualified) name, lookup is case-insensitive
322
+ */
323
+ getItemSync(name) {
324
+ // Case-insensitive search
325
+ return this._items.get(name.toUpperCase());
326
+ }
327
+ /**
328
+ * Attempts to find a schema item within this schema or a (directly) referenced schema
329
+ * @param key The full name or a SchemaItemKey identifying the desired item.
330
+ */
331
+ async lookupItem(key) {
332
+ let schemaName, itemName;
333
+ if (typeof (key) === "string") {
334
+ [schemaName, itemName] = SchemaItem_1.SchemaItem.parseFullName(key);
335
+ }
336
+ else {
337
+ itemName = key.name;
338
+ schemaName = key.schemaName;
339
+ }
340
+ if (!schemaName || schemaName.toUpperCase() === this.name.toUpperCase()) {
341
+ return this.getItem(itemName);
342
+ }
343
+ const refSchema = await this.getReference(schemaName);
344
+ if (!refSchema)
345
+ return undefined;
346
+ return refSchema.getItem(itemName);
347
+ }
348
+ /**
349
+ * Attempts to find a schema item within this schema or a (directly) referenced schema
350
+ * @param key The full name or a SchemaItemKey identifying the desired item.
351
+ */
352
+ lookupItemSync(key) {
353
+ let schemaName, itemName;
354
+ if (typeof (key) === "string") {
355
+ [schemaName, itemName] = SchemaItem_1.SchemaItem.parseFullName(key);
356
+ }
357
+ else {
358
+ itemName = key.name;
359
+ schemaName = key.schemaName;
360
+ }
361
+ if (!schemaName || schemaName.toUpperCase() === this.name.toUpperCase()) {
362
+ return this.getItemSync(itemName);
363
+ }
364
+ const refSchema = this.getReferenceSync(schemaName);
365
+ if (!refSchema)
366
+ return undefined;
367
+ return refSchema.getItemSync(itemName);
368
+ }
369
+ getItems() {
370
+ if (!this._items)
371
+ return new Map().values();
372
+ return this._items.values();
373
+ }
374
+ *getClasses() {
375
+ for (const [, value] of this._items) {
376
+ if (Class_1.ECClass.isECClass(value))
377
+ yield value;
378
+ }
379
+ }
380
+ async getReference(refSchemaName) {
381
+ if (this.references.length === 0)
382
+ return undefined;
383
+ return this.references.find((ref) => ref.name.toLowerCase() === refSchemaName.toLowerCase());
384
+ }
385
+ getReferenceNameByAlias(alias) {
386
+ if (this.references.length === 0)
387
+ return undefined;
388
+ const schema = this.references.find((ref) => ref.alias ? ref.alias.toLowerCase() === alias.toLowerCase() : false);
389
+ return schema ? schema.name : undefined;
390
+ }
391
+ getReferenceSync(refSchemaName) {
392
+ if (this.references.length === 0)
393
+ return undefined;
394
+ return this.references.find((ref) => ref.name.toLowerCase() === refSchemaName.toLowerCase());
395
+ }
396
+ /**
397
+ * Save this Schema's properties to an object for serializing to JSON.
398
+ */
399
+ toJSON() {
400
+ const schemaJson = {};
401
+ schemaJson.$schema = SCHEMAURL3_2_JSON; // $schema is required
402
+ schemaJson.name = this.name; // name is required
403
+ schemaJson.version = this.schemaKey.version.toString(true);
404
+ schemaJson.alias = this.alias; // alias is required
405
+ if (undefined !== this.label) // label is optional
406
+ schemaJson.label = this.label;
407
+ if (undefined !== this.description) // description is optional
408
+ schemaJson.description = this.description;
409
+ if (undefined !== this.references && this.references.length > 0) // references is optional
410
+ schemaJson.references = this.references.map(({ name, schemaKey }) => ({ name, version: schemaKey.version.toString() }));
411
+ const customAttributes = (0, CustomAttribute_1.serializeCustomAttributes)(this.customAttributes);
412
+ if (undefined !== customAttributes)
413
+ schemaJson.customAttributes = customAttributes;
414
+ if (this._items.size > 0) {
415
+ schemaJson.items = {};
416
+ this._items.forEach((schemaItem) => {
417
+ schemaJson.items[schemaItem.name] = schemaItem.toJSON(false, true);
418
+ });
419
+ }
420
+ return schemaJson;
421
+ }
422
+ /**
423
+ * Converts the schema to a DOM XML Document.
424
+ * @param schemaXml An empty DOM document to which the schema will be written
425
+ */
426
+ async toXml(schemaXml) {
427
+ const schemaMetadata = schemaXml.createElement("ECSchema");
428
+ schemaMetadata.setAttribute("xmlns", SCHEMAURL3_2_XML);
429
+ schemaMetadata.setAttribute("version", this.schemaKey.version.toString());
430
+ schemaMetadata.setAttribute("schemaName", this.name);
431
+ schemaMetadata.setAttribute("alias", this.alias ? this.alias : "");
432
+ if (undefined !== this.label)
433
+ schemaMetadata.setAttribute("displayLabel", this.label);
434
+ if (undefined !== this.description)
435
+ schemaMetadata.setAttribute("description", this.description);
436
+ // Map used for CA serialization
437
+ const refSchemaMap = new Map();
438
+ this.references.forEach(({ name, schemaKey, alias }) => {
439
+ const schemaRef = schemaXml.createElement("ECSchemaReference");
440
+ schemaRef.setAttribute("name", name);
441
+ schemaRef.setAttribute("version", schemaKey.version.toString());
442
+ schemaRef.setAttribute("alias", alias ? alias : "");
443
+ schemaMetadata.appendChild(schemaRef);
444
+ refSchemaMap.set(name, schemaKey.version.toString());
445
+ });
446
+ if (this._customAttributes) {
447
+ const parentElem = schemaXml.createElement("ECCustomAttributes");
448
+ for (const [name, attribute] of this._customAttributes) {
449
+ const caElem = await XmlSerializationUtils_1.XmlSerializationUtils.writeCustomAttribute(name, attribute, schemaXml, this);
450
+ parentElem.appendChild(caElem);
451
+ }
452
+ schemaMetadata.appendChild(parentElem);
453
+ }
454
+ for (const [, item] of this._items) {
455
+ const itemXml = await item.toXml(schemaXml);
456
+ schemaMetadata.appendChild(itemXml);
457
+ }
458
+ schemaXml.appendChild(schemaMetadata);
459
+ return schemaXml;
460
+ }
461
+ fromJSONSync(schemaProps) {
462
+ if (undefined === this._schemaKey) {
463
+ const schemaName = schemaProps.name;
464
+ const version = SchemaKey_1.ECVersion.fromString(schemaProps.version);
465
+ this._schemaKey = new SchemaKey_1.SchemaKey(schemaName, version);
466
+ }
467
+ else {
468
+ if (schemaProps.name.toLowerCase() !== this.name.toLowerCase())
469
+ throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.InvalidECJson, `The Schema ${this.name} does not match the provided name, '${schemaProps.name}'.`);
470
+ if (this.schemaKey.version.compare(SchemaKey_1.ECVersion.fromString(schemaProps.version)))
471
+ throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.InvalidECJson, `The Schema ${this.name} has the version '${this.schemaKey.version}' that does not match the provided version '${schemaProps.version}'.`);
472
+ }
473
+ if (SCHEMAURL3_2_JSON !== schemaProps.$schema && SCHEMAURL3_2_XML !== schemaProps.$schema) // TODO: Allow for 3.x URI versions to allow the API to read newer specs. (Start at 3.2 though)
474
+ throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.MissingSchemaUrl, `The Schema ${this.name} has an unsupported namespace '${schemaProps.$schema}'.`);
475
+ if (ECName_1.ECName.validate(schemaProps.alias)) {
476
+ this._alias = schemaProps.alias;
477
+ }
478
+ else {
479
+ throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.InvalidECJson, `The Schema ${schemaProps.name} does not have the required 'alias' attribute.`);
480
+ }
481
+ if (undefined !== schemaProps.label)
482
+ this._label = schemaProps.label;
483
+ if (undefined !== schemaProps.description)
484
+ this._description = schemaProps.description;
485
+ }
486
+ async fromJSON(schemaProps) {
487
+ this.fromJSONSync(schemaProps);
488
+ }
489
+ static async fromJson(jsonObj, context) {
490
+ let schema = new Schema(context);
491
+ const reader = new Helper_1.SchemaReadHelper(JsonParser_1.JsonParser, context);
492
+ const rawSchema = typeof jsonObj === "string" ? JSON.parse(jsonObj) : jsonObj;
493
+ schema = await reader.readSchema(schema, rawSchema);
494
+ return schema;
495
+ }
496
+ static fromJsonSync(jsonObj, context) {
497
+ let schema = new Schema(context);
498
+ const reader = new Helper_1.SchemaReadHelper(JsonParser_1.JsonParser, context);
499
+ const rawSchema = typeof jsonObj === "string" ? JSON.parse(jsonObj) : jsonObj;
500
+ schema = reader.readSchemaSync(schema, rawSchema);
501
+ return schema;
502
+ }
503
+ /**
504
+ * @internal
505
+ */
506
+ static isSchema(object) {
507
+ const schema = object;
508
+ return schema !== undefined && schema.schemaKey !== undefined && schema.context !== undefined;
509
+ }
510
+ }
511
+ exports.Schema = Schema;
512
+ /**
513
+ * Hackish approach that works like a "friend class" so we can access protected members without making them public.
514
+ * We cannot put this into Helper.ts and make it non-export, because we are importing Helper.ts from this file, and the circular import
515
+ * would prevent this class from extending Schema.
516
+ * @internal
517
+ */
518
+ class MutableSchema extends Schema {
519
+ }
520
+ exports.MutableSchema = MutableSchema;
521
521
  //# sourceMappingURL=Schema.js.map