1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, 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 v1.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  public class PreconditionValidator extends ContextAwareBase {
25  
26      boolean valid = true;
27      SaxEventInterpretationContext seic;
28      Attributes attributes;
29      String tag;
30  
31      public PreconditionValidator(ContextAware origin, SaxEventInterpretationContext seic, String name,
32              Attributes attributes) {
33          super(origin);
34          this.setContext(origin.getContext());
35          this.seic = seic;
36          this.tag = name;
37          this.attributes = attributes;
38      }
39  
40      public PreconditionValidator validateZeroAttributes() {
41          if(attributes == null) 
42              return this;
43          
44          if(attributes.getLength() != 0) {
45              addError("Element [" + tag + "] should have no attributes, near line "
46                      + Action.getLineNumber(seic));
47              this.valid = false;
48          }
49          return this;
50      }
51  
52      
53      public PreconditionValidator validateClassAttribute() {
54          return validateGivenAttribute(Action.CLASS_ATTRIBUTE);
55      }
56  
57      public PreconditionValidator validateNameAttribute() {
58          return validateGivenAttribute(Action.NAME_ATTRIBUTE);
59      }
60  
61      public PreconditionValidator validateValueAttribute() {
62          return validateGivenAttribute(JoranConstants.VALUE_ATTR);
63      }
64  
65      public PreconditionValidator validateRefAttribute() {
66          return validateGivenAttribute(JoranConstants.REF_ATTRIBUTE);
67      }
68  
69      public boolean isInvalidAttribute(String attributeName) {
70          String attributeValue = attributes.getValue(attributeName);
71          return OptionHelper.isNullOrEmptyOrAllSpaces(attributeValue);
72      }
73  
74      public PreconditionValidator validateGivenAttribute(String attributeName) {
75          boolean invalid = isInvalidAttribute(attributeName);
76          if (invalid) {
77              addMissingAttributeError(attributeName);
78              this.valid = false;
79          }
80          return this;
81      }
82  
83  
84  
85      /**
86       *
87       * @deprecated replaced by {@link #validateGivenAttribute(String)}
88       */
89      @Deprecated
90      public PreconditionValidator generic(String attributeName) {
91          return validateGivenAttribute(attributeName);
92      }
93  
94      public void addMissingAttributeError(String attributeName) {
95          addError("Missing attribute [" + attributeName + "]. " + getLocationSuffix());
96      }
97  
98      public String getLocationSuffix() {
99          return "See element [" + tag + "] near line " + Action.getLineNumber(seic);
100     }
101 
102 //    public void addWarning(String msg) {
103 //        super.addWarn(msg + getLocationSuffix());
104 //    }
105 //
106 //    public void addError(String msg) {
107 //        super.addError(msg + getLocationSuffix());
108 //    }
109 
110     public boolean isValid() {
111         return valid;
112     }
113 
114 }