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 java.util.HashMap;
017import java.util.Map;
018
019import org.xml.sax.Attributes;
020
021import ch.qos.logback.core.CoreConstants;
022import ch.qos.logback.core.joran.spi.InterpretationContext;
023import ch.qos.logback.core.util.OptionHelper;
024
025public class ConversionRuleAction extends Action {
026    boolean inError = false;
027
028    /**
029     * Instantiates an layout of the given class and sets its name.
030     *
031     */
032    @SuppressWarnings("unchecked")
033    public void begin(InterpretationContext ec, String localName, Attributes attributes) {
034        // Let us forget about previous errors (in this object)
035        inError = false;
036
037        String errorMsg;
038        String conversionWord = attributes.getValue(ActionConst.CONVERSION_WORD_ATTRIBUTE);
039        String converterClass = attributes.getValue(ActionConst.CONVERTER_CLASS_ATTRIBUTE);
040
041        if (OptionHelper.isEmpty(conversionWord)) {
042            inError = true;
043            errorMsg = "No 'conversionWord' attribute in <conversionRule>";
044            addError(errorMsg);
045
046            return;
047        }
048
049        if (OptionHelper.isEmpty(converterClass)) {
050            inError = true;
051            errorMsg = "No 'converterClass' attribute in <conversionRule>";
052            ec.addError(errorMsg);
053
054            return;
055        }
056
057        try {
058            Map<String, String> ruleRegistry = (Map<String, String>) context.getObject(CoreConstants.PATTERN_RULE_REGISTRY);
059            if (ruleRegistry == null) {
060                ruleRegistry = new HashMap<String, String>();
061                context.putObject(CoreConstants.PATTERN_RULE_REGISTRY, ruleRegistry);
062            }
063            // put the new rule into the rule registry
064            addInfo("registering conversion word " + conversionWord + " with class [" + converterClass + "]");
065            ruleRegistry.put(conversionWord, converterClass);
066        } catch (Exception oops) {
067            inError = true;
068            errorMsg = "Could not add conversion rule to PatternLayout.";
069            addError(errorMsg);
070        }
071    }
072
073    /**
074     * Once the children elements are also parsed, now is the time to activate
075     * the appender options.
076     */
077    public void end(InterpretationContext ec, String n) {
078    }
079
080    public void finish(InterpretationContext ec) {
081    }
082}