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.core.joran.action;
016
017import ch.qos.logback.core.joran.spi.SaxEventInterpretationContext;
018import ch.qos.logback.core.model.Model;
019import ch.qos.logback.core.model.ResourceModel;
020import org.xml.sax.Attributes;
021
022/**
023 * An action which builds subclass instances of {@link ResourceModel}.
024 *
025 * @since 1.5.8
026 */
027abstract public class ResourceAction extends BaseModelAction {
028
029    private static final String FILE_ATTR = "file";
030    private static final String URL_ATTR = "url";
031    private static final String RESOURCE_ATTR = "resource";
032    private static final String OPTIONAL_ATTR = "optional";
033
034
035    abstract protected ResourceModel makeNewResourceModel();
036
037    @Override
038    protected boolean validPreconditions(SaxEventInterpretationContext intercon, String name, Attributes attributes) {
039        PreconditionValidator pv = new PreconditionValidator(this, intercon, name, attributes);
040        pv.validateOneAndOnlyOneAttributeProvided(FILE_ATTR, URL_ATTR, RESOURCE_ATTR);
041        return pv.isValid();
042    }
043
044    @Override
045    protected Model buildCurrentModel(SaxEventInterpretationContext interpretationContext, String localName,
046                                      Attributes attributes) {
047        ResourceModel resourceModel = makeNewResourceModel();
048        fillInIncludeModelAttributes(resourceModel, localName, attributes);
049        return resourceModel;
050    }
051
052
053    private void fillInIncludeModelAttributes(ResourceModel resourceModel, String tagName, Attributes attributes) {
054        resourceModel.setTag(tagName);
055        String fileAttribute = attributes.getValue(FILE_ATTR);
056        String urlAttribute = attributes.getValue(URL_ATTR);
057        String resourceAttribute = attributes.getValue(RESOURCE_ATTR);
058        String optionalAttribute = attributes.getValue(OPTIONAL_ATTR);
059        resourceModel.setFile(fileAttribute);
060        resourceModel.setUrl(urlAttribute);
061        resourceModel.setResource(resourceAttribute);
062        resourceModel.setOptional(optionalAttribute);
063    }
064
065}