@itwin/ecschema-locaters 4.0.0-dev.6 → 4.0.0-dev.61

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.
@@ -1,49 +1,49 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SchemaFileUtility = void 0;
4
- /*---------------------------------------------------------------------------------------------
5
- * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
6
- * See LICENSE.md in the project root for license terms and full copyright notice.
7
- *--------------------------------------------------------------------------------------------*/
8
- const fs = require("fs-extra");
9
- const path = require("path");
10
- const xmldom_1 = require("@xmldom/xmldom");
11
- /** @packageDocumentation
12
- * @module Utils
13
- */
14
- /**
15
- * Utility class to assist in creating serialized EC Schemas on the file system.
16
- * @beta
17
- */
18
- class SchemaFileUtility {
19
- /**
20
- * Writes a Schema to an xml file to the specified output path.
21
- * @param schema The Schema to serialize.
22
- * @param outputPath The directory in which to create the file.
23
- */
24
- static async writeSchemaXmlFile(schema, outputPath) {
25
- let xmlDoc = new xmldom_1.DOMParser().parseFromString(`<?xml version="1.0" encoding="UTF-8"?>`, "application/xml");
26
- const baseFile = this.getSchemaPath(schema, outputPath);
27
- xmlDoc = await schema.toXml(xmlDoc);
28
- const serializer = new xmldom_1.XMLSerializer();
29
- const xml = serializer.serializeToString(xmlDoc);
30
- try {
31
- await fs.writeFile(baseFile, xml);
32
- }
33
- catch (err) {
34
- const msg = `An error occurred writing to file '${baseFile}': ${err.message}`;
35
- throw new Error(msg);
36
- }
37
- }
38
- static getSchemaPath(schema, outputPath) {
39
- const realDir = path.normalize(outputPath);
40
- const test = fs.pathExistsSync(realDir);
41
- if (!test) {
42
- const msg = `The output directory '${realDir}' does not exist.`;
43
- throw new Error(msg);
44
- }
45
- return path.resolve(realDir, `${schema.name}.ecschema.xml`);
46
- }
47
- }
48
- exports.SchemaFileUtility = SchemaFileUtility;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SchemaFileUtility = void 0;
4
+ /*---------------------------------------------------------------------------------------------
5
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
6
+ * See LICENSE.md in the project root for license terms and full copyright notice.
7
+ *--------------------------------------------------------------------------------------------*/
8
+ const fs = require("fs-extra");
9
+ const path = require("path");
10
+ const xmldom_1 = require("@xmldom/xmldom");
11
+ /** @packageDocumentation
12
+ * @module Utils
13
+ */
14
+ /**
15
+ * Utility class to assist in creating serialized EC Schemas on the file system.
16
+ * @beta
17
+ */
18
+ class SchemaFileUtility {
19
+ /**
20
+ * Writes a Schema to an xml file to the specified output path.
21
+ * @param schema The Schema to serialize.
22
+ * @param outputPath The directory in which to create the file.
23
+ */
24
+ static async writeSchemaXmlFile(schema, outputPath) {
25
+ let xmlDoc = new xmldom_1.DOMParser().parseFromString(`<?xml version="1.0" encoding="UTF-8"?>`, "application/xml");
26
+ const baseFile = this.getSchemaPath(schema, outputPath);
27
+ xmlDoc = await schema.toXml(xmlDoc);
28
+ const serializer = new xmldom_1.XMLSerializer();
29
+ const xml = serializer.serializeToString(xmlDoc);
30
+ try {
31
+ await fs.writeFile(baseFile, xml);
32
+ }
33
+ catch (err) {
34
+ const msg = `An error occurred writing to file '${baseFile}': ${err.message}`;
35
+ throw new Error(msg);
36
+ }
37
+ }
38
+ static getSchemaPath(schema, outputPath) {
39
+ const realDir = path.normalize(outputPath);
40
+ const test = fs.pathExistsSync(realDir);
41
+ if (!test) {
42
+ const msg = `The output directory '${realDir}' does not exist.`;
43
+ throw new Error(msg);
44
+ }
45
+ return path.resolve(realDir, `${schema.name}.ecschema.xml`);
46
+ }
47
+ }
48
+ exports.SchemaFileUtility = SchemaFileUtility;
49
49
  //# sourceMappingURL=SchemaFileUtility.js.map
@@ -1,34 +1,34 @@
1
- import { ISchemaLocater, Schema, SchemaContext, SchemaKey, SchemaMatchType } from "@itwin/ecschema-metadata";
2
- import { SchemaFileLocater } from "./SchemaFileLocater";
3
- /** @packageDocumentation
4
- * @module Locaters
5
- */
6
- /**
7
- * A SchemaLocator implementation for locating JSON Schema files
8
- * from the file system using configurable search paths.
9
- * @beta
10
- */
11
- export declare class SchemaJsonFileLocater extends SchemaFileLocater implements ISchemaLocater {
12
- /**
13
- * Constructs a SchemaKey based on the information in the Schema JSON
14
- * @param data The Schema JSON as a string
15
- */
16
- protected getSchemaKey(data: string): SchemaKey;
17
- /**
18
- * Attempts to retrieve a Schema with the given SchemaKey by using the configured
19
- * search paths to locate the JSON schema file from the file system.
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
- * Attempts to retrieve a Schema with the given SchemaKey by using the configured
27
- * search paths to locate the JSON schema file from the file system.
28
- * @param key The SchemaKey of the Schema to retrieve.
29
- * @param matchType The SchemaMatchType
30
- * @param context The SchemaContext that will control the lifetime of the schema.
31
- */
32
- getSchemaSync<T extends Schema>(schemaKey: SchemaKey, matchType: SchemaMatchType, context: SchemaContext): T | undefined;
33
- }
1
+ import { ISchemaLocater, Schema, SchemaContext, SchemaKey, SchemaMatchType } from "@itwin/ecschema-metadata";
2
+ import { SchemaFileLocater } from "./SchemaFileLocater";
3
+ /** @packageDocumentation
4
+ * @module Locaters
5
+ */
6
+ /**
7
+ * A SchemaLocator implementation for locating JSON Schema files
8
+ * from the file system using configurable search paths.
9
+ * @beta
10
+ */
11
+ export declare class SchemaJsonFileLocater extends SchemaFileLocater implements ISchemaLocater {
12
+ /**
13
+ * Constructs a SchemaKey based on the information in the Schema JSON
14
+ * @param data The Schema JSON as a string
15
+ */
16
+ protected getSchemaKey(data: string): SchemaKey;
17
+ /**
18
+ * Attempts to retrieve a Schema with the given SchemaKey by using the configured
19
+ * search paths to locate the JSON schema file from the file system.
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
+ * Attempts to retrieve a Schema with the given SchemaKey by using the configured
27
+ * search paths to locate the JSON schema file from the file system.
28
+ * @param key The SchemaKey of the Schema to retrieve.
29
+ * @param matchType The SchemaMatchType
30
+ * @param context The SchemaContext that will control the lifetime of the schema.
31
+ */
32
+ getSchemaSync<T extends Schema>(schemaKey: SchemaKey, matchType: SchemaMatchType, context: SchemaContext): T | undefined;
33
+ }
34
34
  //# sourceMappingURL=SchemaJsonFileLocater.d.ts.map
@@ -1,91 +1,91 @@
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.SchemaJsonFileLocater = void 0;
8
- const fs = require("fs");
9
- const path = require("path");
10
- const ecschema_metadata_1 = require("@itwin/ecschema-metadata");
11
- const SchemaFileLocater_1 = require("./SchemaFileLocater");
12
- /** @packageDocumentation
13
- * @module Locaters
14
- */
15
- /**
16
- * A SchemaLocator implementation for locating JSON Schema files
17
- * from the file system using configurable search paths.
18
- * @beta
19
- */
20
- class SchemaJsonFileLocater extends SchemaFileLocater_1.SchemaFileLocater {
21
- /**
22
- * Constructs a SchemaKey based on the information in the Schema JSON
23
- * @param data The Schema JSON as a string
24
- */
25
- getSchemaKey(data) {
26
- const dataJson = JSON.parse(data);
27
- // Check if the name is present
28
- if (!(dataJson.name))
29
- throw new ecschema_metadata_1.ECObjectsError(ecschema_metadata_1.ECObjectsStatus.InvalidECJson, `Could not retrieve the ECSchema name in the given file.`);
30
- // Check if versions is present
31
- if (!(dataJson.version))
32
- throw new ecschema_metadata_1.ECObjectsError(ecschema_metadata_1.ECObjectsStatus.InvalidECJson, `Could not parse the ECSchema version in the given file.`);
33
- // Get the name and version from the JSON
34
- const schemaName = dataJson.name;
35
- const schemaVersion = dataJson.version;
36
- const key = new ecschema_metadata_1.SchemaKey(schemaName.toString(), ecschema_metadata_1.ECVersion.fromString(schemaVersion.toString()));
37
- return key;
38
- }
39
- /**
40
- * Attempts to retrieve a Schema with the given SchemaKey by using the configured
41
- * search paths to locate the JSON schema file from the file system.
42
- * @param key The SchemaKey of the Schema to retrieve.
43
- * @param matchType The SchemaMatchType.
44
- * @param context The SchemaContext that will control the lifetime of the schema and holds the schema's references, if they exist.
45
- */
46
- async getSchema(schemaKey, matchType, context) {
47
- // Grab all schema files that match the schema key
48
- const candidates = this.findEligibleSchemaKeys(schemaKey, matchType, "json");
49
- if (!candidates || candidates.length === 0)
50
- return undefined;
51
- // eslint-disable-next-line @typescript-eslint/unbound-method
52
- const maxCandidate = candidates.sort(this.compareSchemaKeyByVersion)[candidates.length - 1];
53
- const schemaPath = maxCandidate.fileName;
54
- // Load the file
55
- if (!await this.fileExists(schemaPath))
56
- return undefined;
57
- const schemaText = await this.readUtf8FileToString(schemaPath);
58
- if (!schemaText)
59
- return undefined;
60
- this.addSchemaSearchPaths([path.dirname(schemaPath)]);
61
- const schema = await ecschema_metadata_1.Schema.fromJson(schemaText, context);
62
- return schema;
63
- }
64
- /**
65
- * Attempts to retrieve a Schema with the given SchemaKey by using the configured
66
- * search paths to locate the JSON schema file from the file system.
67
- * @param key The SchemaKey of the Schema to retrieve.
68
- * @param matchType The SchemaMatchType
69
- * @param context The SchemaContext that will control the lifetime of the schema.
70
- */
71
- getSchemaSync(schemaKey, matchType, context) {
72
- // Grab all schema files that match the schema key
73
- const candidates = this.findEligibleSchemaKeys(schemaKey, matchType, "json");
74
- if (!candidates || candidates.length === 0)
75
- return undefined;
76
- // eslint-disable-next-line @typescript-eslint/unbound-method
77
- const maxCandidate = candidates.sort(this.compareSchemaKeyByVersion)[candidates.length - 1];
78
- const schemaPath = maxCandidate.fileName;
79
- // Load the file
80
- if (!fs.existsSync(schemaPath))
81
- return undefined;
82
- const schemaText = fs.readFileSync(schemaPath, "utf-8");
83
- if (!schemaText)
84
- return undefined;
85
- this.addSchemaSearchPaths([path.dirname(schemaPath)]);
86
- const schema = ecschema_metadata_1.Schema.fromJsonSync(schemaText, context);
87
- return schema;
88
- }
89
- }
90
- exports.SchemaJsonFileLocater = SchemaJsonFileLocater;
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.SchemaJsonFileLocater = void 0;
8
+ const fs = require("fs");
9
+ const path = require("path");
10
+ const ecschema_metadata_1 = require("@itwin/ecschema-metadata");
11
+ const SchemaFileLocater_1 = require("./SchemaFileLocater");
12
+ /** @packageDocumentation
13
+ * @module Locaters
14
+ */
15
+ /**
16
+ * A SchemaLocator implementation for locating JSON Schema files
17
+ * from the file system using configurable search paths.
18
+ * @beta
19
+ */
20
+ class SchemaJsonFileLocater extends SchemaFileLocater_1.SchemaFileLocater {
21
+ /**
22
+ * Constructs a SchemaKey based on the information in the Schema JSON
23
+ * @param data The Schema JSON as a string
24
+ */
25
+ getSchemaKey(data) {
26
+ const dataJson = JSON.parse(data);
27
+ // Check if the name is present
28
+ if (!(dataJson.name))
29
+ throw new ecschema_metadata_1.ECObjectsError(ecschema_metadata_1.ECObjectsStatus.InvalidECJson, `Could not retrieve the ECSchema name in the given file.`);
30
+ // Check if versions is present
31
+ if (!(dataJson.version))
32
+ throw new ecschema_metadata_1.ECObjectsError(ecschema_metadata_1.ECObjectsStatus.InvalidECJson, `Could not parse the ECSchema version in the given file.`);
33
+ // Get the name and version from the JSON
34
+ const schemaName = dataJson.name;
35
+ const schemaVersion = dataJson.version;
36
+ const key = new ecschema_metadata_1.SchemaKey(schemaName.toString(), ecschema_metadata_1.ECVersion.fromString(schemaVersion.toString()));
37
+ return key;
38
+ }
39
+ /**
40
+ * Attempts to retrieve a Schema with the given SchemaKey by using the configured
41
+ * search paths to locate the JSON schema file from the file system.
42
+ * @param key The SchemaKey of the Schema to retrieve.
43
+ * @param matchType The SchemaMatchType.
44
+ * @param context The SchemaContext that will control the lifetime of the schema and holds the schema's references, if they exist.
45
+ */
46
+ async getSchema(schemaKey, matchType, context) {
47
+ // Grab all schema files that match the schema key
48
+ const candidates = this.findEligibleSchemaKeys(schemaKey, matchType, "json");
49
+ if (!candidates || candidates.length === 0)
50
+ return undefined;
51
+ // eslint-disable-next-line @typescript-eslint/unbound-method
52
+ const maxCandidate = candidates.sort(this.compareSchemaKeyByVersion)[candidates.length - 1];
53
+ const schemaPath = maxCandidate.fileName;
54
+ // Load the file
55
+ if (!await this.fileExists(schemaPath))
56
+ return undefined;
57
+ const schemaText = await this.readUtf8FileToString(schemaPath);
58
+ if (!schemaText)
59
+ return undefined;
60
+ this.addSchemaSearchPaths([path.dirname(schemaPath)]);
61
+ const schema = await ecschema_metadata_1.Schema.fromJson(schemaText, context);
62
+ return schema;
63
+ }
64
+ /**
65
+ * Attempts to retrieve a Schema with the given SchemaKey by using the configured
66
+ * search paths to locate the JSON schema file from the file system.
67
+ * @param key The SchemaKey of the Schema to retrieve.
68
+ * @param matchType The SchemaMatchType
69
+ * @param context The SchemaContext that will control the lifetime of the schema.
70
+ */
71
+ getSchemaSync(schemaKey, matchType, context) {
72
+ // Grab all schema files that match the schema key
73
+ const candidates = this.findEligibleSchemaKeys(schemaKey, matchType, "json");
74
+ if (!candidates || candidates.length === 0)
75
+ return undefined;
76
+ // eslint-disable-next-line @typescript-eslint/unbound-method
77
+ const maxCandidate = candidates.sort(this.compareSchemaKeyByVersion)[candidates.length - 1];
78
+ const schemaPath = maxCandidate.fileName;
79
+ // Load the file
80
+ if (!fs.existsSync(schemaPath))
81
+ return undefined;
82
+ const schemaText = fs.readFileSync(schemaPath, "utf-8");
83
+ if (!schemaText)
84
+ return undefined;
85
+ this.addSchemaSearchPaths([path.dirname(schemaPath)]);
86
+ const schema = ecschema_metadata_1.Schema.fromJsonSync(schemaText, context);
87
+ return schema;
88
+ }
89
+ }
90
+ exports.SchemaJsonFileLocater = SchemaJsonFileLocater;
91
91
  //# sourceMappingURL=SchemaJsonFileLocater.js.map
@@ -1,34 +1,34 @@
1
- import { ISchemaLocater, Schema, SchemaContext, SchemaKey, SchemaMatchType } from "@itwin/ecschema-metadata";
2
- import { SchemaFileLocater } from "./SchemaFileLocater";
3
- /** @packageDocumentation
4
- * @module Locaters
5
- */
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<T extends Schema>(key: SchemaKey, matchType: SchemaMatchType, context: SchemaContext): Promise<T | undefined>;
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
- getSchemaSync<T extends Schema>(key: SchemaKey, matchType: SchemaMatchType, context: SchemaContext): T | undefined;
28
- /**
29
- * Constructs a SchemaKey based on the information in the Schema XML.
30
- * @param data The Schema XML as a string.
31
- */
32
- getSchemaKey(data: string): SchemaKey;
33
- }
1
+ import { ISchemaLocater, Schema, SchemaContext, SchemaKey, SchemaMatchType } from "@itwin/ecschema-metadata";
2
+ import { SchemaFileLocater } from "./SchemaFileLocater";
3
+ /** @packageDocumentation
4
+ * @module Locaters
5
+ */
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<T extends Schema>(key: SchemaKey, matchType: SchemaMatchType, context: SchemaContext): Promise<T | undefined>;
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
+ getSchemaSync<T extends Schema>(key: SchemaKey, matchType: SchemaMatchType, context: SchemaContext): T | undefined;
28
+ /**
29
+ * Constructs a SchemaKey based on the information in the Schema XML.
30
+ * @param data The Schema XML as a string.
31
+ */
32
+ getSchemaKey(data: string): SchemaKey;
33
+ }
34
34
  //# sourceMappingURL=SchemaXmlFileLocater.d.ts.map
@@ -1,94 +1,94 @@
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.SchemaXmlFileLocater = void 0;
8
- const path = require("path");
9
- const xmldom_1 = require("@xmldom/xmldom");
10
- const ecschema_metadata_1 = require("@itwin/ecschema-metadata");
11
- const SchemaFileLocater_1 = require("./SchemaFileLocater");
12
- /** @packageDocumentation
13
- * @module Locaters
14
- */
15
- /**
16
- * A SchemaLocater implementation for locating XML Schema files
17
- * from the file system using configurable search paths.
18
- * @beta This is a workaround the current lack of a full xml parser.
19
- */
20
- class SchemaXmlFileLocater extends SchemaFileLocater_1.SchemaFileLocater {
21
- /**
22
- * Attempts to retrieve a Schema with the given SchemaKey by using the configured search paths
23
- * to locate the XML Schema file from the file system.
24
- * @param key The SchemaKey of the Schema to retrieve.
25
- * @param matchType The SchemaMatchType.
26
- * @param context The SchemaContext that will control the lifetime of the schema.
27
- */
28
- async getSchema(key, matchType, context) {
29
- const candidates = this.findEligibleSchemaKeys(key, matchType, "xml");
30
- if (0 === candidates.length)
31
- return undefined;
32
- // eslint-disable-next-line @typescript-eslint/unbound-method
33
- const maxCandidate = candidates.sort(this.compareSchemaKeyByVersion)[candidates.length - 1];
34
- const schemaPath = maxCandidate.fileName;
35
- // Load the file
36
- if (undefined === await this.fileExists(schemaPath))
37
- return undefined;
38
- const schemaText = await this.readUtf8FileToString(schemaPath);
39
- if (undefined === schemaText)
40
- return undefined;
41
- const parser = new xmldom_1.DOMParser();
42
- const document = parser.parseFromString(schemaText);
43
- this.addSchemaSearchPaths([path.dirname(schemaPath)]);
44
- const reader = new ecschema_metadata_1.SchemaReadHelper(ecschema_metadata_1.XmlParser, context);
45
- let schema = new ecschema_metadata_1.Schema(context);
46
- schema = await reader.readSchema(schema, document);
47
- return schema;
48
- }
49
- /**
50
- * Attempts to retrieve a Schema with the given SchemaKey by using the configured search paths
51
- * to locate the XML Schema file from the file system.
52
- * @param key The SchemaKey of the Schema to retrieve.
53
- * @param matchType The SchemaMatchType.
54
- * @param context The SchemaContext that will control the lifetime of the schema.
55
- */
56
- getSchemaSync(key, matchType, context) {
57
- const candidates = this.findEligibleSchemaKeys(key, matchType, "xml");
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 schemaPath = maxCandidate.fileName;
63
- // Load the file
64
- if (!this.fileExistsSync(schemaPath))
65
- return undefined;
66
- const schemaText = this.readUtf8FileToStringSync(schemaPath);
67
- if (!schemaText)
68
- return undefined;
69
- const parser = new xmldom_1.DOMParser();
70
- const document = parser.parseFromString(schemaText);
71
- this.addSchemaSearchPaths([path.dirname(schemaPath)]);
72
- const reader = new ecschema_metadata_1.SchemaReadHelper(ecschema_metadata_1.XmlParser, context);
73
- let schema = new ecschema_metadata_1.Schema(context);
74
- schema = reader.readSchemaSync(schema, document);
75
- return schema;
76
- }
77
- /**
78
- * Constructs a SchemaKey based on the information in the Schema XML.
79
- * @param data The Schema XML as a string.
80
- */
81
- getSchemaKey(data) {
82
- const matches = data.match(/<ECSchema ([^]+?)>/g);
83
- if (!matches || matches.length !== 1)
84
- throw new ecschema_metadata_1.ECObjectsError(ecschema_metadata_1.ECObjectsStatus.InvalidSchemaXML, `Could not find '<ECSchema>' tag in the given file`);
85
- const name = matches[0].match(/schemaName="(.+?)"/);
86
- const version = matches[0].match(/version="(.+?)"/);
87
- if (!name || name.length !== 2 || !version || version.length !== 2)
88
- throw new ecschema_metadata_1.ECObjectsError(ecschema_metadata_1.ECObjectsStatus.InvalidSchemaXML, `Could not find the ECSchema 'schemaName' or 'version' tag in the given file`);
89
- const key = new ecschema_metadata_1.SchemaKey(name[1], ecschema_metadata_1.ECVersion.fromString(version[1]));
90
- return key;
91
- }
92
- }
93
- exports.SchemaXmlFileLocater = SchemaXmlFileLocater;
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.SchemaXmlFileLocater = void 0;
8
+ const path = require("path");
9
+ const xmldom_1 = require("@xmldom/xmldom");
10
+ const ecschema_metadata_1 = require("@itwin/ecschema-metadata");
11
+ const SchemaFileLocater_1 = require("./SchemaFileLocater");
12
+ /** @packageDocumentation
13
+ * @module Locaters
14
+ */
15
+ /**
16
+ * A SchemaLocater implementation for locating XML Schema files
17
+ * from the file system using configurable search paths.
18
+ * @beta This is a workaround the current lack of a full xml parser.
19
+ */
20
+ class SchemaXmlFileLocater extends SchemaFileLocater_1.SchemaFileLocater {
21
+ /**
22
+ * Attempts to retrieve a Schema with the given SchemaKey by using the configured search paths
23
+ * to locate the XML Schema file from the file system.
24
+ * @param key The SchemaKey of the Schema to retrieve.
25
+ * @param matchType The SchemaMatchType.
26
+ * @param context The SchemaContext that will control the lifetime of the schema.
27
+ */
28
+ async getSchema(key, matchType, context) {
29
+ const candidates = this.findEligibleSchemaKeys(key, matchType, "xml");
30
+ if (0 === candidates.length)
31
+ return undefined;
32
+ // eslint-disable-next-line @typescript-eslint/unbound-method
33
+ const maxCandidate = candidates.sort(this.compareSchemaKeyByVersion)[candidates.length - 1];
34
+ const schemaPath = maxCandidate.fileName;
35
+ // Load the file
36
+ if (undefined === await this.fileExists(schemaPath))
37
+ return undefined;
38
+ const schemaText = await this.readUtf8FileToString(schemaPath);
39
+ if (undefined === schemaText)
40
+ return undefined;
41
+ const parser = new xmldom_1.DOMParser();
42
+ const document = parser.parseFromString(schemaText);
43
+ this.addSchemaSearchPaths([path.dirname(schemaPath)]);
44
+ const reader = new ecschema_metadata_1.SchemaReadHelper(ecschema_metadata_1.XmlParser, context);
45
+ let schema = new ecschema_metadata_1.Schema(context);
46
+ schema = await reader.readSchema(schema, document);
47
+ return schema;
48
+ }
49
+ /**
50
+ * Attempts to retrieve a Schema with the given SchemaKey by using the configured search paths
51
+ * to locate the XML Schema file from the file system.
52
+ * @param key The SchemaKey of the Schema to retrieve.
53
+ * @param matchType The SchemaMatchType.
54
+ * @param context The SchemaContext that will control the lifetime of the schema.
55
+ */
56
+ getSchemaSync(key, matchType, context) {
57
+ const candidates = this.findEligibleSchemaKeys(key, matchType, "xml");
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 schemaPath = maxCandidate.fileName;
63
+ // Load the file
64
+ if (!this.fileExistsSync(schemaPath))
65
+ return undefined;
66
+ const schemaText = this.readUtf8FileToStringSync(schemaPath);
67
+ if (!schemaText)
68
+ return undefined;
69
+ const parser = new xmldom_1.DOMParser();
70
+ const document = parser.parseFromString(schemaText);
71
+ this.addSchemaSearchPaths([path.dirname(schemaPath)]);
72
+ const reader = new ecschema_metadata_1.SchemaReadHelper(ecschema_metadata_1.XmlParser, context);
73
+ let schema = new ecschema_metadata_1.Schema(context);
74
+ schema = reader.readSchemaSync(schema, document);
75
+ return schema;
76
+ }
77
+ /**
78
+ * Constructs a SchemaKey based on the information in the Schema XML.
79
+ * @param data The Schema XML as a string.
80
+ */
81
+ getSchemaKey(data) {
82
+ const matches = data.match(/<ECSchema ([^]+?)>/g);
83
+ if (!matches || matches.length !== 1)
84
+ throw new ecschema_metadata_1.ECObjectsError(ecschema_metadata_1.ECObjectsStatus.InvalidSchemaXML, `Could not find '<ECSchema>' tag in the given file`);
85
+ const name = matches[0].match(/schemaName="(.+?)"/);
86
+ const version = matches[0].match(/version="(.+?)"/);
87
+ if (!name || name.length !== 2 || !version || version.length !== 2)
88
+ throw new ecschema_metadata_1.ECObjectsError(ecschema_metadata_1.ECObjectsStatus.InvalidSchemaXML, `Could not find the ECSchema 'schemaName' or 'version' tag in the given file`);
89
+ const key = new ecschema_metadata_1.SchemaKey(name[1], ecschema_metadata_1.ECVersion.fromString(version[1]));
90
+ return key;
91
+ }
92
+ }
93
+ exports.SchemaXmlFileLocater = SchemaXmlFileLocater;
94
94
  //# sourceMappingURL=SchemaXmlFileLocater.js.map