View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, 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      public static final String ACTION_CLASS_ATTRIBUTE = "actionClass";
49  
50      /**
51       * Called when the parser encounters an element matching a
52       * {@link ch.qos.logback.core.joran.spi.ElementSelector Pattern}.
53       */
54      public abstract void begin(InterpretationContext ic, String name, Attributes attributes) throws ActionException;
55  
56      /**
57       * Called to pass the body (as text) contained within an element.
58       * @param ic
59       * @param body
60       * @throws ActionException
61       */
62      public void body(InterpretationContext ic, String body) throws ActionException {
63          // NOP
64      }
65  
66      /*
67       * Called when the parser encounters an endElement event matching a {@link ch.qos.logback.core.joran.spi.Pattern
68       * Pattern}.
69       */
70      public abstract void end(InterpretationContext ic, String name) throws ActionException;
71  
72      public String toString() {
73          return this.getClass().getName();
74      }
75  
76      protected int getColumnNumber(InterpretationContext ic) {
77          Interpreter ji = ic.getJoranInterpreter();
78          Locator locator = ji.getLocator();
79          if (locator != null) {
80              return locator.getColumnNumber();
81          }
82          return -1;
83      }
84  
85      protected int getLineNumber(InterpretationContext ic) {
86          Interpreter ji = ic.getJoranInterpreter();
87          Locator locator = ji.getLocator();
88          if (locator != null) {
89              return locator.getLineNumber();
90          }
91          return -1;
92      }
93  
94      protected String getLineColStr(InterpretationContext ic) {
95          return "line: " + getLineNumber(ic) + ", column: " + getColumnNumber(ic);
96      }
97  }