001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2022, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.model.processor.conditional;
015
016import ch.qos.logback.core.Context;
017import ch.qos.logback.core.model.Model;
018import ch.qos.logback.core.model.conditional.IfModel;
019import ch.qos.logback.core.model.conditional.IfModel.BranchState;
020import ch.qos.logback.core.model.conditional.ThenModel;
021import ch.qos.logback.core.model.processor.ModelHandlerBase;
022import ch.qos.logback.core.model.processor.ModelHandlerException;
023import ch.qos.logback.core.model.processor.ModelInterpretationContext;
024
025import static ch.qos.logback.core.spi.ErrorCodes.MISSING_IF_EMPTY_MODEL_STACK;
026
027public class ThenModelHandler extends ModelHandlerBase {
028
029    public ThenModelHandler(Context context) {
030        super(context);
031    }
032
033    static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext ic) {
034        return new ThenModelHandler(context);
035    }
036
037    @Override
038    protected Class<ThenModel> getSupportedModelClass() {
039        return ThenModel.class;
040    }
041
042    @Override
043    public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
044
045        ThenModel thenModel = (ThenModel) model;
046
047        if(mic.isModelStackEmpty()) {
048            addError(MISSING_IF_EMPTY_MODEL_STACK);
049            thenModel.markAsSkipped();
050            return;
051        }
052        Model parent = mic.peekModel();
053
054        if (!(parent instanceof IfModel)) {
055            addError("Unexpected type for parent model [" + parent + "]");
056            thenModel.markAsSkipped();
057            return;
058        }
059              
060        IfModel ifModel = (IfModel) parent;
061        if(ifModel.getBranchState() != BranchState.IF_BRANCH) {
062            thenModel.deepMarkAsSkipped();
063        }
064    }
065
066}