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.joran.action; 015 016import java.util.Stack; 017 018import org.xml.sax.Attributes; 019 020import ch.qos.logback.core.joran.spi.ActionException; 021import ch.qos.logback.core.joran.spi.SaxEventInterpretationContext; 022import ch.qos.logback.core.model.ImplicitModel; 023import ch.qos.logback.core.model.Model; 024import ch.qos.logback.core.spi.ErrorCodes; 025 026/** 027 * 028 * Action dealing with elements corresponding to implicit rules. 029 * 030 * 031 * @author Ceki Gülcü 032 * 033 */ 034// TODO: rename to DefaultImplicitRuleAction (after Model migration) 035public class ImplicitModelAction extends Action { 036 037 Stack<ImplicitModel> currentImplicitModelStack = new Stack<>(); 038 039 @Override 040 public void begin(SaxEventInterpretationContext interpretationContext, String name, Attributes attributes) 041 throws ActionException { 042 ImplicitModel currentImplicitModel = new ImplicitModel(); 043 currentImplicitModel.setTag(name); 044 045 String className = attributes.getValue(CLASS_ATTRIBUTE); 046 currentImplicitModel.setClassName(className); 047 currentImplicitModelStack.push(currentImplicitModel); 048 interpretationContext.pushModel(currentImplicitModel); 049 } 050 051 @Override 052 public void body(SaxEventInterpretationContext ec, String body) { 053 ImplicitModel implicitModel = currentImplicitModelStack.peek(); 054 implicitModel.addText(body); 055 } 056 057 @Override 058 public void end(SaxEventInterpretationContext interpretationContext, String name) throws ActionException { 059 060 ImplicitModel implicitModel = currentImplicitModelStack.peek(); 061 Model otherImplicitModel = interpretationContext.popModel(); 062 063 if (implicitModel != otherImplicitModel) { 064 addError(implicitModel + " does not match " + otherImplicitModel); 065 return; 066 } 067 Model parentModel = interpretationContext.peekModel(); 068 if(parentModel != null) { 069 parentModel.addSubModel(implicitModel); 070 } else { 071 addWarn(ErrorCodes.PARENT_MODEL_NOT_FOUND); 072 addWarn(ErrorCodes.SKIPPING_IMPLICIT_MODEL_ADDITION); 073 } 074 currentImplicitModelStack.pop(); 075 076 } 077 078}