View Javadoc

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