/
ECSchemaXmlContext.ts
81 lines (70 loc) · 3.61 KB
/
ECSchemaXmlContext.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Schema
*/
import { assert } from "@itwin/core-bentley";
import { IModelError } from "@itwin/core-common";
import { IModelJsNative } from "@bentley/imodeljs-native";
import { IModelHost } from "./IModelHost";
/** @internal */
export type SchemaKey = IModelJsNative.ECSchemaXmlContext.SchemaKey;
/** @internal */
export type SchemaMatchType = IModelJsNative.ECSchemaXmlContext.SchemaMatchType;
/** Context used when deserializing a [Schema]($ecschema-metadata) from an XML file.
* A schema may contain references to other schemas, which may reside elsewhere on the local disk than the referencing schema.
* The context maintains a list of directories ("search paths") to search for referenced schemas. Directories can be appended to the list via [[addSchemaPath]].
* When a referenced schema needs to be located, the list of directories is searched in the order in which each was added.
* Once located, the schema is cached to avoid performing repeated lookups in the file system.
* @see [[readSchemaFromXmlFile]] to deserialize a schema.
* @beta
*/
export class ECSchemaXmlContext {
private _nativeContext: IModelJsNative.ECSchemaXmlContext | undefined;
/** Construct a context with an empty list of search paths. */
constructor() {
this._nativeContext = new IModelHost.platform.ECSchemaXmlContext();
}
/** @internal */
public get nativeContext(): IModelJsNative.ECSchemaXmlContext {
assert(undefined !== this._nativeContext);
return this._nativeContext;
}
/** Append a directory to the list of directories that will be searched to locate referenced schemas.
* The directories are searched in the order in which they were added to the list.
* @param searchPath The absolute path to the directory to search.
*/
public addSchemaPath(searchPath: string): void {
this.nativeContext.addSchemaPath(searchPath);
}
/** Set the last locater to be used when trying to find a schema
* @param locater Locater that should be used as the last locater when trying to find a schema
* @internal
*/
public setSchemaLocater(locater: IModelJsNative.ECSchemaXmlContext.SchemaLocaterCallback): void {
this.nativeContext.setSchemaLocater(locater);
}
/** Adds a schema locator to the beginning of the list of locators used to search for schemas.
* This schema locator will be prioritized over other locators when searching for schemas in the current context.
* @param locater Locater to add to the current context
* @internal
*/
public setFirstSchemaLocater(locater: IModelJsNative.ECSchemaXmlContext.SchemaLocaterCallback): void {
this.nativeContext.setFirstSchemaLocater(locater);
}
/** Deserialize a [Schema]($ecschema-metadata) from an ECSchemaXML-formatted file.
* @param filePath The absolute path of the XML file.
* @returns The JSON representation of the schema, as a [SchemaProps]($ecschema-metadata).
* @throws [[IModelError]] if there is a problem reading schema from the XML file
*/
public readSchemaFromXmlFile(filePath: string): any {
const response = this.nativeContext.readSchemaFromXmlFile(filePath);
if (response.error) {
throw new IModelError(response.error.status, response.error.message);
}
assert(undefined !== response.result);
return JSON.parse(response.result);
}
}