001/*
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2026, 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 v2.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 */
014
015package ch.qos.logback.classic.joran.action;
016
017import ch.qos.logback.classic.model.PropertiesConfiguratorModel;
018import ch.qos.logback.core.joran.action.ResourceAction;
019import ch.qos.logback.core.joran.spi.SaxEventInterpretationContext;
020import ch.qos.logback.core.model.Model;
021import org.xml.sax.Attributes;
022
023import static ch.qos.logback.classic.joran.action.ConfigurationAction.SCAN_ATTR;
024
025/**
026 * Build an {@link PropertiesConfiguratorModel} instance from SAX events.
027 *
028 * @author Ceki Gülcü
029 * @since 1.5.8
030 */
031public class PropertiesConfiguratorAction extends ResourceAction {
032
033
034    /**
035     * Creates a new instance of {@link PropertiesConfiguratorModel}.
036     *
037     * @return a new {@link PropertiesConfiguratorModel} instance
038     */
039    protected PropertiesConfiguratorModel makeNewResourceModel() {
040        return new PropertiesConfiguratorModel();
041    }
042
043
044    /**
045     * Builds a {@link PropertiesConfiguratorModel} instance for the current XML element.
046     *
047     * <p>This method extends the parent class behavior by additionally extracting and setting
048     * the scan attribute value on the returned model.</p>
049     *
050     * @param saxEventInterpretationContext the context for interpreting SAX events
051     * @param localName the name of the XML element being processed
052     * @param attributes the attributes of the XML element
053     * @return a configured {@link PropertiesConfiguratorModel} instance
054     * @throws IllegalStateException if the model returned by the parent class is not a
055     *                               {@link PropertiesConfiguratorModel}
056     */
057    @Override
058    public Model buildCurrentModel(SaxEventInterpretationContext saxEventInterpretationContext, String localName,
059                                   Attributes attributes) {
060        Model model = super.buildCurrentModel(saxEventInterpretationContext, localName, attributes);
061
062        if (model instanceof PropertiesConfiguratorModel) {
063            PropertiesConfiguratorModel propertiesConfiguratorModel = (PropertiesConfiguratorModel) model;
064            String scanAttribute = attributes.getValue(SCAN_ATTR);
065            propertiesConfiguratorModel.setScanStr(scanAttribute);
066            return propertiesConfiguratorModel;
067        } else {
068            // this is impossible since makeNewResourceModel() returns a PropertiesConfiguratorModel
069            throw new IllegalStateException("Model is not of type PropertiesConfiguratorModel");
070        }
071    }
072}
073