@itwin/ecschema-metadata
Version:
ECObjects core concepts in typescript
56 lines • 2.55 kB
JavaScript
;
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.SchemaWalker = void 0;
const ECObjects_1 = require("../ECObjects");
const Class_1 = require("../Metadata/Class");
const SchemaPartVisitorDelegate_1 = require("../SchemaPartVisitorDelegate");
/**
* The purpose of this class is to traverse a given schema, allowing clients to hook into
* the traversal process via Visitors to allow for custom processing of the schema elements.
* @internal
*/
class SchemaWalker {
_visitorHelper;
// This is a cache of the schema we are traversing. The schema also exists within the _context but in order
// to not have to go back to the context every time we use this cache.
_schema;
/**
* Initializes a new SchemaWalker instance.
* @param visitor An ISchemaWalkerVisitor implementation whose methods will be called during schema traversal.
*/
constructor(visitor) {
this._visitorHelper = new SchemaPartVisitorDelegate_1.SchemaPartVisitorDelegate(visitor);
}
/**
* Traverses the given Schema, calling ISchemaWalkerVisitor methods along the way.
* @param schema The Schema to traverse.
*/
async traverseSchema(schema) {
this._schema = schema;
await this._visitorHelper.visitSchema(schema);
await this._visitorHelper.visitSchemaPart(schema);
for (const item of this._schema.getItems())
await this.traverseSchemaItem(item);
return schema;
}
async traverseSchemaItem(schemaItem) {
await this._visitorHelper.visitSchemaPart(schemaItem);
if (Class_1.ECClass.isECClass(schemaItem))
await this.traverseClass(schemaItem);
}
async traverseClass(ecClass) {
for (const property of await ecClass.getProperties(true)) {
await this._visitorHelper.visitSchemaPart(property);
}
if (ecClass.schemaItemType === ECObjects_1.SchemaItemType.RelationshipClass) {
await this._visitorHelper.visitSchemaPart(ecClass.source);
await this._visitorHelper.visitSchemaPart(ecClass.target);
}
}
}
exports.SchemaWalker = SchemaWalker;
//# sourceMappingURL=SchemaWalker.js.map