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.ElseModel;
019import ch.qos.logback.core.model.conditional.IfModel;
020import ch.qos.logback.core.model.conditional.IfModel.BranchState;
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
025public class ElseModelHandler extends ModelHandlerBase {
026
027    public ElseModelHandler(Context context) {
028        super(context);
029    }
030
031    static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext ic) {
032        return new ElseModelHandler(context);
033    }
034
035    @Override
036    protected Class<ElseModel> getSupportedModelClass() {
037        return ElseModel.class;
038    }
039
040    @Override
041    public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
042
043        ElseModel elseModel = (ElseModel) model;
044
045        Model parent = mic.peekModel();
046
047        if (!(parent instanceof IfModel)) {
048            addError("Unexpected type for parent model [" + parent + "]");
049            elseModel.markAsSkipped();
050            return;
051        }
052              
053        IfModel ifModel = (IfModel) parent;
054        if(ifModel.getBranchState() != BranchState.ELSE_BRANCH) {
055            elseModel.deepMarkAsSkipped();
056        }
057    }
058
059}