View Javadoc
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  package ch.qos.logback.core.joran.action;
15  
16  import org.xml.sax.Attributes;
17  
18  import ch.qos.logback.core.joran.JoranConstants;
19  import ch.qos.logback.core.joran.spi.SaxEventInterpretationContext;
20  import ch.qos.logback.core.spi.ContextAware;
21  import ch.qos.logback.core.spi.ContextAwareBase;
22  import ch.qos.logback.core.util.OptionHelper;
23  
24  import java.util.Arrays;
25  
26  public class PreconditionValidator extends ContextAwareBase {
27  
28      boolean valid = true;
29      SaxEventInterpretationContext seic;
30      Attributes attributes;
31      String tag;
32  
33      public PreconditionValidator(ContextAware origin, SaxEventInterpretationContext seic, String name,
34              Attributes attributes) {
35          super(origin);
36          this.setContext(origin.getContext());
37          this.seic = seic;
38          this.tag = name;
39          this.attributes = attributes;
40      }
41  
42      public PreconditionValidator validateZeroAttributes() {
43          if(attributes == null) 
44              return this;
45          
46          if(attributes.getLength() != 0) {
47              addError("Element [" + tag + "] should have no attributes, near line "
48                      + Action.getLineNumber(seic));
49              this.valid = false;
50          }
51          return this;
52      }
53  
54      
55      public PreconditionValidator validateClassAttribute() {
56          return validateGivenAttribute(Action.CLASS_ATTRIBUTE);
57      }
58  
59      public PreconditionValidator validateNameAttribute() {
60          return validateGivenAttribute(Action.NAME_ATTRIBUTE);
61      }
62  
63      public PreconditionValidator validateValueAttribute() {
64          return validateGivenAttribute(JoranConstants.VALUE_ATTR);
65      }
66  
67      public PreconditionValidator validateRefAttribute() {
68          return validateGivenAttribute(JoranConstants.REF_ATTRIBUTE);
69      }
70  
71      public PreconditionValidator validateOneAndOnlyOneAttributeProvided(String... names) {
72          int validCount = 0;
73          for(String name : names) {
74              boolean invalid = isInvalidAttribute(name);
75              if(!invalid) {
76                  validCount++;
77              }
78          }
79          if(validCount == 1) {
80              this.valid = true;
81          } else {
82              this.valid = false;
83              addError("Element [" + tag + "] should have at least one of [" + Arrays.toString(names) + "] as an attribute, near line "
84                      + Action.getLineNumber(seic));
85          }
86          return this;
87      }
88  
89      public boolean isInvalidAttribute(String attributeName) {
90          String attributeValue = attributes.getValue(attributeName);
91          return OptionHelper.isNullOrEmptyOrAllSpaces(attributeValue);
92      }
93  
94      public boolean isValidAttribute(String attributeName) {
95          return !isInvalidAttribute(attributeName);
96      }
97  
98      public PreconditionValidator validateGivenAttribute(String attributeName) {
99          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 }