View Javadoc
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 intercon;
28      Attributes attributes;
29      String tag;
30  
31      public PreconditionValidator(ContextAware origin, SaxEventInterpretationContext intercon, String name,
32              Attributes attributes) {
33          super(origin);
34          this.setContext(origin.getContext());
35          this.intercon = intercon;
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(intercon));
47              this.valid = false;
48          }
49          return this;
50      }
51  
52      
53      public PreconditionValidator validateClassAttribute() {
54          return generic(Action.CLASS_ATTRIBUTE);
55      }
56  
57      public PreconditionValidator validateNameAttribute() {
58          return generic(Action.NAME_ATTRIBUTE);
59      }
60  
61      public PreconditionValidator validateValueAttribute() {
62          return generic(JoranConstants.VALUE_ATTR);
63      }
64  
65      public PreconditionValidator validateRefAttribute() {
66          return generic(JoranConstants.REF_ATTRIBUTE);
67      }
68  
69      public PreconditionValidator generic(String attributeName) {
70          String attributeValue = attributes.getValue(attributeName);
71          if (OptionHelper.isNullOrEmptyOrAllSpaces(attributeValue)) {
72              addError("Missing attribute [" + attributeName + "] in element [" + tag + "] near line "
73                      + Action.getLineNumber(intercon));
74              this.valid = false;
75          }
76          return this;
77      }
78  
79      public boolean isValid() {
80          return valid;
81      }
82  
83  }