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