View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *  http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.any23.validator;
19  
20  import org.apache.any23.extractor.html.DomUtils;
21  import org.w3c.dom.Node;
22  
23  import java.io.Serializable;
24  import java.util.List;
25  import java.util.Locale;
26  
27  /**
28   * This class contains the report of a validation performed by the {@link Validator} class.
29   *
30   * @see Validator
31   * @see ValidationReportBuilder
32   * 
33   * @author Michele Mostarda (mostarda@fbk.eu)
34   * @author Davide Palmisano (palmisano@fbk.eu)
35   */
36  // TODO: merge with ErrorReporter
37  public interface ValidationReport extends Serializable {
38  
39      /**
40       * Defines the different issue levels.
41       */
42      enum IssueLevel {
43          ERROR, WARNING, INFO
44      }
45  
46      /**
47       * Returns the list of detected issues.
48       *
49       * @return list of detected issues.
50       */
51      List<Issue> getIssues();
52  
53      /**
54       * Returns the list of activated rules.
55       *
56       * @return list of activated rules.
57       */
58      List<RuleActivation> getRuleActivations();
59  
60      /**
61       * Returns the list of detected errors.
62       *
63       * @return list of detected errors.
64       */
65      List<Error> getErrors();
66  
67      /**
68       * An issue found during the validation process.
69       */
70      class Issue implements Serializable {
71  
72          /**
73          * 
74          */
75          private static final long serialVersionUID = 1L;
76          private final IssueLevel level;
77          private final String message;
78          private final transient Node origin;
79  
80          public Issue(IssueLevel level, String message, Node origin) {
81              if (level == null) {
82                  throw new NullPointerException("level cannot be null.");
83              }
84              if (message == null) {
85                  throw new NullPointerException("message cannot be null.");
86              }
87              if (origin == null) {
88                  throw new NullPointerException("origin cannot be null.");
89              }
90              this.level = level;
91              this.message = message;
92              this.origin = origin;
93          }
94  
95          public String getMessage() {
96              return message;
97          }
98  
99          public IssueLevel getLevel() {
100             return level;
101         }
102 
103         public Node getOrigin() {
104             return origin;
105         }
106 
107         @Override
108         public String toString() {
109             return String.format(Locale.ROOT, "Issue %s '%s' %s", level, message, DomUtils.getXPathForNode(origin));
110         }
111     }
112 
113     /**
114      * This class describes the activation of a rule.
115      */
116     class RuleActivation implements Serializable {
117 
118         /**
119         * 
120         */
121         private static final long serialVersionUID = 1L;
122         private final String ruleStr;
123 
124         public RuleActivation(Rule r) {
125             if (r == null) {
126                 throw new NullPointerException("rule cannot be null.");
127             }
128             ruleStr = r.getHRName();
129         }
130 
131         public String getRuleStr() {
132             return ruleStr;
133         }
134 
135         @Override
136         public String toString() {
137             return ruleStr;
138         }
139     }
140 
141     /**
142      * An error occurred while performing the validation process.
143      */
144     abstract class Error implements Serializable {
145 
146         /**
147         * 
148         */
149         private static final long serialVersionUID = 1L;
150         private final Exception cause;
151         private final String message;
152 
153         public Error(Exception e, String msg) {
154             if (e == null) {
155                 throw new NullPointerException("exception cannot be null.");
156             }
157             if (msg == null) {
158                 throw new NullPointerException("message cannot be null.");
159             }
160             cause = e;
161             message = msg;
162         }
163 
164         public Exception getCause() {
165             return cause;
166         }
167 
168         public String getMessage() {
169             return message;
170         }
171 
172         @Override
173         public String toString() {
174             return String.format(Locale.ROOT, "%s %s %s", this.getClass().getName(), cause, message);
175         }
176     }
177 
178     /**
179      * An error occurred while executing a rule.
180      */
181     class RuleError extends Error {
182 
183         /**
184         * 
185         */
186         private static final long serialVersionUID = 1L;
187         private final Rule origin;
188 
189         public RuleError(Rule r, Exception e, String msg) {
190             super(e, msg);
191             if (r == null) {
192                 throw new NullPointerException("rule cannot be null.");
193             }
194             origin = r;
195         }
196 
197         public Rule getOrigin() {
198             return origin;
199         }
200 
201         @Override
202         public String toString() {
203             return String.format(Locale.ROOT, "%s - %s", super.toString(), origin.getHRName());
204         }
205     }
206 
207     /**
208      * An error occurred while executing a fix.
209      */
210     class FixError extends Error {
211 
212         /**
213         * 
214         */
215         private static final long serialVersionUID = 1L;
216         private final Fix origin;
217 
218         public FixError(Fix f, Exception e, String msg) {
219             super(e, msg);
220             origin = f;
221         }
222 
223         public Fix getOrigin() {
224             return origin;
225         }
226 
227         @Override
228         public String toString() {
229             return String.format(Locale.ROOT, "%s - %s", super.toString(), origin.getHRName());
230         }
231     }
232 
233 }