View Javadoc
1   package ch.qos.logback.classic.joran.sanity;
2   
3   import ch.qos.logback.classic.model.LoggerModel;
4   import ch.qos.logback.classic.model.RootLoggerModel;
5   import ch.qos.logback.core.joran.sanity.Pair;
6   import ch.qos.logback.core.joran.sanity.SanityChecker;
7   import ch.qos.logback.core.model.AppenderModel;
8   import ch.qos.logback.core.model.Model;
9   import ch.qos.logback.core.model.conditional.IfModel;
10  import ch.qos.logback.core.spi.ContextAwareBase;
11  
12  import java.util.ArrayList;
13  import java.util.List;
14  
15  import static ch.qos.logback.core.CoreConstants.CODES_URL;
16  
17  public class IfNestedWithinSecondPhaseElementSC extends ContextAwareBase implements SanityChecker {
18  
19      static final public String NESTED_IF_WARNING_URL = CODES_URL+ "#nested_if_element";
20  
21      @Override
22      public void check(Model model) {
23          if (model == null)
24              return;
25  
26          List<Model> secondPhaseModels = new ArrayList<>();
27          deepFindAllModelsOfType(AppenderModel.class, secondPhaseModels, model);
28          deepFindAllModelsOfType(LoggerModel.class, secondPhaseModels, model);
29          deepFindAllModelsOfType(RootLoggerModel.class, secondPhaseModels, model);
30  
31          List<Pair<Model, Model>> nestedPairs = deepFindNestedSubModelsOfType(IfModel.class, secondPhaseModels);
32  
33          if (nestedPairs.isEmpty())
34              return;
35  
36          addWarn("<if> elements cannot be nested within an <appender>, <logger> or <root> element");
37          addWarn("See also " + NESTED_IF_WARNING_URL);
38          for (Pair<Model, Model> pair : nestedPairs) {
39              Model p = pair.first;
40              int pLine = p.getLineNumber();
41              Model s = pair.second;
42              int sLine = s.getLineNumber();
43              addWarn("Element <"+p.getTag()+"> at line " + pLine + " contains a nested <"+s.getTag()+"> element at line " +sLine);
44          }
45      }
46  
47      @Override
48      public String toString() {
49          return "IfNestedWithinSecondPhaseElementSC";
50      }
51  }