View Javadoc
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.joran.action;
15  
16  import java.util.Stack;
17  
18  import org.xml.sax.Attributes;
19  
20  import ch.qos.logback.core.joran.spi.ActionException;
21  import ch.qos.logback.core.joran.spi.SaxEventInterpretationContext;
22  import ch.qos.logback.core.model.ImplicitModel;
23  import ch.qos.logback.core.model.Model;
24  import ch.qos.logback.core.spi.ErrorCodes;
25  
26  /**
27   * 
28   * Action dealing with elements corresponding to implicit rules.
29   * 
30   * 
31   * @author Ceki Gülcü
32   *
33   */
34  // TODO: rename to DefaultImplicitRuleAction (after Model migration)
35  public class ImplicitModelAction extends Action {
36  
37      Stack<ImplicitModel> currentImplicitModelStack = new Stack<>();
38  
39      @Override
40      public void begin(SaxEventInterpretationContext interpretationContext, String name, Attributes attributes)
41              throws ActionException {
42          ImplicitModel currentImplicitModel = new ImplicitModel();
43          currentImplicitModel.setTag(name);
44  
45          String className = attributes.getValue(CLASS_ATTRIBUTE);
46          currentImplicitModel.setClassName(className);
47          currentImplicitModelStack.push(currentImplicitModel);
48          interpretationContext.pushModel(currentImplicitModel);
49      }
50  
51      @Override
52      public void body(SaxEventInterpretationContext ec, String body) {
53          ImplicitModel implicitModel = currentImplicitModelStack.peek();
54          implicitModel.addText(body);
55      }
56  
57      @Override
58      public void end(SaxEventInterpretationContext interpretationContext, String name) throws ActionException {
59  
60          ImplicitModel implicitModel = currentImplicitModelStack.peek();
61          Model otherImplicitModel = interpretationContext.popModel();
62  
63          if (implicitModel != otherImplicitModel) {
64              addError(implicitModel + " does not match " + otherImplicitModel);
65              return;
66          }
67          Model parentModel = interpretationContext.peekModel();
68          if(parentModel != null) {
69              parentModel.addSubModel(implicitModel);
70          } else {
71              addWarn(ErrorCodes.PARENT_MODEL_NOT_FOUND);      
72              addWarn(ErrorCodes.SKIPPING_IMPLICIT_MODEL_ADDITION);
73          }
74          currentImplicitModelStack.pop();
75  
76      }
77  
78  }