001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2022, 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.classic.model.util;
015
016import java.util.List;
017
018import ch.qos.logback.classic.model.processor.LogbackClassicDefaultNestedComponentRules;
019import ch.qos.logback.core.joran.util.ParentTag_Tag_Class_Tuple;
020import ch.qos.logback.core.model.ImplicitModel;
021import ch.qos.logback.core.model.Model;
022import ch.qos.logback.core.model.util.TagUtil;
023
024/**
025 * Injects missing class names into ImplicitModel instances missing class name.
026 * 
027 * @author ceki
028 * @since 1.3.0-alpha15
029 */
030public class DefaultClassNameHelper {
031
032    List<ParentTag_Tag_Class_Tuple> tupleList = LogbackClassicDefaultNestedComponentRules.TUPLES_LIST;
033
034    /**
035     * This method injects default components classes to implicit models missing a
036     * class name.
037     * 
038     * @param tupleList
039     * @param aModel
040     * @param parent
041     */
042    public void injectDefaultComponentClasses(Model aModel, Model parent) {
043
044        applyInjectionRules(aModel, parent);
045
046        for (Model sub : aModel.getSubModels()) {
047            injectDefaultComponentClasses(sub, aModel);
048        }
049    }
050
051    private void applyInjectionRules(Model aModel, Model parent) {
052        if (parent == null)
053            return;
054
055        String parentTag = TagUtil.unifiedTag(parent);
056        String modelTag = TagUtil.unifiedTag(aModel);
057
058        if (aModel instanceof ImplicitModel) {
059            ImplicitModel implicitModel = (ImplicitModel) aModel;
060            String className = implicitModel.getClassName();
061
062            if (className == null || className.isEmpty()) {
063                for (ParentTag_Tag_Class_Tuple ruleTuple : tupleList) {
064                    if (ruleTuple.parentTag.equals(parentTag) && ruleTuple.tag.equals(modelTag)) {
065                        implicitModel.setClassName(ruleTuple.className);
066                        break;
067                    }
068                }
069
070            }
071        }
072    }
073}