View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2022, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.model.util;
15  
16  import java.util.List;
17  
18  import ch.qos.logback.classic.model.processor.LogbackClassicDefaultNestedComponentRules;
19  import ch.qos.logback.core.joran.util.ParentTag_Tag_Class_Tuple;
20  import ch.qos.logback.core.model.ImplicitModel;
21  import ch.qos.logback.core.model.Model;
22  import ch.qos.logback.core.model.util.TagUtil;
23  
24  /**
25   * Injects missing class names into ImplicitModel instances missing class name.
26   * 
27   * @author ceki
28   * @since 1.3.0-alpha15
29   */
30  public class DefaultClassNameHelper {
31  
32      List<ParentTag_Tag_Class_Tuple> tupleList = LogbackClassicDefaultNestedComponentRules.TUPLES_LIST;
33  
34      /**
35       * This method injects default components classes to implicit models missing a
36       * class name.
37       * 
38       * @param tupleList
39       * @param aModel
40       * @param parent
41       */
42      public void injectDefaultComponentClasses(Model aModel, Model parent) {
43  
44          applyInjectionRules(aModel, parent);
45  
46          for (Model sub : aModel.getSubModels()) {
47              injectDefaultComponentClasses(sub, aModel);
48          }
49      }
50  
51      private void applyInjectionRules(Model aModel, Model parent) {
52          if (parent == null)
53              return;
54  
55          String parentTag = TagUtil.unifiedTag(parent);
56          String modelTag = TagUtil.unifiedTag(aModel);
57  
58          if (aModel instanceof ImplicitModel) {
59              ImplicitModel implicitModel = (ImplicitModel) aModel;
60              String className = implicitModel.getClassName();
61  
62              if (className == null || className.isEmpty()) {
63                  for (ParentTag_Tag_Class_Tuple ruleTuple : tupleList) {
64                      if (ruleTuple.parentTag.equals(parentTag) && ruleTuple.tag.equals(modelTag)) {
65                          implicitModel.setClassName(ruleTuple.className);
66                          break;
67                      }
68                  }
69  
70              }
71          }
72      }
73  }