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.action; 015 016import java.io.File; 017import java.io.IOException; 018import java.io.InputStream; 019import java.net.MalformedURLException; 020import java.net.URI; 021import java.net.URL; 022import java.util.List; 023 024import org.xml.sax.Attributes; 025 026import ch.qos.logback.core.joran.event.SaxEvent; 027import ch.qos.logback.core.joran.event.SaxEventRecorder; 028import ch.qos.logback.core.joran.spi.ActionException; 029import ch.qos.logback.core.joran.spi.JoranException; 030import ch.qos.logback.core.joran.spi.SaxEventInterpretationContext; 031import ch.qos.logback.core.joran.util.ConfigurationWatchListUtil; 032import ch.qos.logback.core.model.IncludeModel; 033import ch.qos.logback.core.model.Model; 034import ch.qos.logback.core.util.Loader; 035import ch.qos.logback.core.util.OptionHelper; 036 037import static ch.qos.logback.core.joran.JoranConstants.INCLUDED_TAG; 038 039/** 040 * 041 * @author ceki 042 * 043 */ 044public class IncludeAction extends Action { 045 046 private static final String FILE_ATTR = "file"; 047 private static final String URL_ATTR = "url"; 048 private static final String RESOURCE_ATTR = "resource"; 049 private static final String OPTIONAL_ATTR = "optional"; 050 051 Model parentModel; 052 IncludeModel includeModel; 053 boolean inError = false; 054 055 @Override 056 public void begin(SaxEventInterpretationContext seic, String tagName, Attributes attributes) throws ActionException { 057 058 String optionalStr = attributes.getValue(OPTIONAL_ATTR); 059 060 this.includeModel = new IncludeModel(); 061 this.includeModel.setOptional(optionalStr); 062 fillInIncludeModelAttributes(includeModel, tagName, attributes); 063 if (!seic.isModelStackEmpty()) { 064 parentModel = seic.peekModel(); 065 } 066 final int lineNumber = getLineNumber(seic); 067 this.includeModel.setLineNumber(lineNumber); 068 seic.pushModel(this.includeModel); 069 } 070 071 private void fillInIncludeModelAttributes(IncludeModel includeModel, String tagName, Attributes attributes) { 072 this.includeModel.setTag(tagName); 073 String fileAttribute = attributes.getValue(FILE_ATTR); 074 String urlAttribute = attributes.getValue(URL_ATTR); 075 String resourceAttribute = attributes.getValue(RESOURCE_ATTR); 076 077 this.includeModel.setFile(fileAttribute); 078 this.includeModel.setUrl(urlAttribute); 079 this.includeModel.setResource(resourceAttribute); 080 } 081 082 083 @Override 084 public void end(SaxEventInterpretationContext seic, String name) throws ActionException { 085 086 if(inError) 087 return; 088 089 Model m = seic.peekModel(); 090 091 if (m != includeModel) { 092 addWarn("The object at the of the stack is not the model [" + includeModel.idString() 093 + "] pushed earlier."); 094 addWarn("This is wholly unexpected."); 095 } 096 097 // do not pop nor add to parent if there is no parent 098 if (parentModel != null) { 099 parentModel.addSubModel(includeModel); 100 seic.popModel(); 101 } 102 } 103}