1  /*
     2   * Copyright the original author or authors.
     3   * 
     4   * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   * 
     8   *      http://www.mozilla.org/MPL/MPL-1.1.html
     9   * 
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  import org.as2lib.aop.advice.AbstractAdvice;
    18  import org.as2lib.aop.Aspect;
    19  import org.as2lib.aop.aspect.AbstractAspect;
    20  import org.as2lib.aop.JoinPoint;
    21  import org.as2lib.env.except.AbstractOperationException;
    22  
    23  /**
    24   * @author Simon Wacker
    25   */
    26  class org.as2lib.aop.aspect.IndentedLoggingAspect extends AbstractAspect implements Aspect {
    27  	
    28  	private var indentationLevel:Number = -1;
    29  	
    30  	private function IndentedLoggingAspect(Void) {
    31  		addAdvice(AbstractAdvice.AROUND, getLoggingMethodsPointcut(), aroundLoggingMethodsAdvice);
    32  		addAdvice(AbstractAdvice.BEFORE, getLoggedMethodsPointcut(), beforeLoggedMethodsAdvice);
    33  		addAdvice(AbstractAdvice.AFTER, getLoggedMethodsPointcut(), afterLoggedMethodsAdvice);
    34  	}
    35  	
    36  	private function aroundLoggingMethodsAdvice(joinPoint:JoinPoint, args:Array) {
    37  		var spaces:String = "";
    38  		for (var i:Number = 0; i < indentationLevel; i++) {
    39  			spaces += "  ";
    40  		}
    41  		args[0] = spaces + String(args[0]);
    42  		return joinPoint.proceed(args);
    43  	}
    44  	
    45  	private function beforeLoggedMethodsAdvice(joinPoint:JoinPoint, args:Array):Void {
    46  		indentationLevel++;
    47  	}
    48  	
    49  	private function afterLoggedMethodsAdvice(joinPoint:JoinPoint):Void {
    50  		indentationLevel--;
    51  	}
    52  	
    53  	private function getLoggedMethodsPointcut(Void):String {
    54  		throw new AbstractOperationException("This operation is marked as abstract and must be overridden by a concrete subclasses.", this, arguments);
    55  		return null;
    56  	}
    57  	
    58  	private function getLoggingMethodsPointcut(Void):String {
    59  		throw new AbstractOperationException("This operation is marked as abstract and must be overridden by a concrete subclasses.", this, arguments);
    60  		return null;
    61  	}
    62  	
    63  }