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