1
2
3
4
5
6
7
8
9
10
11 package chapter3.calculator;
12
13
14
15 import java.util.EmptyStackException;
16
17 import org.xml.sax.Attributes;
18
19 import ch.qos.logback.core.joran.action.Action;
20 import ch.qos.logback.core.joran.spi.InterpretationContext;
21
22
23
24
25
26
27
28
29 public class AddAction extends Action {
30
31 public void begin(InterpretationContext ec, String name, Attributes attributes) {
32 int first = fetchInteger(ec);
33 int second = fetchInteger(ec);
34
35 ec.pushObject(new Integer(first + second));
36 }
37
38
39
40
41
42 int fetchInteger(InterpretationContext ec) {
43 int result = 0;
44
45 try {
46
47 Object o1 = ec.popObject();
48
49 if (o1 instanceof Integer) {
50 result = ((Integer) o1).intValue();
51 } else {
52 String errMsg =
53 "Object [" + o1
54 + "] currently at the top of the stack is not an integer.";
55 ec.addError(errMsg);
56 throw new IllegalArgumentException(errMsg);
57 }
58 } catch (EmptyStackException ese) {
59 ec.addError(("Expecting an integer on the execution stack."));
60 throw ese;
61 }
62 return result;
63 }
64
65 public void end(InterpretationContext ec, String name) {
66
67
68
69
70 }
71 }