1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, 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 v2.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  
15  package ch.qos.logback.core.joran.action;
16  
17  import ch.qos.logback.core.joran.spi.SaxEventInterpretationContext;
18  import ch.qos.logback.core.model.Model;
19  import ch.qos.logback.core.model.ResourceModel;
20  import org.xml.sax.Attributes;
21  
22  /**
23   * An action which builds subclass instances of {@link ResourceModel}.
24   *
25   * @since 1.5.8
26   */
27  abstract public class ResourceAction extends BaseModelAction {
28  
29      private static final String FILE_ATTR = "file";
30      private static final String URL_ATTR = "url";
31      private static final String RESOURCE_ATTR = "resource";
32      private static final String OPTIONAL_ATTR = "optional";
33  
34  
35      abstract protected ResourceModel makeNewResourceModel();
36  
37      @Override
38      protected boolean validPreconditions(SaxEventInterpretationContext intercon, String name, Attributes attributes) {
39          PreconditionValidator pv = new PreconditionValidator(this, intercon, name, attributes);
40          pv.validateOneAndOnlyOneAttributeProvided(FILE_ATTR, URL_ATTR, RESOURCE_ATTR);
41          return pv.isValid();
42      }
43  
44      @Override
45      protected Model buildCurrentModel(SaxEventInterpretationContext interpretationContext, String localName,
46                                        Attributes attributes) {
47          ResourceModel resourceModel = makeNewResourceModel();
48          fillInIncludeModelAttributes(resourceModel, localName, attributes);
49          return resourceModel;
50      }
51  
52  
53      private void fillInIncludeModelAttributes(ResourceModel resourceModel, String tagName, Attributes attributes) {
54          resourceModel.setTag(tagName);
55          String fileAttribute = attributes.getValue(FILE_ATTR);
56          String urlAttribute = attributes.getValue(URL_ATTR);
57          String resourceAttribute = attributes.getValue(RESOURCE_ATTR);
58          String optionalAttribute = attributes.getValue(OPTIONAL_ATTR);
59          resourceModel.setFile(fileAttribute);
60          resourceModel.setUrl(urlAttribute);
61          resourceModel.setResource(resourceAttribute);
62          resourceModel.setOptional(optionalAttribute);
63      }
64  
65  }