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.IfModel;
19  import ch.qos.logback.core.model.conditional.IfModel.BranchState;
20  import ch.qos.logback.core.model.conditional.ThenModel;
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  import static ch.qos.logback.core.spi.ErrorCodes.MISSING_IF_EMPTY_MODEL_STACK;
26  
27  public class ThenModelHandler extends ModelHandlerBase {
28  
29      public ThenModelHandler(Context context) {
30          super(context);
31      }
32  
33      static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext ic) {
34          return new ThenModelHandler(context);
35      }
36  
37      @Override
38      protected Class<ThenModel> getSupportedModelClass() {
39          return ThenModel.class;
40      }
41  
42      @Override
43      public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
44  
45          ThenModel thenModel = (ThenModel) model;
46  
47          if(mic.isModelStackEmpty()) {
48              addError(MISSING_IF_EMPTY_MODEL_STACK);
49              thenModel.markAsSkipped();
50              return;
51          }
52          Model parent = mic.peekModel();
53  
54          if (!(parent instanceof IfModel)) {
55              addError("Unexpected type for parent model [" + parent + "]");
56              thenModel.markAsSkipped();
57              return;
58          }
59                
60          IfModel ifModel = (IfModel) parent;
61          if(ifModel.getBranchState() != BranchState.IF_BRANCH) {
62              thenModel.deepMarkAsSkipped();
63          }
64      }
65  
66  }