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