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.w3c.dom.Node;
21  
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  
26  /**
27   * Default implementation of {@link ValidationReportBuilder}.
28   *
29   * @author Michele Mostarda (mostarda@fbk.eu)
30   * @author Davide Palmisano (palmisano@fbk.eu)
31   */
32  public class DefaultValidationReportBuilder implements ValidationReportBuilder {
33  
34      private List<ValidationReport.Issue> issues;
35      private List<ValidationReport.RuleActivation> ruleActivations;
36      private List<ValidationReport.Error> errors;
37  
38      public DefaultValidationReportBuilder() {
39        //default constructor
40      }
41  
42      public ValidationReport getReport() {
43          return new DefaultValidationReport(
44                  issues == null ? Collections.<ValidationReport.Issue>emptyList() : issues,
45                  ruleActivations == null ? Collections.<ValidationReport.RuleActivation>emptyList() : ruleActivations,
46                  errors == null ? Collections.<ValidationReport.Error>emptyList() : errors 
47          );
48      }
49  
50      public void reportIssue(ValidationReport.IssueLevel issueLevel, String message, Node n) {
51          if(issues == null) {
52              issues = new ArrayList<>();
53          }
54          issues.add( new ValidationReport.Issue(issueLevel, message, n) );
55      }
56  
57      public void reportIssue(ValidationReport.IssueLevel issueLevel, String message) {
58          reportIssue(issueLevel, message, null);
59      }
60  
61      public void traceRuleActivation(Rule r) {
62          if(ruleActivations == null) {
63              ruleActivations = new ArrayList<>();
64          }
65          ruleActivations.add( new ValidationReport.RuleActivation(r) );
66      }
67  
68      public void reportRuleError(Rule r, Exception e, String msg) {
69          if(errors == null) {
70              errors = new ArrayList<>();
71          }
72          errors.add( new ValidationReport.RuleError(r, e, msg) );
73      }
74  
75      public void reportFixError(Fix f, Exception e, String msg) {
76          if(errors == null) {
77              errors = new ArrayList<>();
78          }
79          errors.add( new ValidationReport.FixError(f, e, msg) );
80  
81      }
82  
83      @Override
84      public String toString() {
85          final StringBuilder sb = new StringBuilder();
86          if(ruleActivations != null) {
87              sb.append("Rules {\n");
88              for(ValidationReport.RuleActivation ra :ruleActivations) {
89                  sb.append(ra).append('\n');
90              }
91              sb.append("}\n");
92          }
93          if(issues != null) {
94              sb.append("Issues {\n");
95              for(ValidationReport.Issue issue : issues) {
96                  sb.append( issue.toString() ).append('\n');
97              }
98              sb.append("}\n");
99          }
100         if (errors != null) {
101             sb.append("Errors {\n");
102             for (ValidationReport.Error error : errors) {
103                 sb.append( error.toString() ).append('\n');
104             }
105             sb.append("}\n");
106         }
107         return sb.toString();
108     }
109 
110 }