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.core.model.processor.conditional;
15  
16  import ch.qos.logback.core.Context;
17  import ch.qos.logback.core.model.Model;
18  import ch.qos.logback.core.model.conditional.ElseModel;
19  import ch.qos.logback.core.model.conditional.IfModel;
20  import ch.qos.logback.core.model.conditional.IfModel.BranchState;
21  import ch.qos.logback.core.model.processor.ModelHandlerBase;
22  import ch.qos.logback.core.model.processor.ModelHandlerException;
23  import ch.qos.logback.core.model.processor.ModelInterpretationContext;
24  
25  public class ElseModelHandler extends ModelHandlerBase {
26  
27      public ElseModelHandler(Context context) {
28          super(context);
29      }
30  
31      static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext ic) {
32          return new ElseModelHandler(context);
33      }
34  
35      @Override
36      protected Class<ElseModel> getSupportedModelClass() {
37          return ElseModel.class;
38      }
39  
40      @Override
41      public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
42  
43          ElseModel elseModel = (ElseModel) model;
44  
45          Model parent = mic.peekModel();
46  
47          if (!(parent instanceof IfModel)) {
48              addError("Unexpected type for parent model [" + parent + "]");
49              elseModel.markAsSkipped();
50              return;
51          }
52                
53          IfModel ifModel = (IfModel) parent;
54          if(ifModel.getBranchState() != BranchState.ELSE_BRANCH) {
55              elseModel.deepMarkAsSkipped();
56          }
57      }
58  
59  }