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