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 */
014package ch.qos.logback.core.joran.action;
015
016import org.xml.sax.Attributes;
017
018import ch.qos.logback.core.joran.JoranConstants;
019import ch.qos.logback.core.joran.spi.SaxEventInterpretationContext;
020import ch.qos.logback.core.spi.ContextAware;
021import ch.qos.logback.core.spi.ContextAwareBase;
022import ch.qos.logback.core.util.OptionHelper;
023
024import java.util.Arrays;
025
026public class PreconditionValidator extends ContextAwareBase {
027
028    boolean valid = true;
029    SaxEventInterpretationContext seic;
030    Attributes attributes;
031    String tag;
032
033    public PreconditionValidator(ContextAware origin, SaxEventInterpretationContext seic, String name,
034            Attributes attributes) {
035        super(origin);
036        this.setContext(origin.getContext());
037        this.seic = seic;
038        this.tag = name;
039        this.attributes = attributes;
040    }
041
042    public PreconditionValidator validateZeroAttributes() {
043        if(attributes == null) 
044            return this;
045        
046        if(attributes.getLength() != 0) {
047            addError("Element [" + tag + "] should have no attributes, near line "
048                    + Action.getLineNumber(seic));
049            this.valid = false;
050        }
051        return this;
052    }
053
054    
055    public PreconditionValidator validateClassAttribute() {
056        return validateGivenAttribute(Action.CLASS_ATTRIBUTE);
057    }
058
059    public PreconditionValidator validateNameAttribute() {
060        return validateGivenAttribute(Action.NAME_ATTRIBUTE);
061    }
062
063    public PreconditionValidator validateValueAttribute() {
064        return validateGivenAttribute(JoranConstants.VALUE_ATTR);
065    }
066
067    public PreconditionValidator validateRefAttribute() {
068        return validateGivenAttribute(JoranConstants.REF_ATTRIBUTE);
069    }
070
071    public PreconditionValidator validateOneAndOnlyOneAttributeProvided(String... names) {
072        int validCount = 0;
073        for(String name : names) {
074            boolean invalid = isInvalidAttribute(name);
075            if(!invalid) {
076                validCount++;
077            }
078        }
079        if(validCount == 1) {
080            this.valid = true;
081        } else {
082            this.valid = false;
083            addError("Element [" + tag + "] should have at least one of [" + Arrays.toString(names) + "] as an attribute, near line "
084                    + Action.getLineNumber(seic));
085        }
086        return this;
087    }
088
089    public boolean isInvalidAttribute(String attributeName) {
090        String attributeValue = attributes.getValue(attributeName);
091        return OptionHelper.isNullOrEmptyOrAllSpaces(attributeValue);
092    }
093
094    public boolean isValidAttribute(String attributeName) {
095        return !isInvalidAttribute(attributeName);
096    }
097
098    public PreconditionValidator validateGivenAttribute(String attributeName) {
099        boolean invalid = isInvalidAttribute(attributeName);
100        if (invalid) {
101            addMissingAttributeError(attributeName);
102            this.valid = false;
103        }
104        return this;
105    }
106
107
108
109    /**
110     *
111     * @deprecated replaced by {@link #validateGivenAttribute(String)}
112     */
113    @Deprecated
114    public PreconditionValidator generic(String attributeName) {
115        return validateGivenAttribute(attributeName);
116    }
117
118    public void addMissingAttributeError(String attributeName) {
119        addError("Missing attribute [" + attributeName + "]. " + getLocationSuffix());
120    }
121
122    public String getLocationSuffix() {
123        return "See element [" + tag + "] near line " + Action.getLineNumber(seic);
124    }
125
126//    public void addWarning(String msg) {
127//        super.addWarn(msg + getLocationSuffix());
128//    }
129//
130//    public void addError(String msg) {
131//        super.addError(msg + getLocationSuffix());
132//    }
133
134    public boolean isValid() {
135        return valid;
136    }
137
138}