001/** 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2015, 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.spi; 015 016import java.util.Map; 017import java.util.Stack; 018 019import ch.qos.logback.core.Context; 020import ch.qos.logback.core.joran.action.Action; 021import ch.qos.logback.core.model.Model; 022import ch.qos.logback.core.spi.ContextAwareBase; 023import ch.qos.logback.core.spi.PropertyContainer; 024import ch.qos.logback.core.spi.ScanException; 025import ch.qos.logback.core.util.OptionHelper; 026 027/** 028 * 029 * An InterpretationContext contains the contextual state of a Joran parsing 030 * session. {@link Action} objects depend on this context to exchange and store 031 * information. 032 * 033 * @author Ceki Gülcü 034 */ 035public class SaxEventInterpretationContext extends ContextAwareBase implements PropertyContainer { 036 Stack<Model> modelStack; 037 038 SaxEventInterpreter saxEventInterpreter; 039 040 public SaxEventInterpretationContext(Context context, SaxEventInterpreter saxEventInterpreter) { 041 this.context = context; 042 this.saxEventInterpreter = saxEventInterpreter; 043 this.modelStack = new Stack<>(); 044 } 045 046 public SaxEventInterpreter getSaxEventInterpreter() { 047 return saxEventInterpreter; 048 } 049 050 /** 051 * Return the Model at the top of the model stack, may return null. 052 * 053 * @return 054 */ 055 public Model peekModel() { 056 if(modelStack.isEmpty()) { 057 return null; 058 } 059 return modelStack.peek(); 060 } 061 062 public void pushModel(Model m) { 063 modelStack.push(m); 064 } 065 066 public boolean isModelStackEmpty() { 067 return modelStack.isEmpty(); 068 } 069 070 public Model popModel() { 071 return modelStack.pop(); 072 } 073 074 public Stack<Model> getCopyOfModelStack() { 075 Stack<Model> copy = new Stack<>(); 076 copy.addAll(modelStack); 077 return copy; 078 } 079 080 public void addSubstitutionProperty(String key, String value) { 081 throw new UnsupportedOperationException(); 082 } 083 084 /** 085 * If a key is found in propertiesMap then return it. Otherwise, delegate to the 086 * context. 087 */ 088 public String getProperty(String key) { 089 return context.getProperty(key); 090 } 091 092 public Map<String, String> getCopyOfPropertyMap() { 093 return null; 094 } 095 096 public String subst(String value) { 097 if (value == null) { 098 return null; 099 } 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}