@itwin/ecschema-locaters 4.1.0-dev.75 → 4.1.0-dev.76

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.
@@ -0,0 +1,54 @@
1
+ import { Schema, SchemaContext, SchemaKey, SchemaMatchType } from "@itwin/ecschema-metadata";
2
+ /** @packageDocumentation
3
+ * @module Locaters
4
+ */
5
+ /**
6
+ * A SchemaKey implementation that aids in identifying Schema strings via the
7
+ * addition of the schemaText property. The schemaText is the full XML string
8
+ * representation of the Schema.
9
+ * @beta
10
+ */
11
+ export declare class StringSchemaKey extends SchemaKey {
12
+ schemaText: string;
13
+ /**
14
+ * Initializes a new StringSchemaKey object.
15
+ * @param key The EC SchemaKey identifying the Schema.
16
+ * @param schemaText The string representation of the Schema
17
+ */
18
+ constructor(key: SchemaKey, schemaText: string);
19
+ }
20
+ /**
21
+ * Abstract class to hold common/overlapping functionality between SchemaJsonStringLocater and SchemaXmlStringLocater
22
+ * @beta
23
+ */
24
+ export declare abstract class SchemaStringLocater {
25
+ schemaStrings: string[];
26
+ constructor();
27
+ /**
28
+ * Adds schema strings used by this locator to find the
29
+ * Schemas.
30
+ * @param schemaPaths An array of Schema strings to add
31
+ */
32
+ addSchemaStrings(schemaStrings: string[]): void;
33
+ /**
34
+ * Adds a schema string used by this locator to locate and load Schemas.
35
+ * @param schemaText The text of the Schema
36
+ */
37
+ addSchemaString(schemaString: string): void;
38
+ protected abstract getSchemaKey(data: string): SchemaKey;
39
+ /**
40
+ * Attempts to find all Schema strings that match the desired SchemaKey.
41
+ * @param desiredKey The SchemaKey to match.
42
+ * @param matchType The SchemaMatchType.
43
+ */
44
+ protected findEligibleSchemaKeys(desiredKey: Readonly<SchemaKey>, matchType: SchemaMatchType): StringSchemaKey[];
45
+ abstract getSchema<T extends Schema>(key: SchemaKey, matchType: SchemaMatchType, context: SchemaContext): Promise<T | undefined>;
46
+ /**
47
+ * Compares two Schema versions. If the left-hand version is greater, 1 is returned. If the
48
+ * left-hand version is less, -1 us returned. If the versions are an exact match, 0 is returned.
49
+ * @param lhs The 'left-hand' StringSchemaKey.
50
+ * @param rhs The 'right-hand' StringSchemaKey.
51
+ */
52
+ compareSchemaKeyByVersion: (lhs: StringSchemaKey, rhs: StringSchemaKey) => number;
53
+ }
54
+ //# sourceMappingURL=SchemaStringLocater.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SchemaStringLocater.d.ts","sourceRoot":"","sources":["../../src/SchemaStringLocater.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAE7F;;GAEG;AAEH;;;;;GAKG;AACH,qBAAa,eAAgB,SAAQ,SAAS;IAErC,UAAU,EAAE,MAAM,CAAC;IAE1B;;;;OAIG;gBACS,GAAG,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM;CAI/C;AAED;;;GAGG;AACH,8BAAsB,mBAAmB;IAChC,aAAa,EAAE,MAAM,EAAE,CAAC;;IAM/B;;;;OAIG;IACI,gBAAgB,CAAC,aAAa,EAAE,MAAM,EAAE;IAM/C;;;OAGG;IACI,eAAe,CAAC,YAAY,EAAE,MAAM;IAO3C,SAAS,CAAC,QAAQ,CAAC,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS;IAExD;;;;OAIG;IACH,SAAS,CAAC,sBAAsB,CAAC,UAAU,EAAE,QAAQ,CAAC,SAAS,CAAC,EAAE,SAAS,EAAE,eAAe,GAAG,eAAe,EAAE;aAehG,SAAS,CAAC,CAAC,SAAS,MAAM,EAAE,GAAG,EAAE,SAAS,EAAE,SAAS,EAAE,eAAe,EAAE,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAEvI;;;;;OAKG;IACI,yBAAyB,QAAS,eAAe,OAAO,eAAe,KAAG,MAAM,CAErF;CACH"}
@@ -0,0 +1,85 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.SchemaStringLocater = exports.StringSchemaKey = void 0;
8
+ const ecschema_metadata_1 = require("@itwin/ecschema-metadata");
9
+ /** @packageDocumentation
10
+ * @module Locaters
11
+ */
12
+ /**
13
+ * A SchemaKey implementation that aids in identifying Schema strings via the
14
+ * addition of the schemaText property. The schemaText is the full XML string
15
+ * representation of the Schema.
16
+ * @beta
17
+ */
18
+ class StringSchemaKey extends ecschema_metadata_1.SchemaKey {
19
+ /**
20
+ * Initializes a new StringSchemaKey object.
21
+ * @param key The EC SchemaKey identifying the Schema.
22
+ * @param schemaText The string representation of the Schema
23
+ */
24
+ constructor(key, schemaText) {
25
+ super(key.name, key.version);
26
+ this.schemaText = schemaText;
27
+ }
28
+ }
29
+ exports.StringSchemaKey = StringSchemaKey;
30
+ /**
31
+ * Abstract class to hold common/overlapping functionality between SchemaJsonStringLocater and SchemaXmlStringLocater
32
+ * @beta
33
+ */
34
+ class SchemaStringLocater {
35
+ constructor() {
36
+ /**
37
+ * Compares two Schema versions. If the left-hand version is greater, 1 is returned. If the
38
+ * left-hand version is less, -1 us returned. If the versions are an exact match, 0 is returned.
39
+ * @param lhs The 'left-hand' StringSchemaKey.
40
+ * @param rhs The 'right-hand' StringSchemaKey.
41
+ */
42
+ this.compareSchemaKeyByVersion = (lhs, rhs) => {
43
+ return lhs.compareByVersion(rhs);
44
+ };
45
+ this.schemaStrings = [];
46
+ }
47
+ /**
48
+ * Adds schema strings used by this locator to find the
49
+ * Schemas.
50
+ * @param schemaPaths An array of Schema strings to add
51
+ */
52
+ addSchemaStrings(schemaStrings) {
53
+ // If the path is not in the schemaPaths array, add it
54
+ for (const schemaString of schemaStrings)
55
+ this.addSchemaString(schemaString);
56
+ }
57
+ /**
58
+ * Adds a schema string used by this locator to locate and load Schemas.
59
+ * @param schemaText The text of the Schema
60
+ */
61
+ addSchemaString(schemaString) {
62
+ const schemaKey = this.getSchemaKey(schemaString);
63
+ // If the string is not in the schemaStrings array, add it
64
+ if (!this.schemaStrings.find((entry) => this.getSchemaKey(entry).matches(schemaKey, ecschema_metadata_1.SchemaMatchType.Exact)))
65
+ this.schemaStrings.push(schemaString);
66
+ }
67
+ /**
68
+ * Attempts to find all Schema strings that match the desired SchemaKey.
69
+ * @param desiredKey The SchemaKey to match.
70
+ * @param matchType The SchemaMatchType.
71
+ */
72
+ findEligibleSchemaKeys(desiredKey, matchType) {
73
+ const foundStrings = new Array();
74
+ for (const schemaString of this.schemaStrings) {
75
+ // Get the schema key
76
+ const key = this.getSchemaKey(schemaString);
77
+ // If the key matches, put it in foundFiles
78
+ if (key.matches(desiredKey, matchType))
79
+ foundStrings.push(new StringSchemaKey(key, schemaString));
80
+ }
81
+ return foundStrings;
82
+ }
83
+ }
84
+ exports.SchemaStringLocater = SchemaStringLocater;
85
+ //# sourceMappingURL=SchemaStringLocater.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SchemaStringLocater.js","sourceRoot":"","sources":["../../src/SchemaStringLocater.ts"],"names":[],"mappings":";AAAA;;;+FAG+F;;;AAE/F,gEAA6F;AAE7F;;GAEG;AAEH;;;;;GAKG;AACH,MAAa,eAAgB,SAAQ,6BAAS;IAI5C;;;;OAIG;IACH,YAAY,GAAc,EAAE,UAAkB;QAC5C,KAAK,CAAC,GAAG,CAAC,IAAI,EAAE,GAAG,CAAC,OAAO,CAAC,CAAC;QAC7B,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;IAC/B,CAAC;CACF;AAbD,0CAaC;AAED;;;GAGG;AACH,MAAsB,mBAAmB;IAGvC;QAkDA;;;;;WAKG;QACI,8BAAyB,GAAG,CAAC,GAAoB,EAAE,GAAoB,EAAU,EAAE;YACxF,OAAO,GAAG,CAAC,gBAAgB,CAAC,GAAG,CAAC,CAAC;QACnC,CAAC,CAAC;QAzDA,IAAI,CAAC,aAAa,GAAG,EAAE,CAAC;IAC1B,CAAC;IAED;;;;OAIG;IACI,gBAAgB,CAAC,aAAuB;QAC7C,sDAAsD;QACtD,KAAK,MAAM,YAAY,IAAI,aAAa;YACtC,IAAI,CAAC,eAAe,CAAC,YAAY,CAAC,CAAC;IACvC,CAAC;IAED;;;OAGG;IACI,eAAe,CAAC,YAAoB;QACzC,MAAM,SAAS,GAAG,IAAI,CAAC,YAAY,CAAC,YAAY,CAAC,CAAC;QAClD,0DAA0D;QAC1D,IAAI,CAAC,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,SAAS,EAAE,mCAAe,CAAC,KAAK,CAAC,CAAC;YACzG,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;IAC1C,CAAC;IAID;;;;OAIG;IACO,sBAAsB,CAAC,UAA+B,EAAE,SAA0B;QAC1F,MAAM,YAAY,GAAG,IAAI,KAAK,EAAmB,CAAC;QAElD,KAAK,MAAM,YAAY,IAAI,IAAI,CAAC,aAAa,EAAE;YAC7C,qBAAqB;YACrB,MAAM,GAAG,GAAG,IAAI,CAAC,YAAY,CAAC,YAAY,CAAC,CAAC;YAE5C,2CAA2C;YAC3C,IAAI,GAAG,CAAC,OAAO,CAAC,UAAU,EAAE,SAAS,CAAC;gBACpC,YAAY,CAAC,IAAI,CAAC,IAAI,eAAe,CAAC,GAAG,EAAE,YAAY,CAAC,CAAC,CAAC;SAC7D;QAED,OAAO,YAAY,CAAC;IACtB,CAAC;CAaF;AA9DD,kDA8DC","sourcesContent":["/*---------------------------------------------------------------------------------------------\r\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\r\n* See LICENSE.md in the project root for license terms and full copyright notice.\r\n*--------------------------------------------------------------------------------------------*/\r\n\r\nimport { Schema, SchemaContext, SchemaKey, SchemaMatchType } from \"@itwin/ecschema-metadata\";\r\n\r\n/** @packageDocumentation\r\n * @module Locaters\r\n */\r\n\r\n/**\r\n * A SchemaKey implementation that aids in identifying Schema strings via the\r\n * addition of the schemaText property. The schemaText is the full XML string\r\n * representation of the Schema.\r\n * @beta\r\n */\r\nexport class StringSchemaKey extends SchemaKey {\r\n // The text for the schema loaded\r\n public schemaText: string;\r\n\r\n /**\r\n * Initializes a new StringSchemaKey object.\r\n * @param key The EC SchemaKey identifying the Schema.\r\n * @param schemaText The string representation of the Schema\r\n */\r\n constructor(key: SchemaKey, schemaText: string) {\r\n super(key.name, key.version);\r\n this.schemaText = schemaText;\r\n }\r\n}\r\n\r\n/**\r\n * Abstract class to hold common/overlapping functionality between SchemaJsonStringLocater and SchemaXmlStringLocater\r\n * @beta\r\n */\r\nexport abstract class SchemaStringLocater {\r\n public schemaStrings: string[];\r\n\r\n constructor() {\r\n this.schemaStrings = [];\r\n }\r\n\r\n /**\r\n * Adds schema strings used by this locator to find the\r\n * Schemas.\r\n * @param schemaPaths An array of Schema strings to add\r\n */\r\n public addSchemaStrings(schemaStrings: string[]) {\r\n // If the path is not in the schemaPaths array, add it\r\n for (const schemaString of schemaStrings)\r\n this.addSchemaString(schemaString);\r\n }\r\n\r\n /**\r\n * Adds a schema string used by this locator to locate and load Schemas.\r\n * @param schemaText The text of the Schema\r\n */\r\n public addSchemaString(schemaString: string) {\r\n const schemaKey = this.getSchemaKey(schemaString);\r\n // If the string is not in the schemaStrings array, add it\r\n if (!this.schemaStrings.find((entry) => this.getSchemaKey(entry).matches(schemaKey, SchemaMatchType.Exact)))\r\n this.schemaStrings.push(schemaString);\r\n }\r\n\r\n protected abstract getSchemaKey(data: string): SchemaKey;\r\n\r\n /**\r\n * Attempts to find all Schema strings that match the desired SchemaKey.\r\n * @param desiredKey The SchemaKey to match.\r\n * @param matchType The SchemaMatchType.\r\n */\r\n protected findEligibleSchemaKeys(desiredKey: Readonly<SchemaKey>, matchType: SchemaMatchType): StringSchemaKey[] {\r\n const foundStrings = new Array<StringSchemaKey>();\r\n\r\n for (const schemaString of this.schemaStrings) {\r\n // Get the schema key\r\n const key = this.getSchemaKey(schemaString);\r\n\r\n // If the key matches, put it in foundFiles\r\n if (key.matches(desiredKey, matchType))\r\n foundStrings.push(new StringSchemaKey(key, schemaString));\r\n }\r\n\r\n return foundStrings;\r\n }\r\n\r\n public abstract getSchema<T extends Schema>(key: SchemaKey, matchType: SchemaMatchType, context: SchemaContext): Promise<T | undefined>;\r\n\r\n /**\r\n * Compares two Schema versions. If the left-hand version is greater, 1 is returned. If the\r\n * left-hand version is less, -1 us returned. If the versions are an exact match, 0 is returned.\r\n * @param lhs The 'left-hand' StringSchemaKey.\r\n * @param rhs The 'right-hand' StringSchemaKey.\r\n */\r\n public compareSchemaKeyByVersion = (lhs: StringSchemaKey, rhs: StringSchemaKey): number => {\r\n return lhs.compareByVersion(rhs);\r\n };\r\n}\r\n"]}
@@ -0,0 +1,48 @@
1
+ import { ISchemaLocater, Schema, SchemaContext, SchemaInfo, SchemaKey, SchemaMatchType } from "@itwin/ecschema-metadata";
2
+ import { SchemaStringLocater } from "./SchemaStringLocater";
3
+ /** @packageDocumentation
4
+ * @module Locaters
5
+ */
6
+ /**
7
+ * A SchemaLocator implementation for locating and deserializing EC Schemas from XML strings
8
+ * loaded in memory.
9
+ * @beta
10
+ */
11
+ export declare class SchemaXmlStringLocater extends SchemaStringLocater implements ISchemaLocater {
12
+ /**
13
+ * Constructs a SchemaKey based on the information in the Schema XML.
14
+ * @param schemaXml The Schema XML as a string.
15
+ */
16
+ getSchemaKey(schemaXml: string): SchemaKey;
17
+ /**
18
+ * Attempts to retrieve a Schema with the given SchemaKey by searching the configured
19
+ * Schema strings.
20
+ * @param key The SchemaKey of the Schema to retrieve.
21
+ * @param matchType The SchemaMatchType.
22
+ * @param context The SchemaContext that will control the lifetime of the schema and holds the schema's references, if they exist.
23
+ */
24
+ getSchema<T extends Schema>(schemaKey: SchemaKey, matchType: SchemaMatchType, context: SchemaContext): Promise<T | undefined>;
25
+ /**
26
+ * Gets the schema info which matches the provided SchemaKey. The schema info may be returned before the schema is fully loaded.
27
+ * The fully loaded schema can be gotten later from the context using the getCachedSchema method.
28
+ * @param schemaKey The SchemaKey describing the schema to get from the cache.
29
+ * @param matchType The match type to use when locating the schema
30
+ * @param context The SchemaContext that will control the lifetime of the schema and holds the schema's references, if they exist.
31
+ */
32
+ getSchemaInfo(schemaKey: SchemaKey, matchType: SchemaMatchType, context: SchemaContext): Promise<SchemaInfo | undefined>;
33
+ /**
34
+ * Attempts to retrieve a Schema with the given SchemaKey by searching the configured
35
+ * Schema strings.
36
+ * @param key The SchemaKey of the Schema to retrieve.
37
+ * @param matchType The SchemaMatchType.
38
+ * @param context The SchemaContext that will control the lifetime of the schema and holds the schema's references, if they exist.
39
+ */
40
+ getSchemaSync<T extends Schema>(schemaKey: SchemaKey, matchType: SchemaMatchType, context: SchemaContext): T | undefined;
41
+ /**
42
+ * Parses a valid EC 2.0 version string and returns an ECVersion object. The second digit becomes the minor version,
43
+ * and a zero is inserted as the 'write' digit. Example: "1.1" -> "1.0.1".
44
+ * @param versionString A valid EC 2.0 version string of the format, 'RR.mm'.
45
+ */
46
+ private fromECv2String;
47
+ }
48
+ //# sourceMappingURL=SchemaXmlStringLocater.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SchemaXmlStringLocater.d.ts","sourceRoot":"","sources":["../../src/SchemaXmlStringLocater.ts"],"names":[],"mappings":"AAMA,OAAO,EACuC,cAAc,EAAE,MAAM,EAAE,aAAa,EAAE,UAAU,EAAE,SAAS,EAAE,eAAe,EAC1H,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,mBAAmB,EAAmB,MAAM,uBAAuB,CAAC;AAE7E;;GAEG;AAEH;;;;GAIG;AACH,qBAAa,sBAAuB,SAAQ,mBAAoB,YAAW,cAAc;IAEvF;;;OAGG;IACI,YAAY,CAAC,SAAS,EAAE,MAAM,GAAG,SAAS;IAejD;;;;;;OAMG;IACU,SAAS,CAAC,CAAC,SAAS,MAAM,EAAE,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,eAAe,EAAE,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAO1I;;;;;;QAMI;IACS,aAAa,CAAC,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,eAAe,EAAE,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,UAAU,GAAG,SAAS,CAAC;IAmBrI;;;;;;OAMG;IACI,aAAa,CAAC,CAAC,SAAS,MAAM,EAAE,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,eAAe,EAAE,OAAO,EAAE,aAAa,GAAG,CAAC,GAAG,SAAS;IAoB/H;;;;OAIG;IACH,OAAO,CAAC,cAAc;CAYvB"}
@@ -0,0 +1,106 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.SchemaXmlStringLocater = void 0;
8
+ const xmldom_1 = require("@xmldom/xmldom");
9
+ const ecschema_metadata_1 = require("@itwin/ecschema-metadata");
10
+ const SchemaStringLocater_1 = require("./SchemaStringLocater");
11
+ /** @packageDocumentation
12
+ * @module Locaters
13
+ */
14
+ /**
15
+ * A SchemaLocator implementation for locating and deserializing EC Schemas from XML strings
16
+ * loaded in memory.
17
+ * @beta
18
+ */
19
+ class SchemaXmlStringLocater extends SchemaStringLocater_1.SchemaStringLocater {
20
+ /**
21
+ * Constructs a SchemaKey based on the information in the Schema XML.
22
+ * @param schemaXml The Schema XML as a string.
23
+ */
24
+ getSchemaKey(schemaXml) {
25
+ const matches = schemaXml.match(/<ECSchema ([^]+?)>/g);
26
+ if (!matches || matches.length !== 1)
27
+ throw new ecschema_metadata_1.ECObjectsError(ecschema_metadata_1.ECObjectsStatus.InvalidSchemaXML, `Could not find '<ECSchema>' tag in the given string`);
28
+ const name = matches[0].match(/schemaName="(.+?)"/);
29
+ const version = matches[0].match(/version="(.+?)"/);
30
+ if (!name || name.length !== 2 || !version || version.length !== 2)
31
+ throw new ecschema_metadata_1.ECObjectsError(ecschema_metadata_1.ECObjectsStatus.InvalidSchemaXML, `Could not find the ECSchema 'schemaName' or 'version' tag in the given string`);
32
+ const ecVersion = ecschema_metadata_1.ECVersion.fromString(version[1]);
33
+ const key = new ecschema_metadata_1.SchemaKey(name[1], ecVersion);
34
+ return key;
35
+ }
36
+ /**
37
+ * Attempts to retrieve a Schema with the given SchemaKey by searching the configured
38
+ * Schema strings.
39
+ * @param key The SchemaKey of the Schema to retrieve.
40
+ * @param matchType The SchemaMatchType.
41
+ * @param context The SchemaContext that will control the lifetime of the schema and holds the schema's references, if they exist.
42
+ */
43
+ async getSchema(schemaKey, matchType, context) {
44
+ await this.getSchemaInfo(schemaKey, matchType, context);
45
+ const schema = await context.getCachedSchema(schemaKey, matchType);
46
+ return schema;
47
+ }
48
+ /**
49
+ * Gets the schema info which matches the provided SchemaKey. The schema info may be returned before the schema is fully loaded.
50
+ * The fully loaded schema can be gotten later from the context using the getCachedSchema method.
51
+ * @param schemaKey The SchemaKey describing the schema to get from the cache.
52
+ * @param matchType The match type to use when locating the schema
53
+ * @param context The SchemaContext that will control the lifetime of the schema and holds the schema's references, if they exist.
54
+ */
55
+ async getSchemaInfo(schemaKey, matchType, context) {
56
+ // Grab all schema strings that match the schema key
57
+ const candidates = this.findEligibleSchemaKeys(schemaKey, matchType);
58
+ if (!candidates || candidates.length === 0)
59
+ return undefined;
60
+ // eslint-disable-next-line @typescript-eslint/unbound-method
61
+ const maxCandidate = candidates.sort(this.compareSchemaKeyByVersion)[candidates.length - 1];
62
+ const schemaText = maxCandidate.schemaText;
63
+ const parser = new xmldom_1.DOMParser();
64
+ const document = parser.parseFromString(schemaText);
65
+ const reader = new ecschema_metadata_1.SchemaReadHelper(ecschema_metadata_1.XmlParser, context);
66
+ const schema = new ecschema_metadata_1.Schema(context);
67
+ return reader.readSchemaInfo(schema, document);
68
+ }
69
+ /**
70
+ * Attempts to retrieve a Schema with the given SchemaKey by searching the configured
71
+ * Schema strings.
72
+ * @param key The SchemaKey of the Schema to retrieve.
73
+ * @param matchType The SchemaMatchType.
74
+ * @param context The SchemaContext that will control the lifetime of the schema and holds the schema's references, if they exist.
75
+ */
76
+ getSchemaSync(schemaKey, matchType, context) {
77
+ // Grab all schema strings that match the schema key
78
+ const candidates = this.findEligibleSchemaKeys(schemaKey, matchType);
79
+ if (!candidates || candidates.length === 0)
80
+ return undefined;
81
+ // eslint-disable-next-line @typescript-eslint/unbound-method
82
+ const maxCandidate = candidates.sort(this.compareSchemaKeyByVersion)[candidates.length - 1];
83
+ const schemaText = maxCandidate.schemaText;
84
+ const parser = new xmldom_1.DOMParser();
85
+ const document = parser.parseFromString(schemaText);
86
+ const reader = new ecschema_metadata_1.SchemaReadHelper(ecschema_metadata_1.XmlParser, context);
87
+ let schema = new ecschema_metadata_1.Schema(context);
88
+ schema = reader.readSchemaSync(schema, document);
89
+ return schema;
90
+ }
91
+ /**
92
+ * Parses a valid EC 2.0 version string and returns an ECVersion object. The second digit becomes the minor version,
93
+ * and a zero is inserted as the 'write' digit. Example: "1.1" -> "1.0.1".
94
+ * @param versionString A valid EC 2.0 version string of the format, 'RR.mm'.
95
+ */
96
+ fromECv2String(versionString) {
97
+ const [read, minor] = versionString.split(".");
98
+ if (!read)
99
+ throw new ecschema_metadata_1.ECObjectsError(ecschema_metadata_1.ECObjectsStatus.InvalidECVersion, `The read version is missing from version string, ${versionString}`);
100
+ if (!minor)
101
+ throw new ecschema_metadata_1.ECObjectsError(ecschema_metadata_1.ECObjectsStatus.InvalidECVersion, `The minor version is missing from version string, ${versionString}`);
102
+ return new ecschema_metadata_1.ECVersion(+read, 0, +minor);
103
+ }
104
+ }
105
+ exports.SchemaXmlStringLocater = SchemaXmlStringLocater;
106
+ //# sourceMappingURL=SchemaXmlStringLocater.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SchemaXmlStringLocater.js","sourceRoot":"","sources":["../../src/SchemaXmlStringLocater.ts"],"names":[],"mappings":";AAAA;;;+FAG+F;;;AAE/F,2CAA2C;AAC3C,gEAEkC;AAClC,+DAA6E;AAE7E;;GAEG;AAEH;;;;GAIG;AACH,MAAa,sBAAuB,SAAQ,yCAAmB;IAE7D;;;OAGG;IACI,YAAY,CAAC,SAAiB;QACnC,MAAM,OAAO,GAAG,SAAS,CAAC,KAAK,CAAC,qBAAqB,CAAC,CAAC;QACvD,IAAI,CAAC,OAAO,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC;YAClC,MAAM,IAAI,kCAAc,CAAC,mCAAe,CAAC,gBAAgB,EAAE,qDAAqD,CAAC,CAAC;QAEpH,MAAM,IAAI,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,oBAAoB,CAAC,CAAC;QACpD,MAAM,OAAO,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,iBAAiB,CAAC,CAAC;QACpD,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,IAAI,CAAC,OAAO,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC;YAChE,MAAM,IAAI,kCAAc,CAAC,mCAAe,CAAC,gBAAgB,EAAE,+EAA+E,CAAC,CAAC;QAE9I,MAAM,SAAS,GAAG,6BAAS,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC;QACnD,MAAM,GAAG,GAAG,IAAI,6BAAS,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,SAAS,CAAC,CAAC;QAC9C,OAAO,GAAG,CAAC;IACb,CAAC;IAED;;;;;;OAMG;IACI,KAAK,CAAC,SAAS,CAAmB,SAAoB,EAAE,SAA0B,EAAE,OAAsB;QAC/G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AEnB,6DAA6D;QAC7D,MAAM,YAAY,GAAG,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,yBAAyB,CAAC,CAAC,UAAU,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;QAC5F,MAAM,UAAU,GAAG,YAAY,CAAC,UAAU,CAAC;QAE3C,MAAM,MAAM,GAAG,IAAI,kBAAS,EAAE,CAAC;QAC/B,MAAM,QAAQ,GAAG,MAAM,CAAC,eAAe,CAAC,UAAU,CAAC,CAAC;QAEpD,MAAM,MAAM,GAAG,IAAI,oCAAgB,CAAC,6BAAS,EAAE,OAAO,CAAC,CAAC;QACxD,IAAI,MAAM,GAAW,IAAI,0BAAM,CAAC,OAAO,CAAC,CAAC;QACzC,MAAM,GAAG,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;QAEjD,OAAO,MAAW,CAAC;IACrB,CAAC;IAED;;;;OAIG;IACK,cAAc,CAAC,aAAqB;QAC1C,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,GAAG,aAAa,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAE/C,IAAI,CAAC,IAAI;YACP,MAAM,IAAI,kCAAc,CAAC,mCAAe,CAAC,gBAAgB,EAAE,oDAAoD,aAAa,EAAE,CAAC,CAAC;QAElI,IAAI,CAAC,KAAK;YACR,MAAM,IAAI,kCAAc,CAAC,mCAAe,CAAC,gBAAgB,EAAE,qDAAqD,aAAa,EAAE,CAAC,CAAC;QAEnI,OAAO,IAAI,6BAAS,CAAC,CAAC,IAAI,EAAE,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC;IACzC,CAAC;CAEF;AAzGD,wDAyGC","sourcesContent":["/*---------------------------------------------------------------------------------------------\r\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\r\n* See LICENSE.md in the project root for license terms and full copyright notice.\r\n*--------------------------------------------------------------------------------------------*/\r\n\r\nimport { DOMParser } from \"@xmldom/xmldom\";\r\nimport {\r\n ECObjectsError, ECObjectsStatus, ECVersion, ISchemaLocater, Schema, SchemaContext, SchemaInfo, SchemaKey, SchemaMatchType, SchemaReadHelper, XmlParser,\r\n} from \"@itwin/ecschema-metadata\";\r\nimport { SchemaStringLocater, StringSchemaKey } from \"./SchemaStringLocater\";\r\n\r\n/** @packageDocumentation\r\n * @module Locaters\r\n */\r\n\r\n/**\r\n * A SchemaLocator implementation for locating and deserializing EC Schemas from XML strings\r\n * loaded in memory.\r\n * @beta\r\n */\r\nexport class SchemaXmlStringLocater extends SchemaStringLocater implements ISchemaLocater {\r\n\r\n /**\r\n * Constructs a SchemaKey based on the information in the Schema XML.\r\n * @param schemaXml The Schema XML as a string.\r\n */\r\n public getSchemaKey(schemaXml: string): SchemaKey {\r\n const matches = schemaXml.match(/<ECSchema ([^]+?)>/g);\r\n if (!matches || matches.length !== 1)\r\n throw new ECObjectsError(ECObjectsStatus.InvalidSchemaXML, `Could not find '<ECSchema>' tag in the given string`);\r\n\r\n const name = matches[0].match(/schemaName=\"(.+?)\"/);\r\n const version = matches[0].match(/version=\"(.+?)\"/);\r\n if (!name || name.length !== 2 || !version || version.length !== 2)\r\n throw new ECObjectsError(ECObjectsStatus.InvalidSchemaXML, `Could not find the ECSchema 'schemaName' or 'version' tag in the given string`);\r\n\r\n const ecVersion = ECVersion.fromString(version[1]);\r\n const key = new SchemaKey(name[1], ecVersion);\r\n return key;\r\n }\r\n\r\n /**\r\n * Attempts to retrieve a Schema with the given SchemaKey by searching the configured\r\n * Schema strings.\r\n * @param key The SchemaKey of the Schema to retrieve.\r\n * @param matchType The SchemaMatchType.\r\n * @param context The SchemaContext that will control the lifetime of the schema and holds the schema's references, if they exist.\r\n */\r\n public async getSchema<T extends Schema>(schemaKey: SchemaKey, matchType: SchemaMatchType, context: SchemaContext): Promise<T | undefined> {\r\n await this.getSchemaInfo(schemaKey, matchType, context);\r\n\r\n const schema = await context.getCachedSchema(schemaKey, matchType);\r\n return schema as T;\r\n }\r\n\r\n /**\r\n * Gets the schema info which matches the provided SchemaKey. The schema info may be returned before the schema is fully loaded.\r\n * The fully loaded schema can be gotten later from the context using the getCachedSchema method.\r\n * @param schemaKey The SchemaKey describing the schema to get from the cache.\r\n * @param matchType The match type to use when locating the schema\r\n * @param context The SchemaContext that will control the lifetime of the schema and holds the schema's references, if they exist.\r\n */\r\n public async getSchemaInfo(schemaKey: SchemaKey, matchType: SchemaMatchType, context: SchemaContext): Promise<SchemaInfo | undefined> {\r\n // Grab all schema strings that match the schema key\r\n const candidates: StringSchemaKey[] = this.findEligibleSchemaKeys(schemaKey, matchType);\r\n if (!candidates || candidates.length === 0)\r\n return undefined;\r\n\r\n // eslint-disable-next-line @typescript-eslint/unbound-method\r\n const maxCandidate = candidates.sort(this.compareSchemaKeyByVersion)[candidates.length - 1];\r\n const schemaText = maxCandidate.schemaText;\r\n\r\n const parser = new DOMParser();\r\n const document = parser.parseFromString(schemaText);\r\n\r\n const reader = new SchemaReadHelper(XmlParser, context);\r\n const schema = new Schema(context);\r\n\r\n return reader.readSchemaInfo(schema, document);\r\n }\r\n\r\n /**\r\n * Attempts to retrieve a Schema with the given SchemaKey by searching the configured\r\n * Schema strings.\r\n * @param key The SchemaKey of the Schema to retrieve.\r\n * @param matchType The SchemaMatchType.\r\n * @param context The SchemaContext that will control the lifetime of the schema and holds the schema's references, if they exist.\r\n */\r\n public getSchemaSync<T extends Schema>(schemaKey: SchemaKey, matchType: SchemaMatchType, context: SchemaContext): T | undefined {\r\n // Grab all schema strings that match the schema key\r\n const candidates: StringSchemaKey[] = this.findEligibleSchemaKeys(schemaKey, matchType);\r\n if (!candidates || candidates.length === 0)\r\n return undefined;\r\n\r\n // eslint-disable-next-line @typescript-eslint/unbound-method\r\n const maxCandidate = candidates.sort(this.compareSchemaKeyByVersion)[candidates.length - 1];\r\n const schemaText = maxCandidate.schemaText;\r\n\r\n const parser = new DOMParser();\r\n const document = parser.parseFromString(schemaText);\r\n\r\n const reader = new SchemaReadHelper(XmlParser, context);\r\n let schema: Schema = new Schema(context);\r\n schema = reader.readSchemaSync(schema, document);\r\n\r\n return schema as T;\r\n }\r\n\r\n /**\r\n * Parses a valid EC 2.0 version string and returns an ECVersion object. The second digit becomes the minor version,\r\n * and a zero is inserted as the 'write' digit. Example: \"1.1\" -> \"1.0.1\".\r\n * @param versionString A valid EC 2.0 version string of the format, 'RR.mm'.\r\n */\r\n private fromECv2String(versionString: string): ECVersion {\r\n const [read, minor] = versionString.split(\".\");\r\n\r\n if (!read)\r\n throw new ECObjectsError(ECObjectsStatus.InvalidECVersion, `The read version is missing from version string, ${versionString}`);\r\n\r\n if (!minor)\r\n throw new ECObjectsError(ECObjectsStatus.InvalidECVersion, `The minor version is missing from version string, ${versionString}`);\r\n\r\n return new ECVersion(+read, 0, +minor);\r\n }\r\n\r\n}\r\n"]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=SchemaXmlStringLocater.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SchemaXmlStringLocater.test.d.ts","sourceRoot":"","sources":["../../../src/test/SchemaXmlStringLocater.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,221 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ const chai_1 = require("chai");
8
+ const path = require("path");
9
+ const fs = require("fs");
10
+ const glob = require("glob");
11
+ const ecschema_metadata_1 = require("@itwin/ecschema-metadata");
12
+ const SchemaXmlStringLocater_1 = require("../SchemaXmlStringLocater");
13
+ describe("SchemaXmlStringLocater tests:", () => {
14
+ let locater;
15
+ let context;
16
+ let schemaStrings;
17
+ const loadSchemaString = (schemaName) => {
18
+ const searchPath = path.join(__dirname, "assets", `${schemaName}.ecschema.xml`);
19
+ const file = fs.readFileSync(searchPath);
20
+ return file.toString();
21
+ };
22
+ const loadSchemaStrings = () => {
23
+ const schemas = [];
24
+ const searchPath = path.join(__dirname, "assets", "Schema*.ecschema.xml");
25
+ const result = new glob.GlobSync(searchPath, { sync: true });
26
+ for (const match of result.found) {
27
+ const file = fs.readFileSync(match);
28
+ if (!file)
29
+ continue;
30
+ schemas.push(file.toString());
31
+ }
32
+ return schemas;
33
+ };
34
+ beforeEach(() => {
35
+ schemaStrings = loadSchemaStrings();
36
+ locater = new SchemaXmlStringLocater_1.SchemaXmlStringLocater();
37
+ context = new ecschema_metadata_1.SchemaContext();
38
+ context.addLocater(locater);
39
+ });
40
+ it("locate valid schema with multiple references", async () => {
41
+ locater.addSchemaStrings(schemaStrings);
42
+ const schemaKey = new ecschema_metadata_1.SchemaKey("SchemaA", 1, 1, 1);
43
+ const schema = await context.getSchema(schemaKey, ecschema_metadata_1.SchemaMatchType.Exact);
44
+ chai_1.assert.isDefined(schema);
45
+ chai_1.assert.strictEqual(schema.schemaKey.name, "SchemaA");
46
+ chai_1.assert.strictEqual(schema.schemaKey.version.toString(), "01.01.01");
47
+ });
48
+ it("locate valid schema with multiple references synchronously", () => {
49
+ locater.addSchemaStrings(schemaStrings);
50
+ const schemaKey = new ecschema_metadata_1.SchemaKey("SchemaA", 1, 1, 1);
51
+ const schema = context.getSchemaSync(schemaKey, ecschema_metadata_1.SchemaMatchType.Exact);
52
+ chai_1.assert.isDefined(schema);
53
+ chai_1.assert.strictEqual(schema.schemaKey.name, "SchemaA");
54
+ chai_1.assert.strictEqual(schema.schemaKey.version.toString(), "01.01.01");
55
+ });
56
+ it("getSchema called multiple times for same schema", async () => {
57
+ locater.addSchemaStrings(schemaStrings);
58
+ const schemaKey = new ecschema_metadata_1.SchemaKey("SchemaD", 4, 4, 4);
59
+ const locater1 = await locater.getSchema(schemaKey, ecschema_metadata_1.SchemaMatchType.Exact, new ecschema_metadata_1.SchemaContext());
60
+ const locater2 = await locater.getSchema(schemaKey, ecschema_metadata_1.SchemaMatchType.Exact, new ecschema_metadata_1.SchemaContext());
61
+ const context1 = await context.getSchema(schemaKey, ecschema_metadata_1.SchemaMatchType.Exact);
62
+ const context2 = await context.getSchema(schemaKey, ecschema_metadata_1.SchemaMatchType.Exact);
63
+ // locater should not cache, but context should cache
64
+ chai_1.assert.notEqual(locater1, locater2);
65
+ chai_1.assert.notEqual(locater1, context1);
66
+ chai_1.assert.strictEqual(context1, context2);
67
+ });
68
+ it("getSchema which does not exist, returns undefined", async () => {
69
+ locater.addSchemaStrings(schemaStrings);
70
+ const schemaKey = new ecschema_metadata_1.SchemaKey("DoesNotExist");
71
+ chai_1.assert.isUndefined(await locater.getSchema(schemaKey, ecschema_metadata_1.SchemaMatchType.Exact, context));
72
+ });
73
+ it("addSchemaString, bad schema tag, throws", async () => {
74
+ const schemaString = loadSchemaString("BadSchemaTag");
75
+ try {
76
+ locater.addSchemaString(schemaString);
77
+ }
78
+ catch (e) {
79
+ const error = e;
80
+ chai_1.assert.strictEqual(error.errorNumber, ecschema_metadata_1.ECObjectsStatus.InvalidSchemaXML);
81
+ return;
82
+ }
83
+ chai_1.assert.fail(0, 1, "Expected ECObjects exception");
84
+ });
85
+ it("addSchemaString, bad schema name tag, throws", async () => {
86
+ const schemaString = loadSchemaString("BadSchemaNameTag");
87
+ try {
88
+ locater.addSchemaString(schemaString);
89
+ }
90
+ catch (e) {
91
+ const error = e;
92
+ chai_1.assert.strictEqual(error.errorNumber, ecschema_metadata_1.ECObjectsStatus.InvalidSchemaXML);
93
+ return;
94
+ }
95
+ chai_1.assert.fail(0, 1, "Expected ECObjects exception");
96
+ });
97
+ it("addSchemaString, bad schema version tag, throws", async () => {
98
+ const schemaString = loadSchemaString("BadSchemaVersionTag");
99
+ try {
100
+ locater.addSchemaString(schemaString);
101
+ }
102
+ catch (e) {
103
+ const error = e;
104
+ chai_1.assert.strictEqual(error.errorNumber, ecschema_metadata_1.ECObjectsStatus.InvalidSchemaXML);
105
+ return;
106
+ }
107
+ chai_1.assert.fail(0, 1, "Expected ECObjects exception");
108
+ });
109
+ it("getSchema, full version, succeeds", async () => {
110
+ locater.addSchemaStrings(schemaStrings);
111
+ const stub = await locater.getSchema(new ecschema_metadata_1.SchemaKey("SchemaA", 1, 1, 1), ecschema_metadata_1.SchemaMatchType.Exact, context);
112
+ chai_1.assert.isDefined(stub);
113
+ const key = stub.schemaKey;
114
+ chai_1.assert.strictEqual(key.name, "SchemaA");
115
+ chai_1.assert.strictEqual(key.version.toString(), "01.01.01");
116
+ });
117
+ it("getSchema, reference does not exist, throws.", async () => {
118
+ const schemaString = loadSchemaString("RefDoesNotExist");
119
+ locater.addSchemaString(schemaString);
120
+ try {
121
+ await locater.getSchema(new ecschema_metadata_1.SchemaKey("RefDoesNotExist", 1, 1, 1), ecschema_metadata_1.SchemaMatchType.Exact, context);
122
+ }
123
+ catch (e) {
124
+ const error = e;
125
+ chai_1.assert.strictEqual(error.errorNumber, ecschema_metadata_1.ECObjectsStatus.UnableToLocateSchema);
126
+ return;
127
+ }
128
+ chai_1.assert.fail();
129
+ });
130
+ it("getSchema, references set", async () => {
131
+ locater.addSchemaStrings(schemaStrings);
132
+ const schemaA = await context.getSchema(new ecschema_metadata_1.SchemaKey("SchemaA", 1, 1, 1), ecschema_metadata_1.SchemaMatchType.Exact);
133
+ const schemaB = await context.getSchema(new ecschema_metadata_1.SchemaKey("SchemaB", 2, 2, 2), ecschema_metadata_1.SchemaMatchType.Exact);
134
+ const schemaC = await context.getSchema(new ecschema_metadata_1.SchemaKey("SchemaC", 3, 3, 3), ecschema_metadata_1.SchemaMatchType.Exact);
135
+ const schemaD = await context.getSchema(new ecschema_metadata_1.SchemaKey("SchemaD", 4, 4, 4), ecschema_metadata_1.SchemaMatchType.Exact);
136
+ chai_1.assert.isDefined(schemaA);
137
+ chai_1.assert.strictEqual(schemaA.references.length, 2);
138
+ chai_1.assert.deepEqual(schemaA.references[0], schemaC);
139
+ chai_1.assert.deepEqual(schemaA.references[1], schemaB);
140
+ chai_1.assert.deepEqual(schemaA.references[0].references[0], schemaD);
141
+ chai_1.assert.deepEqual(schemaA.references[1].references[0], schemaC);
142
+ chai_1.assert.deepEqual(schemaA.references[1].references[1], schemaD);
143
+ });
144
+ it("getSchema, exact version, wrong minor, fails", async () => {
145
+ locater.addSchemaStrings(schemaStrings);
146
+ chai_1.assert.isUndefined(await context.getSchema(new ecschema_metadata_1.SchemaKey("SchemaA", 1, 1, 2), ecschema_metadata_1.SchemaMatchType.Exact));
147
+ });
148
+ it("getSchema, latest, succeeds", async () => {
149
+ locater.addSchemaStrings(schemaStrings);
150
+ const stub = await locater.getSchema(new ecschema_metadata_1.SchemaKey("SchemaA", 1, 1, 0), ecschema_metadata_1.SchemaMatchType.Latest, context);
151
+ chai_1.assert.isDefined(stub);
152
+ chai_1.assert.strictEqual(stub.schemaKey.name, "SchemaA");
153
+ chai_1.assert.strictEqual(stub.schemaKey.version.toString(), "02.00.02");
154
+ });
155
+ it("getSchema, latest write compatible, succeeds", async () => {
156
+ locater.addSchemaStrings(schemaStrings);
157
+ const stub = await context.getSchema(new ecschema_metadata_1.SchemaKey("SchemaA", 1, 1, 0), ecschema_metadata_1.SchemaMatchType.LatestWriteCompatible);
158
+ chai_1.assert.isDefined(stub);
159
+ chai_1.assert.strictEqual(stub.schemaKey.name, "SchemaA");
160
+ chai_1.assert.strictEqual(stub.schemaKey.version.toString(), "01.01.01");
161
+ });
162
+ it("getSchema, latest write compatible, write version wrong, fails", async () => {
163
+ locater.addSchemaStrings(schemaStrings);
164
+ chai_1.assert.isUndefined(await context.getSchema(new ecschema_metadata_1.SchemaKey("SchemaA", 1, 2, 0), ecschema_metadata_1.SchemaMatchType.LatestWriteCompatible));
165
+ });
166
+ it("getSchema, latest read compatible, succeeds", async () => {
167
+ locater.addSchemaStrings(schemaStrings);
168
+ const stub = await context.getSchema(new ecschema_metadata_1.SchemaKey("SchemaA", 1, 0, 0), ecschema_metadata_1.SchemaMatchType.LatestReadCompatible);
169
+ chai_1.assert.isDefined(stub);
170
+ chai_1.assert.strictEqual(stub.schemaKey.name, "SchemaA");
171
+ chai_1.assert.strictEqual(stub.schemaKey.version.toString(), "01.01.01");
172
+ });
173
+ it("getSchema, latest read compatible, read version wrong, fails", async () => {
174
+ locater.addSchemaStrings(schemaStrings);
175
+ chai_1.assert.isUndefined(await context.getSchema(new ecschema_metadata_1.SchemaKey("SchemaA", 2, 1, 1), ecschema_metadata_1.SchemaMatchType.LatestReadCompatible));
176
+ });
177
+ it("sync - should ignore commented out schema references", () => {
178
+ locater.addSchemaStrings(schemaStrings);
179
+ const schemaString = loadSchemaString("RefCommentedOut");
180
+ locater.addSchemaString(schemaString);
181
+ const stub = context.getSchemaSync(new ecschema_metadata_1.SchemaKey("RefCommentedOut", 1, 1, 1), ecschema_metadata_1.SchemaMatchType.LatestReadCompatible);
182
+ chai_1.assert.isDefined(stub);
183
+ chai_1.assert.strictEqual(stub.schemaKey.name, "RefCommentedOut");
184
+ chai_1.assert.strictEqual(stub.schemaKey.version.toString(), "01.01.01");
185
+ });
186
+ it("async - should ignore commented out schema references", async () => {
187
+ locater.addSchemaStrings(schemaStrings);
188
+ const schemaString = loadSchemaString("RefCommentedOut");
189
+ locater.addSchemaString(schemaString);
190
+ const stub = await context.getSchema(new ecschema_metadata_1.SchemaKey("RefCommentedOut", 1, 1, 1), ecschema_metadata_1.SchemaMatchType.LatestReadCompatible);
191
+ chai_1.assert.isDefined(stub);
192
+ chai_1.assert.strictEqual(stub.schemaKey.name, "RefCommentedOut");
193
+ chai_1.assert.strictEqual(stub.schemaKey.version.toString(), "01.01.01");
194
+ });
195
+ it("getSchemaKey, valid version and name, succeeds", () => {
196
+ const schemaXml = `<ECSchema schemaName="SchemaA" version="1.1.1"> </ECSchema>`;
197
+ const key = locater.getSchemaKey(schemaXml);
198
+ chai_1.assert.deepEqual(key, new ecschema_metadata_1.SchemaKey("SchemaA", new ecschema_metadata_1.ECVersion(1, 1, 1)));
199
+ });
200
+ it("getSchemaKey, invalid xml, throws", () => {
201
+ const schemaXml = `<ECSchemaBad schemaName="SchemaA" version="1.1.1"> </ECSchemaBad>`;
202
+ (0, chai_1.expect)(() => locater.getSchemaKey(schemaXml)).to.throw(ecschema_metadata_1.ECObjectsError, `Could not find '<ECSchema>' tag in the given string`);
203
+ });
204
+ it("getSchemaKey, invalid schemaName attribute, throws", () => {
205
+ const schemaXml = `<ECSchema schemaNameBad="SchemaA" version="1.1.1"> </ECSchema>`;
206
+ (0, chai_1.expect)(() => locater.getSchemaKey(schemaXml)).to.throw(ecschema_metadata_1.ECObjectsError, `Could not find the ECSchema 'schemaName' or 'version' tag in the given string`);
207
+ });
208
+ it("getSchemaKey, invalid schemaName, throws", () => {
209
+ const schemaXml = `<ECSchema version="1.1.1" schemaName=""> </ECSchema>`;
210
+ (0, chai_1.expect)(() => locater.getSchemaKey(schemaXml)).to.throw(ecschema_metadata_1.ECObjectsError, `Could not find the ECSchema 'schemaName' or 'version' tag in the given string`);
211
+ });
212
+ it("getSchemaKey, invalid version attribute, throws", () => {
213
+ const schemaXml = `<ECSchema schemaName="SchemaA" versionBad="1.1.1"> </ECSchema>`;
214
+ (0, chai_1.expect)(() => locater.getSchemaKey(schemaXml)).to.throw(ecschema_metadata_1.ECObjectsError, `Could not find the ECSchema 'schemaName' or 'version' tag in the given string`);
215
+ });
216
+ it("getSchemaKey, invalid version, throws", () => {
217
+ const schemaXml = `<ECSchema schemaName="SchemaA" version=""> </ECSchema>`;
218
+ (0, chai_1.expect)(() => locater.getSchemaKey(schemaXml)).to.throw(ecschema_metadata_1.ECObjectsError, `Could not find the ECSchema 'schemaName' or 'version' tag in the given string`);
219
+ });
220
+ });
221
+ //# sourceMappingURL=SchemaXmlStringLocater.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SchemaXmlStringLocater.test.js","sourceRoot":"","sources":["../../../src/test/SchemaXmlStringLocater.test.ts"],"names":[],"mappings":";AAAA;;;+FAG+F;;AAE/F,+BAAsC;AACtC,6BAA6B;AAC7B,yBAAyB;AACzB,6BA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AAM,EAAC,GAAG,EAAE,CAAC,OAAO,CAAC,YAAY,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,kCAAc,EAAE,+EAA+E,CAAC,CAAC;IAC1J,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,uCAAuC,EAAE,GAAG,EAAE;QAC/C,MAAM,SAAS,GAAG,wDAAwD,CAAC;QAC3E,IAAA,aAAM,EAAC,GAAG,EAAE,CAAC,OAAO,CAAC,YAAY,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,kCAAc,EAAE,+EAA+E,CAAC,CAAC;IAC1J,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["/*---------------------------------------------------------------------------------------------\r\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\r\n* See LICENSE.md in the project root for license terms and full copyright notice.\r\n*--------------------------------------------------------------------------------------------*/\r\n\r\nimport { assert, expect } from \"chai\";\r\nimport * as path from \"path\";\r\nimport * as fs from \"fs\";\r\nimport * as glob from \"glob\";\r\nimport { ECObjectsError, ECObjectsStatus, ECVersion, SchemaContext, SchemaKey, SchemaMatchType } from \"@itwin/ecschema-metadata\";\r\nimport { SchemaXmlStringLocater } from \"../SchemaXmlStringLocater\";\r\nimport { StringSchemaKey } from \"../SchemaStringLocater\";\r\n\r\ndescribe(\"SchemaXmlStringLocater tests:\", () => {\r\n let locater: SchemaXmlStringLocater;\r\n let context: SchemaContext;\r\n let schemaStrings: string [];\r\n\r\n const loadSchemaString = (schemaName: string) => {\r\n const searchPath = path.join(__dirname, \"assets\", `${schemaName}.ecschema.xml`);\r\n const file = fs.readFileSync(searchPath);\r\n return file.toString();\r\n };\r\n\r\n const loadSchemaStrings = () => {\r\n const schemas: string [] = [];\r\n const searchPath = path.join(__dirname, \"assets\", \"Schema*.ecschema.xml\");\r\n const result = new glob.GlobSync(searchPath, { sync: true });\r\n for (const match of result.found) {\r\n const file = fs.readFileSync(match);\r\n if (!file)\r\n continue;\r\n schemas.push(file.toString());\r\n }\r\n return schemas;\r\n };\r\n\r\n beforeEach(() => {\r\n schemaStrings = loadSchemaStrings();\r\n locater = new SchemaXmlStringLocater();\r\n context = new SchemaContext();\r\n context.addLocater(locater);\r\n });\r\n\r\n it(\"locate valid schema with multiple references\", async () => {\r\n locater.addSchemaStrings(schemaStrings);\r\n const schemaKey = new SchemaKey(\"SchemaA\", 1, 1, 1);\r\n const schema = await context.getSchema(schemaKey, SchemaMatchType.Exact);\r\n\r\n assert.isDefined(schema);\r\n assert.strictEqual(schema!.schemaKey.name, \"SchemaA\");\r\n assert.strictEqual(schema!.schemaKey.version.toString(), \"01.01.01\");\r\n });\r\n\r\n it(\"locate valid schema with multiple references synchronously\", () => {\r\n locater.addSchemaStrings(schemaStrings);\r\n const schemaKey = new SchemaKey(\"SchemaA\", 1, 1, 1);\r\n const schema = context.getSchemaSync(schemaKey, SchemaMatchType.Exact);\r\n\r\n assert.isDefined(schema);\r\n assert.strictEqual(schema!.schemaKey.name, \"SchemaA\");\r\n assert.strictEqual(schema!.schemaKey.version.toString(), \"01.01.01\");\r\n });\r\n\r\n it(\"getSchema called multiple times for same schema\", async () => {\r\n locater.addSchemaStrings(schemaStrings);\r\n const schemaKey = new SchemaKey(\"SchemaD\", 4, 4, 4);\r\n\r\n const locater1 = await locater.getSchema(schemaKey, SchemaMatchType.Exact, new SchemaContext());\r\n const locater2 = await locater.getSchema(schemaKey, SchemaMatchType.Exact, new SchemaContext());\r\n const context1 = await context.getSchema(schemaKey, SchemaMatchType.Exact);\r\n const context2 = await context.getSchema(schemaKey, SchemaMatchType.Exact);\r\n\r\n // locater should not cache, but context should cache\r\n assert.notEqual(locater1, locater2);\r\n assert.notEqual(locater1, context1);\r\n assert.strictEqual(context1, context2);\r\n });\r\n\r\n it(\"getSchema which does not exist, returns undefined\", async () => {\r\n locater.addSchemaStrings(schemaStrings);\r\n const schemaKey = new SchemaKey(\"DoesNotExist\");\r\n\r\n assert.isUndefined(await locater.getSchema(schemaKey, SchemaMatchType.Exact, context));\r\n });\r\n\r\n it(\"addSchemaString, bad schema tag, throws\", async () => {\r\n const schemaString = loadSchemaString(\"BadSchemaTag\");\r\n try {\r\n locater.addSchemaString(schemaString);\r\n } catch (e) {\r\n const error = e as ECObjectsError;\r\n assert.strictEqual(error.errorNumber, ECObjectsStatus.InvalidSchemaXML);\r\n return;\r\n }\r\n\r\n assert.fail(0, 1, \"Expected ECObjects exception\");\r\n });\r\n\r\n it(\"addSchemaString, bad schema name tag, throws\", async () => {\r\n const schemaString = loadSchemaString(\"BadSchemaNameTag\");\r\n try {\r\n locater.addSchemaString(schemaString);\r\n } catch (e) {\r\n const error = e as ECObjectsError;\r\n assert.strictEqual(error.errorNumber, ECObjectsStatus.InvalidSchemaXML);\r\n return;\r\n }\r\n\r\n assert.fail(0, 1, \"Expected ECObjects exception\");\r\n });\r\n\r\n it(\"addSchemaString, bad schema version tag, throws\", async () => {\r\n const schemaString = loadSchemaString(\"BadSchemaVersionTag\");\r\n try {\r\n locater.addSchemaString(schemaString);\r\n } catch (e) {\r\n const error = e as ECObjectsError;\r\n assert.strictEqual(error.errorNumber, ECObjectsStatus.InvalidSchemaXML);\r\n return;\r\n }\r\n\r\n assert.fail(0, 1, \"Expected ECObjects exception\");\r\n });\r\n\r\n it(\"getSchema, full version, succeeds\", async () => {\r\n locater.addSchemaStrings(schemaStrings);\r\n const stub = await locater.getSchema(new SchemaKey(\"SchemaA\", 1, 1, 1), SchemaMatchType.Exact, context);\r\n assert.isDefined(stub);\r\n const key = stub!.schemaKey as StringSchemaKey;\r\n assert.strictEqual(key.name, \"SchemaA\");\r\n assert.strictEqual(key.version.toString(), \"01.01.01\");\r\n });\r\n\r\n it(\"getSchema, reference does not exist, throws.\", async () => {\r\n const schemaString = loadSchemaString(\"RefDoesNotExist\");\r\n locater.addSchemaString(schemaString);\r\n try {\r\n await locater.getSchema(new SchemaKey(\"RefDoesNotExist\", 1, 1, 1), SchemaMatchType.Exact, context);\r\n } catch (e) {\r\n const error = e as ECObjectsError;\r\n assert.strictEqual(error.errorNumber, ECObjectsStatus.UnableToLocateSchema);\r\n return;\r\n }\r\n\r\n assert.fail();\r\n });\r\n\r\n it(\"getSchema, references set\", async () => {\r\n locater.addSchemaStrings(schemaStrings);\r\n\r\n const schemaA = await context.getSchema(new SchemaKey(\"SchemaA\", 1, 1, 1), SchemaMatchType.Exact);\r\n const schemaB = await context.getSchema(new SchemaKey(\"SchemaB\", 2, 2, 2), SchemaMatchType.Exact);\r\n const schemaC = await context.getSchema(new SchemaKey(\"SchemaC\", 3, 3, 3), SchemaMatchType.Exact);\r\n const schemaD = await context.getSchema(new SchemaKey(\"SchemaD\", 4, 4, 4), SchemaMatchType.Exact);\r\n\r\n assert.isDefined(schemaA);\r\n assert.strictEqual(schemaA!.references.length, 2);\r\n assert.deepEqual(schemaA!.references[0], schemaC);\r\n assert.deepEqual(schemaA!.references[1], schemaB);\r\n assert.deepEqual(schemaA!.references[0].references[0], schemaD);\r\n assert.deepEqual(schemaA!.references[1].references[0], schemaC);\r\n assert.deepEqual(schemaA!.references[1].references[1], schemaD);\r\n });\r\n\r\n it(\"getSchema, exact version, wrong minor, fails\", async () => {\r\n locater.addSchemaStrings(schemaStrings);\r\n assert.isUndefined(await context.getSchema(new SchemaKey(\"SchemaA\", 1, 1, 2), SchemaMatchType.Exact));\r\n });\r\n\r\n it(\"getSchema, latest, succeeds\", async () => {\r\n locater.addSchemaStrings(schemaStrings);\r\n const stub = await locater.getSchema(new SchemaKey(\"SchemaA\", 1, 1, 0), SchemaMatchType.Latest, context);\r\n\r\n assert.isDefined(stub);\r\n assert.strictEqual(stub!.schemaKey.name, \"SchemaA\");\r\n assert.strictEqual(stub!.schemaKey.version.toString(), \"02.00.02\");\r\n });\r\n\r\n it(\"getSchema, latest write compatible, succeeds\", async () => {\r\n locater.addSchemaStrings(schemaStrings);\r\n const stub = await context.getSchema(new SchemaKey(\"SchemaA\", 1, 1, 0), SchemaMatchType.LatestWriteCompatible);\r\n\r\n assert.isDefined(stub);\r\n assert.strictEqual(stub!.schemaKey.name, \"SchemaA\");\r\n assert.strictEqual(stub!.schemaKey.version.toString(), \"01.01.01\");\r\n });\r\n\r\n it(\"getSchema, latest write compatible, write version wrong, fails\", async () => {\r\n locater.addSchemaStrings(schemaStrings);\r\n assert.isUndefined(await context.getSchema(new SchemaKey(\"SchemaA\", 1, 2, 0), SchemaMatchType.LatestWriteCompatible));\r\n });\r\n\r\n it(\"getSchema, latest read compatible, succeeds\", async () => {\r\n locater.addSchemaStrings(schemaStrings);\r\n const stub = await context.getSchema(new SchemaKey(\"SchemaA\", 1, 0, 0), SchemaMatchType.LatestReadCompatible);\r\n\r\n assert.isDefined(stub);\r\n assert.strictEqual(stub!.schemaKey.name, \"SchemaA\");\r\n assert.strictEqual(stub!.schemaKey.version.toString(), \"01.01.01\");\r\n });\r\n\r\n it(\"getSchema, latest read compatible, read version wrong, fails\", async () => {\r\n locater.addSchemaStrings(schemaStrings);\r\n assert.isUndefined(await context.getSchema(new SchemaKey(\"SchemaA\", 2, 1, 1), SchemaMatchType.LatestReadCompatible));\r\n });\r\n\r\n it(\"sync - should ignore commented out schema references\", () => {\r\n locater.addSchemaStrings(schemaStrings);\r\n const schemaString = loadSchemaString(\"RefCommentedOut\");\r\n locater.addSchemaString(schemaString);\r\n\r\n const stub = context.getSchemaSync(new SchemaKey(\"RefCommentedOut\", 1, 1, 1), SchemaMatchType.LatestReadCompatible);\r\n\r\n assert.isDefined(stub);\r\n assert.strictEqual(stub!.schemaKey.name, \"RefCommentedOut\");\r\n assert.strictEqual(stub!.schemaKey.version.toString(), \"01.01.01\");\r\n });\r\n\r\n it(\"async - should ignore commented out schema references\", async () => {\r\n locater.addSchemaStrings(schemaStrings);\r\n const schemaString = loadSchemaString(\"RefCommentedOut\");\r\n locater.addSchemaString(schemaString);\r\n\r\n const stub = await context.getSchema(new SchemaKey(\"RefCommentedOut\", 1, 1, 1), SchemaMatchType.LatestReadCompatible);\r\n\r\n assert.isDefined(stub);\r\n assert.strictEqual(stub!.schemaKey.name, \"RefCommentedOut\");\r\n assert.strictEqual(stub!.schemaKey.version.toString(), \"01.01.01\");\r\n });\r\n\r\n it(\"getSchemaKey, valid version and name, succeeds\", () => {\r\n const schemaXml = `<ECSchema schemaName=\"SchemaA\" version=\"1.1.1\"> </ECSchema>`;\r\n const key = locater.getSchemaKey(schemaXml);\r\n assert.deepEqual(key, new SchemaKey(\"SchemaA\", new ECVersion(1, 1, 1)));\r\n });\r\n\r\n it(\"getSchemaKey, invalid xml, throws\", () => {\r\n const schemaXml = `<ECSchemaBad schemaName=\"SchemaA\" version=\"1.1.1\"> </ECSchemaBad>`;\r\n expect(() => locater.getSchemaKey(schemaXml)).to.throw(ECObjectsError, `Could not find '<ECSchema>' tag in the given string`);\r\n });\r\n\r\n it(\"getSchemaKey, invalid schemaName attribute, throws\", () => {\r\n const schemaXml = `<ECSchema schemaNameBad=\"SchemaA\" version=\"1.1.1\"> </ECSchema>`;\r\n expect(() => locater.getSchemaKey(schemaXml)).to.throw(ECObjectsError, `Could not find the ECSchema 'schemaName' or 'version' tag in the given string`);\r\n });\r\n\r\n it(\"getSchemaKey, invalid schemaName, throws\", () => {\r\n const schemaXml = `<ECSchema version=\"1.1.1\" schemaName=\"\"> </ECSchema>`;\r\n expect(() => locater.getSchemaKey(schemaXml)).to.throw(ECObjectsError, `Could not find the ECSchema 'schemaName' or 'version' tag in the given string`);\r\n });\r\n\r\n it(\"getSchemaKey, invalid version attribute, throws\", () => {\r\n const schemaXml = `<ECSchema schemaName=\"SchemaA\" versionBad=\"1.1.1\"> </ECSchema>`;\r\n expect(() => locater.getSchemaKey(schemaXml)).to.throw(ECObjectsError, `Could not find the ECSchema 'schemaName' or 'version' tag in the given string`);\r\n });\r\n\r\n it(\"getSchemaKey, invalid version, throws\", () => {\r\n const schemaXml = `<ECSchema schemaName=\"SchemaA\" version=\"\"> </ECSchema>`;\r\n expect(() => locater.getSchemaKey(schemaXml)).to.throw(ECObjectsError, `Could not find the ECSchema 'schemaName' or 'version' tag in the given string`);\r\n });\r\n});\r\n\r\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@itwin/ecschema-locaters",
3
- "version": "4.1.0-dev.75",
3
+ "version": "4.1.0-dev.76",
4
4
  "description": "EC Schema file locaters",
5
5
  "license": "MIT",
6
6
  "main": "lib/cjs/ecschema-locaters.js",
@@ -54,8 +54,8 @@
54
54
  "rimraf": "^3.0.2",
55
55
  "sinon": "^15.0.4",
56
56
  "typescript": "~5.0.2",
57
- "@itwin/build-tools": "4.1.0-dev.75",
58
- "@itwin/ecschema-metadata": "4.1.0-dev.75"
57
+ "@itwin/build-tools": "4.1.0-dev.76",
58
+ "@itwin/ecschema-metadata": "4.1.0-dev.76"
59
59
  },
60
60
  "dependencies": {
61
61
  "glob": "^7.1.2",
@@ -63,7 +63,7 @@
63
63
  "@xmldom/xmldom": "~0.8.5"
64
64
  },
65
65
  "peerDependencies": {
66
- "@itwin/ecschema-metadata": "^4.1.0-dev.75"
66
+ "@itwin/ecschema-metadata": "^4.1.0-dev.76"
67
67
  },
68
68
  "nyc": {
69
69
  "extends": "./node_modules/@itwin/build-tools/.nycrc",