View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, 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.spi;
15  
16  import java.util.Map;
17  import java.util.Stack;
18  
19  import ch.qos.logback.core.Context;
20  import ch.qos.logback.core.joran.action.Action;
21  import ch.qos.logback.core.model.Model;
22  import ch.qos.logback.core.spi.ContextAwareBase;
23  import ch.qos.logback.core.spi.PropertyContainer;
24  import ch.qos.logback.core.spi.ScanException;
25  import ch.qos.logback.core.util.OptionHelper;
26  
27  /**
28   * 
29   * An InterpretationContext contains the contextual state of a Joran parsing
30   * session. {@link Action} objects depend on this context to exchange and store
31   * information.
32   * 
33   * @author Ceki Gülcü
34   */
35  public class SaxEventInterpretationContext extends ContextAwareBase implements PropertyContainer {
36      Stack<Model> modelStack;
37  
38      SaxEventInterpreter saxEventInterpreter;
39  
40      public SaxEventInterpretationContext(Context context, SaxEventInterpreter saxEventInterpreter) {
41          this.context = context;
42          this.saxEventInterpreter = saxEventInterpreter;
43          this.modelStack = new Stack<>();
44      }
45  
46      public SaxEventInterpreter getSaxEventInterpreter() {
47          return saxEventInterpreter;
48      }
49  
50      /**
51       * Return the Model at the top of the model stack, may return null.
52       * 
53       * @return
54       */
55      public Model peekModel() {
56          if(modelStack.isEmpty()) {
57              return null;
58          }
59          return modelStack.peek();
60      }
61  
62      public void pushModel(Model m) {
63          modelStack.push(m);
64      }
65  
66      public boolean isModelStackEmpty() {
67          return modelStack.isEmpty();
68      }
69  
70      public Model popModel() {
71          return modelStack.pop();
72      }
73  
74      public Stack<Model> getCopyOfModelStack() {
75          Stack<Model> copy = new Stack<>();
76          copy.addAll(modelStack);
77          return copy;
78      }
79  
80      public void addSubstitutionProperty(String key, String value) {
81          throw new UnsupportedOperationException();
82      }
83  
84      /**
85       * If a key is found in propertiesMap then return it. Otherwise, delegate to the
86       * context.
87       */
88      public String getProperty(String key) {
89          return context.getProperty(key);
90      }
91  
92      public Map<String, String> getCopyOfPropertyMap() {
93          return null;
94      }
95  
96      public String subst(String value) {
97          if (value == null) {
98              return null;
99          }
100 
101         try {
102             return OptionHelper.substVars(value, this, context);
103         } catch (ScanException | IllegalArgumentException e) {
104             addError("Problem while parsing [" + value + "]", e);
105             return value;
106         }
107     }
108 
109 }