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;
017
018import ch.qos.logback.core.joran.spi.SaxEventInterpretationContext;
019import ch.qos.logback.core.joran.spi.ElementSelector;
020import ch.qos.logback.core.util.OptionHelper;
021
022public class NewRuleAction extends Action {
023    boolean inError = false;
024
025    /**
026     * Instantiates a layout of the given class and sets its name.
027     */
028    public void begin(SaxEventInterpretationContext ec, String localName, Attributes attributes) {
029        // Let us forget about previous errors (in this object)
030        inError = false;
031        String errorMsg;
032        String pattern = attributes.getValue(Action.PATTERN_ATTRIBUTE);
033        String actionClass = attributes.getValue(Action.ACTION_CLASS_ATTRIBUTE);
034
035        if (OptionHelper.isNullOrEmptyOrAllSpaces(pattern)) {
036            inError = true;
037            errorMsg = "No 'pattern' attribute in <newRule>";
038            addError(errorMsg);
039            return;
040        }
041
042        if (OptionHelper.isNullOrEmptyOrAllSpaces(actionClass)) {
043            inError = true;
044            errorMsg = "No 'actionClass' attribute in <newRule>";
045            addError(errorMsg);
046            return;
047        }
048
049        try {
050            addInfo("About to add new Joran parsing rule [" + pattern + "," + actionClass + "].");
051            ec.getSaxEventInterpreter().getRuleStore().addRule(new ElementSelector(pattern), actionClass);
052        } catch (Exception oops) {
053            inError = true;
054            errorMsg = "Could not add new Joran parsing rule [" + pattern + "," + actionClass + "]";
055            addError(errorMsg);
056        }
057    }
058
059    /**
060     * Once the children elements are also parsed, now is the time to activate the
061     * appender options.
062     */
063    public void end(SaxEventInterpretationContext ec, String n) {
064    }
065
066    public void finish(SaxEventInterpretationContext ec) {
067    }
068}