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 java.util.Stack;
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   * ComputationAction2 will print the result of the compuration made by 
26   * children elements but only if the computation itself is named, that is if the
27   * name attribute of the associated computation element is not null. In other
28   * words, anonymous computations will not print their result.
29   * 
30   * ComputationAction2 differs from ComputationAction1 in its handling of
31   * instance variables. ComputationAction1 has a simple <Code>nameStr</code>
32   * instance variable. This variable is set when the begin() method is called 
33   * and then later used within the end() method. 
34   * 
35   * This simple approach works properly if the begin() and end()
36   * method of a given action are expected to be called in sequence. However,
37   * there are situations where the begin() method of the same action instance is 
38   * invoked multiple times before the matching end() method is invoked. 
39   * 
40   * When this happens, the second call to begin() overwrites values set by
41   * the first invocation to begin(). The solution is to save parameter values 
42   * into a separate stack. The well-formedness of XML will guarantee that a value
43   * saved by one begin() will be consumed only by the matching end() method.
44   * 
45   * Note that in the vast majority of cases there is no need to resort to a 
46   * separate stack for each variable. The situation of successive begin() 
47   * invocations can only occur if: 
48   * 
49   * 1) the associated pattern contains a wildcard, i.e. the &#42; character
50   * 
51   * and
52   * 
53   * 2) the associated element tag can contain itself as a child 
54   *  
55   * For example, "&#42;/computation" pattern means that computations can contain
56   * other computation elements as children. 
57   * 
58   * @author Ceki G&uuml;lc&uuml;
59   */
60  public class ComputationAction2 extends Action {
61      public static final String NAME_ATR = "name";
62  
63      Stack<String> nameStrStack = new Stack<String>();
64  
65      public void begin(InterpretationContext ec, String name, Attributes attributes) {
66          String nameStr = attributes.getValue(NAME_ATR);
67          // save nameStr value in a special stack. Note that the value is saved
68          // even if it is empty or null.
69          nameStrStack.push(nameStr);
70      }
71  
72      public void end(InterpretationContext ec, String name) {
73          // pop nameStr value from the special stack
74          String nameStr = (String) nameStrStack.pop();
75  
76          if (OptionHelper.isEmpty(nameStr)) {
77              // nothing to do
78          } else {
79              Integer i = (Integer) ec.peekObject();
80              System.out.println("The computation named [" + nameStr + "] resulted in the value " + i);
81          }
82      }
83  }