View Javadoc

1   /**
2    * Logback: the generic, reliable, fast and flexible logging framework for Java.
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 ch.qos.logback.core.joran.action;
12  
13  import org.xml.sax.Attributes;
14  import org.xml.sax.Locator;
15  
16  import ch.qos.logback.core.joran.spi.ActionException;
17  import ch.qos.logback.core.joran.spi.InterpretationContext;
18  import ch.qos.logback.core.joran.spi.Interpreter;
19  import ch.qos.logback.core.spi.ContextAwareBase;
20  
21  /**
22   * 
23   * Most of the work for configuring logback is done by Actions.
24   * 
25   * <p>Action methods are invoked as the XML file is parsed.
26   * 
27   * <p>This class is largely inspired from the relevant class in the
28   * commons-digester project of the Apache Software Foundation.
29   * 
30   * @author Craig McClanahan
31   * @author Christopher Lenz
32   * @author Ceki G&uuml;lc&uuml;
33   * 
34   */
35  public abstract class Action extends ContextAwareBase {
36  
37    public static final String NAME_ATTRIBUTE = "name";
38    public static final String VALUE_ATTRIBUTE = "value";
39    public static final String FILE_ATTRIBUTE = "file";
40    public static final String CLASS_ATTRIBUTE = "class";
41    public static final String PATTERN_ATTRIBUTE = "pattern";
42  
43    public static final String ACTION_CLASS_ATTRIBUTE = "actionClass";
44  
45    /**
46     * Called when the parser encounters an element matching a
47     * {@link ch.qos.logback.core.joran.spi.Pattern Pattern}.
48     */
49    public abstract void begin(InterpretationContext ec, String name,
50        Attributes attributes) throws ActionException;
51  
52    public void body(InterpretationContext ec, String body)
53        throws ActionException {
54      // NOP
55    }
56  
57    /*
58     * Called when the parser encounters an endElement event matching a
59     * {@link ch.qos.logback.core.joran.spi.Pattern Pattern}.
60     */
61    public abstract void end(InterpretationContext ec, String name)
62        throws ActionException;
63  
64    public String toString() {
65      return this.getClass().getName();
66    }
67  
68    protected int getColumnNumber(InterpretationContext ec) {
69      Interpreter jp = ec.getJoranInterpreter();
70      Locator locator = jp.getLocator();
71      if (locator != null) {
72        return locator.getColumnNumber();
73      }
74      return -1;
75    }
76  
77    protected int getLineNumber(InterpretationContext ec) {
78      Interpreter jp = ec.getJoranInterpreter();
79      Locator locator = jp.getLocator();
80      if (locator != null) {
81        return locator.getLineNumber();
82      }
83      return -1;
84    }
85  
86    protected String getLineColStr(InterpretationContext ec) {
87      String line = "line: " + getLineNumber(ec) + ", column: "
88          + getColumnNumber(ec);
89      return line;
90    }
91  }