View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, 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  
17  
18  import org.xml.sax.Attributes;
19  
20  import ch.qos.logback.core.joran.action.Action;
21  import ch.qos.logback.core.joran.spi.InterpretationContext;
22  import ch.qos.logback.core.util.OptionHelper;
23  
24  
25  /**
26   * ComputationAction1 will print the result of the compuration made by 
27   * children elements but only if the compuration itself is named, that is if the
28   * name attribute of the associated computation element is not null. In other
29   * words, anonymous computations will not print their result.
30   * 
31   * @author Ceki Gülcü
32   */
33  public class ComputationAction1 extends Action {
34    public static String NAME_ATR = "name";
35  
36    String nameStr;
37  
38    /**
39     * Store the value of the name attribute for future use.
40     */
41    public void begin(InterpretationContext ec, String name, Attributes attributes) {
42      nameStr = attributes.getValue(NAME_ATR);
43    }
44  
45    /**
46     * Children elements have been processed. The sesults should be an integer 
47     * placed at the top of the execution stack.
48     * 
49     * This value will be printed on the console but only if the action is 
50     * named. Anonymous computation will not print their result.
51     */
52    public void end(InterpretationContext ec, String name) {
53      if (OptionHelper.isEmpty(nameStr)) {
54        // nothing to do
55      } else {
56        Integer i = (Integer) ec.peekObject();
57        System.out.println(
58          "The computation named [" + nameStr + "] resulted in the value " + i);
59      }
60    }
61  }