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.classic.joran.action;
16  
17  import ch.qos.logback.classic.model.PropertiesConfiguratorModel;
18  import ch.qos.logback.core.joran.action.ResourceAction;
19  import ch.qos.logback.core.joran.spi.SaxEventInterpretationContext;
20  import ch.qos.logback.core.model.Model;
21  import org.xml.sax.Attributes;
22  
23  import static ch.qos.logback.classic.joran.action.ConfigurationAction.SCAN_ATTR;
24  
25  /**
26   * Build an {@link PropertiesConfiguratorModel} instance from SAX events.
27   *
28   * @author Ceki Gülcü
29   * @since 1.5.8
30   */
31  public class PropertiesConfiguratorAction extends ResourceAction {
32  
33  
34      /**
35       * Creates a new instance of {@link PropertiesConfiguratorModel}.
36       *
37       * @return a new {@link PropertiesConfiguratorModel} instance
38       */
39      protected PropertiesConfiguratorModel makeNewResourceModel() {
40          return new PropertiesConfiguratorModel();
41      }
42  
43  
44      /**
45       * Builds a {@link PropertiesConfiguratorModel} instance for the current XML element.
46       *
47       * <p>This method extends the parent class behavior by additionally extracting and setting
48       * the scan attribute value on the returned model.</p>
49       *
50       * @param saxEventInterpretationContext the context for interpreting SAX events
51       * @param localName the name of the XML element being processed
52       * @param attributes the attributes of the XML element
53       * @return a configured {@link PropertiesConfiguratorModel} instance
54       * @throws IllegalStateException if the model returned by the parent class is not a
55       *                               {@link PropertiesConfiguratorModel}
56       */
57      @Override
58      public Model buildCurrentModel(SaxEventInterpretationContext saxEventInterpretationContext, String localName,
59                                     Attributes attributes) {
60          Model model = super.buildCurrentModel(saxEventInterpretationContext, localName, attributes);
61  
62          if (model instanceof PropertiesConfiguratorModel) {
63              PropertiesConfiguratorModel propertiesConfiguratorModel = (PropertiesConfiguratorModel) model;
64              String scanAttribute = attributes.getValue(SCAN_ATTR);
65              propertiesConfiguratorModel.setScanStr(scanAttribute);
66              return propertiesConfiguratorModel;
67          } else {
68              // this is impossible since makeNewResourceModel() returns a PropertiesConfiguratorModel
69              throw new IllegalStateException("Model is not of type PropertiesConfiguratorModel");
70          }
71      }
72  }
73