1 /**
2 * Logback: the reliable, generic, fast and flexible logging framework.
3 * Copyright (C) 1999-2011, QOS.ch. All rights reserved.
4 *
5 * This program and the accompanying materials are dual-licensed under
6 * either the terms of the Eclipse Public License v1.0 as published by
7 * the Eclipse Foundation
8 *
9 * or (per the licensee's choosing)
10 *
11 * under the terms of the GNU Lesser General Public License version 2.1
12 * as published by the Free Software Foundation.
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 import ch.qos.logback.core.util.OptionHelper;
21
22 /**
23 * This action converts the value attribute of the associated element to an
24 * integer and pushes the resulting Integer object on top of the execution
25 * context stack.
26 *
27 * <p>It also illustrates usage of Joran's error reporting/handling paradigm.
28 *
29 * @author Ceki Gülcü
30 */
31 public class LiteralAction extends Action {
32 public static String VALUE_ATR = "value";
33
34 public void begin(InterpretationContext ic, String name, Attributes attributes) {
35 String valueStr = attributes.getValue(VALUE_ATR);
36
37 if (OptionHelper.isEmpty(valueStr)) {
38 ic.addError("The literal action requires a value attribute");
39 return;
40 }
41
42 try {
43 Integer i = Integer.valueOf(valueStr);
44 ic.pushObject(i);
45 } catch (NumberFormatException nfe) {
46 ic.addError("The value [" + valueStr
47 + "] could not be converted to an Integer", nfe);
48 throw nfe;
49 }
50 }
51
52 public void end(InterpretationContext ic, String name) {
53 // Nothing to do here.
54 // In general, the end() method of actions associated with elements
55 // having no children do not need to perform any processing in their
56 // end() method.
57 }
58 }