@itwin/ecschema-locaters 5.0.0-dev.90 → 5.0.0-dev.92

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 (61) hide show
  1. package/lib/esm/SchemaFileLocater.d.ts +94 -0
  2. package/lib/esm/SchemaFileLocater.d.ts.map +1 -0
  3. package/lib/esm/SchemaFileLocater.js +213 -0
  4. package/lib/esm/SchemaFileLocater.js.map +1 -0
  5. package/lib/esm/SchemaJsonFileLocater.d.ts +42 -0
  6. package/lib/esm/SchemaJsonFileLocater.d.ts.map +1 -0
  7. package/lib/esm/SchemaJsonFileLocater.js +96 -0
  8. package/lib/esm/SchemaJsonFileLocater.js.map +1 -0
  9. package/lib/esm/SchemaStringLocater.d.ts +54 -0
  10. package/lib/esm/SchemaStringLocater.d.ts.map +1 -0
  11. package/lib/esm/SchemaStringLocater.js +83 -0
  12. package/lib/esm/SchemaStringLocater.js.map +1 -0
  13. package/lib/esm/SchemaXml.d.ts +22 -0
  14. package/lib/esm/SchemaXml.d.ts.map +1 -0
  15. package/lib/esm/SchemaXml.js +55 -0
  16. package/lib/esm/SchemaXml.js.map +1 -0
  17. package/lib/esm/SchemaXmlFileLocater.d.ts +70 -0
  18. package/lib/esm/SchemaXmlFileLocater.d.ts.map +1 -0
  19. package/lib/esm/SchemaXmlFileLocater.js +140 -0
  20. package/lib/esm/SchemaXmlFileLocater.js.map +1 -0
  21. package/lib/esm/SchemaXmlStringLocater.d.ts +48 -0
  22. package/lib/esm/SchemaXmlStringLocater.d.ts.map +1 -0
  23. package/lib/esm/SchemaXmlStringLocater.js +100 -0
  24. package/lib/esm/SchemaXmlStringLocater.js.map +1 -0
  25. package/lib/esm/StubSchemaXmlFileLocater.d.ts +81 -0
  26. package/lib/esm/StubSchemaXmlFileLocater.d.ts.map +1 -0
  27. package/lib/esm/StubSchemaXmlFileLocater.js +184 -0
  28. package/lib/esm/StubSchemaXmlFileLocater.js.map +1 -0
  29. package/lib/esm/ecschema-locaters.d.ts +20 -0
  30. package/lib/esm/ecschema-locaters.d.ts.map +1 -0
  31. package/lib/esm/ecschema-locaters.js +24 -0
  32. package/lib/esm/ecschema-locaters.js.map +1 -0
  33. package/lib/esm/test/ConcurrentGetSchemaJson.test.d.ts +2 -0
  34. package/lib/esm/test/ConcurrentGetSchemaJson.test.d.ts.map +1 -0
  35. package/lib/esm/test/ConcurrentGetSchemaJson.test.js +249 -0
  36. package/lib/esm/test/ConcurrentGetSchemaJson.test.js.map +1 -0
  37. package/lib/esm/test/ConcurrentGetSchemaXml.test.d.ts +2 -0
  38. package/lib/esm/test/ConcurrentGetSchemaXml.test.d.ts.map +1 -0
  39. package/lib/esm/test/ConcurrentGetSchemaXml.test.js +186 -0
  40. package/lib/esm/test/ConcurrentGetSchemaXml.test.js.map +1 -0
  41. package/lib/esm/test/SchemaJsonFileLocator.test.d.ts +2 -0
  42. package/lib/esm/test/SchemaJsonFileLocator.test.d.ts.map +1 -0
  43. package/lib/esm/test/SchemaJsonFileLocator.test.js +118 -0
  44. package/lib/esm/test/SchemaJsonFileLocator.test.js.map +1 -0
  45. package/lib/esm/test/SchemaXml.test.d.ts +2 -0
  46. package/lib/esm/test/SchemaXml.test.d.ts.map +1 -0
  47. package/lib/esm/test/SchemaXml.test.js +55 -0
  48. package/lib/esm/test/SchemaXml.test.js.map +1 -0
  49. package/lib/esm/test/SchemaXmlFileLocator.test.d.ts +2 -0
  50. package/lib/esm/test/SchemaXmlFileLocator.test.d.ts.map +1 -0
  51. package/lib/esm/test/SchemaXmlFileLocator.test.js +182 -0
  52. package/lib/esm/test/SchemaXmlFileLocator.test.js.map +1 -0
  53. package/lib/esm/test/SchemaXmlStringLocater.test.d.ts +2 -0
  54. package/lib/esm/test/SchemaXmlStringLocater.test.d.ts.map +1 -0
  55. package/lib/esm/test/SchemaXmlStringLocater.test.js +219 -0
  56. package/lib/esm/test/SchemaXmlStringLocater.test.js.map +1 -0
  57. package/lib/esm/test/StubSchemaXmlFileLocater.test.d.ts +2 -0
  58. package/lib/esm/test/StubSchemaXmlFileLocater.test.d.ts.map +1 -0
  59. package/lib/esm/test/StubSchemaXmlFileLocater.test.js +169 -0
  60. package/lib/esm/test/StubSchemaXmlFileLocater.test.js.map +1 -0
  61. package/package.json +7 -5
@@ -0,0 +1,83 @@
1
+ /*---------------------------------------------------------------------------------------------
2
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
3
+ * See LICENSE.md in the project root for license terms and full copyright notice.
4
+ *--------------------------------------------------------------------------------------------*/
5
+ /** @packageDocumentation
6
+ * @module Locaters
7
+ */
8
+ import { SchemaKey, SchemaMatchType } from "@itwin/ecschema-metadata";
9
+ /**
10
+ * A SchemaKey implementation that aids in identifying Schema strings via the
11
+ * addition of the schemaText property. The schemaText is the full XML string
12
+ * representation of the Schema.
13
+ * @beta
14
+ */
15
+ export class StringSchemaKey extends SchemaKey {
16
+ // The text for the schema loaded
17
+ schemaText;
18
+ /**
19
+ * Initializes a new StringSchemaKey object.
20
+ * @param key The EC SchemaKey identifying the Schema.
21
+ * @param schemaText The string representation of the Schema
22
+ */
23
+ constructor(key, schemaText) {
24
+ super(key.name, key.version);
25
+ this.schemaText = schemaText;
26
+ }
27
+ }
28
+ /**
29
+ * Abstract class to hold common/overlapping functionality between SchemaJsonStringLocater and SchemaXmlStringLocater
30
+ * @beta
31
+ */
32
+ export class SchemaStringLocater {
33
+ schemaStrings;
34
+ constructor() {
35
+ this.schemaStrings = [];
36
+ }
37
+ /**
38
+ * Adds schema strings used by this locator to find the
39
+ * Schemas.
40
+ * @param schemaPaths An array of Schema strings to add
41
+ */
42
+ addSchemaStrings(schemaStrings) {
43
+ // If the path is not in the schemaPaths array, add it
44
+ for (const schemaString of schemaStrings)
45
+ this.addSchemaString(schemaString);
46
+ }
47
+ /**
48
+ * Adds a schema string used by this locator to locate and load Schemas.
49
+ * @param schemaText The text of the Schema
50
+ */
51
+ addSchemaString(schemaString) {
52
+ const schemaKey = this.getSchemaKey(schemaString);
53
+ // If the string is not in the schemaStrings array, add it
54
+ if (!this.schemaStrings.find((entry) => this.getSchemaKey(entry).matches(schemaKey, SchemaMatchType.Exact)))
55
+ this.schemaStrings.push(schemaString);
56
+ }
57
+ /**
58
+ * Attempts to find all Schema strings that match the desired SchemaKey.
59
+ * @param desiredKey The SchemaKey to match.
60
+ * @param matchType The SchemaMatchType.
61
+ */
62
+ findEligibleSchemaKeys(desiredKey, matchType) {
63
+ const foundStrings = new Array();
64
+ for (const schemaString of this.schemaStrings) {
65
+ // Get the schema key
66
+ const key = this.getSchemaKey(schemaString);
67
+ // If the key matches, put it in foundFiles
68
+ if (key.matches(desiredKey, matchType))
69
+ foundStrings.push(new StringSchemaKey(key, schemaString));
70
+ }
71
+ return foundStrings;
72
+ }
73
+ /**
74
+ * Compares two Schema versions. If the left-hand version is greater, 1 is returned. If the
75
+ * left-hand version is less, -1 us returned. If the versions are an exact match, 0 is returned.
76
+ * @param lhs The 'left-hand' StringSchemaKey.
77
+ * @param rhs The 'right-hand' StringSchemaKey.
78
+ */
79
+ compareSchemaKeyByVersion = (lhs, rhs) => {
80
+ return lhs.compareByVersion(rhs);
81
+ };
82
+ }
83
+ //# sourceMappingURL=SchemaStringLocater.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SchemaStringLocater.js","sourceRoot":"","sources":["../../src/SchemaStringLocater.ts"],"names":[],"mappings":"AAAA;;;+FAG+F;AAC/F;;GAEG;AAEH,OAAO,EAAyB,SAAS,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAE7F;;;;;GAKG;AACH,MAAM,OAAO,eAAgB,SAAQ,SAAS;IAC5C,iCAAiC;IAC1B,UAAU,CAAS;IAE1B;;;;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;A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nC,CAAC,CAAC;CACH","sourcesContent":["/*---------------------------------------------------------------------------------------------\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\n* See LICENSE.md in the project root for license terms and full copyright notice.\n*--------------------------------------------------------------------------------------------*/\n/** @packageDocumentation\n * @module Locaters\n */\n\nimport { Schema, SchemaContext, SchemaKey, SchemaMatchType } from \"@itwin/ecschema-metadata\";\n\n/**\n * A SchemaKey implementation that aids in identifying Schema strings via the\n * addition of the schemaText property. The schemaText is the full XML string\n * representation of the Schema.\n * @beta\n */\nexport class StringSchemaKey extends SchemaKey {\n // The text for the schema loaded\n public schemaText: string;\n\n /**\n * Initializes a new StringSchemaKey object.\n * @param key The EC SchemaKey identifying the Schema.\n * @param schemaText The string representation of the Schema\n */\n constructor(key: SchemaKey, schemaText: string) {\n super(key.name, key.version);\n this.schemaText = schemaText;\n }\n}\n\n/**\n * Abstract class to hold common/overlapping functionality between SchemaJsonStringLocater and SchemaXmlStringLocater\n * @beta\n */\nexport abstract class SchemaStringLocater {\n public schemaStrings: string[];\n\n constructor() {\n this.schemaStrings = [];\n }\n\n /**\n * Adds schema strings used by this locator to find the\n * Schemas.\n * @param schemaPaths An array of Schema strings to add\n */\n public addSchemaStrings(schemaStrings: string[]) {\n // If the path is not in the schemaPaths array, add it\n for (const schemaString of schemaStrings)\n this.addSchemaString(schemaString);\n }\n\n /**\n * Adds a schema string used by this locator to locate and load Schemas.\n * @param schemaText The text of the Schema\n */\n public addSchemaString(schemaString: string) {\n const schemaKey = this.getSchemaKey(schemaString);\n // If the string is not in the schemaStrings array, add it\n if (!this.schemaStrings.find((entry) => this.getSchemaKey(entry).matches(schemaKey, SchemaMatchType.Exact)))\n this.schemaStrings.push(schemaString);\n }\n\n protected abstract getSchemaKey(data: string): SchemaKey;\n\n /**\n * Attempts to find all Schema strings that match the desired SchemaKey.\n * @param desiredKey The SchemaKey to match.\n * @param matchType The SchemaMatchType.\n */\n protected findEligibleSchemaKeys(desiredKey: SchemaKey, matchType: SchemaMatchType): StringSchemaKey[] {\n const foundStrings = new Array<StringSchemaKey>();\n\n for (const schemaString of this.schemaStrings) {\n // Get the schema key\n const key = this.getSchemaKey(schemaString);\n\n // If the key matches, put it in foundFiles\n if (key.matches(desiredKey, matchType))\n foundStrings.push(new StringSchemaKey(key, schemaString));\n }\n\n return foundStrings;\n }\n\n public abstract getSchema(key: SchemaKey, matchType: SchemaMatchType, context: SchemaContext): Promise<Schema | undefined>;\n\n /**\n * Compares two Schema versions. If the left-hand version is greater, 1 is returned. If the\n * left-hand version is less, -1 us returned. If the versions are an exact match, 0 is returned.\n * @param lhs The 'left-hand' StringSchemaKey.\n * @param rhs The 'right-hand' StringSchemaKey.\n */\n public compareSchemaKeyByVersion = (lhs: StringSchemaKey, rhs: StringSchemaKey): number => {\n return lhs.compareByVersion(rhs);\n };\n}\n"]}
@@ -0,0 +1,22 @@
1
+ /** @packageDocumentation
2
+ * @module Utils
3
+ */
4
+ import { Schema } from "@itwin/ecschema-metadata";
5
+ /**
6
+ * Utility class to assist in creating serialized EC Schemas on the file system.
7
+ * @beta
8
+ */
9
+ export declare namespace SchemaXml {
10
+ /**
11
+ * Writes a Schema to an xml file to the specified output path.
12
+ * @param schema The Schema to serialize.
13
+ * @param outputPath The directory in which to create the file.
14
+ */
15
+ function writeFile(schema: Schema, outputPath: string): Promise<void>;
16
+ /**
17
+ * Writes a Schema to an xml string.
18
+ * @param schema The Schema to serialize.
19
+ */
20
+ function writeString(schema: Schema): Promise<string>;
21
+ }
22
+ //# sourceMappingURL=SchemaXml.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SchemaXml.d.ts","sourceRoot":"","sources":["../../src/SchemaXml.ts"],"names":[],"mappings":"AAIA;;GAEG;AAKH,OAAO,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAC;AAElD;;;GAGG;AACH,yBAAiB,SAAS,CAAC;IAEzB;;;;OAIG;IACH,SAAsB,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,iBAUjE;IAED;;;OAGG;IACH,SAAsB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAMjE;CAYF"}
@@ -0,0 +1,55 @@
1
+ /*---------------------------------------------------------------------------------------------
2
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
3
+ * See LICENSE.md in the project root for license terms and full copyright notice.
4
+ *--------------------------------------------------------------------------------------------*/
5
+ /** @packageDocumentation
6
+ * @module Utils
7
+ */
8
+ import * as fs from "fs-extra";
9
+ import * as path from "path";
10
+ import { DOMParser, XMLSerializer } from "@xmldom/xmldom";
11
+ /**
12
+ * Utility class to assist in creating serialized EC Schemas on the file system.
13
+ * @beta
14
+ */
15
+ export var SchemaXml;
16
+ (function (SchemaXml) {
17
+ /**
18
+ * Writes a Schema to an xml file to the specified output path.
19
+ * @param schema The Schema to serialize.
20
+ * @param outputPath The directory in which to create the file.
21
+ */
22
+ async function writeFile(schema, outputPath) {
23
+ const xml = await writeString(schema);
24
+ const baseFile = getSchemaPath(schema, outputPath);
25
+ try {
26
+ await fs.writeFile(baseFile, xml);
27
+ }
28
+ catch (err) {
29
+ const msg = `An error occurred writing to file '${baseFile}': ${err.message}`;
30
+ throw new Error(msg);
31
+ }
32
+ }
33
+ SchemaXml.writeFile = writeFile;
34
+ /**
35
+ * Writes a Schema to an xml string.
36
+ * @param schema The Schema to serialize.
37
+ */
38
+ async function writeString(schema) {
39
+ let xmlDoc = new DOMParser().parseFromString(`<?xml version="1.0" encoding="UTF-8"?>`, "application/xml");
40
+ xmlDoc = await schema.toXml(xmlDoc);
41
+ const serializer = new XMLSerializer();
42
+ return serializer.serializeToString(xmlDoc);
43
+ }
44
+ SchemaXml.writeString = writeString;
45
+ function getSchemaPath(schema, outputPath) {
46
+ const realDir = path.normalize(outputPath);
47
+ const test = fs.pathExistsSync(realDir);
48
+ if (!test) {
49
+ const msg = `The output directory '${realDir}' does not exist.`;
50
+ throw new Error(msg);
51
+ }
52
+ return path.resolve(realDir, `${schema.name}.ecschema.xml`);
53
+ }
54
+ })(SchemaXml || (SchemaXml = {}));
55
+ //# sourceMappingURL=SchemaXml.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SchemaXml.js","sourceRoot":"","sources":["../../src/SchemaXml.ts"],"names":[],"mappings":"AAAA;;;+FAG+F;AAC/F;;GAEG;AAEH,OAAO,KAAK,EAAE,MAAM,UAAU,CAAC;AAC/B,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAC7B,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAG1D;;;GAGG;AACH,MAAM,KAAW,SAAS,CAyCzB;AAzCD,WAAiB,SAAS;IAExB;;;;OAIG;IACI,KAAK,UAAU,SAAS,CAAC,MAAc,EAAE,UAAkB;QAChE,MAAM,GAAG,GAAG,MAAM,WAAW,CAAC,MAAM,CAAC,CAAC;QAEtC,MAAM,QAAQ,GAAG,aAAa,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;QACnD,IAAI,CAAC;YACH,MAAM,EAAE,CAAC,SAAS,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAC;QACpC,CAAC;QAAC,OAAO,GAAQ,EAAE,CAAC;YAClB,MAAM,GAAG,GAAG,sCAAsC,QAAQ,MAAM,GAAG,CAAC,OAAO,EAAE,CAAC;YAC9E,MAAM,IAAI,KAAK,CAAC,GAAG,CAAC,CAAC;QACvB,CAAC;IACH,CAAC;IAVqB,mBAAS,YAU9B,CAAA;IAED;;;OAGG;IACI,KAAK,UAAU,WAAW,CAAC,MAAc;QAC9C,IAAI,MAAM,GAAG,IAAI,SAAS,EAAE,CAAC,eAAe,CAAC,wCAAwC,EAAE,iBAAiB,CAAC,CAAC;QAE1G,MAAM,GAAG,MAAM,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;QACpC,MAAM,UAAU,GAAG,IAAI,aAAa,EAAE,CAAC;QACvC,OAAO,UAAU,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC;IAC9C,CAAC;IANqB,qBAAW,cAMhC,CAAA;IAED,SAAS,aAAa,CAAC,MAAc,EAAE,UAAkB;QACvD,MAAM,OAAO,GAAG,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC;QAC3C,MAAM,IAAI,GAAG,EAAE,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;QACxC,IAAI,CAAC,IAAI,EAAE,CAAC;YACV,MAAM,GAAG,GAAG,yBAAyB,OAAO,mBAAmB,CAAC;YAChE,MAAM,IAAI,KAAK,CAAC,GAAG,CAAC,CAAC;QACvB,CAAC;QAED,OAAO,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,GAAG,MAAM,CAAC,IAAI,eAAe,CAAC,CAAC;IAC9D,CAAC;AACH,CAAC,EAzCgB,SAAS,KAAT,SAAS,QAyCzB","sourcesContent":["/*---------------------------------------------------------------------------------------------\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\n* See LICENSE.md in the project root for license terms and full copyright notice.\n*--------------------------------------------------------------------------------------------*/\n/** @packageDocumentation\n * @module Utils\n */\n\nimport * as fs from \"fs-extra\";\nimport * as path from \"path\";\nimport { DOMParser, XMLSerializer } from \"@xmldom/xmldom\";\nimport { Schema } from \"@itwin/ecschema-metadata\";\n\n/**\n * Utility class to assist in creating serialized EC Schemas on the file system.\n * @beta\n */\nexport namespace SchemaXml {\n\n /**\n * Writes a Schema to an xml file to the specified output path.\n * @param schema The Schema to serialize.\n * @param outputPath The directory in which to create the file.\n */\n export async function writeFile(schema: Schema, outputPath: string) {\n const xml = await writeString(schema);\n\n const baseFile = getSchemaPath(schema, outputPath);\n try {\n await fs.writeFile(baseFile, xml);\n } catch (err: any) {\n const msg = `An error occurred writing to file '${baseFile}': ${err.message}`;\n throw new Error(msg);\n }\n }\n\n /**\n * Writes a Schema to an xml string.\n * @param schema The Schema to serialize.\n */\n export async function writeString(schema: Schema): Promise<string> {\n let xmlDoc = new DOMParser().parseFromString(`<?xml version=\"1.0\" encoding=\"UTF-8\"?>`, \"application/xml\");\n\n xmlDoc = await schema.toXml(xmlDoc);\n const serializer = new XMLSerializer();\n return serializer.serializeToString(xmlDoc);\n }\n\n function getSchemaPath(schema: Schema, outputPath: string): string {\n const realDir = path.normalize(outputPath);\n const test = fs.pathExistsSync(realDir);\n if (!test) {\n const msg = `The output directory '${realDir}' does not exist.`;\n throw new Error(msg);\n }\n\n return path.resolve(realDir, `${schema.name}.ecschema.xml`);\n }\n}\n"]}
@@ -0,0 +1,70 @@
1
+ /** @packageDocumentation
2
+ * @module Locaters
3
+ */
4
+ import { ISchemaLocater, Schema, SchemaContext, SchemaInfo, SchemaKey, SchemaMatchType } from "@itwin/ecschema-metadata";
5
+ import { SchemaFileLocater } from "./SchemaFileLocater";
6
+ /**
7
+ * A SchemaLocater implementation for locating XML Schema files
8
+ * from the file system using configurable search paths.
9
+ * @beta This is a workaround the current lack of a full xml parser.
10
+ */
11
+ export declare class SchemaXmlFileLocater extends SchemaFileLocater implements ISchemaLocater {
12
+ /**
13
+ * Attempts to retrieve a Schema with the given SchemaKey by using the configured search paths
14
+ * to locate the XML Schema file from the file system.
15
+ * @param key The SchemaKey of the Schema to retrieve.
16
+ * @param matchType The SchemaMatchType.
17
+ * @param context The SchemaContext that will control the lifetime of the schema.
18
+ */
19
+ getSchema(key: SchemaKey, matchType: SchemaMatchType, context: SchemaContext): Promise<Schema | undefined>;
20
+ /**
21
+ * Gets the schema info which matches the provided SchemaKey. The schema info may be returned before the schema is fully loaded.
22
+ * The fully loaded schema can be gotten later from the context using the getCachedSchema method.
23
+ * @param schemaKey The SchemaKey describing the schema to get from the cache.
24
+ * @param matchType The match type to use when locating the schema
25
+ * @param context The SchemaContext that will control the lifetime of the schema and holds the schema's references, if they exist.
26
+ */
27
+ getSchemaInfo(schemaKey: SchemaKey, matchType: SchemaMatchType, context: SchemaContext): Promise<SchemaInfo | undefined>;
28
+ /**
29
+ * Attempts to retrieve a Schema with the given SchemaKey by using the configured search paths
30
+ * to locate the XML Schema file from the file system.
31
+ * @param key The SchemaKey of the Schema to retrieve.
32
+ * @param matchType The SchemaMatchType.
33
+ * @param context The SchemaContext that will control the lifetime of the schema.
34
+ */
35
+ getSchemaSync(key: SchemaKey, matchType: SchemaMatchType, context: SchemaContext): Schema | undefined;
36
+ /**
37
+ * Constructs a SchemaKey based on the information in the Schema XML.
38
+ * @param data The Schema XML as a string.
39
+ */
40
+ getSchemaKey(data: string): SchemaKey;
41
+ }
42
+ /**
43
+ * This locater is responsible for locating standard schema files that are released by the core-backend package.
44
+ * The locater needs an argument to the known backend assets directory where the core-backend package is installed.
45
+ * This can be accessed by the KnownLocations.nativeAssetsDir getter provided by core-backend.
46
+ *
47
+ * @note This locater is read-only and does not allow adding new schema search paths.
48
+ * @note This locater should be used as a fallback/last chance locater in the schema context as any user defined schema should have higher precedence over the standard schema.
49
+ * @beta This is a workaround due to the current lack of a full xml parser.
50
+ */
51
+ export declare class PublishedSchemaXmlFileLocater extends SchemaXmlFileLocater implements ISchemaLocater {
52
+ /**
53
+ * Constructs a new PublishedSchemaXmlFileLocater
54
+ * @param knownBackendAssetsDir The assets directory where the core-backend package is installed. Can be accessed by the KnownLocations.nativeAssetsDir getter provided by core-backend.
55
+ */
56
+ constructor(knownBackendAssetsDir: string);
57
+ /**
58
+ * Overrides the addSchemaSearchPath method to prevent adding new schema search paths.
59
+ *
60
+ * @param _schemaPath - The schema path to add (ignored).
61
+ */
62
+ addSchemaSearchPath(_schemaPath: string): void;
63
+ /**
64
+ * Overrides the addSchemaSearchPaths method to prevent adding new schema search paths.
65
+ *
66
+ * @param _schemaPaths - The schema paths to add (ignored).
67
+ */
68
+ addSchemaSearchPaths(_schemaPaths: string[]): void;
69
+ }
70
+ //# sourceMappingURL=SchemaXmlFileLocater.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SchemaXmlFileLocater.d.ts","sourceRoot":"","sources":["../../src/SchemaXmlFileLocater.ts"],"names":[],"mappings":"AAIA;;GAEG;AAKH,OAAO,EACuC,cAAc,EAAE,MAAM,EAAE,aAAa,EAAE,UAAU,EAAE,SAAS,EAAE,eAAe,EAC1H,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAiB,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAGvE;;;;GAIG;AACH,qBAAa,oBAAqB,SAAQ,iBAAkB,YAAW,cAAc;IACnF;;;;;;OAMG;IACU,SAAS,CAAC,GAAG,EAAE,SAAS,EAAE,SAAS,EAAE,eAAe,EAAE,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IAOvH;;;;;;QAMI;IACS,aAAa,CAAC,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,eAAe,EAAE,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,UAAU,GAAG,SAAS,CAAC;IA2BrI;;;;;;OAMG;IACI,aAAa,CAAC,GAAG,EAAE,SAAS,EAAE,SAAS,EAAE,eAAe,EAAE,OAAO,EAAE,aAAa,GAAG,MAAM,GAAG,SAAS;IA4B5G;;;OAGG;IACI,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS;CAa7C;AAED;;;;;;;;GAQG;AACH,qBAAa,6BAA8B,SAAQ,oBAAqB,YAAW,cAAc;IAC/F;;;OAGG;gBACgB,qBAAqB,EAAE,MAAM;IAWhD;;;;OAIG;IACa,mBAAmB,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI;IAI9D;;;;OAIG;IACa,oBAAoB,CAAC,YAAY,EAAE,MAAM,EAAE,GAAG,IAAI;CAGnE"}
@@ -0,0 +1,140 @@
1
+ /*---------------------------------------------------------------------------------------------
2
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
3
+ * See LICENSE.md in the project root for license terms and full copyright notice.
4
+ *--------------------------------------------------------------------------------------------*/
5
+ /** @packageDocumentation
6
+ * @module Locaters
7
+ */
8
+ import * as path from "path";
9
+ import * as fs from "fs";
10
+ import { DOMParser } from "@xmldom/xmldom";
11
+ import { ECObjectsError, ECObjectsStatus, ECVersion, Schema, SchemaKey, SchemaReadHelper, XmlParser, } from "@itwin/ecschema-metadata";
12
+ import { SchemaFileLocater } from "./SchemaFileLocater";
13
+ import { globSync } from "glob";
14
+ /**
15
+ * A SchemaLocater implementation for locating XML Schema files
16
+ * from the file system using configurable search paths.
17
+ * @beta This is a workaround the current lack of a full xml parser.
18
+ */
19
+ export class SchemaXmlFileLocater extends SchemaFileLocater {
20
+ /**
21
+ * Attempts to retrieve a Schema with the given SchemaKey by using the configured search paths
22
+ * to locate the XML Schema file from the file system.
23
+ * @param key The SchemaKey of the Schema to retrieve.
24
+ * @param matchType The SchemaMatchType.
25
+ * @param context The SchemaContext that will control the lifetime of the schema.
26
+ */
27
+ async getSchema(key, matchType, context) {
28
+ await this.getSchemaInfo(key, matchType, context);
29
+ const schema = await context.getCachedSchema(key, matchType);
30
+ return schema;
31
+ }
32
+ /**
33
+ * Gets the schema info which matches the provided SchemaKey. The schema info may be returned before the schema is fully loaded.
34
+ * The fully loaded schema can be gotten later from the context using the getCachedSchema method.
35
+ * @param schemaKey The SchemaKey describing the schema to get from the cache.
36
+ * @param matchType The match type to use when locating the schema
37
+ * @param context The SchemaContext that will control the lifetime of the schema and holds the schema's references, if they exist.
38
+ */
39
+ async getSchemaInfo(schemaKey, matchType, context) {
40
+ const candidates = this.findEligibleSchemaKeys(schemaKey, matchType, "xml");
41
+ if (0 === candidates.length)
42
+ return undefined;
43
+ const maxCandidate = candidates.sort(this.compareSchemaKeyByVersion)[candidates.length - 1];
44
+ const schemaPath = maxCandidate.fileName;
45
+ // Load the file
46
+ if (undefined === await this.fileExists(schemaPath))
47
+ return undefined;
48
+ const schemaText = await this.readUtf8FileToString(schemaPath);
49
+ if (undefined === schemaText)
50
+ return undefined;
51
+ const parser = new DOMParser();
52
+ const document = parser.parseFromString(schemaText);
53
+ this.addSchemaSearchPaths([path.dirname(schemaPath)]);
54
+ const reader = new SchemaReadHelper(XmlParser, context);
55
+ const schema = new Schema(context);
56
+ return reader.readSchemaInfo(schema, document);
57
+ }
58
+ /**
59
+ * Attempts to retrieve a Schema with the given SchemaKey by using the configured search paths
60
+ * to locate the XML Schema file from the file system.
61
+ * @param key The SchemaKey of the Schema to retrieve.
62
+ * @param matchType The SchemaMatchType.
63
+ * @param context The SchemaContext that will control the lifetime of the schema.
64
+ */
65
+ getSchemaSync(key, matchType, context) {
66
+ const candidates = this.findEligibleSchemaKeys(key, matchType, "xml");
67
+ if (!candidates || candidates.length === 0)
68
+ return undefined;
69
+ const maxCandidate = candidates.sort(this.compareSchemaKeyByVersion)[candidates.length - 1];
70
+ const schemaPath = maxCandidate.fileName;
71
+ // Load the file
72
+ if (!this.fileExistsSync(schemaPath))
73
+ return undefined;
74
+ const schemaText = this.readUtf8FileToStringSync(schemaPath);
75
+ if (!schemaText)
76
+ return undefined;
77
+ const parser = new DOMParser();
78
+ const document = parser.parseFromString(schemaText);
79
+ this.addSchemaSearchPaths([path.dirname(schemaPath)]);
80
+ const reader = new SchemaReadHelper(XmlParser, context);
81
+ let schema = new Schema(context);
82
+ schema = reader.readSchemaSync(schema, document);
83
+ return schema;
84
+ }
85
+ /**
86
+ * Constructs a SchemaKey based on the information in the Schema XML.
87
+ * @param data The Schema XML as a string.
88
+ */
89
+ getSchemaKey(data) {
90
+ const matches = data.match(/<ECSchema ([^]+?)>/g);
91
+ if (!matches || matches.length !== 1)
92
+ throw new ECObjectsError(ECObjectsStatus.InvalidSchemaXML, `Could not find '<ECSchema>' tag in the given file`);
93
+ const name = matches[0].match(/schemaName="(.+?)"/);
94
+ const version = matches[0].match(/version="(.+?)"/);
95
+ if (!name || name.length !== 2 || !version || version.length !== 2)
96
+ throw new ECObjectsError(ECObjectsStatus.InvalidSchemaXML, `Could not find the ECSchema 'schemaName' or 'version' tag in the given file`);
97
+ const key = new SchemaKey(name[1], ECVersion.fromString(version[1]));
98
+ return key;
99
+ }
100
+ }
101
+ /**
102
+ * This locater is responsible for locating standard schema files that are released by the core-backend package.
103
+ * The locater needs an argument to the known backend assets directory where the core-backend package is installed.
104
+ * This can be accessed by the KnownLocations.nativeAssetsDir getter provided by core-backend.
105
+ *
106
+ * @note This locater is read-only and does not allow adding new schema search paths.
107
+ * @note This locater should be used as a fallback/last chance locater in the schema context as any user defined schema should have higher precedence over the standard schema.
108
+ * @beta This is a workaround due to the current lack of a full xml parser.
109
+ */
110
+ export class PublishedSchemaXmlFileLocater extends SchemaXmlFileLocater {
111
+ /**
112
+ * Constructs a new PublishedSchemaXmlFileLocater
113
+ * @param knownBackendAssetsDir The assets directory where the core-backend package is installed. Can be accessed by the KnownLocations.nativeAssetsDir getter provided by core-backend.
114
+ */
115
+ constructor(knownBackendAssetsDir) {
116
+ super();
117
+ const ecSchemaDir = path.join(knownBackendAssetsDir, "ECSchemas");
118
+ if (!fs.existsSync(ecSchemaDir))
119
+ throw new Error(`The directory ${ecSchemaDir} containing standard schemas does not exist.`);
120
+ // Pre-defined set of standard schema search paths
121
+ this.searchPaths.push(...globSync(path.join(ecSchemaDir, "*/"), { windowsPathsNoEscape: true }).filter(fs.existsSync));
122
+ }
123
+ /**
124
+ * Overrides the addSchemaSearchPath method to prevent adding new schema search paths.
125
+ *
126
+ * @param _schemaPath - The schema path to add (ignored).
127
+ */
128
+ addSchemaSearchPath(_schemaPath) {
129
+ return; // Do nothing, this is a read-only locator
130
+ }
131
+ /**
132
+ * Overrides the addSchemaSearchPaths method to prevent adding new schema search paths.
133
+ *
134
+ * @param _schemaPaths - The schema paths to add (ignored).
135
+ */
136
+ addSchemaSearchPaths(_schemaPaths) {
137
+ return; // Do nothing, this is a read-only locator
138
+ }
139
+ }
140
+ //# sourceMappingURL=SchemaXmlFileLocater.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SchemaXmlFileLocater.js","sourceRoot":"","sources":["../../src/SchemaXmlFileLocater.ts"],"names":[],"mappings":"AAAA;;;+FAG+F;AAC/F;;GAEG;AAEH,OAAO,K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AQ,oBAAoB;IACrE;;;OAGG;IACH,YAAmB,qBAA6B;QAC9C,KAAK,EAAE,CAAC;QAER,MAAM,WAAW,GAAG,IAAI,CAAC,IAAI,CAAC,qBAAqB,EAAE,WAAW,CAAC,CAAC;QAClE,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,WAAW,CAAC;YAC7B,MAAM,IAAI,KAAK,CAAC,iBAAiB,WAAW,8CAA8C,CAAC,CAAC;QAE9F,kDAAkD;QAClD,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,IAAI,CAAC,EAAE,EAAE,oBAAoB,EAAE,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,CAAC,UAAU,CAAC,CAAC,CAAC;IACzH,CAAC;IAED;;;;OAIG;IACa,mBAAmB,CAAC,WAAmB;QACrD,OAAO,CAAC,0CAA0C;IACpD,CAAC;IAED;;;;OAIG;IACa,oBAAoB,CAAC,YAAsB;QACzD,OAAO,CAAC,0CAA0C;IACpD,CAAC;CACF","sourcesContent":["/*---------------------------------------------------------------------------------------------\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\n* See LICENSE.md in the project root for license terms and full copyright notice.\n*--------------------------------------------------------------------------------------------*/\n/** @packageDocumentation\n * @module Locaters\n */\n\nimport * as path from \"path\";\nimport * as fs from \"fs\";\nimport { DOMParser } from \"@xmldom/xmldom\";\nimport {\n ECObjectsError, ECObjectsStatus, ECVersion, ISchemaLocater, Schema, SchemaContext, SchemaInfo, SchemaKey, SchemaMatchType, SchemaReadHelper, XmlParser,\n} from \"@itwin/ecschema-metadata\";\nimport { FileSchemaKey, SchemaFileLocater } from \"./SchemaFileLocater\";\nimport { globSync } from \"glob\";\n\n/**\n * A SchemaLocater implementation for locating XML Schema files\n * from the file system using configurable search paths.\n * @beta This is a workaround the current lack of a full xml parser.\n */\nexport class SchemaXmlFileLocater extends SchemaFileLocater implements ISchemaLocater {\n /**\n * Attempts to retrieve a Schema with the given SchemaKey by using the configured search paths\n * to locate the XML Schema file from the file system.\n * @param key The SchemaKey of the Schema to retrieve.\n * @param matchType The SchemaMatchType.\n * @param context The SchemaContext that will control the lifetime of the schema.\n */\n public async getSchema(key: SchemaKey, matchType: SchemaMatchType, context: SchemaContext): Promise<Schema | undefined> {\n await this.getSchemaInfo(key, matchType, context);\n\n const schema = await context.getCachedSchema(key, matchType);\n return schema;\n }\n\n /**\n * Gets the schema info which matches the provided SchemaKey. The schema info may be returned before the schema is fully loaded.\n * The fully loaded schema can be gotten later from the context using the getCachedSchema method.\n * @param schemaKey The SchemaKey describing the schema to get from the cache.\n * @param matchType The match type to use when locating the schema\n * @param context The SchemaContext that will control the lifetime of the schema and holds the schema's references, if they exist.\n */\n public async getSchemaInfo(schemaKey: SchemaKey, matchType: SchemaMatchType, context: SchemaContext): Promise<SchemaInfo | undefined> {\n const candidates: FileSchemaKey[] = this.findEligibleSchemaKeys(schemaKey, matchType, \"xml\");\n\n if (0 === candidates.length)\n return undefined;\n\n const maxCandidate = candidates.sort(this.compareSchemaKeyByVersion)[candidates.length - 1];\n const schemaPath = maxCandidate.fileName;\n\n // Load the file\n if (undefined === await this.fileExists(schemaPath))\n return undefined;\n\n const schemaText = await this.readUtf8FileToString(schemaPath);\n if (undefined === schemaText)\n return undefined;\n\n const parser = new DOMParser();\n const document = parser.parseFromString(schemaText);\n\n this.addSchemaSearchPaths([path.dirname(schemaPath)]);\n const reader = new SchemaReadHelper(XmlParser, context);\n const schema = new Schema(context);\n\n return reader.readSchemaInfo(schema, document);\n }\n\n /**\n * Attempts to retrieve a Schema with the given SchemaKey by using the configured search paths\n * to locate the XML Schema file from the file system.\n * @param key The SchemaKey of the Schema to retrieve.\n * @param matchType The SchemaMatchType.\n * @param context The SchemaContext that will control the lifetime of the schema.\n */\n public getSchemaSync(key: SchemaKey, matchType: SchemaMatchType, context: SchemaContext): Schema | undefined {\n const candidates: FileSchemaKey[] = this.findEligibleSchemaKeys(key, matchType, \"xml\");\n\n if (!candidates || candidates.length === 0)\n return undefined;\n\n const maxCandidate = candidates.sort(this.compareSchemaKeyByVersion)[candidates.length - 1];\n const schemaPath = maxCandidate.fileName;\n\n // Load the file\n if (!this.fileExistsSync(schemaPath))\n return undefined;\n\n const schemaText = this.readUtf8FileToStringSync(schemaPath);\n if (!schemaText)\n return undefined;\n\n const parser = new DOMParser();\n const document = parser.parseFromString(schemaText);\n\n this.addSchemaSearchPaths([path.dirname(schemaPath)]);\n const reader = new SchemaReadHelper(XmlParser, context);\n let schema: Schema = new Schema(context);\n schema = reader.readSchemaSync(schema, document);\n\n return schema;\n }\n\n /**\n * Constructs a SchemaKey based on the information in the Schema XML.\n * @param data The Schema XML as a string.\n */\n public getSchemaKey(data: string): SchemaKey {\n const matches = data.match(/<ECSchema ([^]+?)>/g);\n if (!matches || matches.length !== 1)\n throw new ECObjectsError(ECObjectsStatus.InvalidSchemaXML, `Could not find '<ECSchema>' tag in the given file`);\n\n const name = matches[0].match(/schemaName=\"(.+?)\"/);\n const version = matches[0].match(/version=\"(.+?)\"/);\n if (!name || name.length !== 2 || !version || version.length !== 2)\n throw new ECObjectsError(ECObjectsStatus.InvalidSchemaXML, `Could not find the ECSchema 'schemaName' or 'version' tag in the given file`);\n\n const key = new SchemaKey(name[1], ECVersion.fromString(version[1]));\n return key;\n }\n}\n\n/**\n * This locater is responsible for locating standard schema files that are released by the core-backend package.\n * The locater needs an argument to the known backend assets directory where the core-backend package is installed.\n * This can be accessed by the KnownLocations.nativeAssetsDir getter provided by core-backend.\n *\n * @note This locater is read-only and does not allow adding new schema search paths.\n * @note This locater should be used as a fallback/last chance locater in the schema context as any user defined schema should have higher precedence over the standard schema.\n * @beta This is a workaround due to the current lack of a full xml parser.\n */\nexport class PublishedSchemaXmlFileLocater extends SchemaXmlFileLocater implements ISchemaLocater {\n /**\n * Constructs a new PublishedSchemaXmlFileLocater\n * @param knownBackendAssetsDir The assets directory where the core-backend package is installed. Can be accessed by the KnownLocations.nativeAssetsDir getter provided by core-backend.\n */\n public constructor(knownBackendAssetsDir: string) {\n super();\n\n const ecSchemaDir = path.join(knownBackendAssetsDir, \"ECSchemas\");\n if (!fs.existsSync(ecSchemaDir))\n throw new Error(`The directory ${ecSchemaDir} containing standard schemas does not exist.`);\n\n // Pre-defined set of standard schema search paths\n this.searchPaths.push(...globSync(path.join(ecSchemaDir, \"*/\"), { windowsPathsNoEscape: true }).filter(fs.existsSync));\n }\n\n /**\n * Overrides the addSchemaSearchPath method to prevent adding new schema search paths.\n *\n * @param _schemaPath - The schema path to add (ignored).\n */\n public override addSchemaSearchPath(_schemaPath: string): void {\n return; // Do nothing, this is a read-only locator\n }\n\n /**\n * Overrides the addSchemaSearchPaths method to prevent adding new schema search paths.\n *\n * @param _schemaPaths - The schema paths to add (ignored).\n */\n public override addSchemaSearchPaths(_schemaPaths: string[]): void {\n return; // Do nothing, this is a read-only locator\n }\n}\n"]}
@@ -0,0 +1,48 @@
1
+ /** @packageDocumentation
2
+ * @module Locaters
3
+ */
4
+ import { ISchemaLocater, Schema, SchemaContext, SchemaInfo, SchemaKey, SchemaMatchType } from "@itwin/ecschema-metadata";
5
+ import { SchemaStringLocater } from "./SchemaStringLocater";
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(schemaKey: SchemaKey, matchType: SchemaMatchType, context: SchemaContext): Promise<Schema | 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(schemaKey: SchemaKey, matchType: SchemaMatchType, context: SchemaContext): Schema | 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":"AAIA;;GAEG;AAGH,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;;;;GAIG;AACH,qBAAa,sBAAuB,SAAQ,mBAAoB,YAAW,cAAc;IAEvF;;;OAGG;IACI,YAAY,CAAC,SAAS,EAAE,MAAM,GAAG,SAAS;IAejD;;;;;;OAMG;IACU,SAAS,CAAC,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,eAAe,EAAE,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IAO7H;;;;;;QAMI;IACS,aAAa,CAAC,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,eAAe,EAAE,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,UAAU,GAAG,SAAS,CAAC;IAkBrI;;;;;;OAMG;IACI,aAAa,CAAC,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,eAAe,EAAE,OAAO,EAAE,aAAa,GAAG,MAAM,GAAG,SAAS;IAmBlH;;;;OAIG;IACH,OAAO,CAAC,cAAc;CAYvB"}
@@ -0,0 +1,100 @@
1
+ /*---------------------------------------------------------------------------------------------
2
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
3
+ * See LICENSE.md in the project root for license terms and full copyright notice.
4
+ *--------------------------------------------------------------------------------------------*/
5
+ /** @packageDocumentation
6
+ * @module Locaters
7
+ */
8
+ import { DOMParser } from "@xmldom/xmldom";
9
+ import { ECObjectsError, ECObjectsStatus, ECVersion, Schema, SchemaKey, SchemaReadHelper, XmlParser, } from "@itwin/ecschema-metadata";
10
+ import { SchemaStringLocater } from "./SchemaStringLocater";
11
+ /**
12
+ * A SchemaLocator implementation for locating and deserializing EC Schemas from XML strings
13
+ * loaded in memory.
14
+ * @beta
15
+ */
16
+ export class SchemaXmlStringLocater extends SchemaStringLocater {
17
+ /**
18
+ * Constructs a SchemaKey based on the information in the Schema XML.
19
+ * @param schemaXml The Schema XML as a string.
20
+ */
21
+ getSchemaKey(schemaXml) {
22
+ const matches = schemaXml.match(/<ECSchema ([^]+?)>/g);
23
+ if (!matches || matches.length !== 1)
24
+ throw new ECObjectsError(ECObjectsStatus.InvalidSchemaXML, `Could not find '<ECSchema>' tag in the given string`);
25
+ const name = matches[0].match(/schemaName="(.+?)"/);
26
+ const version = matches[0].match(/version="(.+?)"/);
27
+ if (!name || name.length !== 2 || !version || version.length !== 2)
28
+ throw new ECObjectsError(ECObjectsStatus.InvalidSchemaXML, `Could not find the ECSchema 'schemaName' or 'version' tag in the given string`);
29
+ const ecVersion = ECVersion.fromString(version[1]);
30
+ const key = new SchemaKey(name[1], ecVersion);
31
+ return key;
32
+ }
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
+ async getSchema(schemaKey, matchType, context) {
41
+ await this.getSchemaInfo(schemaKey, matchType, context);
42
+ const schema = await context.getCachedSchema(schemaKey, matchType);
43
+ return schema;
44
+ }
45
+ /**
46
+ * Gets the schema info which matches the provided SchemaKey. The schema info may be returned before the schema is fully loaded.
47
+ * The fully loaded schema can be gotten later from the context using the getCachedSchema method.
48
+ * @param schemaKey The SchemaKey describing the schema to get from the cache.
49
+ * @param matchType The match type to use when locating the schema
50
+ * @param context The SchemaContext that will control the lifetime of the schema and holds the schema's references, if they exist.
51
+ */
52
+ async getSchemaInfo(schemaKey, matchType, context) {
53
+ // Grab all schema strings that match the schema key
54
+ const candidates = this.findEligibleSchemaKeys(schemaKey, matchType);
55
+ if (!candidates || candidates.length === 0)
56
+ return undefined;
57
+ const maxCandidate = candidates.sort(this.compareSchemaKeyByVersion)[candidates.length - 1];
58
+ const schemaText = maxCandidate.schemaText;
59
+ const parser = new DOMParser();
60
+ const document = parser.parseFromString(schemaText);
61
+ const reader = new SchemaReadHelper(XmlParser, context);
62
+ const schema = new Schema(context);
63
+ return reader.readSchemaInfo(schema, document);
64
+ }
65
+ /**
66
+ * Attempts to retrieve a Schema with the given SchemaKey by searching the configured
67
+ * Schema strings.
68
+ * @param key The SchemaKey of the Schema to retrieve.
69
+ * @param matchType The SchemaMatchType.
70
+ * @param context The SchemaContext that will control the lifetime of the schema and holds the schema's references, if they exist.
71
+ */
72
+ getSchemaSync(schemaKey, matchType, context) {
73
+ // Grab all schema strings that match the schema key
74
+ const candidates = this.findEligibleSchemaKeys(schemaKey, matchType);
75
+ if (!candidates || candidates.length === 0)
76
+ return undefined;
77
+ const maxCandidate = candidates.sort(this.compareSchemaKeyByVersion)[candidates.length - 1];
78
+ const schemaText = maxCandidate.schemaText;
79
+ const parser = new DOMParser();
80
+ const document = parser.parseFromString(schemaText);
81
+ const reader = new SchemaReadHelper(XmlParser, context);
82
+ let schema = new Schema(context);
83
+ schema = reader.readSchemaSync(schema, document);
84
+ return schema;
85
+ }
86
+ /**
87
+ * Parses a valid EC 2.0 version string and returns an ECVersion object. The second digit becomes the minor version,
88
+ * and a zero is inserted as the 'write' digit. Example: "1.1" -> "1.0.1".
89
+ * @param versionString A valid EC 2.0 version string of the format, 'RR.mm'.
90
+ */
91
+ fromECv2String(versionString) {
92
+ const [read, minor] = versionString.split(".");
93
+ if (!read)
94
+ throw new ECObjectsError(ECObjectsStatus.InvalidECVersion, `The read version is missing from version string, ${versionString}`);
95
+ if (!minor)
96
+ throw new ECObjectsError(ECObjectsStatus.InvalidECVersion, `The minor version is missing from version string, ${versionString}`);
97
+ return new ECVersion(+read, 0, +minor);
98
+ }
99
+ }
100
+ //# sourceMappingURL=SchemaXmlStringLocater.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SchemaXmlStringLocater.js","sourceRoot":"","sources":["../../src/SchemaXmlStringLocater.ts"],"names":[],"mappings":"AAAA;;;+FAG+F;AAC/F;;GAEG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EACL,cAAc,EAAE,eAAe,EAAE,SAAS,EAAkB,MAAM,EAA6B,SAAS,EAAmB,gBAAgB,EAAE,SAAS,GACvJ,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,mBAAmB,EAAmB,MAAM,uBAAuB,CAAC;AAE7E;;;;GAIG;AACH,MAAM,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AG,IAAI,gBAAgB,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;QACxD,IAAI,MAAM,GAAW,IAAI,MAAM,CAAC,OAAO,CAAC,CAAC;QACzC,MAAM,GAAG,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;QAEjD,OAAO,MAAM,CAAC;IAChB,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,cAAc,CAAC,eAAe,CAAC,gBAAgB,EAAE,oDAAoD,aAAa,EAAE,CAAC,CAAC;QAElI,IAAI,CAAC,KAAK;YACR,MAAM,IAAI,cAAc,CAAC,eAAe,CAAC,gBAAgB,EAAE,qDAAqD,aAAa,EAAE,CAAC,CAAC;QAEnI,OAAO,IAAI,SAAS,CAAC,CAAC,IAAI,EAAE,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC;IACzC,CAAC;CAEF","sourcesContent":["/*---------------------------------------------------------------------------------------------\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\n* See LICENSE.md in the project root for license terms and full copyright notice.\n*--------------------------------------------------------------------------------------------*/\n/** @packageDocumentation\n * @module Locaters\n */\n\nimport { DOMParser } from \"@xmldom/xmldom\";\nimport {\n ECObjectsError, ECObjectsStatus, ECVersion, ISchemaLocater, Schema, SchemaContext, SchemaInfo, SchemaKey, SchemaMatchType, SchemaReadHelper, XmlParser,\n} from \"@itwin/ecschema-metadata\";\nimport { SchemaStringLocater, StringSchemaKey } from \"./SchemaStringLocater\";\n\n/**\n * A SchemaLocator implementation for locating and deserializing EC Schemas from XML strings\n * loaded in memory.\n * @beta\n */\nexport class SchemaXmlStringLocater extends SchemaStringLocater implements ISchemaLocater {\n\n /**\n * Constructs a SchemaKey based on the information in the Schema XML.\n * @param schemaXml The Schema XML as a string.\n */\n public getSchemaKey(schemaXml: string): SchemaKey {\n const matches = schemaXml.match(/<ECSchema ([^]+?)>/g);\n if (!matches || matches.length !== 1)\n throw new ECObjectsError(ECObjectsStatus.InvalidSchemaXML, `Could not find '<ECSchema>' tag in the given string`);\n\n const name = matches[0].match(/schemaName=\"(.+?)\"/);\n const version = matches[0].match(/version=\"(.+?)\"/);\n if (!name || name.length !== 2 || !version || version.length !== 2)\n throw new ECObjectsError(ECObjectsStatus.InvalidSchemaXML, `Could not find the ECSchema 'schemaName' or 'version' tag in the given string`);\n\n const ecVersion = ECVersion.fromString(version[1]);\n const key = new SchemaKey(name[1], ecVersion);\n return key;\n }\n\n /**\n * Attempts to retrieve a Schema with the given SchemaKey by searching the configured\n * Schema strings.\n * @param key The SchemaKey of the Schema to retrieve.\n * @param matchType The SchemaMatchType.\n * @param context The SchemaContext that will control the lifetime of the schema and holds the schema's references, if they exist.\n */\n public async getSchema(schemaKey: SchemaKey, matchType: SchemaMatchType, context: SchemaContext): Promise<Schema | undefined> {\n await this.getSchemaInfo(schemaKey, matchType, context);\n\n const schema = await context.getCachedSchema(schemaKey, matchType);\n return schema as Schema;\n }\n\n /**\n * Gets the schema info which matches the provided SchemaKey. The schema info may be returned before the schema is fully loaded.\n * The fully loaded schema can be gotten later from the context using the getCachedSchema method.\n * @param schemaKey The SchemaKey describing the schema to get from the cache.\n * @param matchType The match type to use when locating the schema\n * @param context The SchemaContext that will control the lifetime of the schema and holds the schema's references, if they exist.\n */\n public async getSchemaInfo(schemaKey: SchemaKey, matchType: SchemaMatchType, context: SchemaContext): Promise<SchemaInfo | undefined> {\n // Grab all schema strings that match the schema key\n const candidates: StringSchemaKey[] = this.findEligibleSchemaKeys(schemaKey, matchType);\n if (!candidates || candidates.length === 0)\n return undefined;\n\n const maxCandidate = candidates.sort(this.compareSchemaKeyByVersion)[candidates.length - 1];\n const schemaText = maxCandidate.schemaText;\n\n const parser = new DOMParser();\n const document = parser.parseFromString(schemaText);\n\n const reader = new SchemaReadHelper(XmlParser, context);\n const schema = new Schema(context);\n\n return reader.readSchemaInfo(schema, document);\n }\n\n /**\n * Attempts to retrieve a Schema with the given SchemaKey by searching the configured\n * Schema strings.\n * @param key The SchemaKey of the Schema to retrieve.\n * @param matchType The SchemaMatchType.\n * @param context The SchemaContext that will control the lifetime of the schema and holds the schema's references, if they exist.\n */\n public getSchemaSync(schemaKey: SchemaKey, matchType: SchemaMatchType, context: SchemaContext): Schema | undefined {\n // Grab all schema strings that match the schema key\n const candidates: StringSchemaKey[] = this.findEligibleSchemaKeys(schemaKey, matchType);\n if (!candidates || candidates.length === 0)\n return undefined;\n\n const maxCandidate = candidates.sort(this.compareSchemaKeyByVersion)[candidates.length - 1];\n const schemaText = maxCandidate.schemaText;\n\n const parser = new DOMParser();\n const document = parser.parseFromString(schemaText);\n\n const reader = new SchemaReadHelper(XmlParser, context);\n let schema: Schema = new Schema(context);\n schema = reader.readSchemaSync(schema, document);\n\n return schema;\n }\n\n /**\n * Parses a valid EC 2.0 version string and returns an ECVersion object. The second digit becomes the minor version,\n * and a zero is inserted as the 'write' digit. Example: \"1.1\" -> \"1.0.1\".\n * @param versionString A valid EC 2.0 version string of the format, 'RR.mm'.\n */\n private fromECv2String(versionString: string): ECVersion {\n const [read, minor] = versionString.split(\".\");\n\n if (!read)\n throw new ECObjectsError(ECObjectsStatus.InvalidECVersion, `The read version is missing from version string, ${versionString}`);\n\n if (!minor)\n throw new ECObjectsError(ECObjectsStatus.InvalidECVersion, `The minor version is missing from version string, ${versionString}`);\n\n return new ECVersion(+read, 0, +minor);\n }\n\n}\n"]}