001/*
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2024, 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 */
014
015package ch.qos.logback.core.model.processor;
016
017import ch.qos.logback.core.Context;
018import ch.qos.logback.core.CoreConstants;
019import ch.qos.logback.core.model.ConversionRuleModel;
020import ch.qos.logback.core.model.Model;
021import ch.qos.logback.core.pattern.DynamicConverter;
022import ch.qos.logback.core.pattern.color.ConverterSupplierByClassName;
023import ch.qos.logback.core.util.OptionHelper;
024
025import java.util.HashMap;
026import java.util.Map;
027import java.util.function.Supplier;
028
029public class ConversionRuleModelHandler extends ModelHandlerBase {
030
031    private boolean inError;
032
033    public ConversionRuleModelHandler(Context context) {
034        super(context);
035    }
036
037    static public ConversionRuleModelHandler makeInstance(Context context, ModelInterpretationContext mic) {
038        return new ConversionRuleModelHandler(context);
039    }
040
041    @Override
042    public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
043
044        ConversionRuleModel conversionRuleModel = (ConversionRuleModel) model;
045        String converterClass = conversionRuleModel.getClassName();
046
047        if (OptionHelper.isNullOrEmptyOrAllSpaces(converterClass)) {
048            addWarn("Missing className. This should have been caught earlier.");
049            inError = true;
050            return;
051        } else {
052            converterClass = mic.getImport(converterClass);
053        }
054
055        String conversionWord = conversionRuleModel.getConversionWord();
056
057
058        try {
059            Map<String, Supplier<DynamicConverter>> ruleRegistry = (Map<String, Supplier<DynamicConverter>>) context
060                    .getObject(CoreConstants.PATTERN_RULE_REGISTRY_FOR_SUPPLIERS);
061            if (ruleRegistry == null) {
062                ruleRegistry = new HashMap<>();
063                context.putObject(CoreConstants.PATTERN_RULE_REGISTRY_FOR_SUPPLIERS, ruleRegistry);
064            }
065            // put the new rule into the rule registry
066            addInfo("registering conversion word " + conversionWord + " with class [" + converterClass + "]");
067            ConverterSupplierByClassName converterSupplierByClassName = new ConverterSupplierByClassName(conversionWord, converterClass);
068            converterSupplierByClassName.setContext(getContext());
069            ruleRegistry.put(conversionWord, converterSupplierByClassName);
070        } catch (Exception oops) {
071            inError = true;
072            String errorMsg = "Could not add conversion rule to PatternLayout.";
073            addError(errorMsg);
074        }
075
076
077
078    }
079}