View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package chapters.onJoran.calculator;
15  
16  import org.xml.sax.Attributes;
17  
18  import ch.qos.logback.core.joran.action.Action;
19  import ch.qos.logback.core.joran.spi.InterpretationContext;
20  import ch.qos.logback.core.util.OptionHelper;
21  
22  /**
23   * ComputationAction1 will print the result of the compuration made by 
24   * children elements but only if the compuration itself is named, that is if the
25   * name attribute of the associated computation element is not null. In other
26   * words, anonymous computations will not print their result.
27   * 
28   * @author Ceki Gülcü
29   */
30  public class ComputationAction1 extends Action {
31      public static final String NAME_ATR = "name";
32  
33      String nameStr;
34  
35      /**
36       * Store the value of the name attribute for future use.
37       */
38      public void begin(InterpretationContext ec, String name, Attributes attributes) {
39          nameStr = attributes.getValue(NAME_ATR);
40      }
41  
42      /**
43       * Children elements have been processed. The sesults should be an integer 
44       * placed at the top of the execution stack.
45       * 
46       * This value will be printed on the console but only if the action is 
47       * named. Anonymous computation will not print their result.
48       */
49      public void end(InterpretationContext ec, String name) {
50          if (OptionHelper.isEmpty(nameStr)) {
51              // nothing to do
52          } else {
53              Integer i = (Integer) ec.peekObject();
54              System.out.println("The computation named [" + nameStr + "] resulted in the value " + i);
55          }
56      }
57  }