View Javadoc

1   /*
2    * Copyright 2013 University of Glasgow.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package broadwick.config;
17  
18  import lombok.Getter;
19  
20  /**
21   * This class encapsulates errors found during the validation of configuration
22   * files.
23   */
24  @SuppressWarnings("serial")
25  public final class ConfigValidationErrors {
26  
27      /**
28       * Constructs a new exception with null as its detail message.
29       */
30      public ConfigValidationErrors() {
31      }
32  
33      /**
34       * Add an error to the list of validation errors.
35       *
36       * @param error the error to be added to the list of errors.
37       */
38      public void addError(final String error) {
39          numErrors++;
40          errors.append(String.format(errorString,Status.MAJOR, error));
41          if (!error.endsWith(NEWLINE)) {
42              errors.append(NEWLINE);
43          }
44          valid = false;
45      }
46  
47      /**
48       * Add an error to the list of validation errors.
49       *
50       * @param status the error status of this validation error.
51       * @param error the error to be added to the list of errors.
52       */
53      public void addError(final Status status, final String error) {
54          numErrors++;
55          errors.append(String.format(errorString,status, error));
56          if (!error.endsWith(NEWLINE)) {
57              errors.append(NEWLINE);
58          }
59          
60          if (Status.MAJOR.equals(status)) {
61              valid = false;
62          }
63      }
64  
65      /**
66       * Get the validation errors.
67       *
68       * @return the list of validation errors.
69       */
70      public String getValidationErrors() {
71          return errors.toString();
72      }
73  
74      /**
75       * Error status of the configuration status.
76       */
77      public static enum Status {
78  
79          /**
80           * MINOR error, basically a warning, The model should still run but may
81           * not be what the user intended.
82           */
83          MINOR,
84          /**
85           * MAJOR error, the model will not run.
86           */
87          MAJOR
88      }
89  
90      private StringBuilder errors = new StringBuilder();
91      @SuppressWarnings("PMD.UnusedPrivateField")
92      @Getter
93      private boolean valid;
94      @SuppressWarnings("PMD.UnusedPrivateField")
95      @Getter
96      private int numErrors = 0;
97      private String errorString = "\t %s - %s";
98      private static final String NEWLINE = "\n";
99  }