View Javadoc

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