1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, 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 v2.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  
15  package ch.qos.logback.classic.joran.sanity;
16  
17  import ch.qos.logback.classic.model.LoggerModel;
18  import ch.qos.logback.classic.model.RootLoggerModel;
19  import ch.qos.logback.core.joran.sanity.Pair;
20  import ch.qos.logback.core.joran.sanity.SanityChecker;
21  import ch.qos.logback.core.model.AppenderModel;
22  import ch.qos.logback.core.model.Model;
23  import ch.qos.logback.core.model.conditional.IfModel;
24  import ch.qos.logback.core.spi.ContextAwareBase;
25  
26  import java.util.ArrayList;
27  import java.util.List;
28  
29  import static ch.qos.logback.core.CoreConstants.CODES_URL;
30  
31  public class IfNestedWithinSecondPhaseElementSC extends ContextAwareBase implements SanityChecker {
32  
33      static final public String NESTED_IF_WARNING_URL = CODES_URL+ "#nested_if_element";
34  
35      @Override
36      public void check(Model model) {
37          if (model == null)
38              return;
39  
40          List<Model> secondPhaseModels = new ArrayList<>();
41          deepFindAllModelsOfType(AppenderModel.class, secondPhaseModels, model);
42          deepFindAllModelsOfType(LoggerModel.class, secondPhaseModels, model);
43          deepFindAllModelsOfType(RootLoggerModel.class, secondPhaseModels, model);
44  
45          List<Pair<Model, Model>> nestedPairs = deepFindNestedSubModelsOfType(IfModel.class, secondPhaseModels);
46  
47          if (nestedPairs.isEmpty())
48              return;
49  
50          addWarn("<if> elements cannot be nested within an <appender>, <logger> or <root> element");
51          addWarn("See also " + NESTED_IF_WARNING_URL);
52          for (Pair<Model, Model> pair : nestedPairs) {
53              Model p = pair.first;
54              int pLine = p.getLineNumber();
55              Model s = pair.second;
56              int sLine = s.getLineNumber();
57              addWarn("Element <"+p.getTag()+"> at line " + pLine + " contains a nested <"+s.getTag()+"> element at line " +sLine);
58          }
59      }
60  
61      @Override
62      public String toString() {
63          return "IfNestedWithinSecondPhaseElementSC";
64      }
65  }