import {Rule} from "./Rule"; import {ObjectCreationFactory} from "./ObjectCreationFactory"; export class FactoryCreateRule extends Rule { private _constructor:any; private _ignoreCreateExceptions:boolean; constructor(constructor:Function, ignoreCreateExceptions = false) { super(); this._constructor = constructor; this._ignoreCreateExceptions = ignoreCreateExceptions; } begin(name:string, attributes:{[key:string]: any}):void { const instance = this.getFactory(attributes); if (instance) { this.digester.push(instance.createObject(attributes)); } else { if (this.digester.isDebugEnabled) { console.debug(`[FactoryCreateRule]{${this.digester.match} New null object`); } throw new Error(`FactoryCreateRule ${name} error!`); } } end(name:string):void { this.digester.pop(); if (this.digester.isDebugEnabled) { console.debug(`[FactoryCreateRule]{${this.digester.match} Pop ${this._constructor.name}`); } } private getFactory(attributes:{[key:string]: any}):ObjectCreationFactory { if (this._constructor) { if (this.digester.isDebugEnabled) { console.debug(`[FactoryCreateRule]{${this.digester.match} New factory ${this._constructor.name}`); } const factory:ObjectCreationFactory = new (this._constructor); if (factory) { factory.setDigester(this.digester); } return factory; } } }