View Javadoc

1   /**
2    * Logback: the reliable, fast and flexible logging library for Java.
3    * 
4    * Copyright (C) 1999-2006, 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 chapter3.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   *
21   * This action converts the value attribute of the associated element to
22   * an integer and pushes the resulting Integer object on top of the execution
23   * context stack.
24   *
25   * It also illustrates usage of Joran's error handling paradigm.
26   *
27   * @author Ceki Gülcü
28   */
29  public class LiteralAction extends Action {
30    public static String VALUE_ATR = "value";
31  
32    public void begin(InterpretationContext ec, String name, Attributes attributes) {
33      String valueStr = attributes.getValue(VALUE_ATR);
34  
35      if (OptionHelper.isEmpty(valueStr)) {
36        ec.addError("The literal action requires a value attribute");
37        return;
38      }
39  
40      try {
41        Integer i = Integer.valueOf(valueStr);
42        ec.pushObject(i);
43      } catch (NumberFormatException nfe) {
44        ec.addError("The value [" + valueStr + "] could not be converted to an Integer",
45            nfe);
46        throw nfe;
47      }
48    }
49  
50    public void end(InterpretationContext ec, String name) {
51      // Nothing to do here.
52      // In general, the end() method of actions associated with elements
53      // having no children do not need to perform any processing in their
54      // end() method.
55    }
56  }