import {Rule} from "./Rule"; import {StringBuffer} from "../../lang/StringBuffer"; export class CallMethodRule extends Rule { private _bodyText:string; /** * The method name to call on the parent object. */ private _methodName:string; /** * Construct a "set next" rule with the specified method name. * * @param methodName Method name of the parent method to call */ constructor(methodName:string) { super(); this._methodName = methodName; } begin(name:string, attributes:{[key:string]: any}):void { } body(name:string, text:string):void { this._bodyText = text.trim(); } end(name:string):void { const target = this.digester.peek(); if (! target) { const sb = new StringBuffer(); sb.append("[CallMethodRule]{"); sb.append(this.digester.match); sb.append("} Call target is null"); throw new Error(sb.toString()); } if (this.digester.isDebugEnabled) { const sb = new StringBuffer("[CallMethodRule]{"); sb.append(this.digester.match); sb.append("} Call "); sb.append(target.constructor.name); sb.append("."); sb.append(this._methodName); sb.append("("); sb.append(this._bodyText); sb.append(")"); console.debug(sb.toString()); } const invoker = target.constructor.prototype[this._methodName]; if (typeof(invoker) !== 'function') { throw new Error(`${target.constructor.name} no method named "${this._methodName}"`); } invoker.call(target, this._bodyText); } finish():void { this._bodyText = null; } }