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  
26  /**
27   * This class contains the report of a validation performed by
28   * the {@link Validator} class.
29   *
30   * @see Validator
31   * @see ValidationReportBuilder
32   * @author Michele Mostarda (mostarda@fbk.eu)
33   * @author Davide Palmisano (palmisano@fbk.eu)
34   */
35  // TODO: merge with ErrorReporter
36  public interface ValidationReport extends Serializable {
37  
38      /**
39       * Defines the different issue levels.
40       */
41      enum IssueLevel {
42          ERROR,
43          WARNING,
44          INFO
45      }
46  
47      /**
48       * Returns the list of detected issues.
49       *
50       * @return list of detected issues.
51       */
52      List<Issue> getIssues();
53  
54      /**
55       * Returns the list of activated rules.
56       *
57       * @return list of activated rules.
58       */
59      List<RuleActivation> getRuleActivations();
60  
61      /**
62       * Returns the list of detected errors.
63       *
64       * @return list of detected errors.
65       */
66      List<Error> getErrors();
67  
68      /**
69       * An issue found during the validation process.
70       */
71      class Issue implements Serializable {
72  
73          /**
74         * 
75         */
76        private static final long serialVersionUID = 1L;
77          private final IssueLevel level;
78          private final String message;
79          private final transient  Node origin;
80  
81          public Issue(IssueLevel level, String message, Node origin) {
82              if(level == null) {
83                  throw new NullPointerException("level cannot be null.");
84              }
85              if(message == null) {
86                  throw new NullPointerException("message cannot be null.");
87              }
88              if(origin == null) {
89                  throw new NullPointerException("origin cannot be null.");
90              }
91              this.level   = level;
92              this.message = message;
93              this.origin  = origin;
94          }
95  
96          public String getMessage() {
97              return message;
98          }
99  
100         public IssueLevel getLevel() {
101             return level;
102         }
103 
104         public Node getOrigin() {
105             return origin;
106         }
107 
108         @Override
109         public String toString() {
110             return String.format(
111                     "Issue %s '%s' %s",
112                     level,
113                     message,
114                     DomUtils.getXPathForNode(origin)
115             );
116         }
117     }
118 
119     /**
120      * This class describes the activation of a rule. 
121      */
122     class RuleActivation implements Serializable {
123 
124         /**
125        * 
126        */
127       private static final long serialVersionUID = 1L;
128         private final String ruleStr;
129 
130         public RuleActivation(Rule r) {
131             if(r == null) {
132                 throw new NullPointerException("rule cannot be null.");
133             }
134             ruleStr = r.getHRName();
135         }
136 
137         public String getRuleStr() {
138             return ruleStr;
139         }
140 
141         @Override
142          public String toString() {
143             return ruleStr;
144         }
145     }
146 
147     /**
148      * An error occurred while performing the validation process.
149      */
150     abstract class Error implements Serializable {
151 
152         /**
153        * 
154        */
155       private static final long serialVersionUID = 1L;
156         private final Exception cause;
157         private final String message;
158 
159         public Error(Exception e, String msg) {
160             if(e == null) {
161                 throw new NullPointerException("exception cannot be null.");
162             }
163             if(msg == null) {
164                 throw new NullPointerException("message cannot be null.");
165             }
166             cause   = e;
167             message = msg;
168         }
169 
170         public Exception getCause() {
171             return cause;
172         }
173 
174         public String getMessage() {
175             return message;
176         }
177 
178         @Override
179         public String toString() {
180             return String.format("%s %s %s", this.getClass().getName(), cause, message);
181         }
182     }
183 
184     /**
185      * An error occurred while executing a rule.
186      */
187     class RuleError extends Error {
188 
189         /**
190        * 
191        */
192       private static final long serialVersionUID = 1L;
193         private final Rule origin;
194 
195         public RuleError(Rule r, Exception e, String msg) {
196             super(e, msg);
197             if(r == null) {
198                 throw new NullPointerException("rule cannot be null.");
199             }
200             origin = r;
201         }
202 
203         public Rule getOrigin() {
204             return origin;
205         }
206 
207         @Override
208         public String toString() {
209             return String.format("%s - %s", super.toString(), origin.getHRName());
210         }
211     }
212 
213     /**
214      * An error occurred while executing a fix.
215      */
216     class FixError extends Error {
217 
218         /**
219        * 
220        */
221       private static final long serialVersionUID = 1L;
222         private final Fix origin;
223 
224         public FixError(Fix f, Exception e, String msg) {
225              super(e, msg);
226              origin = f;
227         }
228 
229         public Fix getOrigin() {
230             return origin;
231         }
232 
233         @Override
234         public String toString() {
235             return String.format("%s - %s", super.toString(), origin.getHRName());
236         }
237     }
238 
239 }