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