001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.joran.action;
015
016import org.xml.sax.Attributes;
017import org.xml.sax.Locator;
018
019import ch.qos.logback.core.joran.spi.ActionException;
020import ch.qos.logback.core.joran.spi.SaxEventInterpretationContext;
021import ch.qos.logback.core.joran.spi.SaxEventInterpreter;
022import ch.qos.logback.core.spi.ContextAwareBase;
023
024/**
025 *
026 * Most of the work for configuring logback is done by Actions.
027 *
028 * <p>
029 * Action methods are invoked as the XML file is parsed.
030 *
031 * <p>
032 * This class is largely inspired from the relevant class in the
033 * commons-digester project of the Apache Software Foundation.
034 *
035 * @author Craig McClanahan
036 * @author Christopher Lenz
037 * @author Ceki G&uuml;lc&uuml;
038 *
039 */
040public abstract class Action extends ContextAwareBase {
041
042    public static final String NAME_ATTRIBUTE = "name";
043    public static final String KEY_ATTRIBUTE = "key";
044    public static final String VALUE_ATTRIBUTE = "value";
045    public static final String FILE_ATTRIBUTE = "file";
046    public static final String CLASS_ATTRIBUTE = "class";
047    public static final String PATTERN_ATTRIBUTE = "pattern";
048    public static final String SCOPE_ATTRIBUTE = "scope";
049
050    public static final String ACTION_CLASS_ATTRIBUTE = "actionClass";
051
052    /**
053     * Called when the parser encounters an element matching a
054     * {@link ch.qos.logback.core.joran.spi.ElementSelector Pattern}.
055     */
056    public abstract void begin(SaxEventInterpretationContext intercon, String name, Attributes attributes)
057            throws ActionException;
058
059    /**
060     * Called to pass the body (as text) contained within an element.
061     * 
062     * @param ic
063     * @param body
064     * @throws ActionException
065     */
066    public void body(SaxEventInterpretationContext intercon, String body) throws ActionException {
067        // NOP
068    }
069
070    /*
071     * Called when the parser encounters an endElement event matching a {@link
072     * ch.qos.logback.core.joran.spi.Pattern Pattern}.
073     */
074    public abstract void end(SaxEventInterpretationContext intercon, String name) throws ActionException;
075
076    public String toString() {
077        return this.getClass().getName();
078    }
079
080    protected int getColumnNumber(SaxEventInterpretationContext intercon) {
081        SaxEventInterpreter interpreter = intercon.getSaxEventInterpreter();
082        if (interpreter == null)
083            return -1;
084
085        Locator locator = interpreter.getLocator();
086        if (locator != null) {
087            return locator.getColumnNumber();
088        }
089        return -1;
090    }
091
092    // move to InterpretationContext
093    static public int getLineNumber(SaxEventInterpretationContext intercon) {
094        SaxEventInterpreter interpreter = intercon.getSaxEventInterpreter();
095        if (interpreter == null)
096            return -1;
097        Locator locator = interpreter.getLocator();
098        if (locator != null) {
099            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}