1 /**
2 * Logback: the reliable, fast and flexible logging library for Java.
3 *
4 * Copyright (C) 1999-2006, QOS.ch
5 *
6 * This library is free software, you can redistribute it and/or modify it under
7 * the terms of the GNU Lesser General Public License as published by the Free
8 * Software Foundation.
9 */
10
11 package chapter3.calculator;
12
13
14
15 import org.xml.sax.Attributes;
16
17 import ch.qos.logback.core.joran.action.Action;
18 import ch.qos.logback.core.joran.spi.InterpretationContext;
19 import ch.qos.logback.core.util.OptionHelper;
20
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 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(
55 "The computation named [" + nameStr + "] resulted in the value " + i);
56 }
57 }
58 }