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.action;
15  
16  import java.io.File;
17  import java.io.IOException;
18  import java.io.InputStream;
19  import java.net.MalformedURLException;
20  import java.net.URI;
21  import java.net.URL;
22  import java.util.List;
23  
24  import org.xml.sax.Attributes;
25  
26  import ch.qos.logback.core.joran.event.SaxEvent;
27  import ch.qos.logback.core.joran.event.SaxEventRecorder;
28  import ch.qos.logback.core.joran.spi.ActionException;
29  import ch.qos.logback.core.joran.spi.JoranException;
30  import ch.qos.logback.core.joran.spi.SaxEventInterpretationContext;
31  import ch.qos.logback.core.joran.util.ConfigurationWatchListUtil;
32  import ch.qos.logback.core.model.IncludeModel;
33  import ch.qos.logback.core.model.Model;
34  import ch.qos.logback.core.util.Loader;
35  import ch.qos.logback.core.util.OptionHelper;
36  
37  import static ch.qos.logback.core.joran.JoranConstants.INCLUDED_TAG;
38  
39  /**
40   * 
41   * @author ceki
42   *
43   */
44  public class IncludeAction extends Action {
45  
46      private static final String FILE_ATTR = "file";
47      private static final String URL_ATTR = "url";
48      private static final String RESOURCE_ATTR = "resource";
49      private static final String OPTIONAL_ATTR = "optional";
50  
51      Model parentModel;
52      IncludeModel includeModel;
53      boolean inError = false;
54      
55      @Override
56      public void begin(SaxEventInterpretationContext seic, String tagName, Attributes attributes) throws ActionException {
57  
58          String optionalStr = attributes.getValue(OPTIONAL_ATTR);
59  
60          this.includeModel = new IncludeModel();
61          this.includeModel.setOptional(optionalStr);
62          fillInIncludeModelAttributes(includeModel, tagName, attributes);
63          if (!seic.isModelStackEmpty()) {
64              parentModel = seic.peekModel();
65          }
66          final int lineNumber = getLineNumber(seic);
67          this.includeModel.setLineNumber(lineNumber);
68          seic.pushModel(this.includeModel);
69      }
70  
71      private void fillInIncludeModelAttributes(IncludeModel includeModel, String tagName, Attributes attributes) {
72          this.includeModel.setTag(tagName);
73          String fileAttribute = attributes.getValue(FILE_ATTR);
74          String urlAttribute = attributes.getValue(URL_ATTR);
75          String resourceAttribute = attributes.getValue(RESOURCE_ATTR);
76  
77          this.includeModel.setFile(fileAttribute);
78          this.includeModel.setUrl(urlAttribute);
79          this.includeModel.setResource(resourceAttribute);
80      }
81  
82  
83      @Override
84      public void end(SaxEventInterpretationContext seic, String name) throws ActionException {
85          
86          if(inError)
87              return;
88          
89          Model m = seic.peekModel();
90  
91          if (m != includeModel) {
92              addWarn("The object at the of the stack is not the model [" + includeModel.idString()
93                      + "] pushed earlier.");
94              addWarn("This is wholly unexpected.");
95          }
96  
97          // do not pop nor add to parent if there is no parent
98          if (parentModel != null) {
99              parentModel.addSubModel(includeModel);
100             seic.popModel();
101         }
102     }
103 }